blob: 1b69b3f8b0a6b37562b2b77d67f64f7b22198fde [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "WebDocument.h"
33
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010034#include "WebAXObject.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000035#include "WebDOMEvent.h"
36#include "WebDocumentType.h"
37#include "WebElement.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000038#include "WebElementCollection.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000039#include "WebFormElement.h"
40#include "WebFrameImpl.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000041#include "WebNodeList.h"
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +010042#include "bindings/v8/Dictionary.h"
Ben Murdoch1fad5ca2013-08-07 11:05:11 +010043#include "bindings/v8/ExceptionState.h"
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +010044#include "bindings/v8/ScriptState.h"
45#include "bindings/v8/ScriptValue.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046#include "core/accessibility/AXObjectCache.h"
47#include "core/css/CSSParserMode.h"
48#include "core/css/StyleSheetContents.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010049#include "core/dom/CSSSelectorWatch.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010050#include "core/dom/Document.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010051#include "core/dom/DocumentType.h"
52#include "core/dom/Element.h"
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010053#include "core/dom/FullscreenElementStack.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010054#include "core/dom/NodeList.h"
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +010055#include "core/dom/StyleEngine.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010056#include "core/html/HTMLAllCollection.h"
57#include "core/html/HTMLBodyElement.h"
58#include "core/html/HTMLCollection.h"
59#include "core/html/HTMLElement.h"
60#include "core/html/HTMLFormElement.h"
61#include "core/html/HTMLHeadElement.h"
62#include "core/loader/DocumentLoader.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010063#include "core/rendering/RenderObject.h"
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000064#include "platform/weborigin/SecurityOrigin.h"
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010065#include "public/platform/WebURL.h"
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010066#include "wtf/PassRefPtr.h"
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +010067#include <v8.h>
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000068
69using namespace WebCore;
70
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000071namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000072
73WebURL WebDocument::url() const
74{
75 return constUnwrap<Document>()->url();
76}
77
78WebSecurityOrigin WebDocument::securityOrigin() const
79{
80 if (!constUnwrap<Document>())
81 return WebSecurityOrigin();
82 return WebSecurityOrigin(constUnwrap<Document>()->securityOrigin());
83}
84
85WebString WebDocument::encoding() const
86{
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +010087 return constUnwrap<Document>()->encodingName();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000088}
89
90WebString WebDocument::contentLanguage() const
91{
92 return constUnwrap<Document>()->contentLanguage();
93}
94
Torne (Richard Coles)f79f16f2013-10-31 11:16:44 +000095WebString WebDocument::referrer() const
96{
97 return constUnwrap<Document>()->referrer();
98}
99
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000100WebURL WebDocument::openSearchDescriptionURL() const
101{
102 return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
103}
104
105WebFrame* WebDocument::frame() const
106{
107 return WebFrameImpl::fromFrame(constUnwrap<Document>()->frame());
108}
109
110bool WebDocument::isHTMLDocument() const
111{
112 return constUnwrap<Document>()->isHTMLDocument();
113}
114
115bool WebDocument::isXHTMLDocument() const
116{
117 return constUnwrap<Document>()->isXHTMLDocument();
118}
119
120bool WebDocument::isPluginDocument() const
121{
122 return constUnwrap<Document>()->isPluginDocument();
123}
124
125WebURL WebDocument::baseURL() const
126{
127 return constUnwrap<Document>()->baseURL();
128}
129
130WebURL WebDocument::firstPartyForCookies() const
131{
132 return constUnwrap<Document>()->firstPartyForCookies();
133}
134
135WebElement WebDocument::documentElement() const
136{
137 return WebElement(constUnwrap<Document>()->documentElement());
138}
139
140WebElement WebDocument::body() const
141{
142 return WebElement(constUnwrap<Document>()->body());
143}
144
145WebElement WebDocument::head()
146{
147 return WebElement(unwrap<Document>()->head());
148}
149
150WebString WebDocument::title() const
151{
152 return WebString(constUnwrap<Document>()->title());
153}
154
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000155WebElementCollection WebDocument::all()
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000156{
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000157 return WebElementCollection(unwrap<Document>()->all());
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000158}
159
160void WebDocument::images(WebVector<WebElement>& results)
161{
162 RefPtr<HTMLCollection> images = unwrap<Document>()->images();
163 size_t sourceLength = images->length();
164 Vector<WebElement> temp;
165 temp.reserveCapacity(sourceLength);
166 for (size_t i = 0; i < sourceLength; ++i) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000167 Element* element = images->item(i);
168 if (element && element->isHTMLElement())
169 temp.append(WebElement(element));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000170 }
171 results.assign(temp);
172}
173
174void WebDocument::forms(WebVector<WebFormElement>& results) const
175{
176 RefPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
177 size_t sourceLength = forms->length();
178 Vector<WebFormElement> temp;
179 temp.reserveCapacity(sourceLength);
180 for (size_t i = 0; i < sourceLength; ++i) {
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000181 Element* element = forms->item(i);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000182 // Strange but true, sometimes node can be 0.
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000183 if (element && element->isHTMLElement())
184 temp.append(WebFormElement(toHTMLFormElement(element)));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000185 }
186 results.assign(temp);
187}
188
189WebURL WebDocument::completeURL(const WebString& partialURL) const
190{
191 return constUnwrap<Document>()->completeURL(partialURL);
192}
193
194WebElement WebDocument::getElementById(const WebString& id) const
195{
196 return WebElement(constUnwrap<Document>()->getElementById(id));
197}
198
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000199WebElement WebDocument::focusedElement() const
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000200{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000201 return WebElement(constUnwrap<Document>()->focusedElement());
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000202}
203
204WebDocumentType WebDocument::doctype() const
205{
206 return WebDocumentType(constUnwrap<Document>()->doctype());
207}
208
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000209void WebDocument::insertStyleSheet(const WebString& sourceCode)
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000210{
211 RefPtr<Document> document = unwrap<Document>();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100212 ASSERT(document);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000213 RefPtrWillBeRawPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(CSSParserContext(*document.get(), 0));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000214 parsedSheet->parseString(sourceCode);
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000215 document->styleEngine()->addAuthorSheet(parsedSheet);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000216}
217
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100218void WebDocument::watchCSSSelectors(const WebVector<WebString>& webSelectors)
219{
220 RefPtr<Document> document = unwrap<Document>();
221 Vector<String> selectors;
222 selectors.append(webSelectors.data(), webSelectors.size());
223 CSSSelectorWatch::from(*document).watchCSSSelectors(selectors);
224}
225
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000226void WebDocument::cancelFullScreen()
227{
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000228 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*unwrap<Document>()))
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100229 fullscreen->webkitCancelFullScreen();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000230}
231
232WebElement WebDocument::fullScreenElement() const
233{
234 Element* fullScreenElement = 0;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000235 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*const_cast<WebDocument*>(this)->unwrap<Document>()))
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +0100236 fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000237 return WebElement(fullScreenElement);
238}
239
240WebDOMEvent WebDocument::createEvent(const WebString& eventType)
241{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000242 TrackExceptionState exceptionState;
243 WebDOMEvent event(unwrap<Document>()->createEvent(eventType, exceptionState));
244 if (exceptionState.hadException())
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000245 return WebDOMEvent();
246 return event;
247}
248
249WebReferrerPolicy WebDocument::referrerPolicy() const
250{
251 return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
252}
253
254WebElement WebDocument::createElement(const WebString& tagName)
255{
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000256 TrackExceptionState exceptionState;
257 WebElement element(unwrap<Document>()->createElement(tagName, exceptionState));
258 if (exceptionState.hadException())
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000259 return WebElement();
260 return element;
261}
262
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100263WebAXObject WebDocument::accessibilityObject() const
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000264{
265 const Document* document = constUnwrap<Document>();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100266 return WebAXObject(document->axObjectCache()->getOrCreate(document->renderer()));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000267}
268
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100269WebAXObject WebDocument::accessibilityObjectFromID(int axID) const
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000270{
271 const Document* document = constUnwrap<Document>();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100272 return WebAXObject(document->axObjectCache()->objectFromAXID(axID));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000273}
274
275WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
276{
277 WebVector<WebDraggableRegion> draggableRegions;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000278 const Document* document = constUnwrap<Document>();
279 if (document->hasAnnotatedRegions()) {
280 const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
281 draggableRegions = WebVector<WebDraggableRegion>(regions.size());
282 for (size_t i = 0; i < regions.size(); i++) {
283 const AnnotatedRegionValue& value = regions[i];
284 draggableRegions[i].draggable = value.draggable;
285 draggableRegions[i].bounds = WebCore::IntRect(value.bounds);
286 }
287 }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000288 return draggableRegions;
289}
290
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100291v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
292{
293 Document* document = unwrap<Document>();
294 Dictionary dictionary(options, v8::Isolate::GetCurrent());
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000295 TrackExceptionState exceptionState;
296 ScriptValue constructor = document->registerElement(ScriptState::current(), name, dictionary, exceptionState, CustomElement::EmbedderNames);
297 ec = exceptionState.code();
298 if (exceptionState.hadException())
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100299 return v8::Handle<v8::Value>();
300 return constructor.v8Value();
301}
302
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000303WebDocument::WebDocument(const PassRefPtr<Document>& elem)
304 : WebNode(elem)
305{
306}
307
308WebDocument& WebDocument::operator=(const PassRefPtr<Document>& elem)
309{
310 m_private = elem;
311 return *this;
312}
313
314WebDocument::operator PassRefPtr<Document>() const
315{
Ben Murdoch591b9582013-07-10 11:41:44 +0100316 return toDocument(m_private.get());
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000317}
318
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000319} // namespace blink