Skip to content

Commit 7d42948

Browse files
authored
css-has-pseudo: fix order of removal of CSSOM style rules (#1675)
1 parent 02aed87 commit 7d42948

File tree

8 files changed

+19
-13
lines changed

8 files changed

+19
-13
lines changed

plugins/css-has-pseudo/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to CSS Has Pseudo
22

3+
### Unreleased (patch)
4+
5+
- Fix order of removal of CSSOM rules when a browser supports `:has()` natively
6+
37
### 7.0.2
48

59
_December 13, 2024_

plugins/css-has-pseudo/dist/browser-global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/css-has-pseudo/dist/browser-global.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/css-has-pseudo/dist/browser.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/css-has-pseudo/dist/browser.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/css-has-pseudo/dist/browser.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugins/css-has-pseudo/dist/browser.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/css-has-pseudo/src/browser.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,26 @@ export default function cssHasPseudo(document, options) {
241241
function walkStyleSheet(styleSheet) {
242242
try {
243243
// walk a css rule to collect observed css rules
244-
[].forEach.call(styleSheet.cssRules || [], function(rule, index) {
244+
for (let i = (styleSheet.cssRules.length - 1); i >= 0; i--) {
245+
let rule = styleSheet.cssRules[i];
246+
245247
if (rule.selectorText) {
246248
rule.selectorText = rule.selectorText.replace(/\.js-has-pseudo\s/g, '');
247249

248250
try {
249251
// decode the selector text in all browsers to:
250252
const hasSelectors = extractEncodedSelectors(rule.selectorText.toString());
251253
if (hasSelectors.length === 0) {
252-
return;
254+
continue;
253255
}
254256

255257
if (!options.mustPolyfill) {
256-
styleSheet.deleteRule(index);
257-
return;
258+
styleSheet.deleteRule(i);
259+
continue;
258260
}
259261

260-
for (let i = 0; i < hasSelectors.length; i++) {
261-
const hasSelector = hasSelectors[i];
262+
for (let j = 0; j < hasSelectors.length; j++) {
263+
const hasSelector = hasSelectors[j];
262264
if (hasSelector) {
263265
observedItems.push({
264266
rule: rule,
@@ -277,7 +279,7 @@ export default function cssHasPseudo(document, options) {
277279
} else {
278280
walkStyleSheet(rule);
279281
}
280-
});
282+
}
281283
} catch (e) {
282284
if (options.debug) {
283285
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)