This polyfill allows you to query elements using the CSS :has() pseudo-class which has not been implemented in any browsers yet.
yarn add polyfill-css-hasThen, in your JavaScript:
import querySelectorAllWithHas from 'polyfill-css-has';
// Get all paragraphs in the container which have links
var items = querySelectorAllWithHas('.container > p:has(> a)');
// Optionally, pass an element to query against:
var container = document.querySelector('.container');
var items = querySelectorAllWithHas('p:has(> a)', container);The Polyfill works by splitting up your selector into two chunks:
- The scope which is to be queried for elements with
:has()requirements - The specific selector inside the
:has()class
Each of the scope-level elements are then filtered by the :has() selector and returned in an array.
- Does not support additional chained pseudo-classes like
:nth-child()or:empty() - Does not support more than one
:has()element in the selector
To develop locally, ensure you have Node.js > v8.6.0 installed, and run:
yarnTo build a development version of the polyfill, run:
yarn devTo build a production version of the polyfill, run:
yarn buildTo run tests with Jest:
yarn test
# Watch for changes with Git:
yarn test --watch- Compile Typescript/Webpack better for library consumption. http://marcobotto.com/compiling-and-bundling-typescript-libraries-with-webpack/