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

Side by Side Diff: Source/core/css/SelectorChecker.cpp

Issue 1135503004: Clean up nth-index calculation in SelectorChecker. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 7 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com) 3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com) 4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved. 5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> 6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org> 7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/) 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t orchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved. 9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved. 10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 static bool isFirstOfType(Element& element, const QualifiedName& type) 176 static bool isFirstOfType(Element& element, const QualifiedName& type)
177 { 177 {
178 return !ElementTraversal::previousSibling(element, HasTagName(type)); 178 return !ElementTraversal::previousSibling(element, HasTagName(type));
179 } 179 }
180 180
181 static bool isLastOfType(Element& element, const QualifiedName& type) 181 static bool isLastOfType(Element& element, const QualifiedName& type)
182 { 182 {
183 return !ElementTraversal::nextSibling(element, HasTagName(type)); 183 return !ElementTraversal::nextSibling(element, HasTagName(type));
184 } 184 }
185 185
186 static int countElementsBefore(Element& element) 186 static int nthChildIndex(Element& element)
187 { 187 {
188 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) 188 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
189 return nthIndexCache->nthChildIndex(element) - 1; 189 return nthIndexCache->nthChildIndex(element);
190 190
191 int count = 0; 191 int index = 1;
192 for (const Element* sibling = ElementTraversal::previousSibling(element); si bling; sibling = ElementTraversal::previousSibling(*sibling)) 192 for (const Element* sibling = ElementTraversal::previousSibling(element); si bling; sibling = ElementTraversal::previousSibling(*sibling))
193 count++; 193 index++;
194 194
195 return count; 195 return index;
196 } 196 }
197 197
198 static int countElementsOfTypeBefore(Element& element, const QualifiedName& type ) 198 static int nthOfTypeIndex(Element& element, const QualifiedName& type)
199 { 199 {
200 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) 200 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
201 return nthIndexCache->nthChildIndexOfType(element, type) - 1; 201 return nthIndexCache->nthChildIndexOfType(element, type);
202 int count = 0; 202 int index = 1;
203 for (const Element* sibling = ElementTraversal::previousSibling(element, Has TagName(type)); sibling; sibling = ElementTraversal::previousSibling(*sibling, H asTagName(type))) 203 for (const Element* sibling = ElementTraversal::previousSibling(element, Has TagName(type)); sibling; sibling = ElementTraversal::previousSibling(*sibling, H asTagName(type)))
204 ++count; 204 ++index;
205 return count; 205 return index;
206 } 206 }
207 207
208 static int countElementsAfter(Element& element) 208 static int nthLastChildIndex(Element& element)
209 { 209 {
210 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) 210 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
211 return nthIndexCache->nthLastChildIndex(element) - 1; 211 return nthIndexCache->nthLastChildIndex(element);
212 212
213 int count = 0; 213 int index = 1;
214 for (const Element* sibling = ElementTraversal::nextSibling(element); siblin g; sibling = ElementTraversal::nextSibling(*sibling)) 214 for (const Element* sibling = ElementTraversal::nextSibling(element); siblin g; sibling = ElementTraversal::nextSibling(*sibling))
215 ++count; 215 ++index;
216 return count; 216 return index;
217 } 217 }
218 218
219 static int countElementsOfTypeAfter(Element& element, const QualifiedName& type) 219 static int nthLastOfTypeIndex(Element& element, const QualifiedName& type)
220 { 220 {
221 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache()) 221 if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
222 return nthIndexCache->nthLastChildIndexOfType(element, type) - 1; 222 return nthIndexCache->nthLastChildIndexOfType(element, type);
223 223
224 int count = 0; 224 int index = 1;
225 for (const Element* sibling = ElementTraversal::nextSibling(element, HasTagN ame(type)); sibling; sibling = ElementTraversal::nextSibling(*sibling, HasTagNam e(type))) 225 for (const Element* sibling = ElementTraversal::nextSibling(element, HasTagN ame(type)); sibling; sibling = ElementTraversal::nextSibling(*sibling, HasTagNam e(type)))
226 ++count; 226 ++index;
227 return count; 227 return index;
228 } 228 }
229 229
230 bool SelectorChecker::match(const SelectorCheckingContext& context, MatchResult* result) const 230 bool SelectorChecker::match(const SelectorCheckingContext& context, MatchResult* result) const
231 { 231 {
232 return matchSelector(context, result) == SelectorMatches; 232 return matchSelector(context, result) == SelectorMatches;
233 } 233 }
234 234
235 // Recursive check of selectors and combinators 235 // Recursive check of selectors and combinators
236 // It can return 4 different values: 236 // It can return 4 different values:
237 // * SelectorMatches - the selector matches the element e 237 // * SelectorMatches - the selector matches the element e
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
804 return false; 804 return false;
805 return isFirstOfType(element, element.tagQName()) && isLastOfType(el ement, element.tagQName()); 805 return isFirstOfType(element, element.tagQName()) && isLastOfType(el ement, element.tagQName());
806 } 806 }
807 break; 807 break;
808 case CSSSelector::PseudoNthChild: 808 case CSSSelector::PseudoNthChild:
809 if (!selector.parseNth()) 809 if (!selector.parseNth())
810 break; 810 break;
811 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) { 811 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
812 if (m_mode == ResolvingStyle) 812 if (m_mode == ResolvingStyle)
813 parent->setChildrenAffectedByForwardPositionalRules(); 813 parent->setChildrenAffectedByForwardPositionalRules();
814 return selector.matchNth(1 + countElementsBefore(element)); 814 return selector.matchNth(nthChildIndex(element));
815 } 815 }
816 break; 816 break;
817 case CSSSelector::PseudoNthOfType: 817 case CSSSelector::PseudoNthOfType:
818 if (!selector.parseNth()) 818 if (!selector.parseNth())
819 break; 819 break;
820 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) { 820 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
821 if (m_mode == ResolvingStyle) 821 if (m_mode == ResolvingStyle)
822 parent->setChildrenAffectedByForwardPositionalRules(); 822 parent->setChildrenAffectedByForwardPositionalRules();
823 return selector.matchNth(1 + countElementsOfTypeBefore(element, elem ent.tagQName())); 823 return selector.matchNth(nthOfTypeIndex(element, element.tagQName()) );
824 } 824 }
825 break; 825 break;
826 case CSSSelector::PseudoNthLastChild: 826 case CSSSelector::PseudoNthLastChild:
827 if (!selector.parseNth()) 827 if (!selector.parseNth())
828 break; 828 break;
829 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) { 829 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
830 if (m_mode == ResolvingStyle) 830 if (m_mode == ResolvingStyle)
831 parent->setChildrenAffectedByBackwardPositionalRules(); 831 parent->setChildrenAffectedByBackwardPositionalRules();
832 if (!parent->isFinishedParsingChildren()) 832 if (!parent->isFinishedParsingChildren())
833 return false; 833 return false;
834 return selector.matchNth(1 + countElementsAfter(element)); 834 return selector.matchNth(nthLastChildIndex(element));
835 } 835 }
836 break; 836 break;
837 case CSSSelector::PseudoNthLastOfType: 837 case CSSSelector::PseudoNthLastOfType:
838 if (!selector.parseNth()) 838 if (!selector.parseNth())
839 break; 839 break;
840 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) { 840 if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
841 if (m_mode == ResolvingStyle) 841 if (m_mode == ResolvingStyle)
842 parent->setChildrenAffectedByBackwardPositionalRules(); 842 parent->setChildrenAffectedByBackwardPositionalRules();
843 if (!parent->isFinishedParsingChildren()) 843 if (!parent->isFinishedParsingChildren())
844 return false; 844 return false;
845 return selector.matchNth(1 + countElementsOfTypeAfter(element, eleme nt.tagQName())); 845 return selector.matchNth(nthLastOfTypeIndex(element, element.tagQNam e()));
846 } 846 }
847 break; 847 break;
848 case CSSSelector::PseudoTarget: 848 case CSSSelector::PseudoTarget:
849 return element == element.document().cssTarget(); 849 return element == element.document().cssTarget();
850 case CSSSelector::PseudoAny: 850 case CSSSelector::PseudoAny:
851 { 851 {
852 SelectorCheckingContext subContext(context); 852 SelectorCheckingContext subContext(context);
853 subContext.isSubSelector = true; 853 subContext.isSubSelector = true;
854 ASSERT(selector.selectorList()); 854 ASSERT(selector.selectorList());
855 for (subContext.selector = selector.selectorList()->first(); subCont ext.selector; subContext.selector = CSSSelectorList::next(*subContext.selector)) { 855 for (subContext.selector = selector.selectorList()->first(); subCont ext.selector; subContext.selector = CSSSelectorList::next(*subContext.selector)) {
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
1200 } 1200 }
1201 1201
1202 bool SelectorChecker::matchesFocusPseudoClass(const Element& element) 1202 bool SelectorChecker::matchesFocusPseudoClass(const Element& element)
1203 { 1203 {
1204 if (InspectorInstrumentation::forcePseudoState(const_cast<Element*>(&element ), CSSSelector::PseudoFocus)) 1204 if (InspectorInstrumentation::forcePseudoState(const_cast<Element*>(&element ), CSSSelector::PseudoFocus))
1205 return true; 1205 return true;
1206 return element.focused() && isFrameFocused(element); 1206 return element.focused() && isFrameFocused(element);
1207 } 1207 }
1208 1208
1209 } 1209 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698