matchedRules method
Implementation
List<CSSRule> matchedRules(RuleSet ruleSet, Element element) {
List<CSSRule> matchedRules = [];
int candidateCount = 0;
// Reuse a single evaluator per matchedRules() to avoid repeated allocations.
final SelectorEvaluator evaluator = SelectorEvaluator();
// Build ancestor token sets once per call if fast-path is enabled, to avoid
// repeatedly walking the ancestor chain for each candidate rule.
final _AncestorTokenSet? ancestorTokens = DebugFlags.enableCssAncestryFastPath
? _buildAncestorTokens(element)
: null;
if (ruleSet.isEmpty) {
return matchedRules;
}
// #id
String? id = element.id;
if (id != null) {
final list = ruleSet.idRules[id];
candidateCount += (list?.length ?? 0);
matchedRules.addAll(_collectMatchingRulesForList(
list,
element,
evaluator: evaluator,
enableAncestryFastPath: DebugFlags.enableCssAncestryFastPath,
ancestorTokens: ancestorTokens,
));
}
// .class
for (String className in element.classList) {
final list = ruleSet.classRules[className];
candidateCount += (list?.length ?? 0);
matchedRules.addAll(_collectMatchingRulesForList(
list,
element,
evaluator: evaluator,
enableAncestryFastPath: DebugFlags.enableCssAncestryFastPath,
ancestorTokens: ancestorTokens,
));
}
// attribute selector
for (String attribute in element.attributes.keys) {
final list = ruleSet.attributeRules[attribute.toUpperCase()];
candidateCount += (list?.length ?? 0);
matchedRules.addAll(_collectMatchingRulesForList(
list,
element,
evaluator: evaluator,
enableAncestryFastPath: DebugFlags.enableCssAncestryFastPath,
ancestorTokens: ancestorTokens,
));
}
// tag selectors are stored uppercase; normalize element tag for lookup.
final String tagLookup = element.tagName.toUpperCase();
final listTag = ruleSet.tagRules[tagLookup];
candidateCount += (listTag?.length ?? 0);
matchedRules.addAll(_collectMatchingRulesForList(
listTag,
element,
evaluator: evaluator,
enableAncestryFastPath: DebugFlags.enableCssAncestryFastPath,
ancestorTokens: ancestorTokens,
));
// universal
candidateCount += ruleSet.universalRules.length;
matchedRules.addAll(_collectMatchingRulesForList(
ruleSet.universalRules,
element,
evaluator: evaluator,
enableAncestryFastPath: DebugFlags.enableCssAncestryFastPath,
ancestorTokens: ancestorTokens,
));
return matchedRules;
}