2016-06-23 16:47:54 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 16:47:54 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2016-06-23 16:47:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
import {shim} from './utils';
|
2015-03-23 21:10:55 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
describe('ShadowCss', () => {
|
|
|
|
|
it('should handle empty string', () => {
|
|
|
|
|
expect(shim('', 'contenta')).toEqualCss('');
|
|
|
|
|
});
|
2016-08-12 08:39:43 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should add an attribute to every rule', () => {
|
|
|
|
|
const css = 'one {color: red;}two {color: red;}';
|
|
|
|
|
const expected = 'one[contenta] {color:red;}two[contenta] {color:red;}';
|
|
|
|
|
expect(shim(css, 'contenta')).toEqualCss(expected);
|
|
|
|
|
});
|
2017-04-18 14:11:33 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should handle invalid css', () => {
|
|
|
|
|
const css = 'one {color: red;}garbage';
|
|
|
|
|
const expected = 'one[contenta] {color:red;}garbage';
|
|
|
|
|
expect(shim(css, 'contenta')).toEqualCss(expected);
|
|
|
|
|
});
|
fix(compiler): incorrectly encapsulating @import containing colons and semicolons (#38716)
At a high level, the current shadow DOM shim logic works by escaping the content of a CSS rule
(e.g. `div {color: red;}` becomes `div {%BLOCK%}`), using a regex to parse out things like the
selector and the rule body, and then re-adding the content after the selector has been modified.
The problem is that the regex has to be very broad in order capture all of the different use cases,
which can cause it to match strings suffixed with a semi-colon in some places where it shouldn't,
like this URL from Google Fonts `https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap`.
Most of the time this is fine, because the logic that escapes the rule content to `%BLOCK%` will
have converted it to something that won't be matched by the regex. However, it breaks down for rules
like `@import` which don't have a body, but can still have quoted content with characters that can
match the regex.
These changes resolve the issue by making a second pass over the escaped string and replacing all
of the remaining quoted content with `%QUOTED%` before parsing it with the regex. Once everything
has been processed, we make a final pass where we restore the quoted content.
In a previous iteration of this PR, I went with a shorter approach which narrowed down the
regex so that it doesn't capture rules without a body. It fixed the issue, but it also ended
up breaking some of the more contrived unit test cases. I decided not to pursue it further, because
we would've ended up with a very long and brittle regex that likely would've broken in even weirder
ways.
Fixes #38587.
PR Close #38716
2020-09-05 09:30:54 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should add an attribute to every selector', () => {
|
|
|
|
|
const css = 'one, two {color: red;}';
|
|
|
|
|
const expected = 'one[contenta], two[contenta] {color:red;}';
|
|
|
|
|
expect(shim(css, 'contenta')).toEqualCss(expected);
|
|
|
|
|
});
|
fix(compiler): incorrectly encapsulating @import containing colons and semicolons (#38716)
At a high level, the current shadow DOM shim logic works by escaping the content of a CSS rule
(e.g. `div {color: red;}` becomes `div {%BLOCK%}`), using a regex to parse out things like the
selector and the rule body, and then re-adding the content after the selector has been modified.
The problem is that the regex has to be very broad in order capture all of the different use cases,
which can cause it to match strings suffixed with a semi-colon in some places where it shouldn't,
like this URL from Google Fonts `https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap`.
Most of the time this is fine, because the logic that escapes the rule content to `%BLOCK%` will
have converted it to something that won't be matched by the regex. However, it breaks down for rules
like `@import` which don't have a body, but can still have quoted content with characters that can
match the regex.
These changes resolve the issue by making a second pass over the escaped string and replacing all
of the remaining quoted content with `%QUOTED%` before parsing it with the regex. Once everything
has been processed, we make a final pass where we restore the quoted content.
In a previous iteration of this PR, I went with a shorter approach which narrowed down the
regex so that it doesn't capture rules without a body. It fixed the issue, but it also ended
up breaking some of the more contrived unit test cases. I decided not to pursue it further, because
we would've ended up with a very long and brittle regex that likely would've broken in even weirder
ways.
Fixes #38587.
PR Close #38716
2020-09-05 09:30:54 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should support newlines in the selector and content ', () => {
|
|
|
|
|
const css = `
|
|
|
|
|
one,
|
|
|
|
|
two {
|
|
|
|
|
color: red;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
const expected = `
|
|
|
|
|
one[contenta],
|
|
|
|
|
two[contenta] {
|
|
|
|
|
color: red;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
expect(shim(css, 'contenta')).toEqualCss(expected);
|
|
|
|
|
});
|
fix(compiler): incorrectly encapsulating @import containing colons and semicolons (#38716)
At a high level, the current shadow DOM shim logic works by escaping the content of a CSS rule
(e.g. `div {color: red;}` becomes `div {%BLOCK%}`), using a regex to parse out things like the
selector and the rule body, and then re-adding the content after the selector has been modified.
The problem is that the regex has to be very broad in order capture all of the different use cases,
which can cause it to match strings suffixed with a semi-colon in some places where it shouldn't,
like this URL from Google Fonts `https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap`.
Most of the time this is fine, because the logic that escapes the rule content to `%BLOCK%` will
have converted it to something that won't be matched by the regex. However, it breaks down for rules
like `@import` which don't have a body, but can still have quoted content with characters that can
match the regex.
These changes resolve the issue by making a second pass over the escaped string and replacing all
of the remaining quoted content with `%QUOTED%` before parsing it with the regex. Once everything
has been processed, we make a final pass where we restore the quoted content.
In a previous iteration of this PR, I went with a shorter approach which narrowed down the
regex so that it doesn't capture rules without a body. It fixed the issue, but it also ended
up breaking some of the more contrived unit test cases. I decided not to pursue it further, because
we would've ended up with a very long and brittle regex that likely would've broken in even weirder
ways.
Fixes #38587.
PR Close #38716
2020-09-05 09:30:54 +00:00
|
|
|
|
2024-11-08 21:24:59 +00:00
|
|
|
it('should support newlines in the same selector and content ', () => {
|
|
|
|
|
const selector = `.foo:not(
|
|
|
|
|
.bar) {
|
|
|
|
|
background-color:
|
|
|
|
|
green;
|
|
|
|
|
}`;
|
|
|
|
|
expect(shim(selector, 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
'.foo[contenta]:not( .bar) { background-color:green;}',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should handle complicated selectors', () => {
|
|
|
|
|
expect(shim('one::before {}', 'contenta')).toEqualCss('one[contenta]::before {}');
|
|
|
|
|
expect(shim('one two {}', 'contenta')).toEqualCss('one[contenta] two[contenta] {}');
|
|
|
|
|
expect(shim('one > two {}', 'contenta')).toEqualCss('one[contenta] > two[contenta] {}');
|
|
|
|
|
expect(shim('one + two {}', 'contenta')).toEqualCss('one[contenta] + two[contenta] {}');
|
|
|
|
|
expect(shim('one ~ two {}', 'contenta')).toEqualCss('one[contenta] ~ two[contenta] {}');
|
|
|
|
|
expect(shim('.one.two > three {}', 'contenta')).toEqualCss(
|
|
|
|
|
'.one.two[contenta] > three[contenta] {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('one[attr="value"] {}', 'contenta')).toEqualCss('one[attr="value"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr=value] {}', 'contenta')).toEqualCss('one[attr=value][contenta] {}');
|
|
|
|
|
expect(shim('one[attr^="value"] {}', 'contenta')).toEqualCss('one[attr^="value"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr$="value"] {}', 'contenta')).toEqualCss('one[attr$="value"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr*="value"] {}', 'contenta')).toEqualCss('one[attr*="value"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr|="value"] {}', 'contenta')).toEqualCss('one[attr|="value"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr~="value"] {}', 'contenta')).toEqualCss('one[attr~="value"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr="va lue"] {}', 'contenta')).toEqualCss('one[attr="va lue"][contenta] {}');
|
|
|
|
|
expect(shim('one[attr] {}', 'contenta')).toEqualCss('one[attr][contenta] {}');
|
|
|
|
|
expect(shim('[is="one"] {}', 'contenta')).toEqualCss('[is="one"][contenta] {}');
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim('[attr] {}', 'contenta')).toEqualCss('[attr][contenta] {}');
|
2024-09-23 17:55:48 +00:00
|
|
|
});
|
|
|
|
|
|
2024-09-26 21:25:44 +00:00
|
|
|
it('should transform :host with attributes', () => {
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim(':host [attr] {}', 'contenta', 'hosta')).toEqualCss('[hosta] [attr][contenta] {}');
|
2024-09-23 17:55:48 +00:00
|
|
|
expect(shim(':host(create-first-project) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'create-first-project[hosta] {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':host[attr] {}', 'contenta', 'hosta')).toEqualCss('[attr][hosta] {}');
|
|
|
|
|
expect(shim(':host[attr]:where(:not(.cm-button)) {}', 'contenta', 'hosta')).toEqualCss(
|
2024-09-26 21:25:44 +00:00
|
|
|
'[attr][hosta]:where(:not(.cm-button)) {}',
|
2024-09-24 06:55:35 +00:00
|
|
|
);
|
2015-10-30 22:09:04 +00:00
|
|
|
});
|
2015-10-29 17:57:54 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should handle escaped sequences in selectors', () => {
|
|
|
|
|
expect(shim('one\\/two {}', 'contenta')).toEqualCss('one\\/two[contenta] {}');
|
|
|
|
|
expect(shim('one\\:two {}', 'contenta')).toEqualCss('one\\:two[contenta] {}');
|
|
|
|
|
expect(shim('one\\\\:two {}', 'contenta')).toEqualCss('one\\\\[contenta]:two {}');
|
|
|
|
|
expect(shim('.one\\:two {}', 'contenta')).toEqualCss('.one\\:two[contenta] {}');
|
2023-02-16 23:28:34 +00:00
|
|
|
expect(shim('.one\\:\\fc ber {}', 'contenta')).toEqualCss('.one\\:\\fc ber[contenta] {}');
|
2022-12-11 00:40:13 +00:00
|
|
|
expect(shim('.one\\:two .three\\:four {}', 'contenta')).toEqualCss(
|
|
|
|
|
'.one\\:two[contenta] .three\\:four[contenta] {}',
|
|
|
|
|
);
|
2024-09-26 21:25:44 +00:00
|
|
|
expect(shim('div:where(.one) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'div[contenta]:where(.one) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('div:where() {}', 'contenta', 'hosta')).toEqualCss('div[contenta]:where() {}');
|
|
|
|
|
expect(shim(':where(a):where(b) {}', 'contenta', 'hosta')).toEqualCss(
|
2024-11-07 23:47:20 +00:00
|
|
|
':where(a[contenta]):where(b[contenta]) {}',
|
2024-09-26 21:25:44 +00:00
|
|
|
);
|
|
|
|
|
expect(shim('*:where(.one) {}', 'contenta', 'hosta')).toEqualCss('*[contenta]:where(.one) {}');
|
|
|
|
|
expect(shim('*:where(.one) ::ng-deep .foo {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'*[contenta]:where(.one) .foo {}',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-12 20:05:12 +00:00
|
|
|
it('should handle pseudo functions correctly', () => {
|
|
|
|
|
// :where()
|
|
|
|
|
expect(shim(':where(.one) {}', 'contenta', 'hosta')).toEqualCss(':where(.one[contenta]) {}');
|
|
|
|
|
expect(shim(':where(div.one span.two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(div.one[contenta] span.two[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(.one) .two {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(.one[contenta]) .two[contenta] {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(:host) {}', 'contenta', 'hosta')).toEqualCss(':where([hosta]) {}');
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim(':where(:host) .one {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where([hosta]) .one[contenta] {}',
|
|
|
|
|
);
|
2024-09-12 20:05:12 +00:00
|
|
|
expect(shim(':where(.one) :where(:host) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(.one) :where([hosta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(.one :host) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(.one [hosta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('div :where(.one) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'div[contenta] :where(.one[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':host :where(.one .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[hosta] :where(.one[contenta] .two[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(.one, .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(.one[contenta], .two[contenta]) {}',
|
|
|
|
|
);
|
2024-09-12 20:41:45 +00:00
|
|
|
expect(shim(':where(.one > .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(.one[contenta] > .two[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(> .one) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where( > .one[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(:not(.one) ~ .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where([contenta]:not(.one) ~ .two[contenta]) {}',
|
|
|
|
|
);
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim(':where([foo]) {}', 'contenta', 'hosta')).toEqualCss(':where([foo][contenta]) {}');
|
2024-09-12 20:05:12 +00:00
|
|
|
|
|
|
|
|
// :is()
|
2024-09-26 21:25:44 +00:00
|
|
|
expect(shim('div:is(.foo) {}', 'contenta', 'a-host')).toEqualCss('div[contenta]:is(.foo) {}');
|
2024-09-12 20:05:12 +00:00
|
|
|
expect(shim(':is(.dark :host) {}', 'contenta', 'a-host')).toEqualCss(':is(.dark [a-host]) {}');
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim(':is(.dark) :is(:host) {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
':is(.dark) :is([a-host]) {}',
|
|
|
|
|
);
|
2024-09-12 20:05:12 +00:00
|
|
|
expect(shim(':host:is(.foo) {}', 'contenta', 'a-host')).toEqualCss('[a-host]:is(.foo) {}');
|
|
|
|
|
expect(shim(':is(.foo) {}', 'contenta', 'a-host')).toEqualCss(':is(.foo[contenta]) {}');
|
|
|
|
|
expect(shim(':is(.foo, .bar, .baz) {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
':is(.foo[contenta], .bar[contenta], .baz[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':is(.foo, .bar) :host {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
':is(.foo, .bar) [a-host] {}',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// :is() and :where()
|
|
|
|
|
expect(
|
|
|
|
|
shim(
|
|
|
|
|
':is(.foo, .bar) :is(.baz) :where(.one, .two) :host :where(.three:first-child) {}',
|
|
|
|
|
'contenta',
|
|
|
|
|
'a-host',
|
|
|
|
|
),
|
|
|
|
|
).toEqualCss(
|
|
|
|
|
':is(.foo, .bar) :is(.baz) :where(.one, .two) [a-host] :where(.three[contenta]:first-child) {}',
|
|
|
|
|
);
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim(':where(:is(a)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(:is(a[contenta])) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(:is(a, b)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(:is(a[contenta], b[contenta])) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(:host:is(.one, .two)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where([hosta]:is(.one, .two)) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(:host :is(.one, .two)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where([hosta] :is(.one[contenta], .two[contenta])) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(:is(a, b) :is(.one, .two)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':where(:is(a[contenta], b[contenta]) :is(.one[contenta], .two[contenta])) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
shim(
|
|
|
|
|
':where(:where(a:has(.foo), b) :is(.one, .two:where(.foo > .bar))) {}',
|
|
|
|
|
'contenta',
|
|
|
|
|
'hosta',
|
|
|
|
|
),
|
|
|
|
|
).toEqualCss(
|
2024-09-26 21:25:44 +00:00
|
|
|
':where(:where(a[contenta]:has(.foo), b[contenta]) :is(.one[contenta], .two[contenta]:where(.foo > .bar))) {}',
|
2024-09-23 06:20:32 +00:00
|
|
|
);
|
2024-11-07 23:47:20 +00:00
|
|
|
expect(shim(':where(.two):first-child {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:where(.two):first-child {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':first-child:where(.two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:first-child:where(.two) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(.two):nth-child(3) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:where(.two):nth-child(3) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('table :where(td, th):hover { color: lime; }', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'table[contenta] [contenta]:where(td, th):hover { color:lime;}',
|
|
|
|
|
);
|
2024-09-12 20:05:12 +00:00
|
|
|
|
2025-11-07 23:20:37 +00:00
|
|
|
// :nth
|
|
|
|
|
expect(shim(':nth-child(3n of :not(p, a), :is(.foo)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:nth-child(3n of :not(p, a), :is(.foo)) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('li:nth-last-child(-n + 3) {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
'li[contenta]:nth-last-child(-n + 3) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('dd:nth-last-of-type(3n) {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
'dd[contenta]:nth-last-of-type(3n) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('dd:nth-of-type(even) {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
'dd[contenta]:nth-of-type(even) {}',
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-12 20:05:12 +00:00
|
|
|
// complex selectors
|
|
|
|
|
expect(shim(':host:is([foo],[foo-2])>div.example-2 {}', 'contenta', 'a-host')).toEqualCss(
|
2024-09-26 21:25:44 +00:00
|
|
|
'[a-host]:is([foo],[foo-2]) > div.example-2[contenta] {}',
|
2024-09-12 20:05:12 +00:00
|
|
|
);
|
|
|
|
|
expect(shim(':host:is([foo], [foo-2]) > div.example-2 {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
'[a-host]:is([foo], [foo-2]) > div.example-2[contenta] {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':host:has([foo],[foo-2])>div.example-2 {}', 'contenta', 'a-host')).toEqualCss(
|
|
|
|
|
'[a-host]:has([foo],[foo-2]) > div.example-2[contenta] {}',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// :has()
|
|
|
|
|
expect(shim('div:has(a) {}', 'contenta', 'hosta')).toEqualCss('div[contenta]:has(a) {}');
|
|
|
|
|
expect(shim('div:has(a) :host {}', 'contenta', 'hosta')).toEqualCss('div:has(a) [hosta] {}');
|
|
|
|
|
expect(shim(':has(a) :host :has(b) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':has(a) [hosta] [contenta]:has(b) {}',
|
|
|
|
|
);
|
2024-09-12 20:41:45 +00:00
|
|
|
expect(shim('div:has(~ .one) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'div[contenta]:has(~ .one) {}',
|
|
|
|
|
);
|
2024-09-12 20:05:12 +00:00
|
|
|
// Unlike `:is()` or `:where()` the attribute selector isn't placed inside
|
|
|
|
|
// of `:has()`. That is deliberate, `[contenta]:has(a)` would select all
|
|
|
|
|
// `[contenta]` with `a` inside, while `:has(a[contenta])` would select
|
|
|
|
|
// everything that contains `a[contenta]`, targeting elements outside of
|
|
|
|
|
// encapsulated scope.
|
|
|
|
|
expect(shim(':has(a) :has(b) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:has(a) [contenta]:has(b) {}',
|
|
|
|
|
);
|
2024-11-07 20:46:51 +00:00
|
|
|
expect(shim(':has(a, b) {}', 'contenta', 'hosta')).toEqualCss('[contenta]:has(a, b) {}');
|
|
|
|
|
expect(shim(':has(a, b:where(.foo), :is(.bar)) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:has(a, b:where(.foo), :is(.bar)) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
shim(':has(a, b:where(.foo), :is(.bar):first-child):first-letter {}', 'contenta', 'hosta'),
|
|
|
|
|
).toEqualCss('[contenta]:has(a, b:where(.foo), :is(.bar):first-child):first-letter {}');
|
|
|
|
|
expect(
|
|
|
|
|
shim(':where(a, b:where(.foo), :has(.bar):first-child) {}', 'contenta', 'hosta'),
|
|
|
|
|
).toEqualCss(
|
|
|
|
|
':where(a[contenta], b[contenta]:where(.foo), [contenta]:has(.bar):first-child) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':has(.one :host, .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:has(.one [hosta], .two) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':has(.one, :host) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:has(.one, [hosta]) {}',
|
|
|
|
|
);
|
2024-09-12 20:05:12 +00:00
|
|
|
});
|
|
|
|
|
|
2024-09-23 06:20:32 +00:00
|
|
|
it('should handle :host inclusions inside pseudo-selectors selectors', () => {
|
|
|
|
|
expect(shim('.header:not(.admin) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'.header[contenta]:not(.admin) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('.header:is(:host > .toolbar, :host ~ .panel) {}', 'contenta', 'hosta')).toEqualCss(
|
2024-09-26 21:25:44 +00:00
|
|
|
'.header[contenta]:is([hosta] > .toolbar, [hosta] ~ .panel) {}',
|
2024-09-23 06:20:32 +00:00
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
shim('.header:where(:host > .toolbar, :host ~ .panel) {}', 'contenta', 'hosta'),
|
2024-09-26 21:25:44 +00:00
|
|
|
).toEqualCss('.header[contenta]:where([hosta] > .toolbar, [hosta] ~ .panel) {}');
|
2024-09-23 06:20:32 +00:00
|
|
|
expect(shim('.header:not(.admin, :host.super .header) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'.header[contenta]:not(.admin, .super[hosta] .header) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
shim('.header:not(.admin, :host.super .header, :host.mega .header) {}', 'contenta', 'hosta'),
|
|
|
|
|
).toEqualCss('.header[contenta]:not(.admin, .super[hosta] .header, .mega[hosta] .header) {}');
|
|
|
|
|
|
|
|
|
|
expect(shim('.one :where(.two, :host) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'.one :where(.two[contenta], [hosta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('.one :where(:host, .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'.one :where([hosta], .two[contenta]) {}',
|
|
|
|
|
);
|
2024-11-07 23:47:20 +00:00
|
|
|
expect(shim(':is(.foo):is(:host):is(.two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
':is(.foo):is([hosta]):is(.two[contenta]) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':where(.one, :host .two):first-letter {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:where(.one, [hosta] .two):first-letter {}',
|
|
|
|
|
);
|
|
|
|
|
expect(shim(':first-child:where(.one, :host .two) {}', 'contenta', 'hosta')).toEqualCss(
|
|
|
|
|
'[contenta]:first-child:where(.one, [hosta] .two) {}',
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
shim(':where(.one, :host .two):nth-child(3):is(.foo, a:where(.bar)) {}', 'contenta', 'hosta'),
|
|
|
|
|
).toEqualCss('[contenta]:where(.one, [hosta] .two):nth-child(3):is(.foo, a:where(.bar)) {}');
|
2024-09-23 06:20:32 +00:00
|
|
|
});
|
|
|
|
|
|
2022-12-21 09:38:51 +00:00
|
|
|
it('should handle escaped selector with space (if followed by a hex char)', () => {
|
|
|
|
|
// When esbuild runs with optimization.minify
|
|
|
|
|
// selectors are escaped: .über becomes .\fc ber.
|
|
|
|
|
// The space here isn't a separator between 2 selectors
|
|
|
|
|
expect(shim('.\\fc ber {}', 'contenta')).toEqual('.\\fc ber[contenta] {}');
|
|
|
|
|
expect(shim('.\\fc ker {}', 'contenta')).toEqual('.\\fc[contenta] ker[contenta] {}');
|
|
|
|
|
expect(shim('.pr\\fc fung {}', 'contenta')).toEqual('.pr\\fc fung[contenta] {}');
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should handle ::shadow', () => {
|
|
|
|
|
const css = shim('x::shadow > y {}', 'contenta');
|
|
|
|
|
expect(css).toEqualCss('x[contenta] > y[contenta] {}');
|
|
|
|
|
});
|
2015-10-30 22:09:04 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should leave calc() unchanged', () => {
|
|
|
|
|
const styleStr = 'div {height:calc(100% - 55px);}';
|
|
|
|
|
const css = shim(styleStr, 'contenta');
|
|
|
|
|
expect(css).toEqualCss('div[contenta] {height:calc(100% - 55px);}');
|
|
|
|
|
});
|
2015-10-30 22:09:04 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should shim rules with quoted content', () => {
|
|
|
|
|
const styleStr = 'div {background-image: url("a.jpg"); color: red;}';
|
|
|
|
|
const css = shim(styleStr, 'contenta');
|
|
|
|
|
expect(css).toEqualCss('div[contenta] {background-image:url("a.jpg"); color:red;}');
|
|
|
|
|
});
|
2021-01-26 16:16:51 +00:00
|
|
|
|
2025-11-17 06:24:24 +00:00
|
|
|
it('should handle when quoted content contains a closing parenthesis', () => {
|
|
|
|
|
// Regression test for https://github.com/angular/angular/issues/65137
|
|
|
|
|
expect(shim('p { background-image: url(")") } p { color: red }', 'contenta')).toEqualCss(
|
|
|
|
|
'p[contenta] { background-image: url(")") } p[contenta] { color: red }',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should shim rules with an escaped quote inside quoted content', () => {
|
|
|
|
|
const styleStr = 'div::after { content: "\\"" }';
|
|
|
|
|
const css = shim(styleStr, 'contenta');
|
|
|
|
|
expect(css).toEqualCss('div[contenta]::after { content:"\\""}');
|
|
|
|
|
});
|
2021-01-26 16:16:51 +00:00
|
|
|
|
2022-12-11 00:40:13 +00:00
|
|
|
it('should shim rules with curly braces inside quoted content', () => {
|
|
|
|
|
const styleStr = 'div::after { content: "{}" }';
|
|
|
|
|
const css = shim(styleStr, 'contenta');
|
|
|
|
|
expect(css).toEqualCss('div[contenta]::after { content:"{}"}');
|
2021-01-26 16:16:51 +00:00
|
|
|
});
|
2023-05-25 12:11:29 +00:00
|
|
|
|
2024-04-24 14:15:47 +00:00
|
|
|
it('should keep retain multiline selectors', () => {
|
|
|
|
|
// This is needed as shifting in line number will cause sourcemaps to break.
|
|
|
|
|
const styleStr = '.foo,\n.bar { color: red;}';
|
|
|
|
|
const css = shim(styleStr, 'contenta');
|
|
|
|
|
expect(css).toEqual('.foo[contenta], \n.bar[contenta] { color: red;}');
|
|
|
|
|
});
|
|
|
|
|
|
2023-05-25 12:11:29 +00:00
|
|
|
describe('comments', () => {
|
|
|
|
|
// Comments should be kept in the same position as otherwise inline sourcemaps break due to
|
|
|
|
|
// shift in lines.
|
2026-03-18 19:22:15 +00:00
|
|
|
it('should remove inline comments without adding extra lines', () => {
|
|
|
|
|
expect(shim('/* b {c} */ b {c}', 'contenta')).toBe(' b[contenta] {c}');
|
2023-05-25 12:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-18 19:22:15 +00:00
|
|
|
it('should preserve internal newlines from multiline comments', () => {
|
|
|
|
|
expect(shim('/* b {c}\n */ b {c}', 'contenta')).toBe('\n b[contenta] {c}');
|
2023-05-25 12:11:29 +00:00
|
|
|
});
|
|
|
|
|
|
2026-03-18 19:22:15 +00:00
|
|
|
it('should remove multiple inline comments without adding extra lines', () => {
|
2023-05-25 12:11:29 +00:00
|
|
|
expect(shim('/* b {c} */ b {c} /* a {c} */ a {c}', 'contenta')).toBe(
|
2026-03-18 19:22:15 +00:00
|
|
|
' b[contenta] {c} a[contenta] {c}',
|
2023-05-25 12:11:29 +00:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should keep sourceMappingURL comments', () => {
|
|
|
|
|
expect(shim('b {c} /*# sourceMappingURL=data:x */', 'contenta')).toBe(
|
|
|
|
|
'b[contenta] {c} /*# sourceMappingURL=data:x */',
|
|
|
|
|
);
|
|
|
|
|
expect(shim('b {c}/* #sourceMappingURL=data:x */', 'contenta')).toBe(
|
|
|
|
|
'b[contenta] {c}/* #sourceMappingURL=data:x */',
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should handle adjacent comments', () => {
|
2026-03-18 19:22:15 +00:00
|
|
|
expect(shim('/* comment 1 */ /* comment 2 */ b {c}', 'contenta')).toBe(' b[contenta] {c}');
|
2023-05-25 12:11:29 +00:00
|
|
|
});
|
|
|
|
|
});
|
2022-12-11 00:40:13 +00:00
|
|
|
});
|