2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2013 Apple Inc. All rights reserved.
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.
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.
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.
24 #ifndef ContainerNode_h
25 #define ContainerNode_h
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"
35 class ClassCollection;
39 template <typename NodeType> class StaticNodeTypeList;
40 typedef StaticNodeTypeList<Element> StaticElementList;
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,
55 NumberOfDynamicRestyleFlags = 10,
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;
64 class ContainerNode : public Node {
66 virtual ~ContainerNode();
68 Node* firstChild() const { return m_firstChild; }
69 Node* lastChild() const { return m_lastChild; }
70 bool hasChildren() const { return m_firstChild; }
72 bool hasOneChild() const { return m_firstChild && !m_firstChild->nextSibling(); }
73 bool hasOneTextChild() const { return hasOneChild() && m_firstChild->isTextNode(); }
74 bool hasChildCount(unsigned) const;
76 PassRefPtrWillBeRawPtr<HTMLCollection> children();
78 unsigned countChildren() const;
80 PassRefPtrWillBeRawPtr<Element> querySelector(const AtomicString& selectors, ExceptionState&);
81 PassRefPtrWillBeRawPtr<StaticElementList> querySelectorAll(const AtomicString& selectors, ExceptionState&);
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);
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);
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&);
102 void removeChildren();
104 void cloneChildNodes(ContainerNode* clone);
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;
114 bool childrenOrSiblingsAffectedByFocus() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
115 void setChildrenOrSiblingsAffectedByFocus() { setRestyleFlag(ChildrenOrSiblingsAffectedByFocus); }
117 bool childrenOrSiblingsAffectedByHover() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
118 void setChildrenOrSiblingsAffectedByHover() { setRestyleFlag(ChildrenOrSiblingsAffectedByHover); }
120 bool childrenOrSiblingsAffectedByActive() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
121 void setChildrenOrSiblingsAffectedByActive() { setRestyleFlag(ChildrenOrSiblingsAffectedByActive); }
123 bool childrenOrSiblingsAffectedByDrag() const { return hasRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
124 void setChildrenOrSiblingsAffectedByDrag() { setRestyleFlag(ChildrenOrSiblingsAffectedByDrag); }
126 bool childrenAffectedByPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules) || hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
128 bool childrenAffectedByFirstChildRules() const { return hasRestyleFlag(ChildrenAffectedByFirstChildRules); }
129 void setChildrenAffectedByFirstChildRules() { setRestyleFlag(ChildrenAffectedByFirstChildRules); }
131 bool childrenAffectedByLastChildRules() const { return hasRestyleFlag(ChildrenAffectedByLastChildRules); }
132 void setChildrenAffectedByLastChildRules() { setRestyleFlag(ChildrenAffectedByLastChildRules); }
134 bool childrenAffectedByDirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
135 void setChildrenAffectedByDirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByDirectAdjacentRules); }
137 bool childrenAffectedByIndirectAdjacentRules() const { return hasRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
138 void setChildrenAffectedByIndirectAdjacentRules() { setRestyleFlag(ChildrenAffectedByIndirectAdjacentRules); }
140 bool childrenAffectedByForwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
141 void setChildrenAffectedByForwardPositionalRules() { setRestyleFlag(ChildrenAffectedByForwardPositionalRules); }
143 bool childrenAffectedByBackwardPositionalRules() const { return hasRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
144 void setChildrenAffectedByBackwardPositionalRules() { setRestyleFlag(ChildrenAffectedByBackwardPositionalRules); }
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);
153 bool childrenSupportStyleSharing() const { return !hasRestyleFlags(); }
155 // -----------------------------------------------------------------------------
156 // Notification of document structure changes (see core/dom/Node.h for more notification methods)
158 enum ChildrenChangeType { ElementInserted, NonElementInserted, ElementRemoved, NonElementRemoved, AllChildrenRemoved, TextChanged };
159 enum ChildrenChangeSource { ChildrenChangeSourceAPI, ChildrenChangeSourceParser };
160 struct ChildrenChange {
163 static ChildrenChange forInsertion(Node& node, ChildrenChangeSource byParser)
165 ChildrenChange change = {
166 node.isElementNode() ? ElementInserted : NonElementInserted,
167 node.previousSibling(),
174 static ChildrenChange forRemoval(Node& node, Node* previousSibling, Node* nextSibling, ChildrenChangeSource byParser)
176 ChildrenChange change = {
177 node.isElementNode() ? ElementRemoved : NonElementRemoved,
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; }
189 ChildrenChangeType type;
190 RawPtrWillBeMember<Node> siblingBeforeChange;
191 RawPtrWillBeMember<Node> siblingAfterChange;
192 ChildrenChangeSource byParser;
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&);
199 void disconnectDescendantFrames();
201 virtual void trace(Visitor*) OVERRIDE;
204 ContainerNode(TreeScope*, ConstructionType = CreateContainer);
206 void invalidateNodeListCachesInAncestors(const QualifiedName* attrName = 0, Element* attributeOwnerElement = 0);
209 void removeDetachedChildren();
212 void setFirstChild(Node* child) { m_firstChild = child; }
213 void setLastChild(Node* child) { m_lastChild = child; }
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);
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.
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&);
235 void notifyNodeInserted(Node&, ChildrenChangeSource = ChildrenChangeSourceAPI);
236 void notifyNodeInsertedInternal(Node&, NodeVector& postInsertionNotificationTargets);
237 void notifyNodeRemoved(Node&);
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;
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;
250 void attachChildren(const AttachContext& = AttachContext());
251 void detachChildren(const AttachContext& = AttachContext());
253 bool getUpperLeftCorner(FloatPoint&) const;
254 bool getLowerRightCorner(FloatPoint&) const;
256 RawPtrWillBeMember<Node> m_firstChild;
257 RawPtrWillBeMember<Node> m_lastChild;
261 bool childAttachedAllowedWhenAttachingChildren(ContainerNode*);
264 DEFINE_NODE_TYPE_CASTS(ContainerNode, isContainerNode());
266 inline bool ContainerNode::hasChildCount(unsigned count) const
268 Node* child = m_firstChild;
269 while (count && child) {
270 child = child->nextSibling();
273 return !count && !child;
276 inline ContainerNode::ContainerNode(TreeScope* treeScope, ConstructionType type)
277 : Node(treeScope, type)
278 , m_firstChild(nullptr)
279 , m_lastChild(nullptr)
283 inline void ContainerNode::attachChildren(const AttachContext& context)
285 AttachContext childrenContext(context);
286 childrenContext.resolvedStyle = 0;
288 for (Node* child = firstChild(); child; child = child->nextSibling()) {
289 ASSERT(child->needsAttach() || childAttachedAllowedWhenAttachingChildren(this));
290 if (child->needsAttach())
291 child->attach(childrenContext);
295 inline void ContainerNode::detachChildren(const AttachContext& context)
297 AttachContext childrenContext(context);
298 childrenContext.resolvedStyle = 0;
300 for (Node* child = firstChild(); child; child = child->nextSibling())
301 child->detach(childrenContext);
304 inline unsigned Node::countChildren() const
306 if (!isContainerNode())
308 return toContainerNode(this)->countChildren();
311 inline Node* Node::firstChild() const
313 if (!isContainerNode())
315 return toContainerNode(this)->firstChild();
318 inline Node* Node::lastChild() const
320 if (!isContainerNode())
322 return toContainerNode(this)->lastChild();
325 inline ContainerNode* Node::parentElementOrShadowRoot() const
327 ContainerNode* parent = parentNode();
328 return parent && (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
331 inline ContainerNode* Node::parentElementOrDocumentFragment() const
333 ContainerNode* parent = parentNode();
334 return parent && (parent->isElementNode() || parent->isDocumentFragment()) ? parent : 0;
337 inline bool Node::isTreeScope() const
339 return &treeScope().rootNode() == this;
342 inline void getChildNodes(ContainerNode& node, NodeVector& nodes)
344 ASSERT(!nodes.size());
345 for (Node* child = node.firstChild(); child; child = child->nextSibling())
351 #endif // ContainerNode_h