Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@litejs/dom

litejs447MIT25.1.0

A small DOM library for server-side testing, rendering, and handling of HTML files

document, DOM, HTML, DOMParser, CSSOM, CSSStyleSheet, XMLHttpRequest, XMLSerializer, litejs

readme

LiteJS DOM – Coverage Size Buy Me A Tea

Dependency-free DOM library for handling HTML and CSS files on server-side.

// const { document } = require("@litejs/dom");
import { document, CSSStyleSheet, DOMParser, XMLSerializer } from "@litejs/dom";
import { XMLHttpRequest } from "@litejs/dom/net.js";

// Build DOM manually
const el = document.createElement("h1");
el.id = 123;
el.className = "large";

const fragment = document.createDocumentFragment();
fragment.appendChild(document.createTextNode("hello"));
el.appendChild(fragment);

// Replace the DOM tree with HTML
el.innerHTML = "<b>hello world</b>";
console.log(el.toString());
// <h1 id="123" class="large"><b>hello world</b></h1>

// Minify HTML
console.log(el.toString(true));
// <h1 id=123 class=large><b>hello world</b></h1>

el.querySelectorAll("b");
// [ "<b>hello world</b>" ]

// Use XMLHttpRequest in server side
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://litejs.com");
xhr.responseType = "document";
xhr.onload = function() {
    const doc = xhr.responseXML;
    // Work with DOM in familiar way
    console.log(doc.querySelector("title").textContent);
}
xhr.send();

// Minify CSS
const sheet = new CSSStyleSheet({ min: { color: true } })
sheet.replaceSync(".a { color: hsl(0 0% 100%) }")
console.log(sheet.toString())
// .a{color:#fff}

Contributing

Follow Coding Style Guide, run tests npm install; npm test.

Copyright (c) 2014-2025 Lauri Rooden <lauri@rooden.ee>
MIT License | GitHub repo | npm package | Buy Me A Tea