Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2001 Dirk Mueller (mueller (at) kde.org)
      5  * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2013 Apple Inc. All rights reserved.
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Library General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Library General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Library General Public License
     18  * along with this library; see the file COPYING.LIB.  If not, write to
     19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20  * Boston, MA 02110-1301, USA.
     21  *
     22  */
     23 
     24 #ifndef ContainerNode_h
     25 #define ContainerNode_h
     26 
     27 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
     28 #include "core/dom/Node.h"
     29 #include "core/html/CollectionType.h"
     30 #include "wtf/OwnPtr.h"
     31 #include "wtf/Vector.h"
     32 
     33 namespace blink {
     34 
     35 class ClassCollection;
     36 class ExceptionState;
     37 class FloatPoint;
     38 class HTMLCollection;
     39 template <typename NodeType> class StaticNodeTypeList;
     40 typedef StaticNodeTypeList<Element> StaticElementList;
     41 class TagCollection;
     42 
     43 enum DynamicRestyleFlags {
     44     ChildrenOrSiblingsAffectedByFocus = 1 << 0,
     45     ChildrenOrSiblingsAffectedByHover = 1 << 1,
     46     ChildrenOrSiblingsAffectedByActive = 1 << 2,
     47     ChildrenOrSiblingsAffectedByDrag = 1 << 3,
     48     ChildrenAffectedByFirstChildRules = 1 << 4,
     49     ChildrenAffectedByLastChildRules = 1 << 5,
     50     ChildrenAffectedByDirectAdjacentRules = 1 << 6,
     51     ChildrenAffectedByIndirectAdjacentRules = 1 << 7,
     52     ChildrenAffectedByForwardPositionalRules = 1 << 8,
     53     ChildrenAffectedByBackwardPositionalRules = 1 << 9,
     54 
     55     NumberOfDynamicRestyleFlags = 10,
     56 };
     57 
     58 // This constant controls how much buffer is initially allocated
     59 // for a Node Vector that is used to store child Nodes of a given Node.
     60 // FIXME: Optimize the value.
     61 const int initialNodeVectorSize = 11;
     62 typedef WillBeHeapVector<RefPtrWillBeMember<Node>, initialNodeVectorSize> NodeVector;
     63 
     64 class ContainerNode : public Node {
     65 public:
     66     virtual ~ContainerNode();
     67 
     68     Node* firstChild() const { return m_firstChild; }
     69     Node* lastChild() const { return m_lastChild; }
     70     bool hasChildren() const { return m_firstChild; }
     71 
     72     bool hasOneChild() const { return m_firstChild && !m_firstChild->nextSibling(); }
     73     bool hasOneTextChild() const { return hasOneChild() && m_firstChild->isTextNode(); }
     74     bool hasChildCount(unsigned) const;
     75 
     76     PassRefPtrWillBeRawPtr<HTMLCollection> children();
     77 
     78     unsigned countChildren() const;
     79 
     80     PassRefPtrWillBeRawPtr<Element> querySelector(const AtomicString& selectors, ExceptionState&);
     81     PassRefPtrWillBeRawPtr<StaticElementList> querySelectorAll(const AtomicString& selectors, ExceptionState&);
     82 
     83     PassRefPtrWillBeRawPtr<Node> insertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node* refChild, ExceptionState& = ASSERT_NO_EXCEPTION);
     84     PassRefPtrWillBeRawPtr<Node> replaceChild(PassRefPtrWillBeRawPtr<Node> newChild, PassRefPtrWillBeRawPtr<Node> oldChild, ExceptionState& = ASSERT_NO_EXCEPTION);
     85     PassRefPtrWillBeRawPtr<Node> removeChild(PassRefPtrWillBeRawPtr<Node> child, ExceptionState& = ASSERT_NO_EXCEPTION);
     86     PassRefPtrWillBeRawPtr<Node> appendChild(PassRefPtrWillBeRawPtr<Node> newChild, ExceptionState& = ASSERT_NO_EXCEPTION);
     87 
     88     Element* getElementById(const AtomicString& id) const;
     89     PassRefPtrWillBeRawPtr<TagCollection> getElementsByTagName(const AtomicString&);
     90     PassRefPtrWillBeRawPtr<TagCollection> getElementsByTagNameNS(const AtomicString& namespaceURI, const AtomicString& localName);
     91     PassRefPtrWillBeRawPtr<NameNodeList> getElementsByName(const AtomicString& elementName);
     92     PassRefPtrWillBeRawPtr<ClassCollection> getElementsByClassName(const AtomicString& classNames);
     93     PassRefPtrWillBeRawPtr<RadioNodeList> radioNodeList(const AtomicString&, bool onlyMatchImgElements = false);
     94 
     95     // These methods are only used during parsing.
     96     // They don't send DOM mutation events or handle reparenting.
     97     void parserAppendChild(PassRefPtrWillBeRawPtr<Node>);
     98     void parserRemoveChild(Node&);
     99     void parserInsertBefore(PassRefPtrWillBeRawPtr<Node> newChild, Node& refChild);
    100     void parserTakeAllChildrenFrom(ContainerNode&);
    101 
    102     void removeChildren();
    103 
    104     void cloneChildNodes(ContainerNode* clone);
    105 
    106     virtual void attach(const AttachContext& = AttachContext()) OVERRIDE;
    107     virtual void detach(const AttachContext& = AttachContext()) OVERRIDE;
    108     virtual LayoutRect boundingBox() const OVERRIDE FINAL;
    109     virtual void setFocus(bool) OVERRIDE;
    110     void focusStateChanged();
    111     virtual void setActive(bool = true) OVERRIDE;
    112     virtual void setHovered(bool = true) OVERRIDE;
    113 
    114     bool childrenOrSiblingsAffectedByFocus() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
    115     void setChildrenOrSiblingsAffectedByFocus() { setRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
    116 
    117     bool childrenOrSiblingsAffectedByHover() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
    118     void setChildrenOrSiblingsAffectedByHover() { setRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
    119 
    120     bool childrenOrSiblingsAffectedByActive() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
    121     void setChildrenOrSiblingsAffectedByActive() { setRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
    122 
    123     bool childrenOrSiblingsAffectedByDrag() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
    124     void setChildrenOrSiblingsAffectedByDrag() { setRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
    125 
    126     bool childrenAffectedByPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules) || hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
    127 
    128     bool childrenAffectedByFirstChildRules() const { return hasRestyleFlag(ChildrenAffectedByFirstChildRules); }
    129     void setChildrenAffectedByFirstChildRules() { setRestyleFlag(ChildrenAffectedByFirstChildRules); }
    130 
    131     bool childrenAffectedByLastChildRules() const { return hasRestyleFlag(ChildrenAffectedByLastChildRules); }
    132     void setChildrenAffectedByLastChildRules() { setRestyleFlag(ChildrenAffectedByLastChildRules); }
    133 
    134     bool childrenAffectedByDirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
    135     void setChildrenAffectedByDirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
    136 
    137     bool childrenAffectedByIndirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
    138     void setChildrenAffectedByIndirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
    139 
    140     bool childrenAffectedByForwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
    141     void setChildrenAffectedByForwardPositionalRules() { setRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
    142 
    143     bool childrenAffectedByBackwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
    144     void setChildrenAffectedByBackwardPositionalRules() { setRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
    145 
    146     // FIXME: These methods should all be renamed to something better than "check",
    147     // since it's not clear that they alter the style bits of siblings and children.
    148     void checkForChildrenAdjacentRuleChanges();
    149     enum SiblingCheckType { FinishedParsingChildren, SiblingElementInserted, SiblingElementRemoved };
    150     void checkForSiblingStyleChanges(SiblingCheckType, Node* nodeBeforeChange, Node* nodeAfterChange);
    151     void recalcChildStyle(StyleRecalcChange);
    152 
    153     bool childrenSupportStyleSharing() const { return !hasRestyleFlags(); }
    154 
    155     // -----------------------------------------------------------------------------
    156     // Notification of document structure changes (see core/dom/Node.h for more notification methods)
    157 
    158     enum ChildrenChangeType { ElementInserted, NonElementInserted, ElementRemoved, NonElementRemoved, AllChildrenRemoved, TextChanged };
    159     enum ChildrenChangeSource { ChildrenChangeSourceAPI, ChildrenChangeSourceParser };
    160     struct ChildrenChange {
    161         STACK_ALLOCATED();
    162     public:
    163         static ChildrenChange forInsertion(Node& node, ChildrenChangeSource byParser)
    164         {
    165             ChildrenChange change = {
    166                 node.isElementNode() ? ElementInserted : NonElementInserted,
    167                 node.previousSibling(),
    168                 node.nextSibling(),
    169                 byParser
    170             };
    171             return change;
    172         }
    173 
    174         static ChildrenChange forRemoval(Node& node, Node* previousSibling, Node* nextSibling, ChildrenChangeSource byParser)
    175         {
    176             ChildrenChange change = {
    177                 node.isElementNode() ? ElementRemoved : NonElementRemoved,
    178                 previousSibling,
    179                 nextSibling,
    180                 byParser
    181             };
    182             return change;
    183         }
    184 
    185         bool isChildInsertion() const { return type == ElementInserted || type == NonElementInserted; }
    186         bool isChildRemoval() const { return type == ElementRemoved || type == NonElementRemoved; }
    187         bool isChildElementChange() const { return type == ElementInserted || type == ElementRemoved; }
    188 
    189         ChildrenChangeType type;
    190         RawPtrWillBeMember<Node> siblingBeforeChange;
    191         RawPtrWillBeMember<Node> siblingAfterChange;
    192         ChildrenChangeSource byParser;
    193     };
    194 
    195     // Notifies the node that it's list of children have changed (either by adding or removing child nodes), or a child
    196     // node that is of the type CDATA_SECTION_NODE, TEXT_NODE or COMMENT_NODE has changed its value.
    197     virtual void childrenChanged(const ChildrenChange&);
    198 
    199     void disconnectDescendantFrames();
    200 
    201     virtual void trace(Visitor*) OVERRIDE;
    202 
    203 protected:
    204     ContainerNode(TreeScope*, ConstructionType = CreateContainer);
    205 
    206     void invalidateNodeListCachesInAncestors(const QualifiedName* attrName = 0, Element* attributeOwnerElement = 0);
    207 
    208 #if !ENABLE(OILPAN)
    209     void removeDetachedChildren();
    210 #endif
    211 
    212     void setFirstChild(Node* child) { m_firstChild = child; }
    213     void setLastChild(Node* child) { m_lastChild = child; }
    214 
    215     // Utility functions for NodeListsNodeData API.
    216     template <typename Collection> PassRefPtrWillBeRawPtr<Collection> ensureCachedCollection(CollectionType);
    217     template <typename Collection> PassRefPtrWillBeRawPtr<Collection> ensureCachedCollection(CollectionType, const AtomicString& name);
    218     template <typename Collection> PassRefPtrWillBeRawPtr<Collection> ensureCachedCollection(CollectionType, const AtomicString& namespaceURI, const AtomicString& localName);
    219     template <typename Collection> Collection* cachedCollection(CollectionType);
    220 
    221 private:
    222     bool isContainerNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
    223     bool isTextNode() const WTF_DELETED_FUNCTION; // This will catch anyone doing an unnecessary check.
    224 
    225     NodeListsNodeData& ensureNodeLists();
    226     void removeBetween(Node* previousChild, Node* nextChild, Node& oldChild);
    227     void insertBeforeCommon(Node& nextChild, Node& oldChild);
    228     void appendChildCommon(Node& child);
    229     void updateTreeAfterInsertion(Node& child);
    230     void willRemoveChildren();
    231     void willRemoveChild(Node& child);
    232     void removeDetachedChildrenInContainer(ContainerNode&);
    233     void addChildNodesToDeletionQueue(Node*&, Node*&, ContainerNode&);
    234 
    235     void notifyNodeInserted(Node&, ChildrenChangeSource = ChildrenChangeSourceAPI);
    236     void notifyNodeInsertedInternal(Node&, NodeVector& postInsertionNotificationTargets);
    237     void notifyNodeRemoved(Node&);
    238 
    239     bool hasRestyleFlag(DynamicRestyleFlags mask) const { return hasRareData() && hasRestyleFlagInternal(mask); }
    240     bool hasRestyleFlags() const { return hasRareData() && hasRestyleFlagsInternal(); }
    241     void setRestyleFlag(DynamicRestyleFlags);
    242     bool hasRestyleFlagInternal(DynamicRestyleFlags) const;
    243     bool hasRestyleFlagsInternal() const;
    244 
    245     inline bool checkAcceptChildGuaranteedNodeTypes(const Node& newChild, ExceptionState&) const;
    246     inline bool checkAcceptChild(const Node* newChild, const Node* oldChild, ExceptionState&) const;
    247     inline bool containsConsideringHostElements(const Node&) const;
    248     inline bool isChildTypeAllowed(const Node& child) const;
    249 
    250     void attachChildren(const AttachContext& = AttachContext());
    251     void detachChildren(const AttachContext& = AttachContext());
    252 
    253     bool getUpperLeftCorner(FloatPoint&) const;
    254     bool getLowerRightCorner(FloatPoint&) const;
    255 
    256     RawPtrWillBeMember<Node> m_firstChild;
    257     RawPtrWillBeMember<Node> m_lastChild;
    258 };
    259 
    260 #if ENABLE(ASSERT)
    261 bool childAttachedAllowedWhenAttachingChildren(ContainerNode*);
    262 #endif
    263 
    264 DEFINE_NODE_TYPE_CASTS(ContainerNode, isContainerNode());
    265 
    266 inline bool ContainerNode::hasChildCount(unsigned count) const
    267 {
    268     Node* child = m_firstChild;
    269     while (count && child) {
    270         child = child->nextSibling();
    271         --count;
    272     }
    273     return !count && !child;
    274 }
    275 
    276 inline ContainerNode::ContainerNode(TreeScope* treeScope, ConstructionType type)
    277     : Node(treeScope, type)
    278     , m_firstChild(nullptr)
    279     , m_lastChild(nullptr)
    280 {
    281 }
    282 
    283 inline void ContainerNode::attachChildren(const AttachContext& context)
    284 {
    285     AttachContext childrenContext(context);
    286     childrenContext.resolvedStyle = 0;
    287 
    288     for (Node* child = firstChild(); child; child = child->nextSibling()) {
    289         ASSERT(child->needsAttach() || childAttachedAllowedWhenAttachingChildren(this));
    290         if (child->needsAttach())
    291             child->attach(childrenContext);
    292     }
    293 }
    294 
    295 inline void ContainerNode::detachChildren(const AttachContext& context)
    296 {
    297     AttachContext childrenContext(context);
    298     childrenContext.resolvedStyle = 0;
    299 
    300     for (Node* child = firstChild(); child; child = child->nextSibling())
    301         child->detach(childrenContext);
    302 }
    303 
    304 inline unsigned Node::countChildren() const
    305 {
    306     if (!isContainerNode())
    307         return 0;
    308     return toContainerNode(this)->countChildren();
    309 }
    310 
    311 inline Node* Node::firstChild() const
    312 {
    313     if (!isContainerNode())
    314         return 0;
    315     return toContainerNode(this)->firstChild();
    316 }
    317 
    318 inline Node* Node::lastChild() const
    319 {
    320     if (!isContainerNode())
    321         return 0;
    322     return toContainerNode(this)->lastChild();
    323 }
    324 
    325 inline ContainerNode* Node::parentElementOrShadowRoot() const
    326 {
    327     ContainerNode* parent = parentNode();
    328     return parent && (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
    329 }
    330 
    331 inline ContainerNode* Node::parentElementOrDocumentFragment() const
    332 {
    333     ContainerNode* parent = parentNode();
    334     return parent && (parent->isElementNode() || parent->isDocumentFragment()) ? parent : 0;
    335 }
    336 
    337 inline bool Node::isTreeScope() const
    338 {
    339     return &treeScope().rootNode() == this;
    340 }
    341 
    342 inline void getChildNodes(ContainerNode& node, NodeVector& nodes)
    343 {
    344     ASSERT(!nodes.size());
    345     for (Node* child = node.firstChild(); child; child = child->nextSibling())
    346         nodes.append(child);
    347 }
    348 
    349 } // namespace blink
    350 
    351 #endif // ContainerNode_h
    352