Package Exports
- polyfill-pseudoclass-has
- polyfill-pseudoclass-has/dist/esm/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (polyfill-pseudoclass-has) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
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/esm/";
UMD (for environments that do not support ES6 Modules)
Use CommonJS require()
:
const polyfill = require("polyfill-pseudoclass-has/umd/polyfill.umd.js");
or by including it in the code using <script src="..." />
:
<script src="/umd/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
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);