Skip to content

Commit 7dc1017

Browse files
committed
fix(compiler): simplify handling of colon host with a selector list
Update `_convertColonHost` to extract and use only the first argument from a `:host(...)` selector list, ignoring subsequent arguments instead of splitting and duplicating the selector list. Also remove the obsolete test cases from `host_and_host_context_spec.ts`. (cherry picked from commit 1d3bf59)
1 parent 194f723 commit 7dc1017

2 files changed

Lines changed: 15 additions & 26 deletions

File tree

packages/compiler/src/shadow_css.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,20 +434,20 @@ export class ShadowCss {
434434
private _convertColonHost(cssText: string): string {
435435
return cssText.replace(_cssColonHostRe, (_, hostSelectors: string, otherSelectors: string) => {
436436
if (hostSelectors) {
437-
const convertedSelectors: string[] = [];
438-
for (const hostSelector of this._splitOnTopLevelCommas(hostSelectors, true)) {
439-
const trimmedHostSelector = hostSelector.trim();
440-
if (!trimmedHostSelector) break;
441-
const convertedSelector =
437+
const parts = [...this._splitOnTopLevelCommas(hostSelectors, true)];
438+
if (parts.length > 1) {
439+
return ':host(' + hostSelectors + ')' + otherSelectors;
440+
}
441+
const trimmedHostSelector = parts[0].trim();
442+
if (trimmedHostSelector) {
443+
return (
442444
_polyfillHostNoCombinator +
443445
trimmedHostSelector.replace(_polyfillHost, '') +
444-
otherSelectors;
445-
convertedSelectors.push(convertedSelector);
446+
otherSelectors
447+
);
446448
}
447-
return convertedSelectors.join(',');
448-
} else {
449-
return _polyfillHostNoCombinator + otherSelectors;
450449
}
450+
return _polyfillHostNoCombinator + otherSelectors;
451451
});
452452
}
453453

packages/compiler/test/shadow_css/host_and_host_context_spec.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,16 @@ describe('ShadowCss, :host and :host-context', () => {
4141
expect(shim(':host.pr\\fc fung {}', 'contenta', 'a-host')).toEqual('.pr\\fc fung[a-host] {}');
4242
});
4343

44-
it('should handle multiple tag selectors', () => {
45-
expect(shim(':host(ul,li) {}', 'contenta', 'a-host')).toEqualCss('ul[a-host], li[a-host] {}');
46-
expect(shim(':host(ul,li) > .z {}', 'contenta', 'a-host')).toEqualCss(
47-
'ul[a-host] > .z[contenta], li[a-host] > .z[contenta] {}',
48-
);
49-
});
50-
5144
it('should handle compound class selectors', () => {
5245
expect(shim(':host(.a.b) {}', 'contenta', 'a-host')).toEqualCss('.a.b[a-host] {}');
5346
});
5447

55-
it('should handle multiple class selectors', () => {
56-
expect(shim(':host(.x,.y) {}', 'contenta', 'a-host')).toEqualCss('.x[a-host], .y[a-host] {}');
57-
expect(shim(':host(.x,.y) > .z {}', 'contenta', 'a-host')).toEqualCss(
58-
'.x[a-host] > .z[contenta], .y[a-host] > .z[contenta] {}',
48+
it('should ignore :host with a selector list containing top-level commas', () => {
49+
expect(shim(':host(.a, .b) {}', 'contenta', 'a-host')).toEqualCss(
50+
'[contenta]:host(.a, .b) {}',
5951
);
60-
});
61-
62-
it('should handle multiple attribute selectors', () => {
63-
expect(shim(':host([a="b"],[c=d]) {}', 'contenta', 'a-host')).toEqualCss(
64-
'[a="b"][a-host], [c=d][a-host] {}',
52+
expect(shim('.outer :host(.a, .b) .inner {}', 'contenta', 'a-host')).toEqualCss(
53+
'.outer[contenta] [contenta]:host(.a, .b) .inner[contenta] {}',
6554
);
6655
});
6756

0 commit comments

Comments
 (0)