2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "public/web/WebDocument.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "bindings/core/v8/ScriptState.h"
36 #include "bindings/core/v8/ScriptValue.h"
37 #include "bindings/core/v8/V8ElementRegistrationOptions.h"
38 #include "core/css/StyleSheetContents.h"
39 #include "core/dom/CSSSelectorWatch.h"
40 #include "core/dom/Document.h"
41 #include "core/dom/DocumentType.h"
42 #include "core/dom/Element.h"
43 #include "core/dom/Fullscreen.h"
44 #include "core/dom/StyleEngine.h"
45 #include "core/events/Event.h"
46 #include "core/html/HTMLAllCollection.h"
47 #include "core/html/HTMLBodyElement.h"
48 #include "core/html/HTMLCollection.h"
49 #include "core/html/HTMLElement.h"
50 #include "core/html/HTMLFormElement.h"
51 #include "core/html/HTMLHeadElement.h"
52 #include "core/html/HTMLLinkElement.h"
53 #include "core/layout/LayoutObject.h"
54 #include "core/layout/LayoutView.h"
55 #include "core/loader/DocumentLoader.h"
56 #include "modules/accessibility/AXObject.h"
57 #include "modules/accessibility/AXObjectCacheImpl.h"
58 #include "platform/weborigin/SecurityOrigin.h"
59 #include "public/platform/WebURL.h"
60 #include "public/web/WebAXObject.h"
61 #include "public/web/WebDOMEvent.h"
62 #include "public/web/WebDocumentType.h"
63 #include "public/web/WebElement.h"
64 #include "public/web/WebElementCollection.h"
65 #include "public/web/WebFormElement.h"
66 #include "public/web/WebNodeList.h"
67 #include "web/WebLocalFrameImpl.h"
68 #include "wtf/PassRefPtr.h"
73 WebURL
WebDocument::url() const
75 return constUnwrap
<Document
>()->url();
78 WebSecurityOrigin
WebDocument::securityOrigin() const
80 if (!constUnwrap
<Document
>())
81 return WebSecurityOrigin();
82 return WebSecurityOrigin(constUnwrap
<Document
>()->securityOrigin());
85 bool WebDocument::isPrivilegedContext(WebString
& errorMessage
) const
87 const Document
* document
= constUnwrap
<Document
>();
91 bool result
= document
->isPrivilegedContext(message
);
92 errorMessage
= message
;
96 WebString
WebDocument::encoding() const
98 return constUnwrap
<Document
>()->encodingName();
101 WebString
WebDocument::contentLanguage() const
103 return constUnwrap
<Document
>()->contentLanguage();
106 WebString
WebDocument::referrer() const
108 return constUnwrap
<Document
>()->referrer();
111 WebColor
WebDocument::themeColor() const
113 return constUnwrap
<Document
>()->themeColor().rgb();
116 WebURL
WebDocument::openSearchDescriptionURL() const
118 return const_cast<Document
*>(constUnwrap
<Document
>())->openSearchDescriptionURL();
121 WebLocalFrame
* WebDocument::frame() const
123 return WebLocalFrameImpl::fromFrame(constUnwrap
<Document
>()->frame());
126 bool WebDocument::isHTMLDocument() const
128 return constUnwrap
<Document
>()->isHTMLDocument();
131 bool WebDocument::isXHTMLDocument() const
133 return constUnwrap
<Document
>()->isXHTMLDocument();
136 bool WebDocument::isPluginDocument() const
138 return constUnwrap
<Document
>()->isPluginDocument();
141 WebURL
WebDocument::baseURL() const
143 return constUnwrap
<Document
>()->baseURL();
146 WebURL
WebDocument::firstPartyForCookies() const
148 return constUnwrap
<Document
>()->firstPartyForCookies();
151 WebElement
WebDocument::documentElement() const
153 return WebElement(constUnwrap
<Document
>()->documentElement());
156 WebElement
WebDocument::body() const
158 return WebElement(constUnwrap
<Document
>()->body());
161 WebElement
WebDocument::head()
163 return WebElement(unwrap
<Document
>()->head());
166 WebString
WebDocument::title() const
168 return WebString(constUnwrap
<Document
>()->title());
171 WebString
WebDocument::contentAsTextForTesting() const
173 if (Element
* documentElement
= constUnwrap
<Document
>()->documentElement())
174 return WebString(documentElement
->innerText());
178 WebElementCollection
WebDocument::all()
180 return WebElementCollection(unwrap
<Document
>()->all());
183 void WebDocument::forms(WebVector
<WebFormElement
>& results
) const
185 RefPtrWillBeRawPtr
<HTMLCollection
> forms
= const_cast<Document
*>(constUnwrap
<Document
>())->forms();
186 size_t sourceLength
= forms
->length();
187 Vector
<WebFormElement
> temp
;
188 temp
.reserveCapacity(sourceLength
);
189 for (size_t i
= 0; i
< sourceLength
; ++i
) {
190 Element
* element
= forms
->item(i
);
191 // Strange but true, sometimes node can be 0.
192 if (element
&& element
->isHTMLElement())
193 temp
.append(WebFormElement(toHTMLFormElement(element
)));
195 results
.assign(temp
);
198 WebURL
WebDocument::completeURL(const WebString
& partialURL
) const
200 return constUnwrap
<Document
>()->completeURL(partialURL
);
203 WebElement
WebDocument::getElementById(const WebString
& id
) const
205 return WebElement(constUnwrap
<Document
>()->getElementById(id
));
208 WebElement
WebDocument::focusedElement() const
210 return WebElement(constUnwrap
<Document
>()->focusedElement());
213 WebDocumentType
WebDocument::doctype() const
215 return WebDocumentType(constUnwrap
<Document
>()->doctype());
218 void WebDocument::insertStyleSheet(const WebString
& sourceCode
)
220 RefPtrWillBeRawPtr
<Document
> document
= unwrap
<Document
>();
222 RefPtrWillBeRawPtr
<StyleSheetContents
> parsedSheet
= StyleSheetContents::create(CSSParserContext(*document
, 0));
223 parsedSheet
->parseString(sourceCode
);
224 document
->styleEngine().addAuthorSheet(parsedSheet
);
227 void WebDocument::watchCSSSelectors(const WebVector
<WebString
>& webSelectors
)
229 RefPtrWillBeRawPtr
<Document
> document
= unwrap
<Document
>();
230 CSSSelectorWatch
* watch
= CSSSelectorWatch::fromIfExists(*document
);
231 if (!watch
&& webSelectors
.isEmpty())
233 Vector
<String
> selectors
;
234 selectors
.append(webSelectors
.data(), webSelectors
.size());
235 CSSSelectorWatch::from(*document
).watchCSSSelectors(selectors
);
238 void WebDocument::cancelFullScreen()
240 Fullscreen::fullyExitFullscreen(*unwrap
<Document
>());
243 WebElement
WebDocument::fullScreenElement() const
245 Element
* fullScreenElement
= 0;
246 if (Fullscreen
* fullscreen
= Fullscreen::fromIfExists(*const_cast<WebDocument
*>(this)->unwrap
<Document
>()))
247 fullScreenElement
= fullscreen
->webkitCurrentFullScreenElement();
248 return WebElement(fullScreenElement
);
251 WebDOMEvent
WebDocument::createEvent(const WebString
& eventType
)
253 TrackExceptionState exceptionState
;
254 WebDOMEvent
event(unwrap
<Document
>()->createEvent(eventType
, exceptionState
));
255 if (exceptionState
.hadException())
256 return WebDOMEvent();
260 WebReferrerPolicy
WebDocument::referrerPolicy() const
262 return static_cast<WebReferrerPolicy
>(constUnwrap
<Document
>()->referrerPolicy());
265 WebString
WebDocument::outgoingReferrer()
267 return WebString(unwrap
<Document
>()->outgoingReferrer());
270 WebAXObject
WebDocument::accessibilityObject() const
272 const Document
* document
= constUnwrap
<Document
>();
273 AXObjectCacheImpl
* cache
= toAXObjectCacheImpl(document
->axObjectCache());
274 return cache
? WebAXObject(cache
->getOrCreate(document
->layoutView())) : WebAXObject();
277 WebAXObject
WebDocument::accessibilityObjectFromID(int axID
) const
279 const Document
* document
= constUnwrap
<Document
>();
280 AXObjectCacheImpl
* cache
= toAXObjectCacheImpl(document
->axObjectCache());
281 return cache
? WebAXObject(cache
->objectFromAXID(axID
)) : WebAXObject();
284 WebVector
<WebDraggableRegion
> WebDocument::draggableRegions() const
286 WebVector
<WebDraggableRegion
> draggableRegions
;
287 const Document
* document
= constUnwrap
<Document
>();
288 if (document
->hasAnnotatedRegions()) {
289 const Vector
<AnnotatedRegionValue
>& regions
= document
->annotatedRegions();
290 draggableRegions
= WebVector
<WebDraggableRegion
>(regions
.size());
291 for (size_t i
= 0; i
< regions
.size(); i
++) {
292 const AnnotatedRegionValue
& value
= regions
[i
];
293 draggableRegions
[i
].draggable
= value
.draggable
;
294 draggableRegions
[i
].bounds
= IntRect(value
.bounds
);
297 return draggableRegions
;
300 v8::Local
<v8::Value
> WebDocument::registerEmbedderCustomElement(const WebString
& name
, v8::Local
<v8::Value
> options
, WebExceptionCode
& ec
)
302 v8::Isolate
* isolate
= v8::Isolate::GetCurrent();
303 Document
* document
= unwrap
<Document
>();
304 TrackExceptionState exceptionState
;
305 ElementRegistrationOptions registrationOptions
;
306 V8ElementRegistrationOptions::toImpl(isolate
, options
, registrationOptions
, exceptionState
);
307 if (exceptionState
.hadException())
308 return v8::Local
<v8::Value
>();
309 ScriptValue constructor
= document
->registerElement(ScriptState::current(isolate
), name
, registrationOptions
, exceptionState
, CustomElement::EmbedderNames
);
310 ec
= exceptionState
.code();
311 if (exceptionState
.hadException())
312 return v8::Local
<v8::Value
>();
313 return constructor
.v8Value();
316 WebURL
WebDocument::manifestURL() const
318 const Document
* document
= constUnwrap
<Document
>();
319 HTMLLinkElement
* linkElement
= document
->linkManifest();
322 return linkElement
->href();
325 bool WebDocument::manifestUseCredentials() const
327 const Document
* document
= constUnwrap
<Document
>();
328 HTMLLinkElement
* linkElement
= document
->linkManifest();
331 return equalIgnoringCase(linkElement
->fastGetAttribute(HTMLNames::crossoriginAttr
), "use-credentials");
334 WebDocument::WebDocument(const PassRefPtrWillBeRawPtr
<Document
>& elem
)
339 WebDocument
& WebDocument::operator=(const PassRefPtrWillBeRawPtr
<Document
>& elem
)
345 WebDocument::operator PassRefPtrWillBeRawPtr
<Document
>() const
347 return toDocument(m_private
.get());