npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

5,081

Readme

Polyfill for CSS pseudo-class :has() in DOM Selectors API

Code Style: Google

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);