| 1 | This repository is only intended to be used as a reference implementation of Turndown's reference HTML parser. |
| 2 | It is not intended to be used as a general-purpose DOM implementation. No contributions other than bugfixes to |
| 3 | supported Turndown use case will be accepted. |
| 4 | |
| 5 | |
| 6 | # Server-side DOM implementation based on Mozilla's dom.js |
| 7 | |
| 8 | This is a fork of [Angular Domino](https://github.com/angular/domino), which is fork of the original [Domino](https://github.com/fgnass/domino). |
| 9 | |
| 10 | As the name might suggest, domino's goal is to provide a <b>DOM in No</b>de. |
| 11 | |
| 12 | In contrast to the original [dom.js](https://github.com/andreasgal/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](https://github.com/fgnass/dombench). |
| 13 | |
| 14 | Domino currently doesn't use any harmony/ES6 features like proxies or WeakMaps and therefore also runs in older Node versions. |
| 15 | |
| 16 | ## Speed over Compliance |
| 17 | |
| 18 | Domino is intended for _building_ pages rather than scraping them. Hence Domino doesn't execute scripts nor does it download external resources. |
| 19 | |
| 20 | Also Domino doesn't generally implement properties which have been deprecated in HTML5. |
| 21 | |
| 22 | Domino sticks to [DOM level 4](http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#interface-attr), which means that Attributes do not inherit the Node interface. |
| 23 | |
| 24 | <b>Note that</b> because domino does not use proxies, |
| 25 | `Element.attributes` is not a true JavaScript array; it is an object |
| 26 | with a `length` property and an `item(n)` accessor method. See |
| 27 | [github issue #27](https://github.com/fgnass/domino/issues/27) for |
| 28 | further discussion. It does however implement direct indexed accessors |
| 29 | (`element.attributes[i]`) and is live. |
| 30 | |
| 31 | |
| 32 | |
| 33 | ## CSS Selector Support |
| 34 | |
| 35 | Domino provides support for `querySelector()`, `querySelectorAll()`, and `matches()` backed by the [Zest](https://github.com/chjj/zest) selector engine. |
| 36 | |
| 37 | ## Optimization |
| 38 | |
| 39 | Domino represents the DOM tree structure in the same way Webkit and |
| 40 | other browser-based implementations do: as a linked list of children |
| 41 | which is converted to an array-based representation iff the |
| 42 | `Node#childNodes` accessor is used. You will get the best performance |
| 43 | from tree modification code (inserting and removing children) if you |
| 44 | avoid the use of `Node#childNodes` and traverse the tree using |
| 45 | `Node#firstChild`/`Node#nextSibling` (or |
| 46 | `Node#lastChild`/`Node#previousSibling`) or `querySelector()`/etc. |
| 47 | |
| 48 | ## Usage |
| 49 | |
| 50 | Domino supports the DOM level 4 API, and thus API documentation can be |
| 51 | found on standard reference sites. For example, you could start from |
| 52 | MDN's documentation for |
| 53 | [Document](https://developer.mozilla.org/en-US/docs/Web/API/Document) and |
| 54 | [Node](https://developer.mozilla.org/en-US/docs/Web/API/Node). |
| 55 | |
| 56 | The only exception is the initial creation of a document: |
| 57 | ```javascript |
| 58 | var domino = require('domino'); |
| 59 | var Element = domino.impl.Element; // etc |
| 60 | |
| 61 | // alternatively: document = domino.createDocument(htmlString, true) |
| 62 | |
| 63 | var h1 = document.querySelector('h1'); |
| 64 | console.log(h1.innerHTML); |
| 65 | console.log(h1 instanceof Element); |
| 66 | ``` |
| 67 | |
| 68 | There is also an incremental parser available, if you need to interleave |
| 69 | parsing with other processing: |
| 70 | ```javascript |
| 71 | var domino = require('domino'); |
| 72 | |
| 73 | var pauseAfter = function(ms) { |
| 74 | var start = Date.now(); |
| 75 | return function() { return (Date.now() - start) >= ms; }; |
| 76 | }; |
| 77 | |
| 78 | var incrParser = domino.createIncrementalHTMLParser(); |
| 79 | incrParser.write('<p>hello<'); |
| 80 | incrParser.write('b>&am'); |
| 81 | incrParser.process(pauseAfter(1/*ms*/)); // can interleave processing |
| 82 | incrParser.write('p;'); |
| 83 | // ...etc... |
| 84 | incrParser.end(); // when done writing the document |
| 85 | |
| 86 | while (incrParser.process(pauseAfter(10/*ms*/))) { |
| 87 | // ...do other housekeeping... |
| 88 | } |
| 89 | |
| 90 | console.log(incrParser.document().outerHTML); |
| 91 | ``` |
| 92 | |
| 93 | If you want a more standards-compliant way to create a `Document`, you can |
| 94 | also use [DOMImplementation](https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation): |
| 95 | ```javascript |
| 96 | var domino = require('domino'); |
| 97 | var domimpl = domino.createDOMImplementation(); |
| 98 | var doc = domimpl.createHTMLDocument(); |
| 99 | ``` |
| 100 | |
| 101 | By default many domino methods will be stored in writable properties, to |
| 102 | allow polyfills (as browsers do). You can lock down the implementation |
| 103 | if desired as follows: |
| 104 | ```javascript |
| 105 | global.__domino_frozen__ = true; // Must precede any `require('domino')` |
| 106 | var domino = require('domino'); |
| 107 | ``` |
| 108 | |
| 109 | ## Tests |
| 110 | |
| 111 | Domino includes test from the [W3C DOM Conformance Suites](http://www.w3.org/DOM/Test/) |
| 112 | as well as tests from [HTML Working Group](http://www.w3.org/html/wg/wiki/Testing). |
| 113 | |
| 114 | When you checkout this repository for the first time, run the following command to also check out code for the mentioned tests: |
| 115 | |
| 116 | ``` |
| 117 | git submodule update --init --recursive |
| 118 | ``` |
| 119 | |
| 120 | The tests can be run via `npm test` or directly though the [Mocha](http://mochajs.org/) command line: |
| 121 | |
| 122 |  |
| 123 | |
| 124 | ## License and Credits |
| 125 | |
| 126 | The majority of the code was originally written by [Andreas Gal](https://github.com/andreasgal/) and [David Flanagan](https://github.com/davidflanagan) as part of the [dom.js](https://github.com/andreasgal/dom.js) project. Please refer to the included LICENSE file for the original copyright notice and disclaimer. |
| 127 | |
| 128 | [Felix Gnass](https://github.com/fgnass/) extracted the code and turned |
| 129 | it into a stand-alone npm package. |
| 130 | |
| 131 | The code has been maintained since 2013 by [C. Scott Ananian](https://github.com/cscott/) on behalf of the Wikimedia Foundation, which uses it in its |
| 132 | [Parsoid](https://www.mediawiki.org/wiki/Parsoid) project. A large number |
| 133 | of improvements have been made, mostly focusing on correctness, |
| 134 | performance, and (to a lesser extent) completeness of the implementation. |
| 135 | |
| 136 | [1]: https://travis-ci.org/fgnass/domino.svg |
| 137 | [2]: https://travis-ci.org/fgnass/domino |
| 138 | [3]: https://david-dm.org/fgnass/domino.svg |
| 139 | [4]: https://david-dm.org/fgnass/domino |
| 140 | [5]: https://david-dm.org/fgnass/domino/dev-status.svg |
| 141 | [6]: https://david-dm.org/fgnass/domino#info=devDependencies |