Skip to content

Commit 8eb2d82

Browse files
committed
Skip invalid selectors during reconciliation
1 parent c313e1a commit 8eb2d82

5 files changed

Lines changed: 52 additions & 8 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ A small (9 kB compressed) polyfill for CSS Container Queries using [`ResizeObser
88

99
## Browser Support
1010

11-
* Firefox 69+
12-
* Chrome 79+
13-
* Edge 79+
14-
* Safari 13.4+
15-
11+
- Firefox 69+
12+
- Chrome 79+
13+
- Edge 79+
14+
- Safari 13.4+
1615

1716
## Getting Started
1817

@@ -133,4 +132,4 @@ When using the polyfill, you may observe reports of errors like `ResizeObserver
133132

134133
## License
135134

136-
[Apache 2.0](LICENSE)
135+
[Apache 2.0](LICENSE)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "container-query-polyfill",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "",
55
"repository": "github:GoogleChromeLabs/container-query-polyfill",
66
"type": "module",

src/transform.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ const NO_WHERE_SELECTOR_TOKENS = parseComponentValue(
9393
Array.from(tokenize(NO_WHERE_SELECTOR))
9494
);
9595

96+
const DUMMY_ELEMENT = document.createElement('div');
97+
9698
// https://www.w3.org/TR/selectors-4/#single-colon-pseudos
9799
const SINGLE_COLON_PSEUDO_ELEMENTS = new Set([
98100
'before',
@@ -600,7 +602,14 @@ function transformContainerAtRule(
600602
return rule;
601603
}
602604

603-
elementSelectors.add(elementSelector.map(serialize).join(''));
605+
const elementSelectorText = elementSelector.map(serialize).join('');
606+
try {
607+
DUMMY_ELEMENT.matches(elementSelectorText);
608+
elementSelectors.add(elementSelectorText);
609+
} catch {
610+
// If `matches` throws, we won't use the selector when testing elements.
611+
}
612+
604613
return {
605614
...rule,
606615
prelude: styleSelector,

tests/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
'simple_new_long_syntax',
3434
'empty_href',
3535
'@supports',
36+
'@layer',
37+
'unsupported-selectors',
3638
];
3739

3840
const {results, iframe} = document.all;

tests/unsupported-selectors.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<script src="./../dist/container-query-polyfill.modern.js"></script>
3+
<style>
4+
#a {
5+
container-type: inline-size;
6+
}
7+
8+
@container (width > 0) {
9+
h1:foo() {
10+
font-size: 10px;
11+
}
12+
13+
h1 {
14+
font-size: 20px;
15+
}
16+
}
17+
}
18+
</style>
19+
<div id="a">
20+
<h1>Ohai</h1>
21+
</div>
22+
<script type="module">
23+
import {doubleRaf, testSuite, assert} from './test-utils.js';
24+
25+
testSuite('Manual transpiling', async () => {
26+
await doubleRaf();
27+
28+
const computed = getComputedStyle(document.querySelector('h1'));
29+
assert(
30+
computed.fontSize === '20px',
31+
`Expected fontSize to be 20px, got ${computed.fontSize}`
32+
);
33+
});
34+
</script>

0 commit comments

Comments
 (0)