All Projects → developit → Undom

developit / Undom

Licence: mit
🍩 1kb minimally viable DOM Document implementation

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Undom

Ysoserial
A proof-of-concept tool for generating payloads that exploit unsafe Java object deserialization.
Stars: ✭ 4,808 (+869.35%)
Mutual labels:  serialization
Preact Habitat
Zero configuration Preact widgets renderer in any host DOM
Stars: ✭ 444 (-10.48%)
Mutual labels:  preact
Iguana
universal serialization engine
Stars: ✭ 481 (-3.02%)
Mutual labels:  serialization
Webappsec Trusted Types
A browser API to prevent DOM-Based Cross Site Scripting in modern web applications.
Stars: ✭ 424 (-14.52%)
Mutual labels:  polyfill
Protobuf
A pure Elixir implementation of Google Protobuf
Stars: ✭ 442 (-10.89%)
Mutual labels:  serialization
React Lifecycles Compat
Backwards compatibility polyfill for React class components
Stars: ✭ 457 (-7.86%)
Mutual labels:  polyfill
Preact Render To String
📄 Universal rendering for Preact: render JSX and Preact components to HTML.
Stars: ✭ 411 (-17.14%)
Mutual labels:  preact
Binaryprefs
Rapidly fast and lightweight re-implementation of SharedPreferences which stores each preference in files separately, performs disk operations via NIO with memory mapped byte buffers and works IPC (between processes). Written from scratch.
Stars: ✭ 484 (-2.42%)
Mutual labels:  serialization
Airframe
Essential Building Blocks for Scala
Stars: ✭ 442 (-10.89%)
Mutual labels:  serialization
Preact Cli
😺 Your next Preact PWA starts in 30 seconds.
Stars: ✭ 4,510 (+809.27%)
Mutual labels:  preact
Styled Breakpoints
Simple and powerful tool for creating breakpoints in styled components and emotion. 💅
Stars: ✭ 428 (-13.71%)
Mutual labels:  preact
Flatcc
FlatBuffers Compiler and Library in C for C
Stars: ✭ 434 (-12.5%)
Mutual labels:  serialization
Elle
The Elle coroutine-based asynchronous C++ development framework.
Stars: ✭ 459 (-7.46%)
Mutual labels:  serialization
Fastbinaryencoding
Fast Binary Encoding is ultra fast and universal serialization solution for C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Swift
Stars: ✭ 421 (-15.12%)
Mutual labels:  serialization
Quick Xml
Rust high performance xml reader and writer
Stars: ✭ 480 (-3.23%)
Mutual labels:  serialization
Typesystem
Data validation, serialization, deserialization & form rendering. 🔢
Stars: ✭ 416 (-16.13%)
Mutual labels:  serialization
Ijk
Transforms arrays into virtual dom trees; a terse alternative to JSX and h
Stars: ✭ 452 (-8.87%)
Mutual labels:  preact
Object Fit Polyfill
A Javascript polyfill for browsers that don't support the object-fit CSS property.
Stars: ✭ 493 (-0.6%)
Mutual labels:  polyfill
Sugar
A Javascript library for working with native objects.
Stars: ✭ 4,457 (+798.59%)
Mutual labels:  polyfill
Design System Utils
👩‍🎨 Access your design tokens with ease
Stars: ✭ 465 (-6.25%)
Mutual labels:  preact

undom

NPM travis-ci

Minimally viable DOM Document implementation

A bare-bones HTML DOM in a box. If you want the DOM but not a parser, this might be for you.

1kB, works in Node and browsers, plugins coming soon!

JSFiddle Demo: Rendering preact components into an undom Document.

preview


Project Goals

Undom aims to find a sweet spot between size/performance and utility. The goal is to provide the simplest possible implementation of a DOM Document, such that libraries relying on the DOM can run in places where there isn't one available.

The intent to keep things as simple as possible means undom lacks some DOM features like HTML parsing & serialization, Web Components, etc. These features can be added through additional libraries.

Looking to 1.0.0

As of version 1.0.0, the DOM constructors and their prototypes will be shared for all instances of a document, as is the case with JSDOM. Once merged, PR #25 will address this by adding an undom.env() function, which returns a fresh document factory with a new set of constructors & prototypes.


Installation

Via npm:

npm install --save undom


Require Hook

In CommonJS environments, simply import undom/register to patch the global object with a singleton Document.

require('undom/register');

// now you have a DOM.
document.createElement('div');

Usage

// import the library:
import undom from 'undom';

let document = undom();

let foo = document.createElement('foo');
foo.appendChild(document.createTextNode('Hello, World!'));
document.body.appendChild(foo);

Recipe: Serialize to HTML

One task undom doesn't handle for you by default is HTML serialization. A proper implementation of this would be cumbersome to maintain and would rely heavily on getters and setters, which limits browser support. Below is a simple recipe for serializing an undom Element (Document, etc) to HTML.

Small & in ES2015:

Element.prototype.toString = function() { return serialize(this); };

function serialize(el) {
  return el.nodeType==3 ? enc(el.data) : (
    '<'+this.nodeName.toLowerCase() + this.attributes.map(attr).join('') + '>' +
    this.childNodes.map(serialize).join('') + '</'+this.nodeName.toLowerCase()+'>'
  );
}
let attr = a => ` ${a.name}="${enc(a.value)}"`;
let enc = s => s.replace(/[&'"<>]/g, a => `&#${a};`);

ES3 Version

This also does pretty-printing.

function serialize(el) {
	if (el.nodeType===3) return el.textContent;
	var name = String(el.nodeName).toLowerCase(),
		str = '<'+name,
		c, i;
	for (i=0; i<el.attributes.length; i++) {
		str += ' '+el.attributes[i].name+'="'+el.attributes[i].value+'"';
	}
	str += '>';
	for (i=0; i<el.childNodes.length; i++) {
		c = serialize(el.childNodes[i]);
		if (c) str += '\n\t'+c.replace(/\n/g,'\n\t');
	}
	return str + (c?'\n':'') + '</'+name+'>';
}

function enc(s) {
	return s.replace(/[&'"<>]/g, function(a){ return `&#${a};` });
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].