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/v8/Dictionary.h"
35 #include "bindings/v8/ExceptionState.h"
36 #include "bindings/v8/ScriptState.h"
37 #include "bindings/v8/ScriptValue.h"
38 #include "core/accessibility/AXObjectCache.h"
39 #include "core/css/CSSParserMode.h"
40 #include "core/css/StyleSheetContents.h"
41 #include "core/dom/CSSSelectorWatch.h"
42 #include "core/dom/Document.h"
43 #include "core/dom/DocumentType.h"
44 #include "core/dom/Element.h"
45 #include "core/dom/FullscreenElementStack.h"
46 #include "core/dom/StyleEngine.h"
47 #include "core/html/HTMLAllCollection.h"
48 #include "core/html/HTMLBodyElement.h"
49 #include "core/html/HTMLCollection.h"
50 #include "core/html/HTMLElement.h"
51 #include "core/html/HTMLFormElement.h"
52 #include "core/html/HTMLHeadElement.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"
69 using namespace WebCore;
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 WebString WebDocument::encoding() const
87 return constUnwrap<Document>()->encodingName();
90 WebString WebDocument::contentLanguage() const
92 return constUnwrap<Document>()->contentLanguage();
95 WebString WebDocument::referrer() const
97 return constUnwrap<Document>()->referrer();
100 WebColor WebDocument::themeColor() const
102 return constUnwrap<Document>()->themeColor().rgb();
105 WebURL WebDocument::openSearchDescriptionURL() const
107 return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
110 WebLocalFrame* WebDocument::frame() const
112 return WebLocalFrameImpl::fromFrame(constUnwrap<Document>()->frame());
115 bool WebDocument::isHTMLDocument() const
117 return constUnwrap<Document>()->isHTMLDocument();
120 bool WebDocument::isXHTMLDocument() const
122 return constUnwrap<Document>()->isXHTMLDocument();
125 bool WebDocument::isPluginDocument() const
127 return constUnwrap<Document>()->isPluginDocument();
130 WebURL WebDocument::baseURL() const
132 return constUnwrap<Document>()->baseURL();
135 WebURL WebDocument::firstPartyForCookies() const
137 return constUnwrap<Document>()->firstPartyForCookies();
140 WebElement WebDocument::documentElement() const
142 return WebElement(constUnwrap<Document>()->documentElement());
145 WebElement WebDocument::body() const
147 return WebElement(constUnwrap<Document>()->body());
150 WebElement WebDocument::head()
152 return WebElement(unwrap<Document>()->head());
155 WebString WebDocument::title() const
157 return WebString(constUnwrap<Document>()->title());
160 WebElementCollection WebDocument::all()
162 return WebElementCollection(unwrap<Document>()->all());
165 void WebDocument::images(WebVector<WebElement>& results)
167 RefPtrWillBeRawPtr<HTMLCollection> images = unwrap<Document>()->images();
168 size_t sourceLength = images->length();
169 Vector<WebElement> temp;
170 temp.reserveCapacity(sourceLength);
171 for (size_t i = 0; i < sourceLength; ++i) {
172 Element* element = images->item(i);
173 if (element && element->isHTMLElement())
174 temp.append(WebElement(element));
176 results.assign(temp);
179 void WebDocument::forms(WebVector<WebFormElement>& results) const
181 RefPtrWillBeRawPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
182 size_t sourceLength = forms->length();
183 Vector<WebFormElement> temp;
184 temp.reserveCapacity(sourceLength);
185 for (size_t i = 0; i < sourceLength; ++i) {
186 Element* element = forms->item(i);
187 // Strange but true, sometimes node can be 0.
188 if (element && element->isHTMLElement())
189 temp.append(WebFormElement(toHTMLFormElement(element)));
191 results.assign(temp);
194 WebURL WebDocument::completeURL(const WebString& partialURL) const
196 return constUnwrap<Document>()->completeURL(partialURL);
199 WebElement WebDocument::getElementById(const WebString& id) const
201 return WebElement(constUnwrap<Document>()->getElementById(id));
204 WebElement WebDocument::focusedElement() const
206 return WebElement(constUnwrap<Document>()->focusedElement());
209 WebDocumentType WebDocument::doctype() const
211 return WebDocumentType(constUnwrap<Document>()->doctype());
214 void WebDocument::insertStyleSheet(const WebString& sourceCode)
216 RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
218 RefPtrWillBeRawPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(CSSParserContext(*document, 0));
219 parsedSheet->parseString(sourceCode);
220 document->styleEngine()->addAuthorSheet(parsedSheet);
223 void WebDocument::watchCSSSelectors(const WebVector<WebString>& webSelectors)
225 RefPtrWillBeRawPtr<Document> document = unwrap<Document>();
226 Vector<String> selectors;
227 selectors.append(webSelectors.data(), webSelectors.size());
228 CSSSelectorWatch::from(*document).watchCSSSelectors(selectors);
231 void WebDocument::cancelFullScreen()
233 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*unwrap<Document>()))
234 fullscreen->webkitCancelFullScreen();
237 WebElement WebDocument::fullScreenElement() const
239 Element* fullScreenElement = 0;
240 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*const_cast<WebDocument*>(this)->unwrap<Document>()))
241 fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
242 return WebElement(fullScreenElement);
245 WebDOMEvent WebDocument::createEvent(const WebString& eventType)
247 TrackExceptionState exceptionState;
248 WebDOMEvent event(unwrap<Document>()->createEvent(eventType, exceptionState));
249 if (exceptionState.hadException())
250 return WebDOMEvent();
254 WebReferrerPolicy WebDocument::referrerPolicy() const
256 return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
259 WebElement WebDocument::createElement(const WebString& tagName)
261 TrackExceptionState exceptionState;
262 WebElement element(unwrap<Document>()->createElement(tagName, exceptionState));
263 if (exceptionState.hadException())
268 WebAXObject WebDocument::accessibilityObject() const
270 const Document* document = constUnwrap<Document>();
271 return WebAXObject(document->axObjectCache()->getOrCreate(document->renderView()));
274 WebAXObject WebDocument::accessibilityObjectFromID(int axID) const
276 const Document* document = constUnwrap<Document>();
277 return WebAXObject(document->axObjectCache()->objectFromAXID(axID));
280 WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
282 WebVector<WebDraggableRegion> draggableRegions;
283 const Document* document = constUnwrap<Document>();
284 if (document->hasAnnotatedRegions()) {
285 const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
286 draggableRegions = WebVector<WebDraggableRegion>(regions.size());
287 for (size_t i = 0; i < regions.size(); i++) {
288 const AnnotatedRegionValue& value = regions[i];
289 draggableRegions[i].draggable = value.draggable;
290 draggableRegions[i].bounds = WebCore::IntRect(value.bounds);
293 return draggableRegions;
296 v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
298 v8::Isolate* isolate = v8::Isolate::GetCurrent();
299 Document* document = unwrap<Document>();
300 Dictionary dictionary(options, isolate);
301 TrackExceptionState exceptionState;
302 ScriptValue constructor = document->registerElement(ScriptState::current(isolate), name, dictionary, exceptionState, CustomElement::EmbedderNames);
303 ec = exceptionState.code();
304 if (exceptionState.hadException())
305 return v8::Handle<v8::Value>();
306 return constructor.v8Value();
309 WebDocument::WebDocument(const PassRefPtrWillBeRawPtr<Document>& elem)
314 WebDocument& WebDocument::operator=(const PassRefPtrWillBeRawPtr<Document>& elem)
320 WebDocument::operator PassRefPtrWillBeRawPtr<Document>() const
322 return toDocument(m_private.get());