Skip to content
📝 An HTML to DOM parser.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.eslintrc
.gitignore Create npm script `build` that builds a bundle using webpack Oct 10, 2016
.npmignore
.npmrc refactor(types): move to root Apr 2, 2019
.travis.yml feat(types): add TypeScript decelerations Apr 1, 2019
CHANGELOG.md chore(release): 0.2.1 Apr 3, 2019
LICENSE Add MIT license Oct 5, 2016
README.md
index.d.ts
index.js Create the main entry file that determines which parser to export Oct 10, 2016
package.json chore(release): 0.2.1 Apr 3, 2019
tsconfig.json refactor(types): move to root Apr 2, 2019
tslint.json

README.md

html-dom-parser

NPM

NPM version Build Status Coverage Status Dependency status

An HTML to DOM parser that works on both the server and the browser:

HTMLDOMParser(string[, options])

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

repl.it | JSFiddle

Installation

NPM:

$ npm install html-dom-parser --save

Yarn:

$ yarn add html-dom-parser

unpkg (CDN):

<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.js"></script>
<script>
  window.HTMLDOMParser(/* string */);
</script>

Usage

Import parser:

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

// client
var parser = window.HTMLDOMParser;

Parse input:

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

Get output:

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

On the server-side (Node.js), the parser is a wrapper of parseDOM from htmlparser2.

On the client-side (browser), the parser uses the DOM API to mimic the output schema of the server parser.

Testing

$ npm test
$ npm run lint # npm run lint:fix
$ npm run dtslint

License

MIT

You can’t perform that action at this time.