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

apply-html

v2.0.1

Published

It's `.innerHTML = ''` for the 21st century.

Downloads

40

Readme

apply-html

NPM version Downloads Build Status Coverage Status

It's .innerHTML = '' for the 21st century!

Yet another library to diff and patch an existing DOM tree by efficiently comparing it to a string. Why? This library is a little bit different than others. It makes use of an HTML <template>'s unique ability to create an inert document fragment, featuring:

  • A real DOM tree
  • Multiple root nodes
  • Will not trigger resource loading prematurely
  • Will not apply embedded stylesheets prematurely
  • Will not trigger custom element constructors or lifecycle events prematurely

The live DOM is then patched with the inert fragment using a hyper-fast diffing algorithm for real DOM nodes. This ensures that things only start happening if and when they're supposed to, organically.

Play with it on CodePen.

Install

$ npm install --save apply-html

or

<script src="https://wzrd.in/standalone/apply-html"></script>

or

<script type="module">
    import { apply, html } from 'https://unpkg.com/apply-html?module';
</script>

Usage

Patching

const { apply } = require('apply-html');

apply(document.body, '<h1 class="day">Hello World</h1>');

console.log(document.body.innerHTML);
// -> <h1 class="day">Hello World</h1>

apply(document.body, '<h1 class="night">Goodnight Moon</h1>');

console.log(document.body.innerHTML);
// -> <h1 class="night">Goodnight Moon</h1>

Interpolation and Escaping

const { apply, html, raw } = require('apply-html');

const foo = '<em>foo</em>';
const bar = raw('<em>bar</em>');
const baz = html`<strong>baz</strong>`;

apply(document.body, html`
    ${foo}
    ${bar}
    ${baz}
`);

console.log(document.body.innerHTML);
// -> &lt;em&gt;foo&lt;/em&gt;
// -> <em>bar</em>
// -> <strong>baz</strong>

Server-side Rendering

The html and raw functions never touch the DOM so they're completely safe to use server-side.

const http = require('http');
const { html } = require('apply-html');

const content = html`
    <h1>Hello <em>World</em></h1>
    <p>How are you today?</p>
`;

module.exports = http
    .createServer((req, res) => res.end(content.toString()))
    .listen(3000);

API

apply(element, string): Element

  • element {Element} DOM element with children to be patched.
  • string {String|SafeString} String or SafeString containing safe HTML to render.

Updates the content of the given element, making the fewest possible changes required to match the given string of HTML. The string is converted into an HTML <template> and the resulting DOM trees are compared. Returns the updated element.

html`string`: SafeString

A template tag that creates a new SafeString containing a string of HTML. Interpolated values are serialized based on type:

  • Array - Items are serialized then joined with an empty string ('').
  • Boolean|null|undefined - Converted to an empty string ('').
  • Function - Throws a TypeError.
  • Number - Inserted as-is.
  • Object - Converted to an HTML-encoded JSON blob.
  • SafeString - Inserted as-is.
  • String - HTML-encoded to safeguard against XSS. To opt out of escaping, use raw().

raw(string): SafeString

  • string {String} String of safe HTML.

Wraps a string in a SafeString to indicate that it's safe to be inserted into the document. Only use on trusted strings to safeguard against XSS.

SafeString

.raw {String}

The wrapped string.

.length {Number}

Length of the wrapped string. Read only.

.toJSON(): String

Returns the raw string.

.toString(): String

Returns the raw string.

Acknowledgements

Standing on the shoulders of these giants:


MIT © Shannon Moeller