A polyfill that adds support for the CSS pseudo-class :has() to the DOM Selectors API (by extending .querySelector(), .querySelectorAll(), .matches(), .closest() or called directly)
npm install polyfill-pseudoclass-has:has() in DOM Selectors API 
A polyfill that adds support for the CSS pseudo-class :has() to the DOM Selectors API (by extending .querySelector(), .querySelectorAll(), .matches(), .closest() or called directly)
Polyfill does not use global environment variables in the code and therefore it can work both in the browser environment and in the server-side environment (for example with JSDOM)
``bash`
npm install polyfill-pseudoclass-has --save
js
import * as polyfill from "polyfill-pseudoclass-has";
`$3
`js
const polyfill = require("polyfill-pseudoclass-has");
`$3
UMD module can be used as a CommonJS module or by including it in the code using
:
`js
`API
-
SelectorHandler
- constructor(globalSelector: string)
`js
// Example
const selectorHandler = new SelectorHandler("body section:has(.foo):has(.bar) div:has(img)");
` -
query(scopeNode: Element | Document | DocumentFragment): Element | null Search for a single element in the scopeNode (which can be an instance of an Element or a Document or a DocumentFragment). This is similar to Node.querySelector().
`js
// Example
const selectorHandler = new SelectorHandler("div:has(img)");
selectorHandler.query(document);
` -
queryAll(scopeNode: Element | Document | DocumentFragment): NodeListOf Searches for elements in the scopeNode (which can be an instance of an Element or a Document or a DocumentFragment). This is similar to Node.querySelectorAll().
`js
// Example
const selectorHandler = new SelectorHandler("div:has(img)");
selectorHandler.queryAll(document);
`
- matches(element: Element): boolean Checks if an element (is an instance of Element) matches a selector. This is similar to Element.matches().
`js
// Example
const selectorHandler = new SelectorHandler("div:has(img)");
selectorHandler.matches(divElement); // divElement - is an instance of Element
` -
closest(element: Element): Element | null Searches for the closest parent (and self) that matches the specified selector. This is similar to Element.closest().
`js
// Example
const selectorHandler = new SelectorHandler("div:has(img)");
selectorHandler.closest(divElement); // divElement - is an instance of Element
` -
`addToBrowser()` Installing polyfill in the browser global environment. Note: Works only in a browser environment.
`js
// Example
import { addToBrowser } from "polyfill-pseudoclass-has";
addToBrowser();
document.querySelectorAll('div:has(img)'); // now it's polyfilled
` -
`removeFromBrowser()` Uninstalling polyfill from the browser global environment. Note: Works only in a browser environment.
`js
// Example
import { addToBrowser } from "polyfill-pseudoclass-has";
removeFromBrowser();
` -
`addTo(Element, Document, DocumentFragment)` Installing of polyfill in global Element & Document & DocumentFragment nodes (for integration into a third-party environment, for example JSDOM).
`js
// Example
import { addTo } from "polyfill-pseudoclass-has";
addTo(Element, Document, DocumentFragment);
document.querySelectorAll('div:has(img)'); // now it's polyfilled
`
`js
// Example with JSDOM:
import { addTo } from "polyfill-pseudoclass-has";
import { JSDOM } from "jsdom"; const { window: { Element, Document, DocumentFragment } } = new JSDOM();
addTo(Element, Document, DocumentFragment);
` -
`removeFrom(Element, Document, DocumentFragment)` Unstalling of polyfill from global Element & Document & DocumentFragment nodes (for integration into a third-party environment, for example JSDOM).
`js
// Example
import { removeFrom } from "polyfill-pseudoclass-has";
removeFrom(Element.prototype, Document.prototype, DocumentFragment.prototype);
``