Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / LiveNodeListBase.h
1 /*
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.
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
25 #ifndef LiveNodeListBase_h
26 #define LiveNodeListBase_h
27
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"
34
35 namespace WebCore {
36
37 enum NodeListRootType {
38     NodeListIsRootedAtNode,
39     NodeListIsRootedAtDocument
40 };
41
42 class LiveNodeListBase {
43 public:
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)
50     {
51         ASSERT(m_rootType == static_cast<unsigned>(rootType));
52         ASSERT(m_invalidationType == static_cast<unsigned>(invalidationType));
53         ASSERT(m_collectionType == static_cast<unsigned>(collectionType));
54
55         document().registerNodeList(this);
56     }
57
58     virtual ~LiveNodeListBase()
59     {
60         document().unregisterNodeList(this);
61     }
62
63     ContainerNode& rootNode() const;
64
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; }
70
71     virtual void invalidateCache(Document* oldDocument = 0) const = 0;
72     void invalidateCacheForAttribute(const QualifiedName*) const;
73
74     static bool shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType, const QualifiedName&);
75
76 protected:
77     Document& document() const { return m_ownerNode->document(); }
78
79     ALWAYS_INLINE NodeListRootType rootType() const { return static_cast<NodeListRootType>(m_rootType); }
80
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);
93
94 private:
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;
99 };
100
101 ALWAYS_INLINE bool LiveNodeListBase::shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType type, const QualifiedName& attrName)
102 {
103     switch (type) {
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:
118         return false;
119     case InvalidateOnAnyAttrChange:
120         return true;
121     }
122     return false;
123 }
124
125 template <typename NodeListType>
126 Element* LiveNodeListBase::lastMatchingElement(const NodeListType& nodeList)
127 {
128     ContainerNode& root = nodeList.rootNode();
129     Element* element = ElementTraversal::lastWithin(root);
130     while (element && !isMatchingElement(nodeList, *element))
131         element = ElementTraversal::previous(*element, &root);
132     return element;
133 }
134
135 template <class NodeListType>
136 Element* LiveNodeListBase::firstMatchingElement(const NodeListType& nodeList)
137 {
138     ContainerNode& root = nodeList.rootNode();
139     Element* element = ElementTraversal::firstWithin(root);
140     while (element && !isMatchingElement(nodeList, *element))
141         element = ElementTraversal::next(*element, &root);
142     return element;
143 }
144
145 template <class NodeListType>
146 Element* LiveNodeListBase::nextMatchingElement(const NodeListType& nodeList, Element& current)
147 {
148     ContainerNode& root = nodeList.rootNode();
149     Element* next = &current;
150     do {
151         next = ElementTraversal::next(*next, &root);
152     } while (next && !isMatchingElement(nodeList, *next));
153     return next;
154 }
155
156 template <class NodeListType>
157 Element* LiveNodeListBase::previousMatchingElement(const NodeListType& nodeList, Element& current)
158 {
159     ContainerNode& root = nodeList.rootNode();
160     Element* previous = &current;
161     do {
162         previous = ElementTraversal::previous(*previous, &root);
163     } while (previous && !isMatchingElement(nodeList, *previous));
164     return previous;
165 }
166
167 template <class NodeListType>
168 Element* LiveNodeListBase::traverseMatchingElementsForwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset)
169 {
170     ASSERT(currentOffset < offset);
171     Element* next = &currentElement;
172     while ((next = nextMatchingElement(nodeList, *next))) {
173         if (++currentOffset == offset)
174             return next;
175     }
176     return 0;
177 }
178
179 template <class NodeListType>
180 Element* LiveNodeListBase::traverseMatchingElementsBackwardToOffset(const NodeListType& nodeList, unsigned offset, Element& currentElement, unsigned& currentOffset)
181 {
182     ASSERT(currentOffset > offset);
183     Element* previous = &currentElement;
184     while ((previous = previousMatchingElement(nodeList, *previous))) {
185         if (--currentOffset == offset)
186             return previous;
187     }
188     return 0;
189 }
190
191 } // namespace WebCore
192
193 #endif // LiveNodeListBase_h