polyfill-pseudoclass-has
v1.0.1
Published
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)
Downloads
4,899
Maintainers
Readme
Polyfill for CSS pseudo-class :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)
Installation
npm install polyfill-pseudoclass-has --save
Usage
ES6 Modules
import * as polyfill from "polyfill-pseudoclass-has";
CommonJS
const polyfill = require("polyfill-pseudoclass-has");
UMD (for environments that do not support ES6 Modules)
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>
API
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
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.query(document);
queryAll(scopeNode: Element | Document | DocumentFragment): NodeListOf<Element>
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.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);