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

domtagger

v0.7.2

Published

The hyperHTML's template literal parser

Downloads

38,079

Readme

domtagger

Build Status Coverage Status Greenkeeper badge WebReflection status

The hyperHTML's template literal parser, used to handle all repeated updates per each attribute or node.

  • CDN as global utility, via https://unpkg.com/domtagger
  • ESM via import domtagger from 'domtagger'
  • CJS via const domtagger = require('domtagger')

Live test

Example

The tagger accepts a configuration object with mandatory methods that should return a function to invoke per each update.

Optionally, the object could have a type property, as either html or svg string, and a transform method that must return some string as content, after receiving the markup that is going to be used.

var html = domtagger({

  // can be html or svg
  type: 'html',

  // how to handle attributes
  // Note: this callback is simplified for example purpose.
  // The node is the attribute owner
  attribute: function (node, name, attribute) {
    return function (value) {
      var type = typeof value;
      if (type === 'boolean' || type === 'function')
        node[name] = value;
      else if (value == null)
        node.removeAttribute(name);
      else
        node.setAttribute(name, value);
    }
  },

  // how to handle generic content
  // Note: this callback is simplified for example purpose.
  // The comment node is the hole placeholder
  // use domdiff or other techniques to handle nodes
  any: function (comment, childNodes) {
    var parentNode = comment.parentNode;
    return function (html) {
      parentNode.innerHTML = html;
    };
  },

  // how to handle cases where content
  // can only be some text
  // The node is one that can only have text
  text: function (node) {
    return function (textContent) {
      node.textContent = textContent;
    };
  },

  // OPTIONAL
  // a man in the middle for the output
  // The html string is what will be used to generate the content
  // this is always invoked after sanitizing the template parts
  transform: function (html) {
    // it must return the eventually transformed html
    return html;
  },

  // for adventurous 3rd parts libraries only:
  // previously internally known as `sanitize`,
  // it will run before transform and it must return a *string*
  // that contains domconstants.UID/UIDC in the right place
  // or the whole library will break
  convert: function (template) {
    // see domsanitizer logic
    // https://github.com/WebReflection/domsanitizer/blob/master/esm/index.js
    // or see a possible wrap solution/hint/workaround
    // https://github.com/WebReflection/domtagger/issues/17#issuecomment-526151473
    return template.join(domconstants.UIDC).replace(sani, tize);
  }

});

document.body.appendChild(
  render({
    onclick: function (e) {
      alert(e.currentTarget.outerHTML);
    },
    html: 'Hello <strong>domtagger</strong>!',
    text: "isn't this cool?"
  })
);

// render example
function render(model) {
  return html`
    <div onclick=${model.onclick}>
      <!--/* html is sanitized as text automatically */-->
      <div>${model.html}</div>
      <!--👻 textarea can use value=... or its content -->
      <textarea>${model.text}</textarea>
    </div>
  `;
}

About devs-only comments

If you'd like to create a dev only comment that will be removed at runtime once parsed, you can either start the comment with a ghost emoji 👻 or use /* and */ right at the boundaries of the comment.

<!--👻 dev only -->
<!--/* also dev only */-->
<!-- any other regular comment -->