Skip to content

Commit

Permalink
feat(compiler): scope selectors in @starting-style (#53943)
Browse files Browse the repository at this point in the history
make sure selectors inside @starting-style queries are correctly scoped

PR Close #53943
  • Loading branch information
puckowski authored and dylhunn committed Jan 17, 2024
1 parent 0accc64 commit 66e940a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/compiler/src/shadow_css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const animationKeywords = new Set([
'end', 'jump-both', 'jump-end', 'jump-none', 'jump-start', 'start'
]);

/**
* The following array contains all of the CSS at-rule identifiers which are scoped.
*/
const scopedAtRuleIdentifiers =
['@media', '@supports', '@document', '@layer', '@container', '@scope', '@starting-style'];

/**
* The following class has its origin from a port of shadowCSS from webcomponents.js to TypeScript.
* It has since diverge in many ways to tailor Angular's needs.
Expand Down Expand Up @@ -557,10 +563,7 @@ export class ShadowCss {
let content = rule.content;
if (rule.selector[0] !== '@') {
selector = this._scopeSelector(rule.selector, scopeSelector, hostSelector);
} else if (
rule.selector.startsWith('@media') || rule.selector.startsWith('@supports') ||
rule.selector.startsWith('@document') || rule.selector.startsWith('@layer') ||
rule.selector.startsWith('@container') || rule.selector.startsWith('@scope')) {
} else if (scopedAtRuleIdentifiers.some(atRule => rule.selector.startsWith(atRule))) {
content = this._scopeSelectors(rule.content, scopeSelector, hostSelector);
} else if (rule.selector.startsWith('@font-face') || rule.selector.startsWith('@page')) {
content = this._stripScopingSelectors(rule.content);
Expand Down
16 changes: 16 additions & 0 deletions packages/compiler/test/shadow_css/at_rules_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,20 @@ describe('ShadowCss, at-rules', () => {
expect(shim(css, 'contenta')).toEqualCss(expected);
});
});

describe('@starting-style', () => {
it('should scope normal selectors inside a starting-style rule', () => {
const css = `
@starting-style {
img { border-radius: 50%; }
.content { padding: 1em; }
}`;
const result = shim(css, 'host-a');
expect(result).toEqualCss(`
@starting-style {
img[host-a] { border-radius: 50%; }
.content[host-a] { padding: 1em; }
}`);
});
});
});

0 comments on commit 66e940a

Please sign in to comment.