Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include "config.h" | |
| 6 #include "core/dom/NthIndexCache.h" | |
| 7 | |
| 8 #include "core/dom/Document.h" | |
| 9 | |
| 10 namespace blink { | |
| 11 | |
| 12 NthIndexCache* NthIndexCache::s_instance = nullptr; | |
| 13 | |
| 14 NthIndexCache::NthIndexCache(Document& document) | |
| 15 : m_document(&document) | |
| 16 , m_domTreeVersion(document.domTreeVersion()) | |
| 17 { | |
| 18 ASSERT(!s_instance); | |
| 19 s_instance = this; | |
| 20 } | |
| 21 | |
| 22 NthIndexCache::~NthIndexCache() | |
| 23 { | |
| 24 ASSERT(m_domTreeVersion == m_document->domTreeVersion()); | |
| 25 s_instance = nullptr; | |
| 26 } | |
| 27 | |
| 28 NthIndexCache::NthIndexData& NthIndexCache::ensureNthIndexDataFor(Node& parent) | |
| 29 { | |
| 30 if (!m_parentMap) | |
| 31 m_parentMap = adoptPtrWillBeNoop(new ParentMap()); | |
| 32 | |
| 33 ParentMap::AddResult addResult = m_parentMap->add(&parent, nullptr); | |
| 34 if (addResult.isNewEntry) | |
| 35 addResult.storedValue->value = adoptPtrWillBeNoop(new NthIndexData()); | |
| 36 | |
| 37 ASSERT(addResult.storedValue->value); | |
| 38 return *addResult.storedValue->value; | |
| 39 } | |
| 40 | |
| 41 unsigned NthIndexCache::NthIndexData::cacheNthIndices(Element& element) | |
| 42 { | |
| 43 unsigned index = 0; | |
| 44 const unsigned spread = 3; | |
|
esprehn
2015/03/31 17:35:15
You might want a comment that explains what spread
rune
2015/03/31 20:57:37
Done.
| |
| 45 unsigned count = 0; | |
| 46 for (Element* sibling = ElementTraversal::firstChild(*element.parentNode()); sibling; sibling = ElementTraversal::nextSibling(*sibling)) { | |
| 47 if (!(++count % spread)) | |
| 48 m_elementIndexMap.add(sibling, count); | |
| 49 if (sibling == &element) | |
| 50 index = count; | |
| 51 } | |
| 52 ASSERT(count && index); | |
| 53 m_count = count; | |
| 54 return index; | |
| 55 } | |
| 56 | |
| 57 DEFINE_TRACE(NthIndexCache::NthIndexData) | |
| 58 { | |
| 59 #if ENABLE(OILPAN) | |
| 60 visitor->trace(m_elementIndexMap); | |
| 61 #endif | |
| 62 } | |
| 63 | |
| 64 #if !ENABLE(OILPAN) | |
| 65 NthIndexCache::NthIndexData::~NthIndexData() | |
| 66 { | |
| 67 } | |
| 68 #endif | |
| 69 | |
| 70 } // namespace blink | |
| OLD | NEW |