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, 2006, 2007 Apple Inc. All rights reserved.
6 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
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.
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.
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.
25 #ifndef LiveNodeListBase_h
26 #define LiveNodeListBase_h
28 #include "HTMLNames.h"
29 #include "core/dom/Document.h"
30 #include "core/dom/Element.h"
31 #include "core/dom/ElementTraversal.h"
32 #include "core/dom/NodeTraversal.h"
33 #include "core/html/CollectionType.h"
37 enum NodeListRootType {
38 NodeListIsRootedAtNode,
39 NodeListIsRootedAtDocument
42 class LiveNodeListBase {
44 LiveNodeListBase(ContainerNode& ownerNode, NodeListRootType rootType, NodeListInvalidationType invalidationType,
45 CollectionType collectionType)
46 : m_ownerNode(ownerNode)
47 , m_rootType(rootType)
48 , m_invalidationType(invalidationType)
49 , m_collectionType(collectionType)
51 ASSERT(m_rootType == static_cast<unsigned>(rootType));
52 ASSERT(m_invalidationType == static_cast<unsigned>(invalidationType));
53 ASSERT(m_collectionType == static_cast<unsigned>(collectionType));
55 document().registerNodeList(this);
58 virtual ~LiveNodeListBase()
60 document().unregisterNodeList(this);
63 ContainerNode& rootNode() const;
65 void didMoveToDocument(Document& oldDocument, Document& newDocument);
66 ALWAYS_INLINE bool isRootedAtDocument() const { return m_rootType == NodeListIsRootedAtDocument; }
67 ALWAYS_INLINE NodeListInvalidationType invalidationType() const { return static_cast<NodeListInvalidationType>(m_invalidationType); }
68 ALWAYS_INLINE CollectionType type() const { return static_cast<CollectionType>(m_collectionType); }
69 ContainerNode& ownerNode() const { return *m_ownerNode; }
71 virtual void invalidateCache(Document* oldDocument = 0) const = 0;
72 void invalidateCacheForAttribute(const QualifiedName*) const;
74 static bool shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType, const QualifiedName&);
77 Document& document() const { return m_ownerNode->document(); }
79 ALWAYS_INLINE NodeListRootType rootType() const { return static_cast<NodeListRootType>(m_rootType); }
81 template <class NodeListType>
82 static Element* firstMatchingElement(const NodeListType&);
83 template <class NodeListType>
84 static Element* lastMatchingElement(const NodeListType&);
85 template <class NodeListType>
86 static Element* nextMatchingElement(const NodeListType&, Element& current);
87 template <class NodeListType>
88 static Element* previousMatchingElement(const NodeListType&, Element& current);
89 template <class NodeListType>
90 static Element* traverseMatchingElementsForwardToOffset(const NodeListType&, unsigned offset, Element& currentElement, unsigned& currentOffset);
91 template <class NodeListType>
92 static Element* traverseMatchingElementsBackwardToOffset(const NodeListType&, unsigned offset, Element& currentElement, unsigned& currentOffset);
95 RefPtr<ContainerNode> m_ownerNode; // Cannot be null.
96 const unsigned m_rootType : 1;
97 const unsigned m_invalidationType : 4;
98 const unsigned m_collectionType : 5;
101 ALWAYS_INLINE bool LiveNodeListBase::shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType type, const QualifiedName& attrName)
104 case InvalidateOnClassAttrChange:
105 return attrName == HTMLNames::classAttr;
106 case InvalidateOnNameAttrChange:
107 return attrName == HTMLNames::nameAttr;
108 case InvalidateOnIdNameAttrChange:
109 return attrName == HTMLNames::idAttr || attrName == HTMLNames::nameAttr;
110 case InvalidateOnForAttrChange:
111 return attrName == HTMLNames::forAttr;
112 case InvalidateForFormControls:
113 return attrName == HTMLNames::nameAttr || attrName == HTMLNames::idAttr || attrName == HTMLNames::forAttr
114 || attrName == HTMLNames::formAttr || attrName == HTMLNames::typeAttr;
115 case InvalidateOnHRefAttrChange:
116 return attrName == HTMLNames::hrefAttr;
117 case DoNotInvalidateOnAttributeChanges:
119 case InvalidateOnAnyAttrChange:
125 template <typename NodeListType>
126 Element* LiveNodeListBase::lastMatchingElement(const NodeListType& nodeList)
128 ContainerNode& root = nodeList.rootNode();
129 Element* element = ElementTraversal::lastWithin(root);
130 while (element && !isMatchingElement(nodeList, *element))
131 element = ElementTraversal::previous(*element, &root);
135 template <class NodeListType>
136 Element* LiveNodeListBase::firstMatchingElement(const NodeListType& nodeList)
138 ContainerNode& root = nodeList.rootNode();
139 Element* element = ElementTraversal::firstWithin(root);
140 while (element && !isMatchingElement(nodeList, *element))
141 element = ElementTraversal::next(*element, &root);
145 template <class NodeListType>
146 Element* LiveNodeListBase::nextMatchingElement(const NodeListType& nodeList, Element& current)
148 ContainerNode& root = nodeList.rootNode();
149 Element* next = ¤t;
151 next = ElementTraversal::next(*next, &root);
152 } while (next && !isMatchingElement(nodeList, *next));
156 template <class NodeListType>
157 Element* LiveNodeListBase::previousMatchingElement(const NodeListType& nodeList, Element& current)
159 ContainerNode& root = nodeList.rootNode();
160 Element* previous = ¤t;
162 previous = ElementTraversal::previous(*previous, &root);
163 } while (previous && !isMatchingElement(nodeList, *previous));
167 template <class NodeListType>
168 Element* LiveNodeListBase::traverseMatchingElementsForwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset)
170 ASSERT(currentOffset < offset);
171 Element* next = ¤tElement;
172 while ((next = nextMatchingElement(nodeList, *next))) {
173 if (++currentOffset == offset)
179 template <class NodeListType>
180 Element* LiveNodeListBase::traverseMatchingElementsBackwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset)
182 ASSERT(currentOffset > offset);
183 Element* previous = ¤tElement;
184 while ((previous = previousMatchingElement(nodeList, *previous))) {
185 if (--currentOffset == offset)
191 } // namespace WebCore
193 #endif // LiveNodeListBase_h