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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/css/SelectorChecker.cpp
diff --git a/Source/core/css/SelectorChecker.cpp b/Source/core/css/SelectorChecker.cpp
index debd2b0fa86bc375127fa28d27ecccef5a96d777..8c46bd91176849a0eada246d00bb726c8abc55cb 100644
--- a/Source/core/css/SelectorChecker.cpp
+++ b/Source/core/css/SelectorChecker.cpp
@@ -183,48 +183,48 @@ static bool isLastOfType(Element& element, const QualifiedName& type)
return !ElementTraversal::nextSibling(element, HasTagName(type));
}
-static int countElementsBefore(Element& element)
+static int nthChildIndex(Element& element)
{
if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
- return nthIndexCache->nthChildIndex(element) - 1;
+ return nthIndexCache->nthChildIndex(element);
- int count = 0;
+ int index = 1;
for (const Element* sibling = ElementTraversal::previousSibling(element); sibling; sibling = ElementTraversal::previousSibling(*sibling))
- count++;
+ index++;
- return count;
+ return index;
}
-static int countElementsOfTypeBefore(Element& element, const QualifiedName& type)
+static int nthOfTypeIndex(Element& element, const QualifiedName& type)
{
if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
- return nthIndexCache->nthChildIndexOfType(element, type) - 1;
- int count = 0;
+ return nthIndexCache->nthChildIndexOfType(element, type);
+ int index = 1;
for (const Element* sibling = ElementTraversal::previousSibling(element, HasTagName(type)); sibling; sibling = ElementTraversal::previousSibling(*sibling, HasTagName(type)))
- ++count;
- return count;
+ ++index;
+ return index;
}
-static int countElementsAfter(Element& element)
+static int nthLastChildIndex(Element& element)
{
if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
- return nthIndexCache->nthLastChildIndex(element) - 1;
+ return nthIndexCache->nthLastChildIndex(element);
- int count = 0;
+ int index = 1;
for (const Element* sibling = ElementTraversal::nextSibling(element); sibling; sibling = ElementTraversal::nextSibling(*sibling))
- ++count;
- return count;
+ ++index;
+ return index;
}
-static int countElementsOfTypeAfter(Element& element, const QualifiedName& type)
+static int nthLastOfTypeIndex(Element& element, const QualifiedName& type)
{
if (NthIndexCache* nthIndexCache = element.document().nthIndexCache())
- return nthIndexCache->nthLastChildIndexOfType(element, type) - 1;
+ return nthIndexCache->nthLastChildIndexOfType(element, type);
- int count = 0;
+ int index = 1;
for (const Element* sibling = ElementTraversal::nextSibling(element, HasTagName(type)); sibling; sibling = ElementTraversal::nextSibling(*sibling, HasTagName(type)))
- ++count;
- return count;
+ ++index;
+ return index;
}
bool SelectorChecker::match(const SelectorCheckingContext& context, MatchResult* result) const
@@ -811,7 +811,7 @@ bool SelectorChecker::checkPseudoClass(const SelectorCheckingContext& context, u
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByForwardPositionalRules();
- return selector.matchNth(1 + countElementsBefore(element));
+ return selector.matchNth(nthChildIndex(element));
}
break;
case CSSSelector::PseudoNthOfType:
@@ -820,7 +820,7 @@ bool SelectorChecker::checkPseudoClass(const SelectorCheckingContext& context, u
if (ContainerNode* parent = element.parentElementOrDocumentFragment()) {
if (m_mode == ResolvingStyle)
parent->setChildrenAffectedByForwardPositionalRules();
- return selector.matchNth(1 + countElementsOfTypeBefore(element, element.tagQName()));
+ return selector.matchNth(nthOfTypeIndex(element, element.tagQName()));
}
break;
case CSSSelector::PseudoNthLastChild:
@@ -831,7 +831,7 @@ bool SelectorChecker::checkPseudoClass(const SelectorCheckingContext& context, u
parent->setChildrenAffectedByBackwardPositionalRules();
if (!parent->isFinishedParsingChildren())
return false;
- return selector.matchNth(1 + countElementsAfter(element));
+ return selector.matchNth(nthLastChildIndex(element));
}
break;
case CSSSelector::PseudoNthLastOfType:
@@ -842,7 +842,7 @@ bool SelectorChecker::checkPseudoClass(const SelectorCheckingContext& context, u
parent->setChildrenAffectedByBackwardPositionalRules();
if (!parent->isFinishedParsingChildren())
return false;
- return selector.matchNth(1 + countElementsOfTypeAfter(element, element.tagQName()));
+ return selector.matchNth(nthLastOfTypeIndex(element, element.tagQName()));
}
break;
case CSSSelector::PseudoTarget:
« 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