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

@emmetio/html-matcher

v1.3.0

Published

Minimalistic and ultra-fast HTML parser & matcher

Downloads

769,767

Readme

Small and fast HTML matcher

Finds matching opening and closing tag pair for given location in HTML/XML source:

import match from '@emmetio/html-matcher';

const content = '<div><a href="http://emmet.io">Example</a></div>';

// Find tag pair at character 35
const tag = match(content, 35);

console.log(tag.name); // Name of matched tag: "a"
console.log(tag.open); // Range of opening tag: [5, 31]
console.log(tag.end); // Range of closing tag: [38, 42]

// List of attributes found in opening tag
console.log(tag.attributes);

By default, matcher works in HTML, which means if it finds tag name which is known to be empty (for example, <img>) it will not search for it’s closing part. However, such behavior might be unexpected for XML syntaxes where all tags should be either self-closed or provide closing part. In this case, you should pass xml: true option to properly handle XML mode:

import match from '@emmetio/html-matcher';

const content = '<div><img>Caption</img></div>';
const html = match(content, 8);
const xml = match(content, 8, { xml: true });

console.log(html.name); // "img"
console.log(html.open); // [5, 10]
console.log(html.close); // undefined

console.log(xml.name); // "img"
console.log(xml.open); // [5, 10]
console.log(xml.close); // [17, 23]

Special tags

In HTML, some tags has special meaning. For example, a <script> tag: its contents should be completely ignored until we find closing </script> tag. But, if <script> tag contains unknown type attribute value, we should consider it as a regular tag. By default, matcher understands script and style tags as “special” but you can override them with special option:

import match from '@emmetio/html-matcher';

// Treat `<foo-bar>` tag as ”special”: skip its content until `</foo-bar>`. Note that this option overwrites default value with `['script', 'style']` value
match('...', 10, { special: { 'foo-bar': null } });

The special option is an object where key is a tag name and value is an array of type attribute values which, if present in tag, will make it special. If array is not provided, all instances of tag with given name will be considered as special.