matchedPseudoRules method
Implementation
List<CSSStyleRule> matchedPseudoRules(RuleSet ruleSet, Element element) {
final SelectorEvaluator evaluator = SelectorEvaluator();
// Collect candidates from all indexed buckets because many pseudo-element
// selectors (e.g., ".foo div::before") are indexed under tag/class/id
// buckets for matching efficiency.
final List<CSSRule> candidates = [];
// #id
String? id = element.id;
if (id != null) {
candidates.addAll(_collectMatchingRulesForList(
ruleSet.idRules[id],
element,
evaluator: evaluator,
includePseudo: true,
enableAncestryFastPath: false,
));
}
// .class
for (final String className in element.classList) {
candidates.addAll(_collectMatchingRulesForList(
ruleSet.classRules[className],
element,
evaluator: evaluator,
includePseudo: true,
enableAncestryFastPath: false,
));
}
// [attr]
for (final String attribute in element.attributes.keys) {
candidates.addAll(_collectMatchingRulesForList(
ruleSet.attributeRules[attribute.toUpperCase()],
element,
evaluator: evaluator,
includePseudo: true,
enableAncestryFastPath: false,
));
}
// tag
final String tagLookup = element.tagName.toUpperCase();
candidates.addAll(_collectMatchingRulesForList(
ruleSet.tagRules[tagLookup],
element,
evaluator: evaluator,
includePseudo: true,
enableAncestryFastPath: false,
));
// universal
candidates.addAll(_collectMatchingRulesForList(
ruleSet.universalRules,
element,
evaluator: evaluator,
includePseudo: true,
enableAncestryFastPath: false,
));
// legacy pseudo bucket (for selectors without a better rightmost key)
candidates.addAll(_collectMatchingRulesForList(
ruleSet.pseudoRules,
element,
evaluator: evaluator,
includePseudo: true,
enableAncestryFastPath: false,
));
// Deduplicate while preserving order.
final List<CSSStyleRule> list = [];
final Set<CSSStyleRule> seen = {};
for (final CSSRule r in candidates) {
if (r is CSSStyleRule && !seen.contains(r)) {
seen.add(r);
list.add(r);
}
}
return list;
}