initial import
[vuplus_webkit] / Source / WebKit / chromium / src / WebDocument.cpp
1 /*
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
34 #include "AXObjectCache.h"
35 #include "CSSStyleSheet.h"
36 #include "Document.h"
37 #include "DocumentLoader.h"
38 #include "DocumentType.h"
39 #include "Element.h"
40 #include "HTMLAllCollection.h"
41 #include "HTMLBodyElement.h"
42 #include "HTMLCollection.h"
43 #include "HTMLElement.h"
44 #include "HTMLFormElement.h"
45 #include "HTMLHeadElement.h"
46 #include "NodeList.h"
47 #include "SecurityOrigin.h"
48 #include "WebAccessibilityObject.h"
49 #include "WebDocumentType.h"
50 #include "WebElement.h"
51 #include "WebFormElement.h"
52 #include "WebFrameImpl.h"
53 #include "WebNodeCollection.h"
54 #include "WebNodeList.h"
55 #include "WebURL.h"
56
57 #include <wtf/PassRefPtr.h>
58
59 using namespace WebCore;
60
61 namespace WebKit {
62
63 WebURL WebDocument::url() const
64 {
65     return constUnwrap<Document>()->url();
66 }
67
68 WebSecurityOrigin WebDocument::securityOrigin() const 
69 {
70     if (!constUnwrap<Document>())
71         return WebSecurityOrigin();
72     return WebSecurityOrigin(constUnwrap<Document>()->securityOrigin());
73 }
74
75 WebString WebDocument::encoding() const
76 {
77     return constUnwrap<Document>()->encoding();
78 }
79
80 WebURL WebDocument::openSearchDescriptionURL() const
81 {
82     return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
83 }
84
85 WebFrame* WebDocument::frame() const
86 {
87     return WebFrameImpl::fromFrame(constUnwrap<Document>()->frame());
88 }
89
90 bool WebDocument::isHTMLDocument() const
91 {  
92     return constUnwrap<Document>()->isHTMLDocument();
93 }
94
95 bool WebDocument::isXHTMLDocument() const
96 {
97     return constUnwrap<Document>()->isXHTMLDocument();
98 }
99
100 bool WebDocument::isPluginDocument() const
101 {  
102     return constUnwrap<Document>()->isPluginDocument();
103 }
104
105 WebURL WebDocument::baseURL() const
106 {
107     return constUnwrap<Document>()->baseURL();
108 }
109
110 WebURL WebDocument::firstPartyForCookies() const
111 {
112     return constUnwrap<Document>()->firstPartyForCookies();
113 }
114
115 WebElement WebDocument::documentElement() const
116 {
117     return WebElement(constUnwrap<Document>()->documentElement());
118 }
119
120 WebElement WebDocument::body() const
121 {
122     return WebElement(constUnwrap<Document>()->body());
123 }
124
125 WebElement WebDocument::head()
126 {
127     return WebElement(unwrap<Document>()->head());
128 }
129
130 WebString WebDocument::title() const
131 {
132     return WebString(constUnwrap<Document>()->title());
133 }
134
135 WebNodeCollection WebDocument::all()
136 {
137     return WebNodeCollection(unwrap<Document>()->all());
138 }
139
140 void WebDocument::forms(WebVector<WebFormElement>& results) const
141 {
142     RefPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
143     size_t sourceLength = forms->length();
144     Vector<WebFormElement> temp;
145     temp.reserveCapacity(sourceLength);
146     for (size_t i = 0; i < sourceLength; ++i) {
147         Node* node = forms->item(i);
148         // Strange but true, sometimes node can be 0.
149         if (node && node->isHTMLElement())
150             temp.append(WebFormElement(static_cast<HTMLFormElement*>(node)));
151     }
152     results.assign(temp);
153 }
154
155 WebURL WebDocument::completeURL(const WebString& partialURL) const
156 {
157     return constUnwrap<Document>()->completeURL(partialURL);
158 }
159
160 WebElement WebDocument::getElementById(const WebString& id) const
161 {
162     return WebElement(constUnwrap<Document>()->getElementById(id));
163 }
164
165 WebNode WebDocument::focusedNode() const
166 {
167     return WebNode(constUnwrap<Document>()->focusedNode());
168 }
169
170 WebDocumentType WebDocument::doctype() const
171 {
172     return WebDocumentType(constUnwrap<Document>()->doctype());
173 }
174
175 WebAccessibilityObject WebDocument::accessibilityObject() const
176 {
177     const Document* document = constUnwrap<Document>();
178     return WebAccessibilityObject(
179         document->axObjectCache()->getOrCreate(document->renderer()));
180 }
181
182 void WebDocument::insertUserStyleSheet(const WebString& sourceCode, UserStyleLevel level)
183 {
184     RefPtr<Document> document = unwrap<Document>();
185
186     RefPtr<CSSStyleSheet> parsedSheet = CSSStyleSheet::create(document.get());
187     parsedSheet->setIsUserStyleSheet(level == UserStyleUserLevel);
188     parsedSheet->parseString(sourceCode, !document->inQuirksMode());
189     document->addUserSheet(parsedSheet.release());
190 }
191
192 void WebDocument::cancelFullScreen()
193 {
194 #if ENABLE(FULLSCREEN_API)
195     unwrap<Document>()->webkitCancelFullScreen();
196 #endif
197 }
198
199 WebDocument::WebDocument(const PassRefPtr<Document>& elem)
200     : WebNode(elem)
201 {
202 }
203
204 WebDocument& WebDocument::operator=(const PassRefPtr<Document>& elem)
205 {
206     m_private = elem;
207     return *this;
208 }
209
210 WebDocument::operator PassRefPtr<Document>() const
211 {
212     return static_cast<Document*>(m_private.get());
213 }
214
215 } // namespace WebKit