WebCore/html/HTMLBodyElement.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  *           (C) 2000 Simon Hausmann (hausmann@kde.org)
       
     5  *           (C) 2001 Dirk Mueller (mueller@kde.org)
       
     6  * Copyright (C) 2004, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
       
     7  *
       
     8  * This library is free software; you can redistribute it and/or
       
     9  * modify it under the terms of the GNU Library General Public
       
    10  * License as published by the Free Software Foundation; either
       
    11  * version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This library is distributed in the hope that it will be useful,
       
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16  * Library General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU Library General Public License
       
    19  * along with this library; see the file COPYING.LIB.  If not, write to
       
    20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    21  * Boston, MA 02110-1301, USA.
       
    22  */
       
    23 
       
    24 #include "config.h"
       
    25 #include "HTMLBodyElement.h"
       
    26 
       
    27 #include "Attribute.h"
       
    28 #include "CSSStyleSelector.h"
       
    29 #include "CSSStyleSheet.h"
       
    30 #include "CSSValueKeywords.h"
       
    31 #include "EventNames.h"
       
    32 #include "Frame.h"
       
    33 #include "FrameView.h"
       
    34 #include "HTMLFrameElementBase.h"
       
    35 #include "HTMLNames.h"
       
    36 #include "ScriptEventListener.h"
       
    37 
       
    38 namespace WebCore {
       
    39 
       
    40 using namespace HTMLNames;
       
    41 
       
    42 HTMLBodyElement::HTMLBodyElement(const QualifiedName& tagName, Document* document)
       
    43     : HTMLElement(tagName, document)
       
    44 {
       
    45     ASSERT(hasTagName(bodyTag));
       
    46 }
       
    47 
       
    48 PassRefPtr<HTMLBodyElement> HTMLBodyElement::create(Document* document)
       
    49 {
       
    50     return adoptRef(new HTMLBodyElement(bodyTag, document));
       
    51 }
       
    52 
       
    53 PassRefPtr<HTMLBodyElement> HTMLBodyElement::create(const QualifiedName& tagName, Document* document)
       
    54 {
       
    55     return adoptRef(new HTMLBodyElement(tagName, document));
       
    56 }
       
    57 
       
    58 HTMLBodyElement::~HTMLBodyElement()
       
    59 {
       
    60     if (m_linkDecl) {
       
    61         m_linkDecl->setNode(0);
       
    62         m_linkDecl->setParent(0);
       
    63     }
       
    64 }
       
    65 
       
    66 void HTMLBodyElement::createLinkDecl()
       
    67 {
       
    68     m_linkDecl = CSSMutableStyleDeclaration::create();
       
    69     m_linkDecl->setParent(document()->elementSheet());
       
    70     m_linkDecl->setNode(this);
       
    71     m_linkDecl->setStrictParsing(!document()->inCompatMode());
       
    72 }
       
    73 
       
    74 bool HTMLBodyElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
       
    75 {
       
    76     if (attrName == backgroundAttr) {
       
    77         result = (MappedAttributeEntry)(eLastEntry + document()->docID());
       
    78         return false;
       
    79     } 
       
    80     
       
    81     if (attrName == bgcolorAttr ||
       
    82         attrName == textAttr ||
       
    83         attrName == marginwidthAttr ||
       
    84         attrName == leftmarginAttr ||
       
    85         attrName == marginheightAttr ||
       
    86         attrName == topmarginAttr ||
       
    87         attrName == bgpropertiesAttr) {
       
    88         result = eUniversal;
       
    89         return false;
       
    90     }
       
    91 
       
    92     return HTMLElement::mapToEntry(attrName, result);
       
    93 }
       
    94 
       
    95 void HTMLBodyElement::parseMappedAttribute(Attribute* attr)
       
    96 {
       
    97     if (attr->name() == backgroundAttr) {
       
    98         String url = deprecatedParseURL(attr->value());
       
    99         if (!url.isEmpty())
       
   100             addCSSImageProperty(attr, CSSPropertyBackgroundImage, document()->completeURL(url).string());
       
   101     } else if (attr->name() == marginwidthAttr || attr->name() == leftmarginAttr) {
       
   102         addCSSLength(attr, CSSPropertyMarginRight, attr->value());
       
   103         addCSSLength(attr, CSSPropertyMarginLeft, attr->value());
       
   104     } else if (attr->name() == marginheightAttr || attr->name() == topmarginAttr) {
       
   105         addCSSLength(attr, CSSPropertyMarginBottom, attr->value());
       
   106         addCSSLength(attr, CSSPropertyMarginTop, attr->value());
       
   107     } else if (attr->name() == bgcolorAttr) {
       
   108         addCSSColor(attr, CSSPropertyBackgroundColor, attr->value());
       
   109     } else if (attr->name() == textAttr) {
       
   110         addCSSColor(attr, CSSPropertyColor, attr->value());
       
   111     } else if (attr->name() == bgpropertiesAttr) {
       
   112         if (equalIgnoringCase(attr->value(), "fixed"))
       
   113             addCSSProperty(attr, CSSPropertyBackgroundAttachment, CSSValueFixed);
       
   114     } else if (attr->name() == vlinkAttr ||
       
   115                attr->name() == alinkAttr ||
       
   116                attr->name() == linkAttr) {
       
   117         if (attr->isNull()) {
       
   118             if (attr->name() == linkAttr)
       
   119                 document()->resetLinkColor();
       
   120             else if (attr->name() == vlinkAttr)
       
   121                 document()->resetVisitedLinkColor();
       
   122             else
       
   123                 document()->resetActiveLinkColor();
       
   124         } else {
       
   125             if (!m_linkDecl)
       
   126                 createLinkDecl();
       
   127             m_linkDecl->setProperty(CSSPropertyColor, attr->value(), false, false);
       
   128             RefPtr<CSSValue> val = m_linkDecl->getPropertyCSSValue(CSSPropertyColor);
       
   129             if (val && val->isPrimitiveValue()) {
       
   130                 Color col = document()->styleSelector()->getColorFromPrimitiveValue(static_cast<CSSPrimitiveValue*>(val.get()));
       
   131                 if (attr->name() == linkAttr)
       
   132                     document()->setLinkColor(col);
       
   133                 else if (attr->name() == vlinkAttr)
       
   134                     document()->setVisitedLinkColor(col);
       
   135                 else
       
   136                     document()->setActiveLinkColor(col);
       
   137             }
       
   138         }
       
   139         
       
   140         if (attached())
       
   141             document()->recalcStyle(Force);
       
   142     } else if (attr->name() == onloadAttr)
       
   143         document()->setWindowAttributeEventListener(eventNames().loadEvent, createAttributeEventListener(document()->frame(), attr));
       
   144     else if (attr->name() == onbeforeunloadAttr)
       
   145         document()->setWindowAttributeEventListener(eventNames().beforeunloadEvent, createAttributeEventListener(document()->frame(), attr));
       
   146     else if (attr->name() == onunloadAttr)
       
   147         document()->setWindowAttributeEventListener(eventNames().unloadEvent, createAttributeEventListener(document()->frame(), attr));
       
   148     else if (attr->name() == onpagehideAttr)
       
   149         document()->setWindowAttributeEventListener(eventNames().pagehideEvent, createAttributeEventListener(document()->frame(), attr));
       
   150     else if (attr->name() == onpageshowAttr)
       
   151         document()->setWindowAttributeEventListener(eventNames().pageshowEvent, createAttributeEventListener(document()->frame(), attr));
       
   152     else if (attr->name() == onpopstateAttr)
       
   153         document()->setWindowAttributeEventListener(eventNames().popstateEvent, createAttributeEventListener(document()->frame(), attr));
       
   154     else if (attr->name() == onblurAttr)
       
   155         document()->setWindowAttributeEventListener(eventNames().blurEvent, createAttributeEventListener(document()->frame(), attr));
       
   156     else if (attr->name() == onfocusAttr)
       
   157         document()->setWindowAttributeEventListener(eventNames().focusEvent, createAttributeEventListener(document()->frame(), attr));
       
   158 #if ENABLE(ORIENTATION_EVENTS)
       
   159     else if (attr->name() == onorientationchangeAttr)
       
   160         document()->setWindowAttributeEventListener(eventNames().orientationchangeEvent, createAttributeEventListener(document()->frame(), attr));
       
   161 #endif
       
   162     else if (attr->name() == onhashchangeAttr)
       
   163         document()->setWindowAttributeEventListener(eventNames().hashchangeEvent, createAttributeEventListener(document()->frame(), attr));
       
   164     else if (attr->name() == onresizeAttr)
       
   165         document()->setWindowAttributeEventListener(eventNames().resizeEvent, createAttributeEventListener(document()->frame(), attr));
       
   166     else if (attr->name() == onscrollAttr)
       
   167         document()->setWindowAttributeEventListener(eventNames().scrollEvent, createAttributeEventListener(document()->frame(), attr));
       
   168     else if (attr->name() == onstorageAttr)
       
   169         document()->setWindowAttributeEventListener(eventNames().storageEvent, createAttributeEventListener(document()->frame(), attr));
       
   170     else if (attr->name() == ononlineAttr)
       
   171         document()->setWindowAttributeEventListener(eventNames().onlineEvent, createAttributeEventListener(document()->frame(), attr));
       
   172     else if (attr->name() == onofflineAttr)
       
   173         document()->setWindowAttributeEventListener(eventNames().offlineEvent, createAttributeEventListener(document()->frame(), attr));
       
   174     else
       
   175         HTMLElement::parseMappedAttribute(attr);
       
   176 }
       
   177 
       
   178 void HTMLBodyElement::insertedIntoDocument()
       
   179 {
       
   180     HTMLElement::insertedIntoDocument();
       
   181 
       
   182     // FIXME: Perhaps this code should be in attach() instead of here.
       
   183     Element* ownerElement = document()->ownerElement();
       
   184     if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
       
   185         HTMLFrameElementBase* ownerFrameElement = static_cast<HTMLFrameElementBase*>(ownerElement);
       
   186         int marginWidth = ownerFrameElement->getMarginWidth();
       
   187         if (marginWidth != -1)
       
   188             setAttribute(marginwidthAttr, String::number(marginWidth));
       
   189         int marginHeight = ownerFrameElement->getMarginHeight();
       
   190         if (marginHeight != -1)
       
   191             setAttribute(marginheightAttr, String::number(marginHeight));
       
   192     }
       
   193 
       
   194     // FIXME: This call to scheduleRelayout should not be needed here.
       
   195     // But without it we hang during WebKit tests; need to fix that and remove this.
       
   196     if (FrameView* view = document()->view())
       
   197         view->scheduleRelayout();
       
   198 }
       
   199 
       
   200 bool HTMLBodyElement::isURLAttribute(Attribute *attr) const
       
   201 {
       
   202     return attr->name() == backgroundAttr;
       
   203 }
       
   204 
       
   205 String HTMLBodyElement::aLink() const
       
   206 {
       
   207     return getAttribute(alinkAttr);
       
   208 }
       
   209 
       
   210 void HTMLBodyElement::setALink(const String& value)
       
   211 {
       
   212     setAttribute(alinkAttr, value);
       
   213 }
       
   214 
       
   215 String HTMLBodyElement::bgColor() const
       
   216 {
       
   217     return getAttribute(bgcolorAttr);
       
   218 }
       
   219 
       
   220 void HTMLBodyElement::setBgColor(const String& value)
       
   221 {
       
   222     setAttribute(bgcolorAttr, value);
       
   223 }
       
   224 
       
   225 String HTMLBodyElement::link() const
       
   226 {
       
   227     return getAttribute(linkAttr);
       
   228 }
       
   229 
       
   230 void HTMLBodyElement::setLink(const String& value)
       
   231 {
       
   232     setAttribute(linkAttr, value);
       
   233 }
       
   234 
       
   235 String HTMLBodyElement::text() const
       
   236 {
       
   237     return getAttribute(textAttr);
       
   238 }
       
   239 
       
   240 void HTMLBodyElement::setText(const String& value)
       
   241 {
       
   242     setAttribute(textAttr, value);
       
   243 }
       
   244 
       
   245 String HTMLBodyElement::vLink() const
       
   246 {
       
   247     return getAttribute(vlinkAttr);
       
   248 }
       
   249 
       
   250 void HTMLBodyElement::setVLink(const String& value)
       
   251 {
       
   252     setAttribute(vlinkAttr, value);
       
   253 }
       
   254 
       
   255 static int adjustForZoom(int value, FrameView* frameView)
       
   256 {
       
   257     float zoomFactor = frameView->zoomFactor();
       
   258     if (zoomFactor == 1)
       
   259         return value;
       
   260     // Needed because of truncation (rather than rounding) when scaling up.
       
   261     if (zoomFactor > 1)
       
   262         value++;
       
   263     return static_cast<int>(value / zoomFactor);
       
   264 }
       
   265 
       
   266 int HTMLBodyElement::scrollLeft() const
       
   267 {
       
   268     // Update the document's layout.
       
   269     Document* doc = document();
       
   270     doc->updateLayoutIgnorePendingStylesheets();
       
   271     FrameView* view = doc->view();
       
   272     return view ? adjustForZoom(view->scrollX(), view) : 0;
       
   273 }
       
   274 
       
   275 void HTMLBodyElement::setScrollLeft(int scrollLeft)
       
   276 {
       
   277     Document* document = this->document();
       
   278     document->updateLayoutIgnorePendingStylesheets();
       
   279     FrameView* view = document->view();
       
   280     if (!view)
       
   281         return;
       
   282     view->setScrollPosition(IntPoint(static_cast<int>(scrollLeft * view->zoomFactor()), view->scrollY()));
       
   283 }
       
   284 
       
   285 int HTMLBodyElement::scrollTop() const
       
   286 {
       
   287     // Update the document's layout.
       
   288     Document* doc = document();
       
   289     doc->updateLayoutIgnorePendingStylesheets();
       
   290     FrameView* view = doc->view();
       
   291     return view ? adjustForZoom(view->scrollY(), view) : 0;
       
   292 }
       
   293 
       
   294 void HTMLBodyElement::setScrollTop(int scrollTop)
       
   295 {
       
   296     Document* document = this->document();
       
   297     document->updateLayoutIgnorePendingStylesheets();
       
   298     FrameView* view = document->view();
       
   299     if (!view)
       
   300         return;
       
   301     view->setScrollPosition(IntPoint(view->scrollX(), static_cast<int>(scrollTop * view->zoomFactor())));
       
   302 }
       
   303 
       
   304 int HTMLBodyElement::scrollHeight() const
       
   305 {
       
   306     // Update the document's layout.
       
   307     Document* doc = document();
       
   308     doc->updateLayoutIgnorePendingStylesheets();
       
   309     FrameView* view = doc->view();
       
   310     return view ? adjustForZoom(view->contentsHeight(), view) : 0;    
       
   311 }
       
   312 
       
   313 int HTMLBodyElement::scrollWidth() const
       
   314 {
       
   315     // Update the document's layout.
       
   316     Document* doc = document();
       
   317     doc->updateLayoutIgnorePendingStylesheets();
       
   318     FrameView* view = doc->view();
       
   319     return view ? adjustForZoom(view->contentsWidth(), view) : 0;    
       
   320 }
       
   321 
       
   322 void HTMLBodyElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
       
   323 {
       
   324     HTMLElement::addSubresourceAttributeURLs(urls);
       
   325 
       
   326     addSubresourceURL(urls, document()->completeURL(getAttribute(backgroundAttr)));
       
   327 }
       
   328 
       
   329 void HTMLBodyElement::didMoveToNewOwnerDocument()
       
   330 {
       
   331     // When moving body elements between documents, we should have to reset the parent sheet for any
       
   332     // link style declarations.  If we don't we might crash later.
       
   333     // In practice I can't reproduce this theoretical problem.
       
   334     // webarchive/adopt-attribute-styled-body-webarchive.html tries to make sure this crash won't surface.
       
   335     if (m_linkDecl)
       
   336         m_linkDecl->setParent(document()->elementSheet());
       
   337     
       
   338     HTMLElement::didMoveToNewOwnerDocument();
       
   339 }
       
   340 
       
   341 } // namespace WebCore