Convert HTML, SVG, and XML markup strings into DOM objects
JavaScript HTML
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist
src
test
.babelrc
.editorconfig
.eslintrc
.gitattributes
.gitignore
.travis.yml
README.md
UNLICENSE
gulpfile.babel.js
package.json

README.md

dominate

GitHub version Build Status Size

Convert HTML, SVG, and XML markup strings into DOM objects.

Usage

Convert a single element HTML string into a DOM element:

const div = dominate('<div>foo</div>');
div.nodeName; // DIV

Convert a multiple element HTML string into a document fragment:

const frag = dominate('<strong>Hello</strong> <em>World</em>');
frag.nodeName; // #document-fragment

Convert plain text to a DOM text node:

const text = dominate('This is plain text.');
text.nodeName; // #text

Convert an SVG element into a DOM element:

const rect = dominate('<rect x="10" y="10" width="100" height="100"/>');
rect instanceof SVGRectElement; // true

Executes script tags within an HTML string by default:

const script = dominate('<script>console.log("foo");</script>');
document.body.appendChild(script); // console outputs "foo"

Remove script tags to prevent execution:

const el = dominate('<section><script src="foo.js"></script></section>', {scripts: false});
const script = el.querySelector('script'); // null

Use a custom document instead of the default (window.document)

import { jsdom } from 'jsdom';
const doc = jsdom('<html><body></body></html>');

const el = dominate('<div></div>', {context: doc});
el.ownerDocument === doc; // true

Convert XML string into a DOM element:

const xml = dominate('<name>foo</name>', {type: 'xml'});
xml instanceof Element; // true
xml instanceof HTMLElement; // false

Installation

Dominate is CommonJS and AMD compatible with no dependencies. You can download the development or minified version, or install it in one of the following ways:

npm install ryanmorr/dominate

bower install ryanmorr/dominate

Tests

Open test/runner.html in your browser or test with PhantomJS by issuing the following commands:

npm install
npm install -g gulp
gulp test

License

This project is dedicated to the public domain as described by the Unlicense.