The specificityMatchingName option allows you to change the selector that is used to adjust specificity.
The default value is does-not-exist.
If this is an actual class, id or tag name in your code, you will need to set a different option here.
Determining which elements match a :has selector is relatively slow through a polyfill compared to the native feature.
A very large DOM or many and complex :has selectors can cause performance issues.
JavaScript frameworks that rewrite the DOM will be particularly affected by this.
Any contributions to speedup matching are welcome.
Please open an issue to discuss proposed changes if you are interested in contributing.
Specificity
:has transforms will result in at least one attribute selector with specificity 0, 1, 0.
If your selector only has tags we won't be able to match the original specificity.
As selectors are encoded, this plugin (or postcss-preset-env) must be run after any other plugin that transforms selectors.
If other plugins are used, you need to place these in your config before postcss-preset-env or css-has-pseudo.
Please let us know if you have issues with plugins that transform selectors.
Then we can investigate and maybe fix these.
Browser
// initialize cssHasPseudoimport cssHasPseudo from 'css-has-pseudo/browser';cssHasPseudo(document);
or
<!-- When using a CDN url you will have to manually update the version number --><script src="https://unpkg.com/css-has-pseudo@8.0.2/dist/browser-global.js"></script><script>cssHasPseudo(document)</script>
[!TIP]
Please use a versioned url, like this : https://unpkg.com/css-has-pseudo@8.0.2/dist/browser-global.js
Without the version, you might unexpectedly get a new major version of the library with breaking changes.
The hover option determines if :hover pseudo-class should be tracked.
This is disabled by default because it is an expensive operation.
cssHasPseudo(document, { hover: true });
observedAttributes
The observedAttributes option determines which html attributes are observed.
If you do any client side modification of non-standard attributes and use these in combination with :has() you should add these here.
The forcePolyfill option determines if the polyfill is used even when the browser has native support.
This is needed when you set preserve: false in the PostCSS plugin config.
cssHasPseudo(document, { forcePolyfill: true });
debug
The debug option determines if errors are emitted to the console in browser.
By default the polyfill will not emit errors or warnings.
querySelectorAll with support for post CSS 2.1 selectors and :scope selectors.
ECMA Script:
Array.prototype.filter
Array.prototype.forEach
Array.prototype.indexOf
Array.prototype.join
Array.prototype.map
Array.prototype.splice
RegExp.prototype.exec
String.prototype.match
String.prototype.replace
String.prototype.split
CORS
[!IMPORTANT]
Applies to you if you load CSS from a different domain than the page.
In this case the CSS is treated as untrusted and will not be made available to the JavaScript polyfill.
The polyfill will not work without applying the correct configuration for CORS.
DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules
Safari :
SecurityError: Not allowed to access cross-origin stylesheet
Firefox :
DOMException: CSSStyleSheet.cssRules getter: Not allowed to access cross-origin stylesheet
To resolve CORS errors you need to take two steps :
add an HTTP header Access-Control-Allow-Origin: <your-value> when serving your CSS file.
add crossorigin="anonymous" to the <link rel="stylesheet"> tag for your CSS file.
In a node server setting the HTTP header might look like this :
// http://localhost:8080 is the domain of your page!res.setHeader('Access-Control-Allow-Origin', 'https://example.com');
You can also configure a wildcard but please be aware that this might be a security risk.
It is better to only set the header for the domain you want to allow and only on the responses you want to allow.