Skip to content

Commit

Permalink
fix(compiler): apply style on :host attribudes in prod builds.
Browse files Browse the repository at this point in the history
In prod builds, selectors are optimized and spaces a removed. angular#48558 introduced a regression on selectors without spaces. This commit fixes tihs.

Fixes angular#49100
  • Loading branch information
JeanMeche committed Feb 16, 2023
1 parent be21eaa commit 108d011
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/compiler/src/shadow_css.ts
Expand Up @@ -696,7 +696,7 @@ export class ShadowCss {
// (ie: ".\fc ber" for ".über") is not a separator between 2 selectors
// also keep in mind that backslashes are replaced by a placeholder by SafeSelector
// These escaped selectors happen for example when esbuild runs with optimization.minify.
if (part.match(_placeholderRe) && selector[res.index + 1]?.match(/[a-fA-F\d]/)) {
if (part.match(/__esc-ph-(\d+)__/g) && selector[res.index + 1]?.match(/[a-fA-F\d]/)) {
continue;
}

Expand Down Expand Up @@ -735,7 +735,13 @@ class SafeSelector {
// pseudo-class, but writing `.foo\:blue` will match, because the colon was escaped.
// Replace all escape sequences (`\` followed by a character) with a placeholder so
// that our handling of pseudo-selectors doesn't mess with them.
selector = this._escapeRegexMatches(selector, /(\\.)/g);
// Escaped characters have a specific placeholder so they can be detected separately.
selector = selector.replace(/(\\.)/g, (_, keep) => {
const replaceBy = `__esc-ph-${this.index}__`;
this.placeholders.push(keep);
this.index++;
return replaceBy;
});

// Replaces the expression in `:nth-child(2n + 1)` with a placeholder.
// WS and "+" would otherwise be interpreted as selector separators.
Expand All @@ -748,7 +754,7 @@ class SafeSelector {
}

restore(content: string): string {
return content.replace(_placeholderRe, (_ph, index) => this.placeholders[+index]);
return content.replace(/__(?:ph|esc-ph)-(\d+)__/g, (_ph, index) => this.placeholders[+index]);
}

content(): string {
Expand Down Expand Up @@ -804,8 +810,6 @@ const _colonHostContextRe = /:host-context/gim;

const _commentRe = /\/\*[\s\S]*?\*\//g;

const _placeholderRe = /__ph-(\d+)__/g;

function stripComments(input: string): string {
return input.replace(_commentRe, '');
}
Expand Down
Expand Up @@ -27,6 +27,11 @@ describe('ShadowCss, :host and :host-context', () => {
expect(shim(':host([a=b]) {}', 'contenta', 'a-host')).toEqualCss('[a=b][a-host] {}');
});

it('should handle attribute and next operator without spaces', () => {
expect(shim(':host[foo]>div {}', 'contenta', 'a-host'))
.toEqualCss(('[foo][a-host] > div[contenta] {}'));
});

it('should handle multiple tag selectors', () => {
expect(shim(':host(ul,li) {}', 'contenta', 'a-host')).toEqualCss('ul[a-host], li[a-host] {}');
expect(shim(':host(ul,li) > .z {}', 'contenta', 'a-host'))
Expand Down

0 comments on commit 108d011

Please sign in to comment.