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

Side by Side Diff: third_party/WebKit/Source/core/dom/NthIndexCache.cpp

Issue 2588643004: Allow positional selectors to match elements without a parent. (Closed)
Patch Set: Addressed review comments Created 3 years, 11 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
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 #include "core/dom/NthIndexCache.h" 5 #include "core/dom/NthIndexCache.h"
6 6
7 #include "core/dom/Document.h" 7 #include "core/dom/Document.h"
8 #include "core/dom/ElementTraversal.h" 8 #include "core/dom/ElementTraversal.h"
9 9
10 namespace blink { 10 namespace blink {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 if (sibling->tagQName() == tag) 72 if (sibling->tagQName() == tag)
73 ++index; 73 ++index;
74 ++siblingCount; 74 ++siblingCount;
75 } 75 }
76 return index; 76 return index;
77 } 77 }
78 78
79 } // namespace 79 } // namespace
80 80
81 unsigned NthIndexCache::nthChildIndex(Element& element) { 81 unsigned NthIndexCache::nthChildIndex(Element& element) {
82 if (element.isPseudoElement()) 82 if (element.isPseudoElement() || !element.parentNode())
83 return 1; 83 return 1;
84 DCHECK(element.parentNode());
85 NthIndexCache* nthIndexCache = element.document().nthIndexCache(); 84 NthIndexCache* nthIndexCache = element.document().nthIndexCache();
86 NthIndexData* nthIndexData = nullptr; 85 NthIndexData* nthIndexData = nullptr;
87 if (nthIndexCache && nthIndexCache->m_parentMap) 86 if (nthIndexCache && nthIndexCache->m_parentMap)
88 nthIndexData = nthIndexCache->m_parentMap->get(element.parentNode()); 87 nthIndexData = nthIndexCache->m_parentMap->get(element.parentNode());
89 if (nthIndexData) 88 if (nthIndexData)
90 return nthIndexData->nthIndex(element); 89 return nthIndexData->nthIndex(element);
91 unsigned index = uncachedNthChildIndex(element); 90 unsigned index = uncachedNthChildIndex(element);
92 if (nthIndexCache && index > kCachedSiblingCountLimit) 91 if (element.parentNode() && nthIndexCache && index > kCachedSiblingCountLimit)
rune 2017/01/18 21:33:19 parentNode() is always non-null here.
emilio 2017/01/18 23:33:07 Good catches, sorry, should've paged in better all
93 nthIndexCache->cacheNthIndexDataForParent(element); 92 nthIndexCache->cacheNthIndexDataForParent(element);
94 return index; 93 return index;
95 } 94 }
96 95
97 unsigned NthIndexCache::nthLastChildIndex(Element& element) { 96 unsigned NthIndexCache::nthLastChildIndex(Element& element) {
98 if (element.isPseudoElement()) 97 if (element.isPseudoElement())
99 return 1; 98 return 1;
100 DCHECK(element.parentNode());
101 NthIndexCache* nthIndexCache = element.document().nthIndexCache(); 99 NthIndexCache* nthIndexCache = element.document().nthIndexCache();
102 NthIndexData* nthIndexData = nullptr; 100 NthIndexData* nthIndexData = nullptr;
103 if (nthIndexCache && nthIndexCache->m_parentMap) 101 if (element.parentNode() && nthIndexCache && nthIndexCache->m_parentMap)
rune 2017/01/18 21:33:19 Check !parentNode and return 1 as for nthChildInde
104 nthIndexData = nthIndexCache->m_parentMap->get(element.parentNode()); 102 nthIndexData = nthIndexCache->m_parentMap->get(element.parentNode());
105 if (nthIndexData) 103 if (nthIndexData)
106 return nthIndexData->nthLastIndex(element); 104 return nthIndexData->nthLastIndex(element);
107 unsigned index = uncachedNthLastChildIndex(element); 105 unsigned index = uncachedNthLastChildIndex(element);
108 if (nthIndexCache && index > kCachedSiblingCountLimit) 106 if (element.parentNode() && nthIndexCache && index > kCachedSiblingCountLimit)
rune 2017/01/18 21:33:19 No need to add the parentNode() check if you early
109 nthIndexCache->cacheNthIndexDataForParent(element); 107 nthIndexCache->cacheNthIndexDataForParent(element);
110 return index; 108 return index;
111 } 109 }
112 110
113 NthIndexData* NthIndexCache::nthTypeIndexDataForParent(Element& element) const { 111 NthIndexData* NthIndexCache::nthTypeIndexDataForParent(Element& element) const {
114 DCHECK(element.parentNode()); 112 if (!m_parentMapForType || !element.parentNode())
rune 2017/01/18 21:33:19 Early return in nthOfTypeIndex and nthLastOfTypeIn
115 if (!m_parentMapForType)
116 return nullptr; 113 return nullptr;
117 if (const IndexByType* map = m_parentMapForType->get(element.parentNode())) 114 if (const IndexByType* map = m_parentMapForType->get(element.parentNode()))
118 return map->get(element.tagName()); 115 return map->get(element.tagName());
119 return nullptr; 116 return nullptr;
120 } 117 }
121 118
122 unsigned NthIndexCache::nthOfTypeIndex(Element& element) { 119 unsigned NthIndexCache::nthOfTypeIndex(Element& element) {
123 if (element.isPseudoElement()) 120 if (element.isPseudoElement())
124 return 1; 121 return 1;
125 NthIndexCache* nthIndexCache = element.document().nthIndexCache(); 122 NthIndexCache* nthIndexCache = element.document().nthIndexCache();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 } 253 }
257 DCHECK(count); 254 DCHECK(count);
258 m_count = count; 255 m_count = count;
259 } 256 }
260 257
261 DEFINE_TRACE(NthIndexData) { 258 DEFINE_TRACE(NthIndexData) {
262 visitor->trace(m_elementIndexMap); 259 visitor->trace(m_elementIndexMap);
263 } 260 }
264 261
265 } // namespace blink 262 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698