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

Unified 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: Clear cache after each operation Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/dom/NthIndexCache.h
diff --git a/Source/core/dom/NthIndexCache.h b/Source/core/dom/NthIndexCache.h
new file mode 100644
index 0000000000000000000000000000000000000000..a76a3195adae2a3fc2e191e7cab809c530e02a99
--- /dev/null
+++ b/Source/core/dom/NthIndexCache.h
@@ -0,0 +1,91 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NthIndexCache_h
+#define NthIndexCache_h
+
+#include "core/dom/Element.h"
+#include "core/dom/ElementTraversal.h"
+#include "platform/heap/Handle.h"
+#include "wtf/HashMap.h"
+#include "wtf/RefPtr.h"
+
+namespace blink {
+
+class Document;
+
+class NthIndexCache : NoBaseWillBeGarbageCollected<NthIndexCache> {
+ WTF_MAKE_NONCOPYABLE(NthIndexCache);
+public:
+ NthIndexCache(Document& document)
+ : m_document(document) { }
+
+ unsigned nthChildIndex(Element&);
+ unsigned nthLastChildIndex(Element&);
+
+ void clear();
+
+ DECLARE_TRACE();
+
+private:
+ class NthIndexData : public NoBaseWillBeGarbageCollected<NthIndexData> {
+ WTF_MAKE_NONCOPYABLE(NthIndexData);
+ public:
+ NthIndexData() : m_count(0) { }
+
+ inline unsigned nthIndex(Element&);
+ inline unsigned nthLastIndex(Element&);
+
+ unsigned cacheNthIndices(Element&);
+
+ WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexMap;
+ unsigned m_count;
+
+ DECLARE_TRACE();
+ };
+
+ NthIndexData& ensureNthIndexDataFor(Node&);
+ inline unsigned nthIndex(Element&);
+
+ typedef WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMember<NthIndexData>> ParentMap;
sof 2015/03/28 08:33:59 nit: "using" is preferred over "typedef" now.
rune 2015/03/30 09:00:03 Done.
+
+ OwnPtrWillBeMember<ParentMap> m_parentMap;
+ Document& m_document;
sof 2015/03/28 08:33:59 Make this a RawPtrWillBeMember<Document> and have
rune 2015/03/30 09:00:03 Done.
+};
+
+inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element)
+{
+ if (!m_count)
+ return cacheNthIndices(element);
+
+ unsigned index = 0;
+ for (Element* sibling = &element; sibling; sibling = ElementTraversal::previousSibling(*sibling), index++) {
+ auto it = m_elementIndexMap.find(sibling);
+ if (it != m_elementIndexMap.end())
+ return it->value + index;
+ }
+ return index;
+}
+
+inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element)
+{
+ unsigned index = nthIndex(element);
+ return m_count - index + 1;
+}
+
+inline unsigned NthIndexCache::nthChildIndex(Element& element)
+{
+ ASSERT(element.parentNode());
+ return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element);
+}
+
+inline unsigned NthIndexCache::nthLastChildIndex(Element& element)
+{
+ ASSERT(element.parentNode());
+ return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element);
+}
+
+} // namespace blink
+
+#endif // NthIndexCache_h

Powered by Google App Engine
This is Rietveld 408576698