Installations
npm install html-dom-parser
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM, UMD
Node Version
20.18.1
NPM Version
10.8.2
Score
97.1
Supply Chain
99.6
Quality
87.9
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Languages
JavaScript (56.69%)
TypeScript (41.34%)
HTML (1.97%)
Developer
Download Statistics
Total Downloads
195,961,557
Last Day
100,449
Last Week
612,933
Last Month
5,633,850
Last Year
73,177,122
GitHub Statistics
93 Stars
1,626 Commits
22 Forks
2 Watching
1 Branches
8 Contributors
Bundle Size
10.09 kB
Minified
3.37 kB
Minified + Gzipped
Package Meta Information
Latest Version
5.0.13
Package Id
html-dom-parser@5.0.13
Unpacked Size
128.58 kB
Size
29.56 kB
File Count
54
NPM Version
10.8.2
Node Version
20.18.1
Publised On
25 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
195,961,557
Last day
-23.7%
100,449
Compared to previous day
Last week
-50.4%
612,933
Compared to previous week
Last month
-8.9%
5,633,850
Compared to previous month
Last year
40.9%
73,177,122
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
40
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
1import parse from 'html-dom-parser'; 2 3parse('<p>Hello, World!</p>');
Output
1[ 2 Element { 3 type: 'tag', 4 parent: null, 5 prev: null, 6 next: null, 7 startIndex: null, 8 endIndex: null, 9 children: [ 10 Text { 11 type: 'text', 12 parent: [Circular], 13 prev: null, 14 next: null, 15 startIndex: null, 16 endIndex: null, 17 data: 'Hello, World!' 18 } 19 ], 20 name: 'p', 21 attribs: {} 22 } 23]
Install
NPM:
1npm install html-dom-parser --save
Yarn:
1yarn add html-dom-parser
CDN:
1<script src="https://unpkg.com/html-dom-parser@latest/dist/html-dom-parser.min.js"></script> 2<script> 3 window.HTMLDOMParser(/* string */); 4</script>
Usage
Import with ES Modules:
1import parse from 'html-dom-parser';
Require with CommonJS:
1const parse = require('html-dom-parser').default;
Parse empty string:
1parse('');
Output:
1[]
Parse string:
1parse('Hello, World!');
Output
1[ 2 Text { 3 type: 'text', 4 parent: null, 5 prev: null, 6 next: null, 7 startIndex: null, 8 endIndex: null, 9 data: 'Hello, World!' 10 } 11]
Parse element with attributes:
1parse('<p class="foo" style="color: #bada55">Hello, <em>world</em>!</p>');
Output
1[ 2 Element { 3 type: 'tag', 4 parent: null, 5 prev: null, 6 next: null, 7 startIndex: null, 8 endIndex: null, 9 children: [ [Text], [Element], [Text] ], 10 name: 'p', 11 attribs: { class: 'foo', style: 'color: #bada55' } 12 } 13]
The server parser is a wrapper of htmlparser2 parseDOM
but with the root parent node excluded. The next section shows the available options you can use with the server parse.
The client parser mimics the server parser by using the DOM API to parse the HTML string.
Options (server only)
Because the server parser is a wrapper of htmlparser2, which implements domhandler, you can alter how the server parser parses your code with the following options:
1/** 2 * These are the default options being used if you omit the optional options object. 3 * htmlparser2 will use the same options object for its domhandler so the options 4 * should be combined into a single object like so: 5 */ 6const options = { 7 /** 8 * Options for the domhandler class. 9 * https://github.com/fb55/domhandler/blob/master/src/index.ts#L16 10 */ 11 withStartIndices: false, 12 withEndIndices: false, 13 xmlMode: false, 14 /** 15 * Options for the htmlparser2 class. 16 * https://github.com/fb55/htmlparser2/blob/master/src/Parser.ts#L104 17 */ 18 xmlMode: false, // Will overwrite what is used for the domhandler, otherwise inherited. 19 decodeEntities: true, 20 lowerCaseTags: true, // !xmlMode by default 21 lowerCaseAttributeNames: true, // !xmlMode by default 22 recognizeCDATA: false, // xmlMode by default 23 recognizeSelfClosing: false, // xmlMode by default 24 Tokenizer: Tokenizer, 25};
If you're parsing SVG, you can set lowerCaseTags
to true
without having to enable xmlMode
. This will return all tag names in camelCase and not the HTML standard of lowercase.
[!NOTE] If you're parsing code client-side (in-browser), you cannot control the parsing options. Client-side parsing automatically handles returning some HTML tags in camelCase, such as specific SVG elements, but returns all other tags lowercased according to the HTML standard.
Migration
v5
Migrated to TypeScript. CommonJS imports require the .default
key:
1const parse = require('html-dom-parser').default;
v4
Upgraded htmlparser2 to v9.
v3
Upgraded domhandler to v5. Parser options like normalizeWhitespace
have been removed.
v2
Removed Internet Explorer (IE11) support.
v1
Upgraded domhandler
to v4 and htmlparser2
to v6.
Release
Release and publish are automated by Release Please.
Special Thanks
License
No vulnerabilities found.
Reason
security policy file detected
Details
- Info: security policy file detected: .github/SECURITY.md:1
- Info: Found linked content: .github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: .github/SECURITY.md:1
- Info: Found text in security policy: .github/SECURITY.md:1
Reason
30 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/release-please.yml:20
Reason
SAST tool is run on all commits
Details
- Info: all commits (27) are checked with a SAST tool
Reason
5 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-pfq8-rq6v-vf5m
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/commitlint.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/commitlint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/commitlint.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/commitlint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/lint.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/lint.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/lint.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/lint.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release-please.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/release-please.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release-please.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/release-please.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release-please.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/release-please.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/size-limit.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/size-limit.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/size-limit.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/size-limit.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:38: update your workflow using https://app.stepsecurity.io/secureworkflow/remarkablemark/html-dom-parser/test.yml/master?enable=pin
- Info: 0 out of 11 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 4 third-party GitHubAction dependencies pinned
- Info: 5 out of 5 npmCommand dependencies pinned
Reason
branch protection is not maximal on development and all release branches
Details
- Info: 'allow deletion' disabled on branch 'master'
- Info: 'force pushes' disabled on branch 'master'
- Warn: 'branch protection settings apply to administrators' is disabled on branch 'master'
- Info: 'stale review dismissal' is required to merge on branch 'master'
- Warn: branch 'master' does not require approvers
- Warn: codeowners review is not required on branch 'master'
- Warn: 'last push approval' is disabled on branch 'master'
- Warn: 'up-to-date branches' is disabled on branch 'master'
- Info: status check found to merge onto on branch 'master'
- Info: PRs are required in order to make changes on branch 'master'
Reason
Found 0/4 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/release-please.yml:23
- Warn: no topLevel permission defined: .github/workflows/assign-reviewer.yml:1
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Warn: no topLevel permission defined: .github/workflows/commitlint.yml:1
- Warn: no topLevel permission defined: .github/workflows/lint.yml:1
- Warn: no topLevel permission defined: .github/workflows/release-please.yml:1
- Warn: no topLevel permission defined: .github/workflows/size-limit.yml:1
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Score
5.9
/10
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More