Skip to content

has pseudo : fix cleanup of rules in browsers with native support (backport) #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/css-has-pseudo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to CSS Has Pseudo

### 4.0.2 (December 12, 2022)

- Fix: correctly cleanup style rules when a browser has native support.

### 4.0.1 (August 23, 2022)

- Fix: assign global browser polyfill to `window`, `self` or a blank object.
Expand Down
2 changes: 1 addition & 1 deletion plugins/css-has-pseudo/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "css-has-pseudo",
"description": "Style elements relative to other elements in CSS",
"version": "4.0.1",
"version": "4.0.2",
"contributors": [
{
"name": "Antonio Laguna",
Expand Down
19 changes: 5 additions & 14 deletions plugins/css-has-pseudo/src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@ import '@mrhenry/core-web/modules/~element-qsa-has.js';
import extractEncodedSelectors from './encode/extract.mjs';
import encodeCSS from './encode/encode.mjs';

function hasNativeSupport(document) {
function hasNativeSupport() {
try {
// Chrome does not support forgiving selector lists in :has()
document.querySelector(':has(*, :does-not-exist, > *)');
document.querySelector(':has(:has(any))');

// Safari incorrectly returns the html element with this query
if (document.querySelector(':has(:scope *)')) {
return false;
}

if (!('CSS' in self) || !('supports' in self.CSS) || !self.CSS.supports(':has(any)')) {
if (!('CSS' in self) || !('supports' in self.CSS) || !self.CSS.supports('selector(:has(div))')) {
return false;
}

Expand All @@ -40,7 +31,7 @@ export default function cssHasPseudo(document, options) {
forcePolyfill: (!!options.forcePolyfill) || false,
};

options.mustPolyfill = options.forcePolyfill || !hasNativeSupport(document);
options.mustPolyfill = options.forcePolyfill || !hasNativeSupport();

if (!Array.isArray(options.observedAttributes)) {
options.observedAttributes = [];
Expand Down Expand Up @@ -238,7 +229,7 @@ export default function cssHasPseudo(document, options) {
function walkStyleSheet(styleSheet) {
try {
// walk a css rule to collect observed css rules
[].forEach.call(styleSheet.cssRules || [], (rule) => {
[].forEach.call(styleSheet.cssRules || [], (rule, index) => {
if (rule.selectorText) {
rule.selectorText = rule.selectorText.replace(/\.js-has-pseudo\s/g, '');

Expand All @@ -250,7 +241,7 @@ export default function cssHasPseudo(document, options) {
}

if (!options.mustPolyfill) {
rule.deleteRule();
styleSheet.deleteRule(index);
return;
}

Expand Down