Bug 1180118 - Part 1: Add a method to match a single nsCSSSelector (without pseudo-elements) against an Element. r=bzbarsky

This commit is contained in:
Cameron McCormack
2015-08-05 22:42:20 +10:00
parent 40a97b6415
commit 7fff04f539
3 changed files with 51 additions and 0 deletions

View File

@@ -159,6 +159,12 @@ public:
void ToString(nsAString& aString, mozilla::CSSStyleSheet* aSheet,
bool aAppend = false) const;
#ifdef DEBUG
bool IsRestrictedSelector() const {
return PseudoType() == nsCSSPseudoElements::ePseudo_NotPseudoElement;
}
#endif
private:
void AddPseudoClassInternal(nsPseudoClassList *aPseudoClass);
nsCSSSelector* Clone(bool aDeepNext, bool aDeepNegations) const;

View File

@@ -2293,6 +2293,36 @@ static bool SelectorMatches(Element* aElement,
#undef STATE_CHECK
/* static */ bool
nsCSSRuleProcessor::RestrictedSelectorMatches(
Element* aElement,
nsCSSSelector* aSelector,
TreeMatchContext& aTreeMatchContext)
{
MOZ_ASSERT(aSelector->IsRestrictedSelector(),
"aSelector must not have a pseudo-element");
// We match aSelector as if :visited and :link both match visited and
// unvisited links.
NodeMatchContext nodeContext(EventStates(),
nsCSSRuleProcessor::IsLink(aElement));
if (nodeContext.mIsRelevantLink) {
aTreeMatchContext.SetHaveRelevantLink();
}
aTreeMatchContext.ResetForUnvisitedMatching();
bool matches = SelectorMatches(aElement, aSelector, nodeContext,
aTreeMatchContext, SelectorMatchesFlags::NONE);
if (nodeContext.mIsRelevantLink) {
aTreeMatchContext.ResetForVisitedMatching();
if (SelectorMatches(aElement, aSelector, nodeContext, aTreeMatchContext,
SelectorMatchesFlags::NONE)) {
matches = true;
}
}
return matches;
}
// Right now, there are four operators:
// ' ', the descendant combinator, is greedy
// '~', the indirect adjacent sibling combinator, is greedy

View File

@@ -112,6 +112,21 @@ public:
*/
static bool IsLink(mozilla::dom::Element* aElement);
/**
* Returns true if the given aElement matches aSelector.
* Like nsCSSRuleProcessor.cpp's SelectorMatches (and unlike
* SelectorMatchesTree), this does not check an entire selector list
* separated by combinators.
*
* :visited and :link will match both visited and non-visited links,
* as if aTreeMatchContext->mVisitedHandling were eLinksVisitedOrUnvisited.
*
* aSelector is restricted to not containing pseudo-elements.
*/
static bool RestrictedSelectorMatches(mozilla::dom::Element* aElement,
nsCSSSelector* aSelector,
TreeMatchContext& aTreeMatchContext);
// nsIStyleRuleProcessor
virtual void RulesMatching(ElementRuleProcessorData* aData) override;