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 🙏

© 2026 – Pkg Stats / Ryan Hefner

hast-util-dom-attributes

v4.0.0

Published

Attribute utilities for hast elements, consistent with the DOM.

Readme

hast-util-dom-attributes

Attribute utilities for hast elements, consistent with the DOM.

In hast, the values of the properties record may be, in addition to the intuitively reasonable string, boolean, number, and (string | number)[], also null and undefined (v2/v3). This is convenient when constructing a tree, but things get a bit complicated when you want to check for presence or obtain a string in a type-safe way.

Below are the results observed from the input/output of hast-util-to-html. You can see that simply relying on truthy/falsy or using String does not yield correct results.

| properties: | toHtml(node) | | ---------------------- | -------------------------- | | undefined (v2) | <div></div> | | {} | <div></div> | | { foo: true } | <div foo></div> | | { foo: false } | <div></div> | | { foo: 42 } | <div foo="42"></div> | | { foo: 0 } | <div foo="0"></div> | | { foo: "bar" } | <div foo="bar"></div> | | { foo: "" } | <div foo=""></div> | | { foo: [42, "bar"] } | <div foo="42 bar"></div> | | { foo: [] } | <div foo=""></div> | | { foo: null } | <div></div> | | { foo: undefined } | <div></div> |

Writing attributes with both camel case and kebab case names can create collisions. For example, both dataFoo and data-foo serialize to data-foo. If both keys have values that are serialized, the resulting HTML contains duplicate attributes, and the value read by the DOM or getAttribute depends on their insertion order. Writing either name with setAttribute produces a single dataFoo property for that attribute in the returned properties.

In hast, both class and className correspond to the HTML class attribute, and their values can be strings or arrays. For example, { class: "foo bar" }, { class: ["foo", "bar"] }, { className: "foo bar" }, and { className: ["foo", "bar"] } all serialize to class="foo bar". Reading a property directly therefore requires handling both names and both value forms to determine which classes an element has. getClassList provides access to those classes without making callers account for these differences in representation.

Hast consumers such as hast-util-select expect className to be an array of individual class names. setAttribute and setClassList produce that representation so class selectors match complete tokens.

Removing an attribute also requires accounting for its aliases. Given { class: "foo", className: "bar" }, deleting either key leaves a class attribute in the serialized HTML; both keys must be removed. removeAttribute removes all properties that represent the requested attribute, so callers do not need to find and delete each alias themselves.

External hast trees may already contain attribute names such as class or data-foo, while writes through these utilities use className or dataFoo. Reads return the first value that would be serialized for the requested attribute. setAttribute and setClassList collapse aliases of the requested attribute into one property, preserving unrelated keys and values, including null and undefined. These values remain subject to the usual toHtml serialization rules shown above. Names that serialize to distinct attributes, such as data--foo and data-foo, remain distinct; property-name conversion is used only when it preserves the serialized attribute name. This lets callers use trees from other transforms without preparing their properties first.

Reading attributes should not modify a shared tree, and writing attributes should let the caller decide when to update it. All utilities accept read-only elements. setAttribute, removeAttribute, and setClassList return a new hast.Properties record, which can be assigned to the original element while preserving its identity in the tree:

target.properties = setAttribute(target, "data-foo", "bar");
target.properties = setClassList(target, ["foo", "bar"]);

For nested SVG elements, pass { root } to these attribute utilities so attribute names are resolved in the original element's namespace.