Home | History | Annotate | Download | only in web
      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 "public/platform/WebURL.h"
     35 #include "wtf/PassRefPtr.h"
     36 #include "WebAccessibilityObject.h"
     37 #include "WebDOMEvent.h"
     38 #include "WebDocumentType.h"
     39 #include "WebElement.h"
     40 #include "WebFormElement.h"
     41 #include "WebFrameImpl.h"
     42 #include "WebNodeCollection.h"
     43 #include "WebNodeList.h"
     44 #include "bindings/v8/Dictionary.h"
     45 #include "bindings/v8/ExceptionState.h"
     46 #include "bindings/v8/ScriptState.h"
     47 #include "bindings/v8/ScriptValue.h"
     48 #include "core/accessibility/AXObjectCache.h"
     49 #include "core/css/CSSParserMode.h"
     50 #include "core/css/StyleSheetContents.h"
     51 #include "core/dom/Document.h"
     52 #include "core/dom/DocumentStyleSheetCollection.h"
     53 #include "core/dom/DocumentType.h"
     54 #include "core/dom/Element.h"
     55 #include "core/dom/FullscreenElementStack.h"
     56 #include "core/dom/NodeList.h"
     57 #include "core/html/HTMLAllCollection.h"
     58 #include "core/html/HTMLBodyElement.h"
     59 #include "core/html/HTMLCollection.h"
     60 #include "core/html/HTMLElement.h"
     61 #include "core/html/HTMLFormElement.h"
     62 #include "core/html/HTMLHeadElement.h"
     63 #include "core/loader/DocumentLoader.h"
     64 #include "core/rendering/RenderObject.h"
     65 #include "weborigin/SecurityOrigin.h"
     66 #include <v8.h>
     67 
     68 using namespace WebCore;
     69 
     70 namespace WebKit {
     71 
     72 WebURL WebDocument::url() const
     73 {
     74     return constUnwrap<Document>()->url();
     75 }
     76 
     77 WebSecurityOrigin WebDocument::securityOrigin() const
     78 {
     79     if (!constUnwrap<Document>())
     80         return WebSecurityOrigin();
     81     return WebSecurityOrigin(constUnwrap<Document>()->securityOrigin());
     82 }
     83 
     84 WebString WebDocument::encoding() const
     85 {
     86     return constUnwrap<Document>()->encoding();
     87 }
     88 
     89 WebString WebDocument::contentLanguage() const
     90 {
     91     return constUnwrap<Document>()->contentLanguage();
     92 }
     93 
     94 WebURL WebDocument::openSearchDescriptionURL() const
     95 {
     96     return const_cast<Document*>(constUnwrap<Document>())->openSearchDescriptionURL();
     97 }
     98 
     99 WebFrame* WebDocument::frame() const
    100 {
    101     return WebFrameImpl::fromFrame(constUnwrap<Document>()->frame());
    102 }
    103 
    104 bool WebDocument::isHTMLDocument() const
    105 {
    106     return constUnwrap<Document>()->isHTMLDocument();
    107 }
    108 
    109 bool WebDocument::isXHTMLDocument() const
    110 {
    111     return constUnwrap<Document>()->isXHTMLDocument();
    112 }
    113 
    114 bool WebDocument::isPluginDocument() const
    115 {
    116     return constUnwrap<Document>()->isPluginDocument();
    117 }
    118 
    119 WebURL WebDocument::baseURL() const
    120 {
    121     return constUnwrap<Document>()->baseURL();
    122 }
    123 
    124 WebURL WebDocument::firstPartyForCookies() const
    125 {
    126     return constUnwrap<Document>()->firstPartyForCookies();
    127 }
    128 
    129 WebElement WebDocument::documentElement() const
    130 {
    131     return WebElement(constUnwrap<Document>()->documentElement());
    132 }
    133 
    134 WebElement WebDocument::body() const
    135 {
    136     return WebElement(constUnwrap<Document>()->body());
    137 }
    138 
    139 WebElement WebDocument::head()
    140 {
    141     return WebElement(unwrap<Document>()->head());
    142 }
    143 
    144 WebString WebDocument::title() const
    145 {
    146     return WebString(constUnwrap<Document>()->title());
    147 }
    148 
    149 WebNodeCollection WebDocument::all()
    150 {
    151     return WebNodeCollection(unwrap<Document>()->all());
    152 }
    153 
    154 void WebDocument::images(WebVector<WebElement>& results)
    155 {
    156     RefPtr<HTMLCollection> images = unwrap<Document>()->images();
    157     size_t sourceLength = images->length();
    158     Vector<WebElement> temp;
    159     temp.reserveCapacity(sourceLength);
    160     for (size_t i = 0; i < sourceLength; ++i) {
    161         Node* node = images->item(i);
    162         if (node && node->isHTMLElement())
    163             temp.append(WebElement(toElement(node)));
    164     }
    165     results.assign(temp);
    166 }
    167 
    168 void WebDocument::forms(WebVector<WebFormElement>& results) const
    169 {
    170     RefPtr<HTMLCollection> forms = const_cast<Document*>(constUnwrap<Document>())->forms();
    171     size_t sourceLength = forms->length();
    172     Vector<WebFormElement> temp;
    173     temp.reserveCapacity(sourceLength);
    174     for (size_t i = 0; i < sourceLength; ++i) {
    175         Node* node = forms->item(i);
    176         // Strange but true, sometimes node can be 0.
    177         if (node && node->isHTMLElement())
    178             temp.append(WebFormElement(toHTMLFormElement(node)));
    179     }
    180     results.assign(temp);
    181 }
    182 
    183 WebURL WebDocument::completeURL(const WebString& partialURL) const
    184 {
    185     return constUnwrap<Document>()->completeURL(partialURL);
    186 }
    187 
    188 WebElement WebDocument::getElementById(const WebString& id) const
    189 {
    190     return WebElement(constUnwrap<Document>()->getElementById(id));
    191 }
    192 
    193 WebNode WebDocument::focusedNode() const
    194 {
    195     return WebNode(constUnwrap<Document>()->focusedElement());
    196 }
    197 
    198 WebDocumentType WebDocument::doctype() const
    199 {
    200     return WebDocumentType(constUnwrap<Document>()->doctype());
    201 }
    202 
    203 void WebDocument::insertUserStyleSheet(const WebString& sourceCode, UserStyleLevel styleLevel)
    204 {
    205     RefPtr<Document> document = unwrap<Document>();
    206 
    207     RefPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(document.get());
    208     parsedSheet->setIsUserStyleSheet(styleLevel == UserStyleUserLevel);
    209     parsedSheet->parseString(sourceCode);
    210     if (parsedSheet->isUserStyleSheet())
    211         document->styleSheetCollection()->addUserSheet(parsedSheet);
    212     else
    213         document->styleSheetCollection()->addAuthorSheet(parsedSheet);
    214 }
    215 
    216 void WebDocument::cancelFullScreen()
    217 {
    218     if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(unwrap<Document>()))
    219         fullscreen->webkitCancelFullScreen();
    220 }
    221 
    222 WebElement WebDocument::fullScreenElement() const
    223 {
    224     Element* fullScreenElement = 0;
    225     if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(const_cast<WebDocument*>(this)->unwrap<Document>()))
    226         fullScreenElement = fullscreen->webkitCurrentFullScreenElement();
    227     return WebElement(fullScreenElement);
    228 }
    229 
    230 WebDOMEvent WebDocument::createEvent(const WebString& eventType)
    231 {
    232     TrackExceptionState es;
    233     WebDOMEvent event(unwrap<Document>()->createEvent(eventType, es));
    234     if (es.hadException())
    235         return WebDOMEvent();
    236     return event;
    237 }
    238 
    239 WebReferrerPolicy WebDocument::referrerPolicy() const
    240 {
    241     return static_cast<WebReferrerPolicy>(constUnwrap<Document>()->referrerPolicy());
    242 }
    243 
    244 WebElement WebDocument::createElement(const WebString& tagName)
    245 {
    246     TrackExceptionState es;
    247     WebElement element(unwrap<Document>()->createElement(tagName, es));
    248     if (es.hadException())
    249         return WebElement();
    250     return element;
    251 }
    252 
    253 WebAccessibilityObject WebDocument::accessibilityObject() const
    254 {
    255     const Document* document = constUnwrap<Document>();
    256     return WebAccessibilityObject(
    257         document->axObjectCache()->getOrCreate(document->renderer()));
    258 }
    259 
    260 WebAccessibilityObject WebDocument::accessibilityObjectFromID(int axID) const
    261 {
    262     const Document* document = constUnwrap<Document>();
    263     return WebAccessibilityObject(
    264         document->axObjectCache()->objectFromAXID(axID));
    265 }
    266 
    267 WebVector<WebDraggableRegion> WebDocument::draggableRegions() const
    268 {
    269     WebVector<WebDraggableRegion> draggableRegions;
    270     const Document* document = constUnwrap<Document>();
    271     if (document->hasAnnotatedRegions()) {
    272         const Vector<AnnotatedRegionValue>& regions = document->annotatedRegions();
    273         draggableRegions = WebVector<WebDraggableRegion>(regions.size());
    274         for (size_t i = 0; i < regions.size(); i++) {
    275             const AnnotatedRegionValue& value = regions[i];
    276             draggableRegions[i].draggable = value.draggable;
    277             draggableRegions[i].bounds = WebCore::IntRect(value.bounds);
    278         }
    279     }
    280     return draggableRegions;
    281 }
    282 
    283 v8::Handle<v8::Value> WebDocument::registerEmbedderCustomElement(const WebString& name, v8::Handle<v8::Value> options, WebExceptionCode& ec)
    284 {
    285     Document* document = unwrap<Document>();
    286     Dictionary dictionary(options, v8::Isolate::GetCurrent());
    287     TrackExceptionState es;
    288     ScriptValue constructor = document->registerElement(ScriptState::current(), name, dictionary, es, CustomElement::EmbedderNames);
    289     ec = es.code();
    290     if (es.hadException())
    291         return v8::Handle<v8::Value>();
    292     return constructor.v8Value();
    293 }
    294 
    295 WebDocument::WebDocument(const PassRefPtr<Document>& elem)
    296     : WebNode(elem)
    297 {
    298 }
    299 
    300 WebDocument& WebDocument::operator=(const PassRefPtr<Document>& elem)
    301 {
    302     m_private = elem;
    303     return *this;
    304 }
    305 
    306 WebDocument::operator PassRefPtr<Document>() const
    307 {
    308     return toDocument(m_private.get());
    309 }
    310 
    311 } // namespace WebKit
    312