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)
npm install polyfill-pseudoclass-has --save
import * as polyfill from "polyfill-pseudoclass-has";
const polyfill = require("polyfill-pseudoclass-has");
UMD module can be used as a CommonJS module or by including it in the code using <script src="..." />
:
<script src="/dist/polyfill.umd.js" />
<script>
const polyfill = window['polyfill-pseudoclass-has']; // can also be used via globalThis['polyfill-pseudoclass-has']
...
</script>
-
SelectorHandler
-
constructor(globalSelector: string)
// Example const selectorHandler = new SelectorHandler("body section:has(.foo):has(.bar) div:has(img)");
-
query(scopeNode: Element | Document | DocumentFragment): Element | null
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().
// Example const selectorHandler = new SelectorHandler("div:has(img)"); selectorHandler.query(document);
-
queryAll(scopeNode: Element | Document | DocumentFragment): NodeListOf<Element>
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().
// 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().
// 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().
// 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.
// 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.
// 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).
// Example import { addTo } from "polyfill-pseudoclass-has"; addTo(Element, Document, DocumentFragment); document.querySelectorAll('div:has(img)'); // now it's polyfilled
// 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).
// Example import { removeFrom } from "polyfill-pseudoclass-has"; removeFrom(Element.prototype, Document.prototype, DocumentFragment.prototype);