Visitar URL original
fix(compiler): avoid leaking ::ng-deep detection state between rules · angular/angular@fa63bfa · GitHub
Skip to content

Commit fa63bfa

Browse files
irontaekNothingEverHappens
authored andcommitted
fix(compiler): avoid leaking ::ng-deep detection state between rules
The check that skips encapsulating nested selectors under `::ng-deep` used `.test()` on a global regex, so `lastIndex` carried over to the next rule (or the next stylesheet) when nothing else reset it. A following `::ng-deep` rule could then be missed and its nested selectors scoped. (cherry picked from commit ff0dbf1)
1 parent db91010 commit fa63bfa

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎packages/compiler/src/shadow_css.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ export class ShadowCss {
598598
if (rule.selector[0] !== '@') {
599599
if (rule.isBlock) {
600600
const selectorParts = selector.split(_selectorSplitRe);
601-
const containsDeep = selectorParts.some((part) => _shadowDeepSelectors.test(part));
601+
const containsDeep = selectorParts.some((part) => _shadowDeepSelectorRe.test(part));
602602

603603
selector = this._scopeSelector({
604604
selector,
@@ -1013,6 +1013,8 @@ const _polyfillHostNoCombinatorRe = /-shadowcsshost-no-combinator([^\s,]*)/;
10131013
// Support for `>>>`, `deep`, `::ng-deep` is then also deprecated and will be removed in the future.
10141014
// see https://github.com/angular/angular/pull/17677
10151015
const _shadowDeepSelectors = /(?:>>>)|(?:\/deep\/)|(?:::ng-deep)/g;
1016+
// Non-global copy for `.test()`, which would otherwise carry `lastIndex` over to the next rule.
1017+
const _shadowDeepSelectorRe = new RegExp(_shadowDeepSelectors.source);
10161018

10171019
// Splits the selector into independent parts by `,` (comma) unless comma is within parenthesis,
10181020
// for example `:is(.one, two)`. Negative lookup after comma allows not splitting inside nested

‎packages/compiler/test/shadow_css/nesting_spec.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,11 @@ describe('ShadowCss nesting', () => {
465465
'.foo[contenta] .bar { .baz { color: red; } }',
466466
);
467467
});
468+
469+
it('should not let a previous ::ng-deep rule affect the next one', () => {
470+
// An empty rule body leaves nothing to reset the global regex's `lastIndex`.
471+
expect(
472+
shim('.foo ::ng-deep .bar {}\n::ng-deep .baz { .qux { color: red; } }', 'contenta'),
473+
).toEqualCss('.foo[contenta] .bar {} .baz { .qux { color: red; } }');
474+
});
468475
});

0 commit comments

Comments
 (0)