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/accessibility/AXObjectCache.h"
39 #include "core/css/StyleSheetContents.h"
40 #include "core/dom/CSSSelectorWatch.h"
41 #include "core/dom/Document.h"
42 #include "core/dom/DocumentType.h"
43 #include "core/dom/Element.h"
44 #include "core/dom/Fullscreen.h"
45 #include "core/dom/StyleEngine.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/loader/DocumentLoader.h"
54 #include "core/rendering/RenderObject.h"
55 #include "core/rendering/RenderView.h"
56 #include "platform/weborigin/SecurityOrigin.h"
57 #include "public/platform/WebURL.h"
58 #include "public/web/WebAXObject.h"
59 #include "public/web/WebDOMEvent.h"
60 #include "public/web/WebDocumentType.h"
61 #include "public/web/WebElement.h"
62 #include "public/web/WebElementCollection.h"
63 #include "public/web/WebFormElement.h"
64 #include "public/web/WebNodeList.h"
65 #include "web/WebLocalFrameImpl.h"
66 #include "wtf/PassRefPtr.h"
71 WebURL WebDocument::url() const
73 return constUnwrap<Document>()->url();
76 WebSecurityOrigin WebDocument::securityOrigin() const
78 if (!constUnwrap<Document>())
79 return WebSecurityOrigin();
80 return WebSecurityOrigin(constUnwrap<Document>()->securityOrigin());
83 WebString WebDocument::encoding() const
85 return constUnwrap<Document>()->encodingName();
88 WebString WebDocument::contentLanguage() const
90 return constUnwrap<Document>()->contentLanguage();
93 WebString WebDocument::referrer() const
95 return constUnwrap<Document>()->referrer();
98 WebColor WebDocument::themeColor() const
100 return constUnwrap<Document>()->themeColor().rgb();
103 WebURL WebDocument::openSearchDescriptionURL() const
105 return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
108 WebLocalFrame* WebDocument::frame() const
110 return WebLocalFrameImpl::fromFrame(constUnwrap<Document>()->frame());
113 bool WebDocument::isHTMLDocument() const
115 return constUnwrap<Document>()->isHTMLDocument();
118 bool WebDocument::isXHTMLDocument() const
120 return constUnwrap<Document>()->isXHTMLDocument();
123 bool WebDocument::isPluginDocument() const
125 return constUnwrap<Document>()->isPluginDocument();
128 WebURL WebDocument::baseURL() const
130 return constUnwrap<Document>()->baseURL();
133 WebURL WebDocument::firstPartyForCookies() const
135 return constUnwrap<Document>()->firstPartyForCookies();
138 WebElement WebDocument::documentElement() const
140 return WebElement(constUnwrap<Document>()->documentElement());
143 WebElement WebDocument::body() const
145 return WebElement(constUnwrap<Document>()->body());
148 WebElement WebDocument::head()
150 return WebElement(unwrap<Document>()->head());
153 WebString WebDocument::title() const
155 return WebString(constUnwrap<Document>()->title());
158 WebElementCollection WebDocument::all()
160 return WebElementCollection(unwrap<Document>()->all());
163 void WebDocument::images(WebVector<WebElement>& results)
165 RefPtrWillBeRawPtr<HTMLCollection> images = unwrap<Document>()->images();
166 size_t sourceLength = images->length();
167 Vector<WebElement> temp;
168 temp.reserveCapacity(sourceLength);
169 for (size_t i = 0; i < sourceLength; ++i) {
170 Element* element = images->item(i);
171 if (element && element->isHTMLElement())
172 temp.append(WebElement(element));
174 results.assign(temp);
177 void WebDocument::forms(WebVector<WebFormElement>& results) const
179 RefPtrWillBeRawPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
180 size_t sourceLength = forms->length();
181 Vector<WebFormElement> temp;
182 temp.reserveCapacity(sourceLength);
183 for (size_t i = 0; i < sourceLength; ++i) {
184 Element* element = forms->item(i);
185 // Strange but true, sometimes node can be 0.
186 if (element && element->isHTMLElement())
187 temp.append(WebFormElement(toHTMLFormElement(element)));
189 results.assign(temp);
192 WebURL WebDocument::completeURL(const WebString& partialURL) const
194 return constUnwrap<Document>()->completeURL(partialURL);
197 WebElement WebDocument::getElementById(const WebString& id) const
199 return WebElement(constUnwrap<Document>()->getElementById(id));
202 WebElement WebDocument::focusedElement() const
204 return WebElement(constUnwrap<Document>()->focusedElement());
207 WebDocumentType WebDocument::doctype() const
209 return WebDocumentType(constUnwrap<Document>()->doctype());
212 void WebDocument::insertStyleSheet(const WebString& sourceCode)
214 RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
216 RefPtrWillBeRawPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(CSSParserContext(*document, 0));
217 parsedSheet->parseString(sourceCode);
218 document->styleEngine()->addAuthorSheet(parsedSheet);
221 void WebDocument::watchCSSSelectors(const WebVector<WebString>& webSelectors)
223 RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
224 Vector<String> selectors;
225 selectors.append(webSelectors.data(), webSelectors.size());
226 CSSSelectorWatch::from(*document).watchCSSSelectors(selectors);
229 void WebDocument::cancelFullScreen()
231 Fullscreen::fullyExitFullscreen(*unwrap<Document>());
234 WebElement WebDocument::fullScreenElement() const
236 Element* fullScreenElement = 0;
237 if (Fullscreen* fullscreen = Fullscreen::fromIfExists(*const_cast<WebDocument*>(this)->unwrap<Document>()))
238 fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
239 return WebElement(fullScreenElement);
242 WebDOMEvent WebDocument::createEvent(const WebString& eventType)
244 TrackExceptionState exceptionState;
245 WebDOMEvent event(unwrap<Document>()->createEvent(eventType, exceptionState));
246 if (exceptionState.hadException())
247 return WebDOMEvent();
251 WebReferrerPolicy WebDocument::referrerPolicy() const
253 return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
256 WebElement WebDocument::createElement(const WebString& tagName)
258 TrackExceptionState exceptionState;
259 WebElement element(unwrap<Document>()->createElement(tagName, exceptionState));
260 if (exceptionState.hadException())
265 WebSize WebDocument::scrollOffset() const
267 if (FrameView* view = constUnwrap<Document>()->view())
268 return view->scrollOffset();
272 WebSize WebDocument::minimumScrollOffset() const
274 if (FrameView* view = constUnwrap<Document>()->view())
275 return toIntSize(view->minimumScrollPosition());
279 WebSize WebDocument::maximumScrollOffset() const
281 if (FrameView* view = constUnwrap<Document>()->view())
282 return toIntSize(view->maximumScrollPosition());
286 void WebDocument::setIsTransitionDocument()
288 // This ensures the transition UA stylesheet gets applied.
289 unwrap<Document>()->setIsTransitionDocument();
292 void WebDocument::beginExitTransition(const WebString& cssSelector)
294 RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
295 document->hideTransitionElements(cssSelector);
296 document->styleEngine()->enableExitTransitionStylesheets();
299 WebAXObject WebDocument::accessibilityObject() const
301 const Document* document = constUnwrap<Document>();
302 AXObjectCache* cache = document->axObjectCache();
303 return cache ? WebAXObject(cache->getOrCreateAXObjectFromRenderView(document->renderView())) : WebAXObject();
306 WebAXObject WebDocument::accessibilityObjectFromID(int axID) const
308 const Document* document = constUnwrap<Document>();
309 AXObjectCache* cache = document->axObjectCache();
310 return cache ? WebAXObject(cache->objectFromAXID(axID)) : WebAXObject();
313 WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
315 WebVector<WebDraggableRegion> draggableRegions;
316 const Document* document = constUnwrap<Document>();
317 if (document->hasAnnotatedRegions()) {
318 const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
319 draggableRegions = WebVector<WebDraggableRegion>(regions.size());
320 for (size_t i = 0; i < regions.size(); i++) {
321 const AnnotatedRegionValue& value = regions[i];
322 draggableRegions[i].draggable = value.draggable;
323 draggableRegions[i].bounds = IntRect(value.bounds);
326 return draggableRegions;
329 v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
331 v8::Isolate* isolate = v8::Isolate::GetCurrent();
332 Document* document = unwrap<Document>();
333 TrackExceptionState exceptionState;
334 ElementRegistrationOptions registrationOptions;
335 V8ElementRegistrationOptions::toImpl(isolate, options, registrationOptions, exceptionState);
336 if (exceptionState.hadException())
337 return v8::Handle<v8::Value>();
338 ScriptValue constructor = document->registerElement(ScriptState::current(isolate), name, registrationOptions, exceptionState, CustomElement::EmbedderNames);
339 ec = exceptionState.code();
340 if (exceptionState.hadException())
341 return v8::Handle<v8::Value>();
342 return constructor.v8Value();
345 WebURL WebDocument::manifestURL() const
347 const Document* document = constUnwrap<Document>();
348 HTMLLinkElement* linkElement = document->linkManifest();
351 return linkElement->href();
354 WebDocument::WebDocument(const PassRefPtrWillBeRawPtr<Document>& elem)
359 WebDocument& WebDocument::operator=(const PassRefPtrWillBeRawPtr<Document>& elem)
365 WebDocument::operator PassRefPtrWillBeRawPtr<Document>() const
367 return toDocument(m_private.get());