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

jsdon

v0.2.3

Published

A DOM serializer based on LinkeDOM idea

Downloads

64,444

Readme

JSDON

Build Status Coverage Status

Social Media Photo by Reign Abarintos on Unsplash

A DOM de/serializer based on LinkeDOM idea and the JSDON specification (which is something I've just made up).

Why

I like the idea we can represent the DOM linearly, and we can travel via postMessage or any other capable JSON PL anything we like, simplifying diffing, when needed, updates, changes, and so on.

This module just provides the basics to transform back and forward any document as JSON, enabling new ways to deal with Web pages, SVG images, or XML documents.

JavaScript DOM Object Notation

This notation considers two kinds of representations, plus one:

  • a leaf is a node that cannot contain anything else
  • a branch is a node that can contain either branches or leafs
  • a tree is a branch extension that represents a whole document

Leaf

A leaf is represented by its type, and at least one, or more, string values, representing data the leaf carries with it.

// attribute node
[2,"name"]
[2,"name","value"]

// text node
[3,"content"]

// comment node
[8,"content"]

// document type node (either html or svg)
[10,"html"]

Branch

A branch is represented by its type, followed by zero, one, or more leafs, or branches. A branch has a delimiter too, but multiple delimiters get merged as summary of their value.

// element
// <div></div>
[1,"div",-1]

// element with attributes
// <div id="unique" contenteditable>!</div>
[1,"div",2,"id","unique",2,"contenteditable",3,"!",-1]

// element with a branch
// <div><p></p></div>
[1,"div",1,"p",-2]

// element with mixed content
// <div>before<p>between</p>after</div>
[1,"div",3,"before",1,"p",3,"between",-1,3,"after",-1]

// fragment
[11,3,"text",-1]

Tree

A tree is just a branch, but it starts with a document type, and it returns a new document.

// most basic document with no doctype
[9,-1]

// most basic html with a <!doctype html>
[9,10,"html",-1]

// most basic html with a doctype and an html node
// <!doctype html><html lang="en"></html>
[9,10,"html",1,"html",2,"lang","en",-2]

How To

import {fromJSON, toJSON} from 'jsdon';

// create a single array with nodes details
const array = toJSON(document);

// restore either JSON string form array or array itself
const doc = fromJSON(array);

This module makes it possible to shrink HTML into a linear, stream-able, representation of the tree, linearized via linkedom logic, to transport any kind of layout without extra bloat, also preserving Custom Elements builtin extends.

API

toJSON(node[, filter])

By default, it puts in the output every node as is, but if there is a filter callback, each node passes through such callback and, if it returns false, such node won't be added to the final output.

A common use case could be avoiding empty text nodes:

toJSON(document, node => {
  if (
    node.nodeType === node.TEXT_NODE &&
    node.textContent.trim().length === 0
  ) {
    // drop all unnecessary empty text nodes
    return false;
  }
  // accepts all others
  return true;
});

fromJSON(value[, document])

The value can be either a string, that will be parsed via JSON.parse, or an array representing serialized markup.

The extra, optional, document parameter, is to let environments not running within a browser provide their own document, assuming this also has a defaultView property that exposes a DOMParser global, so that it's possible to create new HTML, SVG, or even XML, documents with it.

Serialized Nodes

  • ELEMENT_NODE
  • ATTRIBUTE_NODE
  • TEXT_NODE
  • COMMENT_NODE
  • DOCUMENT_NODE
  • DOCUMENT_TYPE_NODE
  • DOCUMENT_FRAGMENT_NODE