Skip to content

Commit

Permalink
refactor(core): restructure logic in isCssClassMatching function
Browse files Browse the repository at this point in the history
The logic in `isCssClassMatching` is only interested in two areas in the attributes:
implicit attributes and the `AttributeMarker.Classes` area, with the first area only
of interest for projection matching, not directive matching. This commit splits these
two searches to make this more apparent.
  • Loading branch information
JoostK committed Mar 12, 2024
1 parent ee77837 commit 9bf3c1a
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions packages/core/src/render3/node_selector_matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,30 @@ function isCssClassMatching(
assertEqual(
cssClassToMatch, cssClassToMatch.toLowerCase(), 'Class name expected to be lowercase.');
let i = 0;
// Indicates whether we are processing value from the implicit
// attribute section (i.e. before the first marker in the array).
let isImplicitAttrsSection = true;
while (i < attrs.length) {
let item = attrs[i++];
if (typeof item === 'string' && isImplicitAttrsSection) {
const value = attrs[i++] as string;
if (isProjectionMode && item === 'class') {
// We found a `class` attribute in the implicit attribute section,
// check if it matches the value of the `cssClassToMatch` argument.
if (classIndexOf(value.toLowerCase(), cssClassToMatch, 0) !== -1) {
return true;
}
}
} else if (item === AttributeMarker.Classes) {
if (!isProjectionMode && isInlineTemplate(tNode)) {
// Matching directives (i.e. when not matching for projection mode) should not consider the
// class bindings that are present on inline templates, as those class bindings only target
// the root node of the template, not the template itself.
return false;
if (isProjectionMode) {
for (; i < attrs.length && typeof attrs[i] === 'string'; i += 2) {
// Search for an implicit `class` attribute and check if its value matches `cssClassToMatch`.
if (attrs[i] === 'class' &&
classIndexOf((attrs[i + 1] as string).toLowerCase(), cssClassToMatch, 0) !== -1) {
return true;
}
// We found the classes section. Start searching for the class.
while (i < attrs.length && typeof (item = attrs[i++]) == 'string') {
// while we have strings
if (item.toLowerCase() === cssClassToMatch) return true;
}
} else if (isInlineTemplate(tNode)) {
// Matching directives (i.e. when not matching for projection mode) should not consider the
// class bindings that are present on inline templates, as those class bindings only target
// the root node of the template, not the template itself.
return false;
}

// Resume the search for classes after the `Classes` marker.
i = attrs.indexOf(AttributeMarker.Classes, i);
if (i > -1) {
// We found the classes section. Start searching for the class.
let item: TAttributes[number];
while (++i < attrs.length && typeof (item = attrs[i]) === 'string') {
if (item.toLowerCase() === cssClassToMatch) {
return true;
}
return false;
} else if (typeof item === 'number') {
// We've come across a first marker, which indicates
// that the implicit attribute section is over.
isImplicitAttrsSection = false;
}
}
return false;
Expand Down

0 comments on commit 9bf3c1a

Please sign in to comment.