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

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

Issue 1023393002: Cache element indices for :nth-child and :nth-last-child selectors. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: More review issues Created 5 years, 8 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef NthIndexCache_h
6 #define NthIndexCache_h
7
8 #include "core/dom/Element.h"
9 #include "core/dom/ElementTraversal.h"
10 #include "platform/heap/Handle.h"
11 #include "wtf/HashMap.h"
12 #include "wtf/OwnPtr.h"
13 #include "wtf/RefPtr.h"
14
15 namespace blink {
16
17 class Document;
18
19 class NthIndexCache final : NoBaseWillBeGarbageCollected<NthIndexCache> {
20 WTF_MAKE_NONCOPYABLE(NthIndexCache);
21 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexCache);
22 public:
23 static PassOwnPtr<NthIndexCache> create(Document& document)
24 {
25 return adoptPtrWillBeNoop(new NthIndexCache(document));
26 }
27
28 unsigned nthChildIndex(Element&);
29 unsigned nthLastChildIndex(Element&);
30
31 void clear();
32
33 DECLARE_TRACE();
34
35 private:
36 explicit NthIndexCache(Document& document)
37 : m_document(document) { }
38
39 class NthIndexData final : public NoBaseWillBeGarbageCollected<NthIndexData> {
40 WTF_MAKE_NONCOPYABLE(NthIndexData);
41 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData);
42 public:
43 NthIndexData() : m_count(0) { }
44
45 inline unsigned nthIndex(Element&);
46 inline unsigned nthLastIndex(Element&);
47
48 unsigned cacheNthIndices(Element&);
49
50 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM ap;
51 unsigned m_count;
52
53 DECLARE_TRACE();
54 };
55
56 NthIndexData& ensureNthIndexDataFor(Node&);
57 inline unsigned nthIndex(Element&);
58
59 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe mber<NthIndexData>>;
60
61 OwnPtrWillBeMember<ParentMap> m_parentMap;
62 RawPtrWillBeMember<Document> m_document;
63 };
64
65 class NthIndexCacheScope {
66 STACK_ALLOCATED();
67 WTF_MAKE_NONCOPYABLE(NthIndexCacheScope);
68 public:
69 NthIndexCacheScope(Document&);
70 ~NthIndexCacheScope();
71 private:
72 RawPtrWillBeMember<Document> m_document;
73 };
74
75 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element)
76 {
77 if (!m_count)
78 return cacheNthIndices(element);
79
80 unsigned index = 0;
81 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling), index++) {
82 auto it = m_elementIndexMap.find(sibling);
83 if (it != m_elementIndexMap.end())
84 return it->value + index;
85 }
86 return index;
87 }
88
89 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element)
90 {
91 unsigned index = nthIndex(element);
92 return m_count - index + 1;
93 }
94
95 inline unsigned NthIndexCache::nthChildIndex(Element& element)
96 {
97 ASSERT(element.parentNode());
98 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element);
99 }
100
101 inline unsigned NthIndexCache::nthLastChildIndex(Element& element)
102 {
103 ASSERT(element.parentNode());
104 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element);
105 }
106
107 } // namespace blink
108
109 #endif // NthIndexCache_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698