Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: Source/core/dom/LiveNodeList.h

Issue 177213010: Move LiveNodeListBase class to its own header (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/core.gypi ('k') | Source/core/dom/LiveNodeList.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. 5 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
11 * 11 *
12 * This library is distributed in the hope that it will be useful, 12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details. 15 * Library General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Library General Public License 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 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, 19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA. 20 * Boston, MA 02110-1301, USA.
21 * 21 *
22 */ 22 */
23 23
24 #ifndef LiveNodeList_h 24 #ifndef LiveNodeList_h
25 #define LiveNodeList_h 25 #define LiveNodeList_h
26 26
27 #include "HTMLNames.h" 27 #include "core/dom/LiveNodeListBase.h"
28 #include "core/dom/Document.h"
29 #include "core/dom/NodeList.h" 28 #include "core/dom/NodeList.h"
30 #include "core/html/CollectionIndexCache.h" 29 #include "core/html/CollectionIndexCache.h"
31 #include "core/html/CollectionType.h" 30 #include "core/html/CollectionType.h"
32 #include "wtf/Forward.h" 31 #include "wtf/PassRefPtr.h"
33 #include "wtf/RefPtr.h"
34 32
35 namespace WebCore { 33 namespace WebCore {
36 34
37 class Element; 35 class Element;
38 36
39 enum NodeListRootType {
40 NodeListIsRootedAtNode,
41 NodeListIsRootedAtDocument,
42 NodeListIsRootedAtDocumentIfOwnerHasItemrefAttr,
43 };
44
45 class LiveNodeListBase {
46 public:
47 LiveNodeListBase(ContainerNode* ownerNode, NodeListRootType rootType, NodeLi stInvalidationType invalidationType,
48 CollectionType collectionType)
49 : m_ownerNode(ownerNode)
50 , m_rootType(rootType)
51 , m_invalidationType(invalidationType)
52 , m_collectionType(collectionType)
53 {
54 ASSERT(m_ownerNode);
55 ASSERT(m_rootType == static_cast<unsigned>(rootType));
56 ASSERT(m_invalidationType == static_cast<unsigned>(invalidationType));
57 ASSERT(m_collectionType == static_cast<unsigned>(collectionType));
58
59 document().registerNodeList(this);
60 }
61
62 virtual ~LiveNodeListBase()
63 {
64 document().unregisterNodeList(this);
65 }
66
67 ContainerNode& rootNode() const;
68
69 void didMoveToDocument(Document& oldDocument, Document& newDocument);
70 ALWAYS_INLINE bool hasIdNameCache() const { return !isLiveNodeListType(type( )); }
71 ALWAYS_INLINE bool isRootedAtDocument() const { return m_rootType == NodeLis tIsRootedAtDocument || m_rootType == NodeListIsRootedAtDocumentIfOwnerHasItemref Attr; }
72 ALWAYS_INLINE NodeListInvalidationType invalidationType() const { return sta tic_cast<NodeListInvalidationType>(m_invalidationType); }
73 ALWAYS_INLINE CollectionType type() const { return static_cast<CollectionTyp e>(m_collectionType); }
74 ContainerNode* ownerNode() const { return m_ownerNode.get(); }
75 ALWAYS_INLINE void invalidateCache(const QualifiedName* attrName) const
76 {
77 if (!attrName || shouldInvalidateTypeOnAttributeChange(invalidationType( ), *attrName))
78 invalidateCache();
79 else if (hasIdNameCache() && (*attrName == HTMLNames::idAttr || *attrNam e == HTMLNames::nameAttr))
80 invalidateIdNameCacheMaps();
81 }
82 virtual void invalidateCache(Document* oldDocument = 0) const = 0;
83
84 static bool shouldInvalidateTypeOnAttributeChange(NodeListInvalidationType, const QualifiedName&);
85
86 protected:
87 Document& document() const { return m_ownerNode->document(); }
88
89 ALWAYS_INLINE NodeListRootType rootType() const { return static_cast<NodeLis tRootType>(m_rootType); }
90
91 template <typename Collection>
92 static Element* iterateForPreviousNode(const Collection&, Node* current);
93 template <typename Collection>
94 static Element* itemBefore(const Collection&, const Element* previousItem);
95
96 private:
97 void invalidateIdNameCacheMaps() const;
98
99 RefPtr<ContainerNode> m_ownerNode; // Cannot be null.
100 const unsigned m_rootType : 2;
101 const unsigned m_invalidationType : 4;
102 const unsigned m_collectionType : 5;
103 };
104
105 ALWAYS_INLINE bool LiveNodeListBase::shouldInvalidateTypeOnAttributeChange(NodeL istInvalidationType type, const QualifiedName& attrName)
106 {
107 switch (type) {
108 case InvalidateOnClassAttrChange:
109 return attrName == HTMLNames::classAttr;
110 case InvalidateOnNameAttrChange:
111 return attrName == HTMLNames::nameAttr;
112 case InvalidateOnIdNameAttrChange:
113 return attrName == HTMLNames::idAttr || attrName == HTMLNames::nameAttr;
114 case InvalidateOnForAttrChange:
115 return attrName == HTMLNames::forAttr;
116 case InvalidateForFormControls:
117 return attrName == HTMLNames::nameAttr || attrName == HTMLNames::idAttr || attrName == HTMLNames::forAttr
118 || attrName == HTMLNames::formAttr || attrName == HTMLNames::typeAtt r;
119 case InvalidateOnHRefAttrChange:
120 return attrName == HTMLNames::hrefAttr;
121 case DoNotInvalidateOnAttributeChanges:
122 return false;
123 case InvalidateOnAnyAttrChange:
124 return true;
125 }
126 return false;
127 }
128
129 class LiveNodeList : public NodeList, public LiveNodeListBase { 37 class LiveNodeList : public NodeList, public LiveNodeListBase {
130 public: 38 public:
131 LiveNodeList(PassRefPtr<ContainerNode> ownerNode, CollectionType collectionT ype, NodeListInvalidationType invalidationType, NodeListRootType rootType = Node ListIsRootedAtNode) 39 LiveNodeList(PassRefPtr<ContainerNode> ownerNode, CollectionType collectionT ype, NodeListInvalidationType invalidationType, NodeListRootType rootType = Node ListIsRootedAtNode)
132 : LiveNodeListBase(ownerNode.get(), rootType, invalidationType, 40 : LiveNodeListBase(ownerNode.get(), rootType, invalidationType,
133 collectionType) 41 collectionType)
134 { } 42 { }
135 43
136 virtual unsigned length() const OVERRIDE FINAL { return m_collectionIndexCac he.nodeCount(*this); } 44 virtual unsigned length() const OVERRIDE FINAL { return m_collectionIndexCac he.nodeCount(*this); }
137 virtual Node* item(unsigned offset) const OVERRIDE FINAL { return m_collecti onIndexCache.nodeAt(*this, offset); } 45 virtual Node* item(unsigned offset) const OVERRIDE FINAL { return m_collecti onIndexCache.nodeAt(*this, offset); }
138 virtual bool nodeMatches(const Element&) const = 0; 46 virtual bool nodeMatches(const Element&) const = 0;
139 47
140 virtual void invalidateCache(Document* oldDocument) const OVERRIDE FINAL; 48 virtual void invalidateCache(Document* oldDocument) const OVERRIDE FINAL;
141 bool shouldOnlyIncludeDirectChildren() const { return false; } 49 bool shouldOnlyIncludeDirectChildren() const { return false; }
142 50
143 // Collection IndexCache API. 51 // Collection IndexCache API.
144 bool canTraverseBackward() const { return true; } 52 bool canTraverseBackward() const { return true; }
145 Element* itemBefore(const Element* previousItem) const; 53 Element* itemBefore(const Element* previousItem) const;
146 Element* traverseToFirstElement(const ContainerNode& root) const; 54 Element* traverseToFirstElement(const ContainerNode& root) const;
147 Element* traverseForwardToOffset(unsigned offset, Element& currentNode, unsi gned& currentOffset, const ContainerNode& root) const; 55 Element* traverseForwardToOffset(unsigned offset, Element& currentNode, unsi gned& currentOffset, const ContainerNode& root) const;
148 56
149 private: 57 private:
150 virtual Node* virtualOwnerNode() const OVERRIDE FINAL; 58 virtual Node* virtualOwnerNode() const OVERRIDE FINAL;
151 59
152 mutable CollectionIndexCache<LiveNodeList, Element> m_collectionIndexCache; 60 mutable CollectionIndexCache<LiveNodeList, Element> m_collectionIndexCache;
153 }; 61 };
154 62
155 } // namespace WebCore 63 } // namespace WebCore
156 64
157 #endif // LiveNodeList_h 65 #endif // LiveNodeList_h
OLDNEW
« no previous file with comments | « Source/core/core.gypi ('k') | Source/core/dom/LiveNodeList.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698