Package Exports
- html-dom-parser
- html-dom-parser/index.js
- html-dom-parser/lib/client/html-to-dom.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (html-dom-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
html-dom-parser
HTML to DOM parser that works on both the server (Node.js) and the client (browser):
HTMLDOMParser(string[, options])The parser converts an HTML string to a JavaScript object that describes the DOM tree.
Example
const parse = require('html-dom-parser');
parse('<p>Hello, World!</p>');Output:
[
Element {
type: 'tag',
parent: null,
prev: null,
next: null,
startIndex: null,
endIndex: null,
children: [
Text {
type: 'text',
parent: [Circular],
prev: null,
next: null,
startIndex: null,
endIndex: null,
data: 'Hello, World!'
}
],
name: 'p',
attribs: {}
}
]Install
NPM:
npm install html-dom-parser --saveYarn:
yarn add html-dom-parserCDN:
<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.min.js"></script>
<script>
window.HTMLDOMParser(/* string */);
</script>Usage
Import or require the module:
// ES Modules
import parse from 'html-dom-parser';
// CommonJS
const parse = require('html-dom-parser');Parse empty string:
parse('');Output:
[];Parse string:
parse('Hello, World!');[
Text {
type: 'text',
parent: null,
prev: null,
next: null,
startIndex: null,
endIndex: null,
data: 'Hello, World!'
}
]Parse element with attributes:
parse('<p class="foo" style="color: #bada55">Hello, <em>world</em>!</p>');Output:
[
Element {
type: 'tag',
parent: null,
prev: null,
next: null,
startIndex: null,
endIndex: null,
children: [ [Text], [Element], [Text] ],
name: 'p',
attribs: { class: 'foo', style: 'color: #bada55' }
}
]The server parser is a wrapper of htmlparser2 parseDOM but with the root parent node excluded.
The client parser mimics the server parser by using the DOM API to parse the HTML string.
Testing
Run server and client tests:
npm testGenerate HTML coverage report for server tests:
npx nyc report --reporter=htmlLint files:
npm run lint
npm run lint:fixTest TypeScript declaration file for style and correctness:
npm run lint:dtsRelease
Only collaborators with credentials can release and publish:
npm run release
git push --follow-tags && npm publish