webengine/osswebengine/WebCore/dom/Element.cpp
changeset 0 dd21522fd290
child 13 10e98eab6f85
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     4  *           (C) 2001 Peter Kelly (pmk@post.com)
       
     5  *           (C) 2001 Dirk Mueller (mueller@kde.org)
       
     6  * Copyright (C) 2004, 2005, 2006, 2007 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 "Element.h"
       
    26 
       
    27 #include "CSSStyleSelector.h"
       
    28 #include "Document.h"
       
    29 #include "Editor.h"
       
    30 #include "ExceptionCode.h"
       
    31 #include "FocusController.h"
       
    32 #include "Frame.h"
       
    33 #include "FrameView.h"
       
    34 #include "HTMLElement.h"
       
    35 #include "HTMLNames.h"
       
    36 #include "KURL.h"
       
    37 #include "NamedAttrMap.h"
       
    38 #include "Page.h"
       
    39 #include "RenderBlock.h"
       
    40 #include "SelectionController.h"
       
    41 #include "TextIterator.h"
       
    42 #include "TextStream.h"
       
    43 #include "XMLNames.h"
       
    44 
       
    45 namespace WebCore {
       
    46 
       
    47 using namespace HTMLNames;
       
    48 using namespace XMLNames;
       
    49 
       
    50 class ElementRareData {
       
    51 public:
       
    52     ElementRareData(Element*);
       
    53     void resetComputedStyle(Element*);
       
    54 
       
    55     IntSize m_minimumSizeForResizing;
       
    56     RenderStyle* m_computedStyle;
       
    57     bool m_needsFocusAppearanceUpdateSoonAfterAttach;
       
    58 };
       
    59 
       
    60 typedef HashMap<const Element*, ElementRareData*> ElementRareDataMap;
       
    61 
       
    62 static ElementRareDataMap& rareDataMap()
       
    63 {
       
    64     static ElementRareDataMap* dataMap = new ElementRareDataMap;
       
    65     return *dataMap;
       
    66 }
       
    67 
       
    68 static ElementRareData* rareDataFromMap(const Element* element)
       
    69 {
       
    70     return rareDataMap().get(element);
       
    71 }
       
    72 
       
    73 static inline IntSize defaultMinimumSizeForResizing()
       
    74 {
       
    75     return IntSize(INT_MAX, INT_MAX);
       
    76 }
       
    77 
       
    78 inline ElementRareData::ElementRareData(Element* element)
       
    79     : m_minimumSizeForResizing(defaultMinimumSizeForResizing())
       
    80     , m_computedStyle(0)
       
    81     , m_needsFocusAppearanceUpdateSoonAfterAttach(false)
       
    82 {
       
    83 }
       
    84 
       
    85 void ElementRareData::resetComputedStyle(Element* element)
       
    86 {
       
    87     if (!m_computedStyle)
       
    88         return;
       
    89     m_computedStyle->deref(element->document()->renderArena());
       
    90     m_computedStyle = 0;
       
    91 }
       
    92 
       
    93 Element::Element(const QualifiedName& qName, Document *doc)
       
    94     : ContainerNode(doc)
       
    95     , m_tagName(qName)
       
    96 {
       
    97 }
       
    98 
       
    99 Element::~Element()
       
   100 {
       
   101     if (namedAttrMap)
       
   102         namedAttrMap->detachFromElement();
       
   103 
       
   104     if (!m_attrWasSpecifiedOrElementHasRareData)
       
   105         ASSERT(!rareDataMap().contains(this));
       
   106     else {
       
   107         ElementRareDataMap& dataMap = rareDataMap();
       
   108         ElementRareDataMap::iterator it = dataMap.find(this);
       
   109         ASSERT(it != dataMap.end());
       
   110         delete it->second;
       
   111         dataMap.remove(it);
       
   112     }
       
   113 }
       
   114 
       
   115 inline ElementRareData* Element::rareData()
       
   116 {
       
   117     return m_attrWasSpecifiedOrElementHasRareData ? rareDataFromMap(this) : 0;
       
   118 }
       
   119 
       
   120 inline const ElementRareData* Element::rareData() const
       
   121 {
       
   122     return m_attrWasSpecifiedOrElementHasRareData ? rareDataFromMap(this) : 0;
       
   123 }
       
   124 
       
   125 ElementRareData* Element::createRareData()
       
   126 {
       
   127     if (m_attrWasSpecifiedOrElementHasRareData)
       
   128         return rareDataMap().get(this);
       
   129     ASSERT(!rareDataMap().contains(this));
       
   130     ElementRareData* data = new ElementRareData(this);
       
   131     rareDataMap().set(this, data);
       
   132     m_attrWasSpecifiedOrElementHasRareData = true;
       
   133     return data;
       
   134 }
       
   135 
       
   136 PassRefPtr<Node> Element::cloneNode(bool deep)
       
   137 {
       
   138     ExceptionCode ec = 0;
       
   139     RefPtr<Element> clone = document()->createElementNS(namespaceURI(), nodeName(), ec);
       
   140     ASSERT(!ec);
       
   141     
       
   142     // clone attributes
       
   143     if (namedAttrMap)
       
   144         *clone->attributes() = *namedAttrMap;
       
   145 
       
   146     clone->copyNonAttributeProperties(this);
       
   147     
       
   148     if (deep)
       
   149         cloneChildNodes(clone.get());
       
   150 
       
   151     return clone.release();
       
   152 }
       
   153 
       
   154 void Element::removeAttribute(const QualifiedName& name, ExceptionCode& ec)
       
   155 {
       
   156     if (namedAttrMap) {
       
   157         namedAttrMap->removeNamedItem(name, ec);
       
   158         if (ec == NOT_FOUND_ERR)
       
   159             ec = 0;
       
   160     }
       
   161 }
       
   162 
       
   163 void Element::setAttribute(const QualifiedName& name, const String &value)
       
   164 {
       
   165     ExceptionCode ec = 0;
       
   166     setAttribute(name, value.impl(), ec);
       
   167 }
       
   168 
       
   169 // Virtual function, defined in base class.
       
   170 NamedAttrMap *Element::attributes() const
       
   171 {
       
   172     return attributes(false);
       
   173 }
       
   174 
       
   175 NamedAttrMap* Element::attributes(bool readonly) const
       
   176 {
       
   177     updateStyleAttributeIfNeeded();
       
   178     if (!readonly && !namedAttrMap)
       
   179         createAttributeMap();
       
   180     return namedAttrMap.get();
       
   181 }
       
   182 
       
   183 Node::NodeType Element::nodeType() const
       
   184 {
       
   185     return ELEMENT_NODE;
       
   186 }
       
   187 
       
   188 const AtomicStringList* Element::getClassList() const
       
   189 {
       
   190     return 0;
       
   191 }
       
   192 
       
   193 const AtomicString& Element::getIDAttribute() const
       
   194 {
       
   195     return namedAttrMap ? namedAttrMap->id() : nullAtom;
       
   196 }
       
   197 
       
   198 bool Element::hasAttribute(const QualifiedName& name) const
       
   199 {
       
   200     return hasAttributeNS(name.namespaceURI(), name.localName());
       
   201 }
       
   202 
       
   203 const AtomicString& Element::getAttribute(const QualifiedName& name) const
       
   204 {
       
   205     if (name == styleAttr)
       
   206         updateStyleAttributeIfNeeded();
       
   207 
       
   208     if (namedAttrMap)
       
   209         if (Attribute* a = namedAttrMap->getAttributeItem(name))
       
   210             return a->value();
       
   211 
       
   212     return nullAtom;
       
   213 }
       
   214 
       
   215 void Element::scrollIntoView(bool alignToTop) 
       
   216 {
       
   217     document()->updateLayoutIgnorePendingStylesheets();
       
   218     IntRect bounds = getRect();    
       
   219     if (renderer()) {
       
   220         // Align to the top / bottom and to the closest edge.
       
   221         if (alignToTop)
       
   222             renderer()->enclosingLayer()->scrollRectToVisible(bounds, RenderLayer::gAlignToEdgeIfNeeded, RenderLayer::gAlignTopAlways);
       
   223         else
       
   224             renderer()->enclosingLayer()->scrollRectToVisible(bounds, RenderLayer::gAlignToEdgeIfNeeded, RenderLayer::gAlignBottomAlways);
       
   225     }
       
   226 }
       
   227 
       
   228 void Element::scrollIntoViewIfNeeded(bool centerIfNeeded)
       
   229 {
       
   230     document()->updateLayoutIgnorePendingStylesheets();
       
   231     IntRect bounds = getRect();    
       
   232     if (renderer()) {
       
   233         if (centerIfNeeded)
       
   234             renderer()->enclosingLayer()->scrollRectToVisible(bounds, RenderLayer::gAlignCenterIfNeeded, RenderLayer::gAlignCenterIfNeeded);
       
   235         else
       
   236             renderer()->enclosingLayer()->scrollRectToVisible(bounds, RenderLayer::gAlignToEdgeIfNeeded, RenderLayer::gAlignToEdgeIfNeeded);
       
   237     }
       
   238 }
       
   239 
       
   240 void Element::scrollByUnits(int units, ScrollGranularity granularity)
       
   241 {
       
   242     document()->updateLayoutIgnorePendingStylesheets();
       
   243     if (RenderObject *rend = renderer()) {
       
   244         if (rend->hasOverflowClip()) {
       
   245             ScrollDirection direction = ScrollDown;
       
   246             if (units < 0) {
       
   247                 direction = ScrollUp;
       
   248                 units = -units;
       
   249             }
       
   250             rend->layer()->scroll(direction, granularity, units);
       
   251         }
       
   252     }
       
   253 }
       
   254 
       
   255 void Element::scrollByLines(int lines)
       
   256 {
       
   257     scrollByUnits(lines, ScrollByLine);
       
   258 }
       
   259 
       
   260 void Element::scrollByPages(int pages)
       
   261 {
       
   262     scrollByUnits(pages, ScrollByPage);
       
   263 }
       
   264 
       
   265 int Element::offsetLeft()
       
   266 {
       
   267     document()->updateLayoutIgnorePendingStylesheets();
       
   268     if (RenderObject* rend = renderer())
       
   269         return rend->offsetLeft();
       
   270     return 0;
       
   271 }
       
   272 
       
   273 int Element::offsetTop()
       
   274 {
       
   275     document()->updateLayoutIgnorePendingStylesheets();
       
   276     if (RenderObject* rend = renderer())
       
   277         return rend->offsetTop();
       
   278     return 0;
       
   279 }
       
   280 
       
   281 int Element::offsetWidth()
       
   282 {
       
   283     document()->updateLayoutIgnorePendingStylesheets();
       
   284     if (RenderObject* rend = renderer())
       
   285         return rend->offsetWidth();
       
   286     return 0;
       
   287 }
       
   288 
       
   289 int Element::offsetHeight()
       
   290 {
       
   291     document()->updateLayoutIgnorePendingStylesheets();
       
   292     if (RenderObject* rend = renderer())
       
   293         return rend->offsetHeight();
       
   294     return 0;
       
   295 }
       
   296 
       
   297 Element* Element::offsetParent()
       
   298 {
       
   299     document()->updateLayoutIgnorePendingStylesheets();
       
   300     if (RenderObject* rend = renderer())
       
   301         if (RenderObject* offsetParent = rend->offsetParent())
       
   302             return static_cast<Element*>(offsetParent->element());
       
   303     return 0;
       
   304 }
       
   305 
       
   306 int Element::clientLeft()
       
   307 {
       
   308     document()->updateLayoutIgnorePendingStylesheets();
       
   309 
       
   310     if (RenderObject* rend = renderer())
       
   311         return rend->clientLeft();
       
   312     return 0;
       
   313 }
       
   314 
       
   315 int Element::clientTop()
       
   316 {
       
   317     document()->updateLayoutIgnorePendingStylesheets();
       
   318 
       
   319     if (RenderObject* rend = renderer())
       
   320         return rend->clientTop();
       
   321     return 0;
       
   322 }
       
   323 
       
   324 int Element::clientWidth()
       
   325 {
       
   326     document()->updateLayoutIgnorePendingStylesheets();
       
   327 
       
   328     // When in strict mode, clientWidth for the document element should return the width of the containing frame.
       
   329     // When in quirks mode, clientWidth for the body element should return the width of the containing frame.
       
   330     bool inCompatMode = document()->inCompatMode();
       
   331     if ((!inCompatMode && document()->documentElement() == this) ||
       
   332         (inCompatMode && isHTMLElement() && document()->body() == this)) {
       
   333         if (FrameView* view = document()->view())
       
   334             return view->visibleWidth();
       
   335     }
       
   336     
       
   337 
       
   338     if (RenderObject* rend = renderer())
       
   339         return rend->clientWidth();
       
   340     return 0;
       
   341 }
       
   342 
       
   343 int Element::clientHeight()
       
   344 {
       
   345     document()->updateLayoutIgnorePendingStylesheets();
       
   346 
       
   347     // When in strict mode, clientHeight for the document element should return the height of the containing frame.
       
   348     // When in quirks mode, clientHeight for the body element should return the height of the containing frame.
       
   349     bool inCompatMode = document()->inCompatMode();     
       
   350 
       
   351     if ((!inCompatMode && document()->documentElement() == this) ||
       
   352         (inCompatMode && isHTMLElement() && document()->body() == this)) {
       
   353         if (FrameView* view = document()->view())
       
   354             return view->visibleHeight();
       
   355     }
       
   356     
       
   357     if (RenderObject* rend = renderer())
       
   358         return rend->clientHeight();
       
   359     return 0;
       
   360 }
       
   361 
       
   362 int Element::scrollLeft()
       
   363 {
       
   364     document()->updateLayoutIgnorePendingStylesheets();
       
   365     if (RenderObject* rend = renderer())
       
   366         return rend->scrollLeft();
       
   367     return 0;
       
   368 }
       
   369 
       
   370 int Element::scrollTop()
       
   371 {
       
   372     document()->updateLayoutIgnorePendingStylesheets();
       
   373     if (RenderObject* rend = renderer())
       
   374         return rend->scrollTop();
       
   375     return 0;
       
   376 }
       
   377 
       
   378 void Element::setScrollLeft(int newLeft)
       
   379 {
       
   380     document()->updateLayoutIgnorePendingStylesheets();
       
   381     if (RenderObject *rend = renderer())
       
   382         rend->setScrollLeft(newLeft);
       
   383 }
       
   384 
       
   385 void Element::setScrollTop(int newTop)
       
   386 {
       
   387     document()->updateLayoutIgnorePendingStylesheets();
       
   388     if (RenderObject *rend = renderer())
       
   389         rend->setScrollTop(newTop);
       
   390 }
       
   391 
       
   392 int Element::scrollWidth()
       
   393 {
       
   394     document()->updateLayoutIgnorePendingStylesheets();
       
   395     if (RenderObject* rend = renderer())
       
   396         return rend->scrollWidth();
       
   397     return 0;
       
   398 }
       
   399 
       
   400 int Element::scrollHeight()
       
   401 {
       
   402     document()->updateLayoutIgnorePendingStylesheets();
       
   403     if (RenderObject* rend = renderer())
       
   404         return rend->scrollHeight();
       
   405     return 0;
       
   406 }
       
   407 
       
   408 static inline bool inHTMLDocument(const Element* e)
       
   409 {
       
   410     return e && e->document()->isHTMLDocument();
       
   411 }
       
   412 
       
   413 const AtomicString& Element::getAttribute(const String& name) const
       
   414 {
       
   415     String localName = inHTMLDocument(this) ? name.lower() : name;
       
   416     if (localName == styleAttr.localName())
       
   417         updateStyleAttributeIfNeeded();
       
   418     
       
   419     if (namedAttrMap)
       
   420         if (Attribute* a = namedAttrMap->getAttributeItem(localName))
       
   421             return a->value();
       
   422     
       
   423     return nullAtom;
       
   424 }
       
   425 
       
   426 const AtomicString& Element::getAttributeNS(const String& namespaceURI, const String& localName) const
       
   427 {
       
   428     return getAttribute(QualifiedName(nullAtom, localName, namespaceURI));
       
   429 }
       
   430 
       
   431 void Element::setAttribute(const String& name, const String& value, ExceptionCode& ec)
       
   432 {
       
   433     if (!Document::isValidName(name)) {
       
   434         ec = INVALID_CHARACTER_ERR;
       
   435         return;
       
   436     }
       
   437 
       
   438     String localName = inHTMLDocument(this) ? name.lower() : name;
       
   439 
       
   440     // allocate attributemap if necessary
       
   441     Attribute* old = attributes(false)->getAttributeItem(localName);
       
   442 
       
   443     // NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly
       
   444     if (namedAttrMap->isReadOnlyNode()) {
       
   445         ec = NO_MODIFICATION_ALLOWED_ERR;
       
   446         return;
       
   447     }
       
   448     
       
   449     if (inDocument())
       
   450         document()->incDOMTreeVersion();
       
   451 
       
   452     if (localName == idAttr.localName())
       
   453         updateId(old ? old->value() : nullAtom, value);
       
   454     
       
   455     if (old && value.isNull())
       
   456         namedAttrMap->removeAttribute(old->name());
       
   457     else if (!old && !value.isNull())
       
   458         namedAttrMap->addAttribute(createAttribute(QualifiedName(nullAtom, localName, nullAtom), value.impl()));
       
   459     else if (old && !value.isNull()) {
       
   460         old->setValue(value);
       
   461         attributeChanged(old);
       
   462     }
       
   463 }
       
   464 
       
   465 void Element::setAttribute(const QualifiedName& name, StringImpl* value, ExceptionCode& ec)
       
   466 {
       
   467     if (inDocument())
       
   468         document()->incDOMTreeVersion();
       
   469 
       
   470     // allocate attributemap if necessary
       
   471     Attribute* old = attributes(false)->getAttributeItem(name);
       
   472 
       
   473     // NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly
       
   474     if (namedAttrMap->isReadOnlyNode()) {
       
   475         ec = NO_MODIFICATION_ALLOWED_ERR;
       
   476         return;
       
   477     }
       
   478 
       
   479     if (name == idAttr)
       
   480         updateId(old ? old->value() : nullAtom, value);
       
   481     
       
   482     if (old && !value)
       
   483         namedAttrMap->removeAttribute(name);
       
   484     else if (!old && value)
       
   485         namedAttrMap->addAttribute(createAttribute(name, value));
       
   486     else if (old && value) {
       
   487         old->setValue(value);
       
   488         attributeChanged(old);
       
   489     }
       
   490 }
       
   491 
       
   492 Attribute* Element::createAttribute(const QualifiedName& name, StringImpl* value)
       
   493 {
       
   494     return new Attribute(name, value);
       
   495 }
       
   496 
       
   497 void Element::setAttributeMap(NamedAttrMap* list)
       
   498 {
       
   499     if (inDocument())
       
   500         document()->incDOMTreeVersion();
       
   501 
       
   502     // If setting the whole map changes the id attribute, we need to call updateId.
       
   503 
       
   504     Attribute *oldId = namedAttrMap ? namedAttrMap->getAttributeItem(idAttr) : 0;
       
   505     Attribute *newId = list ? list->getAttributeItem(idAttr) : 0;
       
   506 
       
   507     if (oldId || newId)
       
   508         updateId(oldId ? oldId->value() : nullAtom, newId ? newId->value() : nullAtom);
       
   509 
       
   510     if (namedAttrMap)
       
   511         namedAttrMap->element = 0;
       
   512 
       
   513     namedAttrMap = list;
       
   514 
       
   515     if (namedAttrMap) {
       
   516         namedAttrMap->element = this;
       
   517         unsigned len = namedAttrMap->length();
       
   518         for (unsigned i = 0; i < len; i++)
       
   519             attributeChanged(namedAttrMap->attrs[i]);
       
   520         // FIXME: What about attributes that were in the old map that are not in the new map?
       
   521     }
       
   522 }
       
   523 
       
   524 bool Element::hasAttributes() const
       
   525 {
       
   526     updateStyleAttributeIfNeeded();
       
   527     return namedAttrMap && namedAttrMap->length() > 0;
       
   528 }
       
   529 
       
   530 String Element::nodeName() const
       
   531 {
       
   532     return m_tagName.toString();
       
   533 }
       
   534 
       
   535 String Element::nodeNamePreservingCase() const
       
   536 {
       
   537     return m_tagName.toString();
       
   538 }
       
   539 
       
   540 void Element::setPrefix(const AtomicString &_prefix, ExceptionCode& ec)
       
   541 {
       
   542     checkSetPrefix(_prefix, ec);
       
   543     if (ec)
       
   544         return;
       
   545 
       
   546     m_tagName.setPrefix(_prefix);
       
   547 }
       
   548 
       
   549 String Element::baseURI() const
       
   550 {
       
   551     KURL xmlbase(getAttribute(baseAttr).deprecatedString());
       
   552 
       
   553     if (!xmlbase.protocol().isEmpty())
       
   554         return xmlbase.url();
       
   555 
       
   556     Node* parent = parentNode();
       
   557     if (parent)
       
   558         return KURL(parent->baseURI().deprecatedString(), xmlbase.url()).url();
       
   559 
       
   560     return xmlbase.url();
       
   561 }
       
   562 
       
   563 Node* Element::insertAdjacentElement(const String& where, Node* newChild, int& exception)
       
   564 {
       
   565     if (!newChild) {
       
   566         // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alternative
       
   567         exception = TYPE_MISMATCH_ERR;
       
   568         return 0;
       
   569     }
       
   570     
       
   571     // In Internet Explorer if the element has no parent and where is "beforeBegin" or "afterEnd",
       
   572     // a document fragment is created and the elements appended in the correct order. This document
       
   573     // fragment isn't returned anywhere.
       
   574     //
       
   575     // This is impossible for us to implement as the DOM tree does not allow for such structures,
       
   576     // Opera also appears to disallow such usage.
       
   577     
       
   578     if (equalIgnoringCase(where, "beforeBegin")) {
       
   579         if (Node* p = parent())
       
   580             return p->insertBefore(newChild, this, exception) ? newChild : 0;
       
   581     } else if (equalIgnoringCase(where, "afterBegin")) {
       
   582         return insertBefore(newChild, firstChild(), exception) ? newChild : 0;
       
   583     } else if (equalIgnoringCase(where, "beforeEnd")) {
       
   584         return appendChild(newChild, exception) ? newChild : 0;
       
   585     } else if (equalIgnoringCase(where, "afterEnd")) {
       
   586         if (Node* p = parent())
       
   587             return p->insertBefore(newChild, nextSibling(), exception) ? newChild : 0;
       
   588     } else {
       
   589         // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alternative
       
   590         exception = NOT_SUPPORTED_ERR;
       
   591     }
       
   592     return 0;
       
   593 }
       
   594 
       
   595 bool Element::contains(const Node* node) const
       
   596 {
       
   597     if (!node)
       
   598         return false;
       
   599     return this == node || node->isDescendantOf(this);
       
   600 }
       
   601 
       
   602 void Element::createAttributeMap() const
       
   603 {
       
   604     namedAttrMap = new NamedAttrMap(const_cast<Element*>(this));
       
   605 }
       
   606 
       
   607 bool Element::isURLAttribute(Attribute *attr) const
       
   608 {
       
   609     return false;
       
   610 }
       
   611 
       
   612 RenderStyle *Element::styleForRenderer(RenderObject *parentRenderer)
       
   613 {
       
   614     return document()->styleSelector()->styleForElement(this);
       
   615 }
       
   616 
       
   617 RenderObject *Element::createRenderer(RenderArena *arena, RenderStyle *style)
       
   618 {
       
   619     if (document()->documentElement() == this && style->display() == NONE) {
       
   620         // Ignore display: none on root elements.  Force a display of block in that case.
       
   621         RenderBlock* result = new (arena) RenderBlock(this);
       
   622         if (result) result->setStyle(style);
       
   623         return result;
       
   624     }
       
   625     return RenderObject::createObject(this, style);
       
   626 }
       
   627 
       
   628 
       
   629 void Element::insertedIntoDocument()
       
   630 {
       
   631     // need to do superclass processing first so inDocument() is true
       
   632     // by the time we reach updateId
       
   633     ContainerNode::insertedIntoDocument();
       
   634 
       
   635     if (hasID()) {
       
   636         NamedAttrMap* attrs = attributes(true);
       
   637         if (attrs) {
       
   638             Attribute* idItem = attrs->getAttributeItem(idAttr);
       
   639             if (idItem && !idItem->isNull())
       
   640                 updateId(nullAtom, idItem->value());
       
   641         }
       
   642     }
       
   643 }
       
   644 
       
   645 void Element::removedFromDocument()
       
   646 {
       
   647     if (hasID()) {
       
   648         NamedAttrMap* attrs = attributes(true);
       
   649         if (attrs) {
       
   650             Attribute* idItem = attrs->getAttributeItem(idAttr);
       
   651             if (idItem && !idItem->isNull())
       
   652                 updateId(idItem->value(), nullAtom);
       
   653         }
       
   654     }
       
   655 
       
   656     ContainerNode::removedFromDocument();
       
   657 }
       
   658 
       
   659 void Element::attach()
       
   660 {
       
   661     createRendererIfNeeded();
       
   662     ContainerNode::attach();
       
   663     if (ElementRareData* rd = rareData()) {
       
   664         if (rd->m_needsFocusAppearanceUpdateSoonAfterAttach) {
       
   665             if (isFocusable() && document()->focusedNode() == this)
       
   666                 document()->updateFocusAppearanceSoon();
       
   667             rd->m_needsFocusAppearanceUpdateSoonAfterAttach = false;
       
   668         }
       
   669     }
       
   670 }
       
   671 
       
   672 void Element::detach()
       
   673 {
       
   674     cancelFocusAppearanceUpdate();
       
   675     if (ElementRareData* rd = rareData())
       
   676         rd->resetComputedStyle(this);
       
   677     ContainerNode::detach();
       
   678 }
       
   679 
       
   680 void Element::recalcStyle(StyleChange change)
       
   681 {
       
   682     // ### should go away and be done in renderobject
       
   683     RenderStyle* _style = renderStyle();
       
   684     bool hasParentStyle = parentNode() ? parentNode()->renderStyle() : false;
       
   685 
       
   686 #if ENABLE(SVG)
       
   687     if (!hasParentStyle && isShadowNode() && isSVGElement())
       
   688         hasParentStyle = true;
       
   689 #endif
       
   690 
       
   691     if ((change > NoChange || changed())) {
       
   692         if (ElementRareData* rd = rareData())
       
   693             rd->resetComputedStyle(this);
       
   694     }
       
   695     if (hasParentStyle && (change >= Inherit || changed())) {
       
   696         RenderStyle *newStyle = document()->styleSelector()->styleForElement(this);
       
   697         StyleChange ch = diff(_style, newStyle);
       
   698         if (ch == Detach) {
       
   699             if (attached())
       
   700                 detach();
       
   701             // ### Suboptimal. Style gets calculated again.
       
   702             attach();
       
   703             // attach recalulates the style for all children. No need to do it twice.
       
   704             setChanged(NoStyleChange);
       
   705             setHasChangedChild(false);
       
   706             newStyle->deref(document()->renderArena());
       
   707             return;
       
   708         }
       
   709         else if (ch != NoChange) {
       
   710             if (newStyle)
       
   711                 setRenderStyle(newStyle);
       
   712         }
       
   713         else if (changed() && newStyle && (document()->usesSiblingRules() || document()->usesDescendantRules())) {
       
   714             // Although no change occurred, we use the new style so that the cousin style sharing code won't get
       
   715             // fooled into believing this style is the same.  This is only necessary if the document actually uses
       
   716             // sibling/descendant rules, since otherwise it isn't possible for ancestor styles to affect sharing of
       
   717             // descendants.
       
   718             if (renderer())
       
   719                 renderer()->setStyleInternal(newStyle);
       
   720             else
       
   721                 setRenderStyle(newStyle);
       
   722         }
       
   723 
       
   724         newStyle->deref(document()->renderArena());
       
   725 
       
   726         if (change != Force) {
       
   727             if (document()->usesDescendantRules() && styleChangeType() == FullStyleChange)
       
   728                 change = Force;
       
   729             else
       
   730                 change = ch;
       
   731         }
       
   732     }
       
   733 
       
   734     for (Node *n = firstChild(); n; n = n->nextSibling()) {
       
   735         if (change >= Inherit || n->isTextNode() || n->hasChangedChild() || n->changed())
       
   736             n->recalcStyle(change);
       
   737     }
       
   738 
       
   739     setChanged(NoStyleChange);
       
   740     setHasChangedChild(false);
       
   741 }
       
   742 
       
   743 bool Element::childTypeAllowed(NodeType type)
       
   744 {
       
   745     switch (type) {
       
   746         case ELEMENT_NODE:
       
   747         case TEXT_NODE:
       
   748         case COMMENT_NODE:
       
   749         case PROCESSING_INSTRUCTION_NODE:
       
   750         case CDATA_SECTION_NODE:
       
   751         case ENTITY_REFERENCE_NODE:
       
   752             return true;
       
   753             break;
       
   754         default:
       
   755             return false;
       
   756     }
       
   757 }
       
   758 
       
   759 void Element::dispatchAttrRemovalEvent(Attribute*)
       
   760 {
       
   761     ASSERT(!eventDispatchForbidden());
       
   762 #if 0
       
   763     if (!document()->hasListenerType(Document::DOMATTRMODIFIED_LISTENER))
       
   764         return;
       
   765     ExceptionCode ec = 0;
       
   766     dispatchEvent(new MutationEvent(DOMAttrModifiedEvent, true, false, attr, attr->value(),
       
   767         attr->value(), document()->attrName(attr->id()), MutationEvent::REMOVAL), ec);
       
   768 #endif
       
   769 }
       
   770 
       
   771 void Element::dispatchAttrAdditionEvent(Attribute *attr)
       
   772 {
       
   773     ASSERT(!eventDispatchForbidden());
       
   774 #if 0
       
   775     if (!document()->hasListenerType(Document::DOMATTRMODIFIED_LISTENER))
       
   776         return;
       
   777     ExceptionCode ec = 0;
       
   778     dispatchEvent(new MutationEvent(DOMAttrModifiedEvent, true, false, attr, attr->value(),
       
   779         attr->value(),document()->attrName(attr->id()), MutationEvent::ADDITION), ec);
       
   780 #endif
       
   781 }
       
   782 
       
   783 String Element::openTagStartToString() const
       
   784 {
       
   785     String result = "<" + nodeName();
       
   786 
       
   787     NamedAttrMap *attrMap = attributes(true);
       
   788 
       
   789     if (attrMap) {
       
   790         unsigned numAttrs = attrMap->length();
       
   791         for (unsigned i = 0; i < numAttrs; i++) {
       
   792             result += " ";
       
   793 
       
   794             Attribute *attribute = attrMap->attributeItem(i);
       
   795             result += attribute->name().toString();
       
   796             if (!attribute->value().isNull()) {
       
   797                 result += "=\"";
       
   798                 // FIXME: substitute entities for any instances of " or '
       
   799                 result += attribute->value();
       
   800                 result += "\"";
       
   801             }
       
   802         }
       
   803     }
       
   804 
       
   805     return result;
       
   806 }
       
   807 
       
   808 String Element::toString() const
       
   809 {
       
   810     String result = openTagStartToString();
       
   811 
       
   812     if (hasChildNodes()) {
       
   813         result += ">";
       
   814 
       
   815         for (Node *child = firstChild(); child != NULL; child = child->nextSibling()) {
       
   816             result += child->toString();
       
   817         }
       
   818 
       
   819         result += "</";
       
   820         result += nodeName();
       
   821         result += ">";
       
   822     } else {
       
   823         result += " />";
       
   824     }
       
   825 
       
   826     return result;
       
   827 }
       
   828 
       
   829 void Element::updateId(const AtomicString& oldId, const AtomicString& newId)
       
   830 {
       
   831     if (!inDocument())
       
   832         return;
       
   833 
       
   834     if (oldId == newId)
       
   835         return;
       
   836 
       
   837     Document* doc = document();
       
   838     if (!oldId.isEmpty())
       
   839         doc->removeElementById(oldId, this);
       
   840     if (!newId.isEmpty())
       
   841         doc->addElementById(newId, this);
       
   842 }
       
   843 
       
   844 #ifndef NDEBUG
       
   845 void Element::dump(TextStream *stream, DeprecatedString ind) const
       
   846 {
       
   847     updateStyleAttributeIfNeeded();
       
   848     if (namedAttrMap) {
       
   849         for (unsigned i = 0; i < namedAttrMap->length(); i++) {
       
   850             Attribute *attr = namedAttrMap->attributeItem(i);
       
   851             *stream << " " << attr->name().localName().deprecatedString().ascii()
       
   852                     << "=\"" << attr->value().deprecatedString().ascii() << "\"";
       
   853         }
       
   854     }
       
   855 
       
   856     ContainerNode::dump(stream,ind);
       
   857 }
       
   858 #endif
       
   859 
       
   860 #ifndef NDEBUG
       
   861 void Element::formatForDebugger(char *buffer, unsigned length) const
       
   862 {
       
   863     String result;
       
   864     String s;
       
   865     
       
   866     s = nodeName();
       
   867     if (s.length() > 0) {
       
   868         result += s;
       
   869     }
       
   870           
       
   871     s = getAttribute(idAttr);
       
   872     if (s.length() > 0) {
       
   873         if (result.length() > 0)
       
   874             result += "; ";
       
   875         result += "id=";
       
   876         result += s;
       
   877     }
       
   878           
       
   879     s = getAttribute(classAttr);
       
   880     if (s.length() > 0) {
       
   881         if (result.length() > 0)
       
   882             result += "; ";
       
   883         result += "class=";
       
   884         result += s;
       
   885     }
       
   886           
       
   887     strncpy(buffer, result.deprecatedString().latin1(), length - 1);
       
   888 }
       
   889 #endif
       
   890 
       
   891 PassRefPtr<Attr> Element::setAttributeNode(Attr *attr, ExceptionCode& ec)
       
   892 {
       
   893     ASSERT(attr);
       
   894     return static_pointer_cast<Attr>(attributes(false)->setNamedItem(attr, ec));
       
   895 }
       
   896 
       
   897 PassRefPtr<Attr> Element::setAttributeNodeNS(Attr* attr, ExceptionCode& ec)
       
   898 {
       
   899     ASSERT(attr);
       
   900     return static_pointer_cast<Attr>(attributes(false)->setNamedItem(attr, ec));
       
   901 }
       
   902 
       
   903 PassRefPtr<Attr> Element::removeAttributeNode(Attr *attr, ExceptionCode& ec)
       
   904 {
       
   905     if (!attr || attr->ownerElement() != this) {
       
   906         ec = NOT_FOUND_ERR;
       
   907         return 0;
       
   908     }
       
   909     if (document() != attr->document()) {
       
   910         ec = WRONG_DOCUMENT_ERR;
       
   911         return 0;
       
   912     }
       
   913 
       
   914     NamedAttrMap *attrs = attributes(true);
       
   915     if (!attrs)
       
   916         return 0;
       
   917 
       
   918     return static_pointer_cast<Attr>(attrs->removeNamedItem(attr->qualifiedName(), ec));
       
   919 }
       
   920 
       
   921 void Element::setAttributeNS(const String& namespaceURI, const String& qualifiedName, const String& value, ExceptionCode& ec)
       
   922 {
       
   923     String prefix, localName;
       
   924     if (!Document::parseQualifiedName(qualifiedName, prefix, localName)) {
       
   925         ec = INVALID_CHARACTER_ERR;
       
   926         return;
       
   927     }
       
   928     setAttribute(QualifiedName(prefix, localName, namespaceURI), value.impl(), ec);
       
   929 }
       
   930 
       
   931 void Element::removeAttribute(const String& name, ExceptionCode& ec)
       
   932 {
       
   933     String localName = inHTMLDocument(this) ? name.lower() : name;
       
   934 
       
   935     if (namedAttrMap) {
       
   936         namedAttrMap->removeNamedItem(localName, ec);
       
   937         if (ec == NOT_FOUND_ERR)
       
   938             ec = 0;
       
   939     }
       
   940 }
       
   941 
       
   942 void Element::removeAttributeNS(const String& namespaceURI, const String& localName, ExceptionCode& ec)
       
   943 {
       
   944     removeAttribute(QualifiedName(nullAtom, localName, namespaceURI), ec);
       
   945 }
       
   946 
       
   947 PassRefPtr<Attr> Element::getAttributeNode(const String& name)
       
   948 {
       
   949     NamedAttrMap* attrs = attributes(true);
       
   950     if (!attrs)
       
   951         return 0;
       
   952     String localName = inHTMLDocument(this) ? name.lower() : name;
       
   953     return static_pointer_cast<Attr>(attrs->getNamedItem(localName));
       
   954 }
       
   955 
       
   956 PassRefPtr<Attr> Element::getAttributeNodeNS(const String& namespaceURI, const String& localName)
       
   957 {
       
   958     NamedAttrMap* attrs = attributes(true);
       
   959     if (!attrs)
       
   960         return 0;
       
   961     return static_pointer_cast<Attr>(attrs->getNamedItem(QualifiedName(nullAtom, localName, namespaceURI)));
       
   962 }
       
   963 
       
   964 bool Element::hasAttribute(const String& name) const
       
   965 {
       
   966     NamedAttrMap* attrs = attributes(true);
       
   967     if (!attrs)
       
   968         return false;
       
   969     String localName = inHTMLDocument(this) ? name.lower() : name;
       
   970     return attrs->getAttributeItem(localName);
       
   971 }
       
   972 
       
   973 bool Element::hasAttributeNS(const String& namespaceURI, const String& localName) const
       
   974 {
       
   975     NamedAttrMap* attrs = attributes(true);
       
   976     if (!attrs)
       
   977         return false;
       
   978     return attrs->getAttributeItem(QualifiedName(nullAtom, localName, namespaceURI));
       
   979 }
       
   980 
       
   981 CSSStyleDeclaration *Element::style()
       
   982 {
       
   983     return 0;
       
   984 }
       
   985 
       
   986 void Element::focus(bool restorePreviousSelection)
       
   987 {
       
   988     Document* doc = document();
       
   989     if (doc->focusedNode() == this)
       
   990         return;
       
   991 
       
   992     doc->updateLayoutIgnorePendingStylesheets();
       
   993     
       
   994     if (!supportsFocus())
       
   995         return;
       
   996     
       
   997     if (Page* page = doc->page())
       
   998         page->focusController()->setFocusedNode(this, doc->frame());
       
   999 
       
  1000     if (!isFocusable()) {
       
  1001         createRareData()->m_needsFocusAppearanceUpdateSoonAfterAttach = true;
       
  1002         return;
       
  1003     }
       
  1004         
       
  1005     cancelFocusAppearanceUpdate();
       
  1006     updateFocusAppearance(restorePreviousSelection);
       
  1007 }
       
  1008 
       
  1009 void Element::updateFocusAppearance(bool restorePreviousSelection)
       
  1010 {
       
  1011     if (this == rootEditableElement()) { 
       
  1012         Frame* frame = document()->frame();
       
  1013         if (!frame)
       
  1014             return;
       
  1015 
       
  1016         // FIXME: We should restore the previous selection if there is one.
       
  1017         Selection newSelection = hasTagName(htmlTag) || hasTagName(bodyTag) ? Selection(Position(this, 0), DOWNSTREAM) : Selection::selectionFromContentsOfNode(this);
       
  1018         
       
  1019         if (frame->shouldChangeSelection(newSelection)) {
       
  1020             frame->selectionController()->setSelection(newSelection);
       
  1021             frame->revealSelection();
       
  1022         }
       
  1023     } else if (renderer() && !renderer()->isWidget())
       
  1024         renderer()->enclosingLayer()->scrollRectToVisible(getRect());
       
  1025 }
       
  1026 
       
  1027 void Element::blur()
       
  1028 {
       
  1029     cancelFocusAppearanceUpdate();
       
  1030     Document* doc = document();
       
  1031     if (doc->focusedNode() == this) {
       
  1032         if (doc->frame())
       
  1033             doc->frame()->page()->focusController()->setFocusedNode(0, doc->frame());
       
  1034         else
       
  1035             doc->setFocusedNode(0);
       
  1036     }
       
  1037 }
       
  1038 
       
  1039 String Element::innerText() const
       
  1040 {
       
  1041     // We need to update layout, since plainText uses line boxes in the render tree.
       
  1042     document()->updateLayoutIgnorePendingStylesheets();
       
  1043 
       
  1044     if (!renderer())
       
  1045         return textContent(true);
       
  1046 
       
  1047     return plainText(rangeOfContents(const_cast<Element*>(this)).get());
       
  1048 }
       
  1049 
       
  1050 String Element::outerText() const
       
  1051 {
       
  1052     // Getting outerText is the same as getting innerText, only
       
  1053     // setting is different. You would think this should get the plain
       
  1054     // text for the outer range, but this is wrong, <br> for instance
       
  1055     // would return different values for inner and outer text by such
       
  1056     // a rule, but it doesn't in WinIE, and we want to match that.
       
  1057     return innerText();
       
  1058 }
       
  1059 
       
  1060 String Element::title() const
       
  1061 {
       
  1062     return String();
       
  1063 }
       
  1064 
       
  1065 IntSize Element::minimumSizeForResizing() const
       
  1066 {
       
  1067     const ElementRareData* rd = rareData();
       
  1068     return rd ? rd->m_minimumSizeForResizing : defaultMinimumSizeForResizing();
       
  1069 }
       
  1070 
       
  1071 void Element::setMinimumSizeForResizing(const IntSize& size)
       
  1072 {
       
  1073     if (size == defaultMinimumSizeForResizing() && !rareData())
       
  1074         return;
       
  1075     createRareData()->m_minimumSizeForResizing = size;
       
  1076 }
       
  1077 
       
  1078 RenderStyle* Element::computedStyle()
       
  1079 {
       
  1080     if (RenderStyle* usedStyle = renderStyle())
       
  1081         return usedStyle;
       
  1082 
       
  1083     if (!attached())
       
  1084         // FIXME: Try to do better than this. Ensure that styleForElement() works for elements that are not in the
       
  1085         // document tree and figure out when to destroy the computed style for such elements.
       
  1086         return 0;
       
  1087 
       
  1088     ElementRareData* rd = createRareData();
       
  1089     if (!rd->m_computedStyle)
       
  1090         rd->m_computedStyle = document()->styleSelector()->styleForElement(this, parent() ? parent()->computedStyle() : 0);
       
  1091     return rd->m_computedStyle;
       
  1092 }
       
  1093 
       
  1094 void Element::cancelFocusAppearanceUpdate()
       
  1095 {
       
  1096     if (ElementRareData* rd = rareData())
       
  1097         rd->m_needsFocusAppearanceUpdateSoonAfterAttach = false;
       
  1098     if (document()->focusedNode() == this)
       
  1099         document()->cancelFocusAppearanceUpdate();
       
  1100 }
       
  1101 
       
  1102 }