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 "core/HTMLNames.h"
29 #include "core/dom/Document.h"
30 #include "core/dom/ElementTraversal.h"
31 #include "core/html/CollectionType.h"
32 #include "platform/heap/Handle.h"
33
34 namespace blink {
35
36 enum NodeListRootType {
37 NodeListIsRootedAtNode,
38 NodeListIsRootedAtDocument
39 };
40
41 class LiveNodeListBase : public WillBeGarbageCollectedMixin {
42 public:
LiveNodeListBase(ContainerNode & ownerNode,NodeListRootType rootType,NodeListInvalidationType invalidationType,CollectionType collectionType)43 LiveNodeListBase(ContainerNode& ownerNode, NodeListRootType rootType, NodeListInvalidationType invalidationType,
44 CollectionType collectionType)
45 : m_ownerNode(ownerNode)
46 , m_rootType(rootType)
47 , m_invalidationType(invalidationType)
48 , m_collectionType(collectionType)
49 {
50 ASSERT(m_rootType == static_cast<unsigned>(rootType));
51 ASSERT(m_invalidationType == static_cast<unsigned>(invalidationType));
52 ASSERT(m_collectionType == static_cast<unsigned>(collectionType));
53
54 document().registerNodeList(this);
55 }
56
~LiveNodeListBase()57 virtual ~LiveNodeListBase()
58 {
59 #if !ENABLE(OILPAN)
60 document().unregisterNodeList(this);
61 #endif
62 }
63
64 ContainerNode& rootNode() const;
65
66 void didMoveToDocument(Document& oldDocument, Document& newDocument);
isRootedAtDocument()67 ALWAYS_INLINE bool isRootedAtDocument() const { return m_rootType == NodeListIsRootedAtDocument; }
invalidationType()68 ALWAYS_INLINE NodeListInvalidationType invalidationType() const { return static_cast<NodeListInvalidationType>(m_invalidationType); }
type()69 ALWAYS_INLINE CollectionType type() const { return static_cast<CollectionType>(m_collectionType); }
ownerNode()70 ContainerNode& ownerNode() const { return *m_ownerNode; }
71
72 virtual void invalidateCache(Document* oldDocument = 0) const = 0;
73 void invalidateCacheForAttribute(const QualifiedName*) const;
74
75 static bool shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType, const QualifiedName&);
76
77 protected:
document()78 Document& document() const { return m_ownerNode->document(); }
79
rootType()80 ALWAYS_INLINE NodeListRootType rootType() const { return static_cast<NodeListRootType>(m_rootType); }
81
82 template <typename MatchFunc>
83 static Element* traverseMatchingElementsForwardToOffset(Element& currentElement, const ContainerNode* stayWithin, unsigned offset, unsigned& currentOffset, MatchFunc);
84 template <typename MatchFunc>
85 static Element* traverseMatchingElementsBackwardToOffset(Element& currentElement, const ContainerNode* stayWithin, unsigned offset, unsigned& currentOffset, MatchFunc);
86
trace(Visitor * visitor)87 virtual void trace(Visitor* visitor) { visitor->trace(m_ownerNode); }
88
89 private:
90 RefPtrWillBeMember<ContainerNode> m_ownerNode; // Cannot be null.
91 const unsigned m_rootType : 1;
92 const unsigned m_invalidationType : 4;
93 const unsigned m_collectionType : 5;
94 };
95
shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType type,const QualifiedName & attrName)96 ALWAYS_INLINE bool LiveNodeListBase::shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType type, const QualifiedName& attrName)
97 {
98 switch (type) {
99 case InvalidateOnClassAttrChange:
100 return attrName == HTMLNames::classAttr;
101 case InvalidateOnNameAttrChange:
102 return attrName == HTMLNames::nameAttr;
103 case InvalidateOnIdNameAttrChange:
104 return attrName == HTMLNames::idAttr || attrName == HTMLNames::nameAttr;
105 case InvalidateOnForAttrChange:
106 return attrName == HTMLNames::forAttr;
107 case InvalidateForFormControls:
108 return attrName == HTMLNames::nameAttr || attrName == HTMLNames::idAttr || attrName == HTMLNames::forAttr
109 || attrName == HTMLNames::formAttr || attrName == HTMLNames::typeAttr;
110 case InvalidateOnHRefAttrChange:
111 return attrName == HTMLNames::hrefAttr;
112 case DoNotInvalidateOnAttributeChanges:
113 return false;
114 case InvalidateOnAnyAttrChange:
115 return true;
116 }
117 return false;
118 }
119
120 template <typename MatchFunc>
traverseMatchingElementsForwardToOffset(Element & currentElement,const ContainerNode * stayWithin,unsigned offset,unsigned & currentOffset,MatchFunc isMatch)121 Element* LiveNodeListBase::traverseMatchingElementsForwardToOffset(Element& currentElement, const ContainerNode* stayWithin, unsigned offset, unsigned& currentOffset, MatchFunc isMatch)
122 {
123 ASSERT(currentOffset < offset);
124 for (Element* next = ElementTraversal::next(currentElement, stayWithin, isMatch); next; next = ElementTraversal::next(*next, stayWithin, isMatch)) {
125 if (++currentOffset == offset)
126 return next;
127 }
128 return 0;
129 }
130
131 template <typename MatchFunc>
traverseMatchingElementsBackwardToOffset(Element & currentElement,const ContainerNode * stayWithin,unsigned offset,unsigned & currentOffset,MatchFunc isMatch)132 Element* LiveNodeListBase::traverseMatchingElementsBackwardToOffset(Element& currentElement, const ContainerNode* stayWithin, unsigned offset, unsigned& currentOffset, MatchFunc isMatch)
133 {
134 ASSERT(currentOffset > offset);
135 for (Element* previous = ElementTraversal::previous(currentElement, stayWithin, isMatch); previous; previous = ElementTraversal::previous(*previous, stayWithin, isMatch)) {
136 if (--currentOffset == offset)
137 return previous;
138 }
139 return 0;
140 }
141
142 } // namespace blink
143
144 #endif // LiveNodeListBase_h
145