📝 An HTML to DOM parser.
Switch branches/tags
Clone or download

README.md

html-dom-parser

NPM

NPM version Build Status Coverage Status Dependency status

An isomorphic HTML to DOM parser:

Parser(string[, options])

The parser converts an HTML string to a JavaScript object that describes the DOM tree.

repl.it | JSFiddle

Installation

NPM:

npm install --save html-dom-parser

Yarn:

yarn add html-dom-parser

CDN:

<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.js"></script>

Usage

Import parser:

// server
var Parser = require('html-dom-parser');

// client
var Parser = window.HTMLDOMParser;

Parse input:

Parser('<p>Hello, world!</p>');

Return output:

[ { type: 'tag',
    name: 'p',
    attribs: {},
    children:
     [ { data: 'Hello, world!',
         type: 'text',
         next: null,
         prev: null,
         parent: [Circular] } ],
    next: null,
    prev: null,
    parent: null } ]

The server parser is a wrapper of htmlparser2's parseDOM() and the client parser uses the browser's DOM API to mimic the output of the server parser.

Testing

$ npm test
$ npm run lint

License

MIT