• Stars
    star
    769
  • Rank 59,078 (Top 2 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created almost 13 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Server-side DOM implementation based on Mozilla's dom.js

Server-side DOM implementation based on Mozilla's dom.js

Build Status dependency status dev dependency status

As the name might suggest, domino's goal is to provide a DOM in Node.

In contrast to the original dom.js project, domino was not designed to run untrusted code. Hence it doesn't have to hide its internals behind a proxy facade which makes the code not only simpler, but also more performant.

Domino currently doesn't use any harmony/ES6 features like proxies or WeakMaps and therefore also runs in older Node versions.

Speed over Compliance

Domino is intended for building pages rather than scraping them. Hence Domino doesn't execute scripts nor does it download external resources.

Also Domino doesn't generally implement properties which have been deprecated in HTML5.

Domino sticks to DOM level 4, which means that Attributes do not inherit the Node interface.

Note that because domino does not use proxies, Element.attributes is not a true JavaScript array; it is an object with a length property and an item(n) accessor method. See github issue #27 for further discussion. It does however implement direct indexed accessors (element.attributes[i]) and is live.

CSS Selector Support

Domino provides support for querySelector(), querySelectorAll(), and matches() backed by the Zest selector engine.

Optimization

Domino represents the DOM tree structure in the same way Webkit and other browser-based implementations do: as a linked list of children which is converted to an array-based representation iff the Node#childNodes accessor is used. You will get the best performance from tree modification code (inserting and removing children) if you avoid the use of Node#childNodes and traverse the tree using Node#firstChild/Node#nextSibling (or Node#lastChild/Node#previousSibling) or querySelector()/etc.

Usage

Domino supports the DOM level 4 API, and thus API documentation can be found on standard reference sites. For example, you could start from MDN's documentation for Document and Node.

The only exception is the initial creation of a document:

var domino = require('domino');
var Element = domino.impl.Element; // etc

var window = domino.createWindow('<h1>Hello world</h1>', 'http://example.com');
var document = window.document;

// alternatively: document = domino.createDocument(htmlString, true)

var h1 = document.querySelector('h1');
console.log(h1.innerHTML);
console.log(h1 instanceof Element);

There is also an incremental parser available, if you need to interleave parsing with other processing:

var domino = require('domino');

var pauseAfter = function(ms) {
  var start = Date.now();
  return function() { return (Date.now() - start) >= ms; };
};

var incrParser = domino.createIncrementalHTMLParser();
incrParser.write('<p>hello<');
incrParser.write('b>&am');
incrParser.process(pauseAfter(1/*ms*/)); // can interleave processing
incrParser.write('p;');
// ...etc...
incrParser.end(); // when done writing the document

while (incrParser.process(pauseAfter(10/*ms*/))) {
  // ...do other housekeeping...
}

console.log(incrParser.document().outerHTML);

If you want a more standards-compliant way to create a Document, you can also use DOMImplementation:

var domino = require('domino');
var domimpl = domino.createDOMImplementation();
var doc = domimpl.createHTMLDocument();

By default many domino methods will be stored in writable properties, to allow polyfills (as browsers do). You can lock down the implementation if desired as follows:

global.__domino_frozen__ = true; // Must precede any `require('domino')`
var domino = require('domino');

Tests

Domino includes test from the W3C DOM Conformance Suites as well as tests from HTML Working Group.

The tests can be run via npm test or directly though the Mocha command line:

Screenshot

License and Credits

The majority of the code was originally written by Andreas Gal and David Flanagan as part of the dom.js project. Please refer to the included LICENSE file for the original copyright notice and disclaimer.

Felix Gnass extracted the code and turned it into a stand-alone npm package.

The code has been maintained since 2013 by C. Scott Ananian on behalf of the Wikimedia Foundation, which uses it in its Parsoid project. A large number of improvements have been made, mostly focusing on correctness, performance, and (to a lesser extent) completeness of the implementation.

More Repositories

1

spin.js

A spinning activity indicator
CSS
9,297
star
2

node-dev

Zero-conf Node.js reloading
JavaScript
2,250
star
3

inbox-app

Google Inbox packaged as Electron app
JavaScript
271
star
4

classname-variants

Variant API for plain class names
TypeScript
159
star
5

typed-rpc

Lightweight JSON-RPC solution for TypeScript projects
TypeScript
128
star
6

express-jsdom

Server-side DOM for express.js
JavaScript
56
star
7

filewatcher

Wrapper around fs.watch that falls back to fs.watchFile
JavaScript
54
star
8

linger

Busy-indicator for the terminal
JavaScript
51
star
9

google-calendar-app

Google Calendar packaged as electron app
JavaScript
51
star
10

instant-server

Instant HTTP server with live-reload
JavaScript
46
star
11

instant

transparent live-reloading
JavaScript
45
star
12

gateway

Node.js middleware to execute CGI scripts
JavaScript
37
star
13

jshint.tmbundle

JSHint TextMate Bundle
JavaScript
36
star
14

form2json

Alternative decoder for form-urlencoded data
JavaScript
31
star
15

retrace

Use source-maps on the server to make browser stack traces more readable
JavaScript
31
star
16

sendevent

Connect middleware for server-sent-events with iframe fallback
JavaScript
26
star
17

tamper

Node.js middleware to capture and modify response bodies
JavaScript
22
star
18

type-assurance

Lightweight type guards and assertions
TypeScript
22
star
19

zepto-node

JavaScript
21
star
20

mkay

Lightweight Jade/HAML-like DOM builder for jQuery and Zepto.js
JavaScript
21
star
21

react-api-query

Hooks to use react-query with a typed API client
TypeScript
20
star
22

glitter

WebGL experiment that creates a glitter effect based on the device orientation.
JavaScript
15
star
23

uniqs

Tiny utility to de-duplicate lists
JavaScript
11
star
24

diffparser

Unified diff parser for Node and the browser
JavaScript
11
star
25

orca.wtf

JavaScript
11
star
26

npm-as-nom

Wrapper around npm that screams COOKIE!! every time you type nom instead
JavaScript
10
star
27

gmail-app

Gmail packaged as Electron app
CSS
10
star
28

googlemaps-react-primitives

Google Maps primitives for React
TypeScript
8
star
29

stacked

lightweight middleware infrastructure
JavaScript
6
star
30

configurify

A browserify transform to expose server-side configuration options
JavaScript
6
star
31

rework-palette

Rework plugin to resolve custom CSS color palettes
JavaScript
5
star
32

php-proxy-middleware

Node middleware to forward requests to a built-in PHP server
JavaScript
5
star
33

rework-clearfix

JavaScript
5
star
34

deep-match

Check if two values deeply match
JavaScript
4
star
35

mdi-json

Material Design Icons as JSON
JavaScript
4
star
36

rework-parent

rework plugin to add parent selector support
JavaScript
4
star
37

dynaform

A jQuery plugin to dynamically create forms
JavaScript
4
star
38

dombench

Benchmark tool for Node.js DOM implementations
JavaScript
4
star
39

with-server

Command line utility to start/stop a local server in order to execute end-to-end tests
JavaScript
3
star
40

simple-image-resize

TypeScript
3
star
41

materialize

Tiny utility to turn a list into an object
JavaScript
3
star
42

fgnass.github.com

My personal website
HTML
3
star
43

ssi-middleware

Express middleware to render Server Side Includes
JavaScript
2
star
44

friendly-webdriver

Thin wrapper around the official Selenium JavaScript bindings
JavaScript
2
star
45

dommy

A Document dummy that mocks just enough of the DOM API to render HTML.
JavaScript
2
star
46

unexpected-webdriver

selenium-webdriver plugin for the unexpected assertion libary
JavaScript
2
star
47

d1-level

An abstract-level database backed by Cloudflare D1
TypeScript
1
star
48

statdir

collect stats for all files in a directory
JavaScript
1
star
49

dirwatcher

recursively watch directories for modifications
JavaScript
1
star
50

redux-route

Routing for Redux
JavaScript
1
star
51

browser-resolve-sync

Node.js resolve algorithm with browser field support
JavaScript
1
star
52

fwatch

JavaScript
1
star