Installations
npm install @node-projects/node-html-parser-esm
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.10.0
NPM Version
9.7.1
Score
73.9
Supply Chain
98.9
Quality
82.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
HTML (97.76%)
JavaScript (1.41%)
TypeScript (0.84%)
Developer
node-projects
Download Statistics
Total Downloads
15,210
Last Day
1
Last Week
37
Last Month
179
Last Year
6,246
GitHub Statistics
15 Stars
244 Commits
1 Forks
2 Watching
1 Branches
2 Contributors
Package Meta Information
Latest Version
6.2.0
Package Id
@node-projects/node-html-parser-esm@6.2.0
Unpacked Size
66.86 kB
Size
18.36 kB
File Count
22
NPM Version
9.7.1
Node Version
20.10.0
Publised On
26 Dec 2023
Total Downloads
Cumulative downloads
Total Downloads
15,210
Last day
0%
1
Compared to previous day
Last week
362.5%
37
Compared to previous week
Last month
-59.5%
179
Compared to previous month
Last year
4.1%
6,246
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
23
ESM Fork
This is a fork wich compiles to esm modules for usage direct in browser. You need to load javascript package "he" to the browser
Fast HTML Parser
Fast HTML Parser is a very fast HTML parser. Which will generate a simplified DOM tree, with 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).
Install
1npm install --save node-html-parser
Note: when using Fast HTML Parser in a Typescript project the minimum Typescript version supported is
^4.1.2
.
Performance
-- 2022-08-10
1html-parser :24.1595 ms/file ± 18.7667 2htmljs-parser :4.72064 ms/file ± 5.67689 3html-dom-parser :2.18055 ms/file ± 2.96136 4html5parser :1.69639 ms/file ± 2.17111 5cheerio :12.2122 ms/file ± 8.10916 6parse5 :6.50626 ms/file ± 4.02352 7htmlparser2 :2.38179 ms/file ± 3.42389 8htmlparser :17.4820 ms/file ± 128.041 9high5 :3.95188 ms/file ± 2.52313 10node-html-parser:2.04288 ms/file ± 1.25203 11node-html-parser (last release):2.00527 ms/file ± 1.21317
Tested with htmlparser-benchmark.
Usage
1import { parse } from 'node-html-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>');
Global Methods
parse(data[, options])
Parse the data provided, and return the root of the generated DOM.
-
data, data to parse
-
options, parse options
1{ 2 lowerCaseTagName: false, // convert tag name to lower case (hurts performance heavily) 3 comment: false, // retrieve comments (hurts performance slightly) 4 voidTag:{ 5 tags: ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'], // optional and case insensitive, default value is ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'] 6 addClosingSlash: true // optional, default false. void tag serialisation, add a final slash <br/> 7 }, 8 blockTextElements: { 9 script: true, // keep text content when parsing 10 noscript: true, // keep text content when parsing 11 style: true, // keep text content when parsing 12 pre: true // keep text content when parsing 13 } 14}
valid(data[, options])
Parse the data provided, return true if the given data is valid, and return false if not.
Class
1classDiagram 2direction TB 3class HTMLElement{ 4 this trimRight() 5 this removeWhitespace() 6 Node[] querySelectorAll(string selector) 7 Node querySelector(string selector) 8 HTMLElement[] getElementsByTagName(string tagName) 9 Node closest(string selector) 10 Node appendChild(Node node) 11 this insertAdjacentHTML('beforebegin' | 'afterbegin' | 'beforeend' | 'afterend' where, string html) 12 this setAttribute(string key, string value) 13 this setAttributes(Record~string, string~ attrs) 14 this removeAttribute(string key) 15 string getAttribute(string key) 16 this exchangeChild(Node oldNode, Node newNode) 17 this removeChild(Node node) 18 string toString() 19 this set_content(string content) 20 this set_content(Node content) 21 this set_content(Node[] content) 22 this remove() 23 this replaceWith((string | Node)[] ...nodes) 24 ClassList classList 25 HTMLElement clone() 26 HTMLElement getElementById(string id) 27 string text 28 string rawText 29 string tagName 30 string structuredText 31 string structure 32 Node firstChild 33 Node lastChild 34 Node nextSibling 35 HTMLElement nextElementSibling 36 Node previousSibling 37 HTMLElement previousElementSibling 38 string innerHTML 39 string outerHTML 40 string textContent 41 Record~string, string~ attributes 42 [number, number] range 43} 44class Node{ 45 <<abstract>> 46 string toString() 47 Node clone() 48 this remove() 49 number nodeType 50 string innerText 51 string textContent 52} 53class ClassList{ 54 add(string c) 55 replace(string c1, string c2) 56 remove(string c) 57 toggle(string c) 58 boolean contains(string c) 59 number length 60 string[] value 61 string toString() 62} 63class CommentNode{ 64 CommentNode clone() 65 string toString() 66} 67class TextNode{ 68 TextNode clone() 69 string toString() 70 string rawText 71 string trimmedRawText 72 string trimmedText 73 string text 74 boolean isWhitespace 75} 76Node --|> HTMLElement 77Node --|> CommentNode 78Node --|> TextNode 79Node ..> ClassList
HTMLElement Methods
trimRight()
Trim element from right (in block) after seeing pattern in a TextNode.
removeWhitespace()
Remove whitespaces in this sub tree.
querySelectorAll(selector)
Query CSS selector to find matching nodes.
Note: Full range of CSS3 selectors supported since v3.0.0.
querySelector(selector)
Query CSS Selector to find matching node.
getElementsByTagName(tagName)
Get all elements with the specified tagName.
Note: Use * for all elements.
closest(selector)
Query closest element by css selector.
appendChild(node)
Append a child node to childNodes
insertAdjacentHTML(where, html)
Parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position.
setAttribute(key: string, value: string)
Set value
to key
attribute.
setAttributes(attrs: Record<string, string>)
Set attributes of the element.
removeAttribute(key: string)
Remove key
attribute.
getAttribute(key: string)
Get key
attribute.
exchangeChild(oldNode: Node, newNode: Node)
Exchanges given child with new child.
removeChild(node: Node)
Remove child node.
toString()
Same as outerHTML
set_content(content: string | Node | Node[])
Set content. Notice: Do not set content of the root node.
remove()
Remove current element.
replaceWith(...nodes: (string | Node)[])
Replace current element with other node(s).
classList
classList.add
Add class name.
classList.replace(old: string, new: string)
Replace class name with another one.
classList.remove()
Remove class name.
classList.toggle(className: string):void
Toggle class. Remove it if it is already included, otherwise add.
classList.contains(className: string): boolean
Returns true if the classname is already in the classList.
classList.value
Get class names.
clone()
Clone a node.
getElementById(id: string): HTMLElement;
Get element by it's ID.
HTMLElement Properties
text
Get unescaped text value of current node and its children. Like innerText
.
(slow for the first time)
rawText
Get escaped (as-is) text value of current node and its children. May have
&
in it. (fast)
tagName
Get or Set tag name of HTMLElement. Notice: the returned value would be an uppercase string.
structuredText
Get structured Text.
structure
Get DOM structure.
firstChild
Get first child node.
lastChild
Get last child node.
innerHTML
Set or Get innerHTML.
outerHTML
Get outerHTML.
nextSibling
Returns a reference to the next child node of the current element's parent.
nextElementSibling
Returns a reference to the next child element of the current element's parent.
previousSibling
Returns a reference to the previous child node of the current element's parent.
previousElementSibling
Returns a reference to the previous child element of the current element's parent.
textContent
Get or Set textContent of current element, more efficient than set_content.
attributes
Get all attributes of current element. Notice: do not try to change the returned value.
range
Corresponding source code start and end indexes (ie [ 0, 40 ])
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
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
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'
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
22 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
Score
1.7
/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