A small DOM implementation for server-side


Keywords
document, dom, DOMParser, XMLSerializer, litejs, html, xml, xmlhttprequest
License
MIT
Install
npm install dom-lite@23.2.0

Documentation

LiteJS DOM – Coverage Size Buy Me A Tea

Dependency-free DOM library for handling HTML files on server-side.
DOM spec | Selectors Level 3

Examples

const { document, DOMParser, XMLSerializer } = require("@litejs/dom");

// 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 parsed HTML
el.innerHTML = "<b>hello world</b>";
el.toString();
// <h1 id="123" class="large"><b>hello world</b></h1>

// minify output
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 { XMLHttpRequest } = require("@litejs/dom/net.js");
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://litejs.com");
xhr.responseType = "document";
xhr.onload = function() {
	const document = xhr.responseXML;
	// Work with DOM in familiar way
	console.log(document.querySelector("title").textContent);
}
xhr.send();

Contributing

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

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