WebCore/html/HTMLDocument.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     4  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Library General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Library General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Library General Public License
       
    17  * along with this library; see the file COPYING.LIB.  If not, write to
       
    18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19  * Boston, MA 02110-1301, USA.
       
    20  *
       
    21  * Portions are Copyright (C) 2002 Netscape Communications Corporation.
       
    22  * Other contributors: David Baron <dbaron@fas.harvard.edu>
       
    23  *
       
    24  * This library is free software; you can redistribute it and/or
       
    25  * modify it under the terms of the GNU Lesser General Public
       
    26  * License as published by the Free Software Foundation; either
       
    27  * version 2.1 of the License, or (at your option) any later version.
       
    28  *
       
    29  * This library is distributed in the hope that it will be useful,
       
    30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    32  * Lesser General Public License for more details.
       
    33  *
       
    34  * You should have received a copy of the GNU Lesser General Public
       
    35  * License along with this library; if not, write to the Free Software
       
    36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
       
    37  *
       
    38  * Alternatively, the document type parsing portions of this file may be used
       
    39  * under the terms of either the Mozilla Public License Version 1.1, found at
       
    40  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
       
    41  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
       
    42  * (the "GPL"), in which case the provisions of the MPL or the GPL are
       
    43  * applicable instead of those above.  If you wish to allow use of your
       
    44  * version of this file only under the terms of one of those two
       
    45  * licenses (the MPL or the GPL) and not to allow others to use your
       
    46  * version of this file under the LGPL, indicate your decision by
       
    47  * deleting the provisions above and replace them with the notice and
       
    48  * other provisions required by the MPL or the GPL, as the case may be.
       
    49  * If you do not delete the provisions above, a recipient may use your
       
    50  * version of this file under any of the LGPL, the MPL or the GPL.
       
    51  */
       
    52 
       
    53 #include "config.h"
       
    54 #include "HTMLDocument.h"
       
    55 
       
    56 #include "CSSPropertyNames.h"
       
    57 #include "CSSStyleSelector.h"
       
    58 #include "CookieJar.h"
       
    59 #include "DocumentLoader.h"
       
    60 #include "DocumentType.h"
       
    61 #include "ExceptionCode.h"
       
    62 #include "FocusController.h"
       
    63 #include "Frame.h"
       
    64 #include "FrameLoader.h"
       
    65 #include "FrameTree.h"
       
    66 #include "FrameView.h"
       
    67 #include "HTMLDocumentParser.h"
       
    68 #include "HTMLBodyElement.h"
       
    69 #include "HTMLElementFactory.h"
       
    70 #include "HTMLNames.h"
       
    71 #include "LegacyHTMLDocumentParser.h"
       
    72 #include "InspectorController.h"
       
    73 #include "KURL.h"
       
    74 #include "Page.h"
       
    75 #include "Settings.h"
       
    76 #include <wtf/text/CString.h>
       
    77 
       
    78 #include "DocTypeStrings.cpp"
       
    79 
       
    80 namespace WebCore {
       
    81 
       
    82 using namespace HTMLNames;
       
    83 
       
    84 HTMLDocument::HTMLDocument(Frame* frame, const KURL& url)
       
    85     : Document(frame, url, false, true)
       
    86 {
       
    87     clearXMLVersion();
       
    88     setParseMode(Compat);
       
    89 }
       
    90 
       
    91 HTMLDocument::~HTMLDocument()
       
    92 {
       
    93 }
       
    94 
       
    95 int HTMLDocument::width()
       
    96 {
       
    97     updateLayoutIgnorePendingStylesheets();
       
    98     FrameView* frameView = view();
       
    99     return frameView ? frameView->contentsWidth() : 0;
       
   100 }
       
   101 
       
   102 int HTMLDocument::height()
       
   103 {
       
   104     updateLayoutIgnorePendingStylesheets();
       
   105     FrameView* frameView = view();
       
   106     return frameView ? frameView->contentsHeight() : 0;
       
   107 }
       
   108 
       
   109 String HTMLDocument::dir()
       
   110 {
       
   111     HTMLElement* b = body();
       
   112     if (!b)
       
   113         return String();
       
   114     return b->getAttribute(dirAttr);
       
   115 }
       
   116 
       
   117 void HTMLDocument::setDir(const String& value)
       
   118 {
       
   119     HTMLElement* b = body();
       
   120     if (b)
       
   121         b->setAttribute(dirAttr, value);
       
   122 }
       
   123 
       
   124 String HTMLDocument::designMode() const
       
   125 {
       
   126     return inDesignMode() ? "on" : "off";
       
   127 }
       
   128 
       
   129 void HTMLDocument::setDesignMode(const String& value)
       
   130 {
       
   131     InheritedBool mode;
       
   132     if (equalIgnoringCase(value, "on"))
       
   133         mode = on;
       
   134     else if (equalIgnoringCase(value, "off"))
       
   135         mode = off;
       
   136     else
       
   137         mode = inherit;
       
   138     Document::setDesignMode(mode);
       
   139 }
       
   140 
       
   141 String HTMLDocument::compatMode() const
       
   142 {
       
   143     return inCompatMode() ? "BackCompat" : "CSS1Compat";
       
   144 }
       
   145 
       
   146 Element* HTMLDocument::activeElement()
       
   147 {
       
   148     if (Node* node = focusedNode())
       
   149         if (node->isElementNode())
       
   150             return static_cast<Element*>(node);
       
   151     return body();
       
   152 }
       
   153 
       
   154 bool HTMLDocument::hasFocus()
       
   155 {
       
   156     Page* page = this->page();
       
   157     if (!page)
       
   158         return false;
       
   159     if (!page->focusController()->isActive())
       
   160         return false;
       
   161     if (Frame* focusedFrame = page->focusController()->focusedFrame()) {
       
   162         if (focusedFrame->tree()->isDescendantOf(frame()))
       
   163             return true;
       
   164     }
       
   165     return false;
       
   166 }
       
   167 
       
   168 String HTMLDocument::bgColor()
       
   169 {
       
   170     HTMLElement* b = body();
       
   171     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   172 
       
   173     if (!bodyElement)
       
   174         return String();
       
   175     return bodyElement->bgColor();
       
   176 }
       
   177 
       
   178 void HTMLDocument::setBgColor(const String& value)
       
   179 {
       
   180     HTMLElement* b = body();
       
   181     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   182 
       
   183     if (bodyElement)
       
   184         bodyElement->setBgColor(value);
       
   185 }
       
   186 
       
   187 String HTMLDocument::fgColor()
       
   188 {
       
   189     HTMLElement* b = body();
       
   190     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   191 
       
   192     if (!bodyElement)
       
   193         return String();
       
   194     return bodyElement->text();
       
   195 }
       
   196 
       
   197 void HTMLDocument::setFgColor(const String& value)
       
   198 {
       
   199     HTMLElement* b = body();
       
   200     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   201 
       
   202     if (bodyElement)
       
   203         bodyElement->setText(value);
       
   204 }
       
   205 
       
   206 String HTMLDocument::alinkColor()
       
   207 {
       
   208     HTMLElement* b = body();
       
   209     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   210 
       
   211     if (!bodyElement)
       
   212         return String();
       
   213     return bodyElement->aLink();
       
   214 }
       
   215 
       
   216 void HTMLDocument::setAlinkColor(const String& value)
       
   217 {
       
   218     HTMLElement* b = body();
       
   219     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   220 
       
   221     if (bodyElement) {
       
   222         // This check is a bit silly, but some benchmarks like to set the
       
   223         // document's link colors over and over to the same value and we
       
   224         // don't want to incur a style update each time.
       
   225         if (bodyElement->aLink() != value)
       
   226             bodyElement->setALink(value);
       
   227     }
       
   228 }
       
   229 
       
   230 String HTMLDocument::linkColor()
       
   231 {
       
   232     HTMLElement* b = body();
       
   233     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   234 
       
   235     if (!bodyElement)
       
   236         return String();
       
   237     return bodyElement->link();
       
   238 }
       
   239 
       
   240 void HTMLDocument::setLinkColor(const String& value)
       
   241 {
       
   242     HTMLElement* b = body();
       
   243     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   244 
       
   245     if (bodyElement) {
       
   246         // This check is a bit silly, but some benchmarks like to set the
       
   247         // document's link colors over and over to the same value and we
       
   248         // don't want to incur a style update each time.
       
   249         if (bodyElement->link() != value)
       
   250             bodyElement->setLink(value);
       
   251     }
       
   252 }
       
   253 
       
   254 String HTMLDocument::vlinkColor()
       
   255 {
       
   256     HTMLElement* b = body();
       
   257     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   258 
       
   259     if (!bodyElement)
       
   260         return String();
       
   261     return bodyElement->vLink();
       
   262 }
       
   263 
       
   264 void HTMLDocument::setVlinkColor(const String& value)
       
   265 {
       
   266     HTMLElement* b = body();
       
   267     HTMLBodyElement* bodyElement = (b && b->hasTagName(bodyTag)) ? static_cast<HTMLBodyElement*>(b) : 0;
       
   268 
       
   269     if (bodyElement) {
       
   270         // This check is a bit silly, but some benchmarks like to set the
       
   271         // document's link colors over and over to the same value and we
       
   272         // don't want to incur a style update each time.
       
   273         if (bodyElement->vLink() != value)
       
   274             bodyElement->setVLink(value);
       
   275     }
       
   276 }
       
   277 
       
   278 void HTMLDocument::captureEvents()
       
   279 {
       
   280 }
       
   281 
       
   282 void HTMLDocument::releaseEvents()
       
   283 {
       
   284 }
       
   285 
       
   286 DocumentParser* HTMLDocument::createParser()
       
   287 {
       
   288     bool reportErrors = false;
       
   289 #if ENABLE(INSPECTOR)
       
   290     if (Page* page = this->page())
       
   291         reportErrors = page->inspectorController()->windowVisible();
       
   292 #endif
       
   293 
       
   294     if (settings() && settings()->html5ParserEnabled())
       
   295         return new HTMLDocumentParser(this, reportErrors);
       
   296 
       
   297     return new LegacyHTMLDocumentParser(this, reportErrors);
       
   298 }
       
   299 
       
   300 // --------------------------------------------------------------------------
       
   301 // not part of the DOM
       
   302 // --------------------------------------------------------------------------
       
   303 
       
   304 bool HTMLDocument::childAllowed(Node *newChild)
       
   305 {
       
   306     return newChild->hasTagName(htmlTag) || newChild->isCommentNode() || (newChild->nodeType() == DOCUMENT_TYPE_NODE && !doctype());
       
   307 }
       
   308 
       
   309 PassRefPtr<Element> HTMLDocument::createElement(const AtomicString& name, ExceptionCode& ec)
       
   310 {
       
   311     if (!isValidName(name)) {
       
   312         ec = INVALID_CHARACTER_ERR;
       
   313         return 0;
       
   314     }
       
   315     return HTMLElementFactory::createHTMLElement(QualifiedName(nullAtom, name.lower(), xhtmlNamespaceURI), this, 0, false);
       
   316 }
       
   317 
       
   318 void HTMLDocument::addItemToMap(HashCountedSet<AtomicStringImpl*>& map, const AtomicString& name)
       
   319 {
       
   320     if (name.isEmpty())
       
   321         return;
       
   322     map.add(name.impl());
       
   323     if (Frame* f = frame())
       
   324         f->script()->namedItemAdded(this, name);
       
   325 }
       
   326 
       
   327 void HTMLDocument::removeItemFromMap(HashCountedSet<AtomicStringImpl*>& map, const AtomicString& name)
       
   328 {
       
   329     if (name.isEmpty())
       
   330         return;
       
   331     if (Frame* f = frame())
       
   332         f->script()->namedItemRemoved(this, name);
       
   333     map.remove(name.impl());
       
   334 }
       
   335 
       
   336 void HTMLDocument::addNamedItem(const AtomicString& name)
       
   337 {
       
   338     addItemToMap(m_namedItemCounts, name);
       
   339 }
       
   340 
       
   341 void HTMLDocument::removeNamedItem(const AtomicString& name)
       
   342 { 
       
   343     removeItemFromMap(m_namedItemCounts, name);
       
   344 }
       
   345 
       
   346 void HTMLDocument::addExtraNamedItem(const AtomicString& name)
       
   347 {
       
   348     addItemToMap(m_extraNamedItemCounts, name);
       
   349 }
       
   350 
       
   351 void HTMLDocument::removeExtraNamedItem(const AtomicString& name)
       
   352 { 
       
   353     removeItemFromMap(m_extraNamedItemCounts, name);
       
   354 }
       
   355 
       
   356 void HTMLDocument::determineParseMode()
       
   357 {
       
   358     // FIXME: It's terrible that this code runs separately and isn't just built in to the
       
   359     // HTML tokenizer/parser.
       
   360 
       
   361     // This code more or less mimics Mozilla's implementation (specifically the
       
   362     // doctype parsing implemented by David Baron in Mozilla's nsParser.cpp).
       
   363     //
       
   364     // There are three possible parse modes:
       
   365     // COMPAT - quirks mode emulates WinIE and NS4.  CSS parsing is also relaxed in this mode, e.g., unit types can
       
   366     // be omitted from numbers.
       
   367     // ALMOST STRICT - This mode is identical to strict mode except for its treatment of line-height in the inline box model.  For
       
   368     // now (until the inline box model is re-written), this mode is identical to STANDARDS mode.
       
   369     // STRICT - no quirks apply.  Web pages will obey the specifications to the letter.
       
   370     bool wasInCompatMode = inCompatMode();
       
   371     DocumentType* docType = doctype();
       
   372     if (!docType || !equalIgnoringCase(docType->name(), "html"))
       
   373         // No doctype found at all or the doctype is not HTML.  Default to quirks mode and Html4.
       
   374         setParseMode(Compat);
       
   375     else if (!doctype()->systemId().isEmpty() && equalIgnoringCase(docType->systemId(), "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"))
       
   376         // Assume quirks mode for this particular system ID.  In the HTML5 spec, this is the only
       
   377         // system identifier that is examined.
       
   378         setParseMode(Compat);
       
   379     else if (docType->publicId().isEmpty())
       
   380         // A doctype without a public ID means use strict mode.
       
   381         setParseMode(Strict);
       
   382     else {
       
   383         // We have to check a list of public IDs to see what we
       
   384         // should do.
       
   385         String lowerPubID = docType->publicId().lower();
       
   386         CString pubIDStr = lowerPubID.latin1();
       
   387        
       
   388         // Look up the entry in our gperf-generated table.
       
   389         const PubIDInfo* doctypeEntry = findDoctypeEntry(pubIDStr.data(), pubIDStr.length());
       
   390         if (!doctypeEntry)
       
   391             // The DOCTYPE is not in the list.  Assume strict mode.
       
   392             setParseMode(Strict);
       
   393         else {
       
   394             switch (docType->systemId().isEmpty() ?
       
   395                     doctypeEntry->mode_if_no_sysid :
       
   396                     doctypeEntry->mode_if_sysid) {
       
   397                 case PubIDInfo::eQuirks3:
       
   398                 case PubIDInfo::eQuirks:
       
   399                     setParseMode(Compat);
       
   400                     break;
       
   401                 case PubIDInfo::eAlmostStandards:
       
   402                     setParseMode(AlmostStrict);
       
   403                     break;
       
   404                  default:
       
   405                     ASSERT(false);
       
   406             }
       
   407         }
       
   408     }
       
   409     
       
   410     if (inCompatMode() != wasInCompatMode) {
       
   411         clearPageUserSheet();
       
   412         clearPageGroupUserSheets();
       
   413         updateStyleSelector();
       
   414     }
       
   415 }
       
   416 
       
   417 void HTMLDocument::clear()
       
   418 {
       
   419     // FIXME: This does nothing, and that seems unlikely to be correct.
       
   420     // We've long had a comment saying that IE doesn't support this.
       
   421     // But I do see it in the documentation for Mozilla.
       
   422 }
       
   423 
       
   424 bool HTMLDocument::isFrameSet() const
       
   425 {
       
   426     HTMLElement* bodyElement = body();
       
   427     return bodyElement && bodyElement->renderer() && bodyElement->hasTagName(framesetTag);
       
   428 }
       
   429 
       
   430 }