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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@percy/dom

v1.31.5

Published

Serializes a document's DOM into a DOM string suitable for re-rendering.

Downloads

1,948,032

Readme

@percy/dom

Serializes a document's DOM into a DOM string suitable for re-rendering.

Usage

ES6 imports

import serializeDOM from '@percy/dom';

// optional arguments shown with defaults
const domSnapshot = serializeDOM(options)

Browser injection

// via puppeteer
await page.addScriptTag({ path: require.resolve('@percy/dom') })
const domSnapshot = await page.evaluate(() => PercyDOM.serialize(options))

Options

  • enableJavaScript — When true, does not serialize some DOM elements
  • domTransformation — Function to transform the DOM after serialization
  • disableShadowDOM — disable shadow DOM capturing, usually to be used when enableJavascript: true
  • reshuffleInvalidTags — moves DOM tags which are outside </body> to its inside to make the DOM compliant.

Serialized Content

The following serialization happens to a cloned instance of the document in order.

Input elements

Input elements (input, textarea, select) are serialized by setting respective DOM attributes to their matching JavaScript property counterparts. For example checked, selected, and value.

Frame elements

Frame elements are serialized when they are CORS accessible and if they haven't been built by JavaScript when JavaScript is enabled. They are serialized by recursively serializing the iframe's own document element.

CSSOM rules

When JavaScript is not enabled, CSSOM rules are serialized by iterating over and appending each rule to a new stylesheet inserted into the document's head.

Canvas elements

Canvas elements' drawing buffers are serialized as data URIs and the canvas elements are replaced with image elements. The image elements reference the serialized data URI and have the same HTML attributes as their respective canvas elements. The image elements also have a max-width of 100% to accommodate responsive layouts in situations where canvases may be expected to resize with JS.

Video elements

Videos without a poster attribute will have the current frame of the video serialized into an image and set as the poster attribute automatically. This is to ensure videos have a stable image to display when screenshots are captured.

Shadow DOM

Shadow dom #shadow-root (open) is serialized into declarative shadow DOM (<template shadowroot="open">) form Shadow host element is annotated with special identifier attribute named data-percy-shadow-host. This identifier attribute may be used when passing domTransformation.

Other elements

All other elements are not serialized. The resulting cloned document is passed to any provided domTransformation option before the serialize function returns a DOM string.

Examples

  1. perform DOM transformations on serialized DOM

this example contains scenario of nested shadow DOMs

import serializeDOM from '@percy/dom';

const domSnapshot = serializeDOM({
    domTransformation: (documentElement) => {
      function insertHelloHeader(root) {
        h1 = document.createElement('h1');
        h1.innerText = 'Inserted using dom transformations';
        root.append(h1);

        root.querySelectorAll('[data-percy-shadow-host]')
          .forEach(
            shadowHost => {
              if (shadowHost?.shadowRoot)
                insertHelloHeader(shadowHost.shadowRoot)
            });

      }
      insertHelloHeader(documentElement);
    }
  });