Installations
npm install node-html-better-parser
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
16.20.2
NPM Version
8.19.4
Score
98.2
Supply Chain
100
Quality
79.3
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
HTML (97.77%)
TypeScript (2.23%)
Developer
Sharcoux
Download Statistics
Total Downloads
1,570,603
Last Day
3,602
Last Week
23,518
Last Month
221,832
Last Year
1,540,249
GitHub Statistics
144 Commits
1 Forks
1 Watching
2 Branches
1 Contributors
Package Meta Information
Latest Version
1.4.7
Package Id
node-html-better-parser@1.4.7
Unpacked Size
154.81 kB
Size
25.55 kB
File Count
10
NPM Version
8.19.4
Node Version
16.20.2
Publised On
09 Sept 2024
Total Downloads
Cumulative downloads
Total Downloads
1,570,603
Last day
-29.6%
3,602
Compared to previous day
Last week
-51.5%
23,518
Compared to previous week
Last month
-17.8%
221,832
Compared to previous month
Last year
5,857.5%
1,540,249
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Node HTML Better Parser
Fork from Fast HTML Parser
Fast HTML Parser is a very fast HTML parser. Which will generate a simplified DOM tree, with basic element query support.
Per the design, it intends to parse massive HTML files in lowest price, thus the
performance is the top priority. For this reason, some malformatted HTML may not
be able to parse correctly, but most usual errors are covered (eg. HTML4 style
no closing <li>
, <td>
etc).
About this fork: I created this fork to provide a simple API for editing the html and especially the attributes.
Install
1npm install --save node-html-better-parser
Performance
Faster than htmlparser2!
1fast-html-parser: 2.18409 ms/file ± 1.37431 2high5 : 4.55435 ms/file ± 2.51132 3htmlparser : 27.6920 ms/file ± 171.588 4htmlparser2-dom : 6.22320 ms/file ± 3.48772 5htmlparser2 : 3.58360 ms/file ± 2.23658 6hubbub : 16.1774 ms/file ± 8.95079 7libxmljs : 7.19406 ms/file ± 7.04495 8parse5 : 10.7590 ms/file ± 8.09687
Tested with htmlparser-benchmark.
Usage
1import { parse } from 'node-html-better-parser'; 2 3const root = parse('<ul id="list"><li>Hello World</li></ul>'); 4 5console.log(root.firstChild.structure); 6// ul#list 7// li 8// #text 9 10console.log(root.querySelector('#list')); 11// { tagName: 'ul', 12// rawAttrs: 'id="list"', 13// childNodes: 14// [ { tagName: 'li', 15// rawAttrs: '', 16// childNodes: [Object], 17// classNames: [] } ], 18// id: 'list', 19// classNames: [] } 20console.log(root.toString()); 21// <ul id="list"><li>Hello World</li></ul> 22root.set_content('<li>Hello World</li>'); 23root.toString(); // <li>Hello World</li>
1var HTMLParser = require('node-html-parser'); 2 3var root = HTMLParser.parse('<ul id="list"><li>Hello World</li></ul>');
API
parse(data[, options])
Parse given data, and return root of the generated DOM.
The root is actually a fictive node that contains the nodes found in the content provided. The content can be found in root.childNodes
, or accessed with root.firstChild
if the parsed HTML has a root node (think about <b>siblings</b> <i>nodes</i>
).
- data, data to parse
- options, parse options
1{ 2 lowerCaseTagName: false, // convert tag name to lower case (hurt performance heavily) 3 script: false, // retrieve content in <script> (hurt performance slightly) 4 style: false, // retrieve content in <style> (hurt performance slightly) 5 pre: false, // retrieve content in <pre> (hurt performance slightly) 6 comment: false // retrieve comments (hurt performance slightly) 7}
isBlock(node: HTMLElement)
Returns weither the provided node is a block. A node is considered a block is it has a tag represented as block instead of inline in html specs.
HTMLElement
properties
property name | description |
---|---|
text | Get unescaped text value of current node and its children. Like innerText . (slow for the first time) |
tagName | Get the tag name of the current element |
classNames | Get the classes of the current element |
parentNode | Get the parent HTMLNode |
rawText | Get escpaed (as-it) text value of current node and its children. May have & in it. (fast) |
structuredText | Get structured Text |
structure | Get DOM structure |
childNodes | Get the children nodes |
children | Get the children HTMLElement |
firstChild | Get first child node |
lastChild | Get last child node |
attributes | Get attributes |
rawAttributes | Get escaped (as-it) attributes |
innerHTML | Get innerHTML |
outerHTML | Get outerHTML |
methods
method | parameters | return |
---|---|---|
trimRight() | Trim element from right (in block) after seeing pattern in a TextNode. | |
removeWhitespace() | Remove whitespaces in this sub tree. | |
querySelectorAll(selector: string) | * selector: CSS selector Note: only tagName , #id , .class selectors supported. | Query CSS selector to find matching nodes. |
querySelector(selector: string) | * selector: CSS selector Note: only tagName , #id , .class selectors supported. And not behave the same as standard querySelectorAll() as it will stop searching sub tree after find a match. | Query CSS selector to find matching node. |
appendChild(node: Node) | * node: HTMLElement or Text node | Append a child node to childNodes |
setAttribute(key: string, value: string | null) | * key: The HTML attribute key * value: The HTML attribute name | Set value to key attribute. If value is null, remove the attribute instead. |
setAttributes(attributes: Attributes) | * attributes: An object containing the key: value pairs we expect for this node | Replace all the current attributes by the provided attribute set. |
removeAttributes(key: string) | * key: The HTML attribute key | Remove the attribute denoted by key . |
toString() | Same as outerHTML | |
set_content(content: string | Node | Node[]) | * content: The new content for this node | Set content for this node. Notice: Do not set content of the root node. |
No vulnerabilities found.
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
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
2.8
/10
Last Scanned on 2024-12-30
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