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

posthtml-match-helper

v2.0.3

Published

A helper to expand CSS selectors into PostHTML matcher objects

Downloads

220,385

Readme

Expand CSS selectors into PostHTML matcher objects

Version Build License Downloads

Introduction

This PostHTML plugin can turn simple CSS selectors into matcher objects.

Supported features:

  • Tags: "div" returns {tag: "div"}.
  • Ids: "#bar" returns {attrs: {id: "bar"}}.
  • Classes: .foo returns {attrs: { class: /(?:^|\s)foo(?:\\s|$)/ }}. Any number of classnames supported.
  • Attribute selectors: any number of standard attribute selectors can be used1 including the following non-standard:
    • [attr!=value]: matches attributes with values that do not contain value.
  • Multiple node selectors: "div, span" returns [{tag: "div"}, {tag: "span"}].

1 Multiple attribute selectors for the same attribute are not supported (this includes mixing classnames and attribute selectors matching class).

The basic template for selectors (and order of features) looks like this:

"tag#id.class.name[attr*=value][otherattr^='start']"

Basic usage

import matchHelper from "posthtml-match-helper";

tree.match(matchHelper("div.class"), function (node) {
  // do stuff with matched node...
});

Advanced usage

import matchHelper from "posthtml-match-helper";

tree.match(matchHelper("input.my-control[type!='radio'][checked], input[value^='foo'][checked]"), function (node) {
  // do stuff with node that matched either of the selectors...
});

Classnames with escaped characters

If you need to match nodes with classnames that use escaped characters, like those in Tailwind CSS utilities with arbitrary values, use the following syntax:

import matchHelper from "posthtml-match-helper";

tree.match(matchHelper("input.\\[display:none\\]"), function (node) {
  // do stuff with node that matched either of the selectors...
});

The helper function

Arguments

  • matcher (string) - A CSS selector that describes the node you want to match in PostHTML.

Returns

A matcher object or an array of matcher objects.