OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NthIndexCache_h | 5 #ifndef NthIndexCache_h |
6 #define NthIndexCache_h | 6 #define NthIndexCache_h |
7 | 7 |
8 #include "core/CoreExport.h" | 8 #include "core/CoreExport.h" |
9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
10 #include "core/dom/ElementTraversal.h" | 10 #include "core/dom/ElementTraversal.h" |
11 #include "platform/heap/Handle.h" | 11 #include "platform/heap/Handle.h" |
12 #include "wtf/HashMap.h" | 12 #include "wtf/HashMap.h" |
13 #include "wtf/OwnPtr.h" | 13 #include "wtf/OwnPtr.h" |
14 #include "wtf/RefPtr.h" | 14 #include "wtf/RefPtr.h" |
15 | 15 |
16 namespace blink { | 16 namespace blink { |
17 | 17 |
18 class Document; | 18 class Document; |
| 19 class HasTagName { |
| 20 public: |
| 21 explicit HasTagName(const QualifiedName& tagName) : m_tagName(tagName) { } |
| 22 bool operator() (const Element& element) const { return element.hasTagName(m
_tagName); } |
| 23 private: |
| 24 const QualifiedName m_tagName; |
| 25 }; |
19 | 26 |
20 class CORE_EXPORT NthIndexCache final { | 27 class CORE_EXPORT NthIndexCache final { |
21 STACK_ALLOCATED(); | 28 STACK_ALLOCATED(); |
22 WTF_MAKE_NONCOPYABLE(NthIndexCache); | 29 WTF_MAKE_NONCOPYABLE(NthIndexCache); |
23 public: | 30 public: |
24 explicit NthIndexCache(Document&); | 31 explicit NthIndexCache(Document&); |
25 ~NthIndexCache(); | 32 ~NthIndexCache(); |
26 | 33 |
27 inline unsigned nthChildIndex(Element&); | 34 inline unsigned nthChildIndex(Element&); |
| 35 inline unsigned nthChildIndexOfType(Element&, const QualifiedName&); |
28 inline unsigned nthLastChildIndex(Element&); | 36 inline unsigned nthLastChildIndex(Element&); |
| 37 inline unsigned nthLastChildIndexOfType(Element&, const QualifiedName&); |
29 | 38 |
30 private: | 39 private: |
31 class NthIndexData final : public NoBaseWillBeGarbageCollected<NthIndexData>
{ | 40 class NthIndexData final : public NoBaseWillBeGarbageCollected<NthIndexData>
{ |
32 WTF_MAKE_NONCOPYABLE(NthIndexData); | 41 WTF_MAKE_NONCOPYABLE(NthIndexData); |
33 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData); | 42 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData); |
34 public: | 43 public: |
35 NthIndexData() { } | 44 NthIndexData() { } |
36 | 45 |
37 inline unsigned nthIndex(Element&); | 46 inline unsigned nthIndex(Element&); |
| 47 inline unsigned nthIndexOfType(Element&, const QualifiedName&); |
38 inline unsigned nthLastIndex(Element&); | 48 inline unsigned nthLastIndex(Element&); |
| 49 inline unsigned nthLastIndexOfType(Element&, const QualifiedName&); |
39 | 50 |
40 unsigned cacheNthIndices(Element&); | 51 unsigned cacheNthIndices(Element&); |
| 52 unsigned cacheNthIndicesOfType(Element&, const QualifiedName&); |
41 | 53 |
42 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM
ap; | 54 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM
ap; |
43 unsigned m_count = 0; | 55 unsigned m_count = 0; |
44 | 56 |
45 DECLARE_TRACE(); | 57 DECLARE_TRACE(); |
46 }; | 58 }; |
47 | 59 |
| 60 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe
mber<NthIndexData>>; |
| 61 OwnPtrWillBeMember<ParentMap> m_parentMap; |
| 62 |
| 63 using IndexByType = WillBeHeapHashMap<String, OwnPtrWillBeMember<NthIndexDat
a>>; |
| 64 |
| 65 using ParentMapForType = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrW
illBeMember<IndexByType>>; |
| 66 OwnPtrWillBeMember<ParentMapForType> m_parentMapForType; |
| 67 |
| 68 |
48 NthIndexData& ensureNthIndexDataFor(Node&); | 69 NthIndexData& ensureNthIndexDataFor(Node&); |
| 70 NthIndexCache::IndexByType& ensureTypeIndexMap(Node&); |
49 | 71 |
50 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe
mber<NthIndexData>>; | 72 NthIndexCache::NthIndexData& nthIndexDataWithTagName(Element&); |
51 | 73 |
52 OwnPtrWillBeMember<ParentMap> m_parentMap; | |
53 RawPtrWillBeMember<Document> m_document; | 74 RawPtrWillBeMember<Document> m_document; |
54 uint64_t m_domTreeVersion; | 75 uint64_t m_domTreeVersion; |
55 }; | 76 }; |
56 | 77 |
57 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) | 78 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) |
58 { | 79 { |
59 if (element.isPseudoElement()) | 80 if (element.isPseudoElement()) |
60 return 1; | 81 return 1; |
61 if (!m_count) | 82 if (!m_count) |
62 return cacheNthIndices(element); | 83 return cacheNthIndices(element); |
63 | 84 |
64 unsigned index = 0; | 85 unsigned index = 0; |
65 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ
ousSibling(*sibling), index++) { | 86 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ
ousSibling(*sibling), index++) { |
66 auto it = m_elementIndexMap.find(sibling); | 87 auto it = m_elementIndexMap.find(sibling); |
67 if (it != m_elementIndexMap.end()) | 88 if (it != m_elementIndexMap.end()) |
68 return it->value + index; | 89 return it->value + index; |
69 } | 90 } |
70 return index; | 91 return index; |
71 } | 92 } |
72 | 93 |
| 94 inline unsigned NthIndexCache::NthIndexData::nthIndexOfType(Element& element, co
nst QualifiedName& type) |
| 95 { |
| 96 if (element.isPseudoElement()) |
| 97 return 1; |
| 98 if (!m_count) |
| 99 return cacheNthIndicesOfType(element, type); |
| 100 unsigned index = 0; |
| 101 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ
ousSibling(*sibling, HasTagName(type)), index++) { |
| 102 auto it = m_elementIndexMap.find(sibling); |
| 103 if (it != m_elementIndexMap.end()) |
| 104 return it->value + index; |
| 105 } |
| 106 return index; |
| 107 } |
| 108 |
73 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) | 109 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) |
74 { | 110 { |
75 if (element.isPseudoElement()) | 111 if (element.isPseudoElement()) |
76 return 1; | 112 return 1; |
77 unsigned index = nthIndex(element); | 113 unsigned index = nthIndex(element); |
78 return m_count - index + 1; | 114 return m_count - index + 1; |
79 } | 115 } |
80 | 116 |
| 117 inline unsigned NthIndexCache::NthIndexData::nthLastIndexOfType(Element& element
, const QualifiedName& type) |
| 118 { |
| 119 if (element.isPseudoElement()) |
| 120 return 1; |
| 121 unsigned index = nthIndexOfType(element, type); |
| 122 return m_count - index + 1; |
| 123 } |
| 124 |
81 inline unsigned NthIndexCache::nthChildIndex(Element& element) | 125 inline unsigned NthIndexCache::nthChildIndex(Element& element) |
82 { | 126 { |
83 ASSERT(element.parentNode()); | 127 ASSERT(element.parentNode()); |
84 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); | 128 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); |
85 } | 129 } |
86 | 130 |
| 131 inline unsigned NthIndexCache::nthChildIndexOfType(Element& element, const Quali
fiedName& type) |
| 132 { |
| 133 ASSERT(element.parentNode()); |
| 134 return nthIndexDataWithTagName(element).nthIndexOfType(element, type); |
| 135 } |
| 136 |
87 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) | 137 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) |
88 { | 138 { |
89 ASSERT(element.parentNode()); | 139 ASSERT(element.parentNode()); |
90 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); | 140 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); |
91 } | 141 } |
92 | 142 |
| 143 inline unsigned NthIndexCache::nthLastChildIndexOfType(Element& element, const Q
ualifiedName& type) |
| 144 { |
| 145 ASSERT(element.parentNode()); |
| 146 return nthIndexDataWithTagName(element).nthLastIndexOfType(element, type); |
| 147 } |
| 148 |
| 149 |
93 } // namespace blink | 150 } // namespace blink |
94 | 151 |
95 #endif // NthIndexCache_h | 152 #endif // NthIndexCache_h |
OLD | NEW |