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 "WebDocument.h"
34 #include "WebAXObject.h"
35 #include "WebDOMEvent.h"
36 #include "WebDocumentType.h"
37 #include "WebElement.h"
38 #include "WebFormElement.h"
39 #include "WebFrameImpl.h"
40 #include "WebNodeCollection.h"
41 #include "WebNodeList.h"
42 #include "bindings/v8/Dictionary.h"
43 #include "bindings/v8/ExceptionState.h"
44 #include "bindings/v8/ScriptState.h"
45 #include "bindings/v8/ScriptValue.h"
46 #include "core/accessibility/AXObjectCache.h"
47 #include "core/css/CSSParserMode.h"
48 #include "core/css/StyleSheetContents.h"
49 #include "core/dom/CSSSelectorWatch.h"
50 #include "core/dom/Document.h"
51 #include "core/dom/DocumentType.h"
52 #include "core/dom/Element.h"
53 #include "core/dom/FullscreenElementStack.h"
54 #include "core/dom/NodeList.h"
55 #include "core/dom/StyleEngine.h"
56 #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"
63 #include "core/rendering/RenderObject.h"
64 #include "public/platform/WebURL.h"
65 #include "weborigin/SecurityOrigin.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 WebURL WebDocument::openSearchDescriptionURL() const
102 return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
105 WebFrame* WebDocument::frame() const
107 return WebFrameImpl::fromFrame(constUnwrap<Document>()->frame());
110 bool WebDocument::isHTMLDocument() const
112 return constUnwrap<Document>()->isHTMLDocument();
115 bool WebDocument::isXHTMLDocument() const
117 return constUnwrap<Document>()->isXHTMLDocument();
120 bool WebDocument::isPluginDocument() const
122 return constUnwrap<Document>()->isPluginDocument();
125 WebURL WebDocument::baseURL() const
127 return constUnwrap<Document>()->baseURL();
130 WebURL WebDocument::firstPartyForCookies() const
132 return constUnwrap<Document>()->firstPartyForCookies();
135 WebElement WebDocument::documentElement() const
137 return WebElement(constUnwrap<Document>()->documentElement());
140 WebElement WebDocument::body() const
142 return WebElement(constUnwrap<Document>()->body());
145 WebElement WebDocument::head()
147 return WebElement(unwrap<Document>()->head());
150 WebString WebDocument::title() const
152 return WebString(constUnwrap<Document>()->title());
155 WebNodeCollection WebDocument::all()
157 return WebNodeCollection(unwrap<Document>()->all());
160 void WebDocument::images(WebVector<WebElement>& results)
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) {
167 Node* node = images->item(i);
168 if (node && node->isHTMLElement())
169 temp.append(WebElement(toElement(node)));
171 results.assign(temp);
174 void WebDocument::forms(WebVector<WebFormElement>& results) const
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) {
181 Node* node = forms->item(i);
182 // Strange but true, sometimes node can be 0.
183 if (node && node->isHTMLElement())
184 temp.append(WebFormElement(toHTMLFormElement(node)));
186 results.assign(temp);
189 WebURL WebDocument::completeURL(const WebString& partialURL) const
191 return constUnwrap<Document>()->completeURL(partialURL);
194 WebElement WebDocument::getElementById(const WebString& id) const
196 return WebElement(constUnwrap<Document>()->getElementById(id));
199 WebNode WebDocument::focusedNode() const
201 return WebNode(constUnwrap<Document>()->focusedElement());
204 WebDocumentType WebDocument::doctype() const
206 return WebDocumentType(constUnwrap<Document>()->doctype());
209 void WebDocument::insertUserStyleSheet(const WebString& sourceCode, UserStyleLevel styleLevel)
211 RefPtr<Document> document = unwrap<Document>();
213 RefPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(*document.get());
214 parsedSheet->setIsUserStyleSheet(styleLevel == UserStyleUserLevel);
215 parsedSheet->parseString(sourceCode);
216 if (parsedSheet->isUserStyleSheet())
217 document->styleEngine()->addUserSheet(parsedSheet);
219 document->styleEngine()->addAuthorSheet(parsedSheet);
222 void WebDocument::watchCSSSelectors(const WebVector<WebString>& webSelectors)
224 RefPtr<Document> document = unwrap<Document>();
225 Vector<String> selectors;
226 selectors.append(webSelectors.data(), webSelectors.size());
227 CSSSelectorWatch::from(*document).watchCSSSelectors(selectors);
230 void WebDocument::cancelFullScreen()
232 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(unwrap<Document>()))
233 fullscreen->webkitCancelFullScreen();
236 WebElement WebDocument::fullScreenElement() const
238 Element* fullScreenElement = 0;
239 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(const_cast<WebDocument*>(this)->unwrap<Document>()))
240 fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
241 return WebElement(fullScreenElement);
244 WebDOMEvent WebDocument::createEvent(const WebString& eventType)
246 TrackExceptionState es;
247 WebDOMEvent event(unwrap<Document>()->createEvent(eventType, es));
248 if (es.hadException())
249 return WebDOMEvent();
253 WebReferrerPolicy WebDocument::referrerPolicy() const
255 return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
258 WebElement WebDocument::createElement(const WebString& tagName)
260 TrackExceptionState es;
261 WebElement element(unwrap<Document>()->createElement(tagName, es));
262 if (es.hadException())
267 WebAXObject WebDocument::accessibilityObject() const
269 const Document* document = constUnwrap<Document>();
270 return WebAXObject(document->axObjectCache()->getOrCreate(document->renderer()));
273 WebAXObject WebDocument::accessibilityObjectFromID(int axID) const
275 const Document* document = constUnwrap<Document>();
276 return WebAXObject(document->axObjectCache()->objectFromAXID(axID));
279 WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
281 WebVector<WebDraggableRegion> draggableRegions;
282 const Document* document = constUnwrap<Document>();
283 if (document->hasAnnotatedRegions()) {
284 const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
285 draggableRegions = WebVector<WebDraggableRegion>(regions.size());
286 for (size_t i = 0; i < regions.size(); i++) {
287 const AnnotatedRegionValue& value = regions[i];
288 draggableRegions[i].draggable = value.draggable;
289 draggableRegions[i].bounds = WebCore::IntRect(value.bounds);
292 return draggableRegions;
295 v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
297 Document* document = unwrap<Document>();
298 Dictionary dictionary(options, v8::Isolate::GetCurrent());
299 TrackExceptionState es;
300 ScriptValue constructor = document->registerElement(ScriptState::current(), name, dictionary, es, CustomElement::EmbedderNames);
302 if (es.hadException())
303 return v8::Handle<v8::Value>();
304 return constructor.v8Value();
307 WebDocument::WebDocument(const PassRefPtr<Document>& elem)
312 WebDocument& WebDocument::operator=(const PassRefPtr<Document>& elem)
318 WebDocument::operator PassRefPtr<Document>() const
320 return toDocument(m_private.get());
323 } // namespace WebKit