Gathering detailed insights and metrics for node-html-parser
Gathering detailed insights and metrics for node-html-parser
Gathering detailed insights and metrics for node-html-parser
Gathering detailed insights and metrics for node-html-parser
@node-projects/web-component-designer-htmlparserservice-nodehtmlparser
web-component-designer addon: Parser Service using the Node HTML Parser
sax
An evented streaming XML parser in JavaScript
@trysound/sax
An evented streaming XML parser in JavaScript
http-parser-js
A pure JS HTTP parser for node.
A very fast HTML parser, generating a simplified DOM, with basic element query support.
npm install node-html-parser
59.2
Supply Chain
100
Quality
83.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,123 Stars
497 Commits
111 Forks
8 Watching
3 Branches
2 Contributors
Updated on 24 Nov 2024
Minified
Minified + Gzipped
HTML (98.2%)
JavaScript (1.31%)
TypeScript (0.49%)
Cumulative downloads
Total Downloads
Last day
-6.4%
508,084
Compared to previous day
Last week
-0.1%
2,892,933
Compared to previous week
Last month
5.5%
12,595,478
Compared to previous month
Last year
31.2%
118,535,528
Compared to previous year
2
34
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).
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
.
-- 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.
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>');
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 fixNestedATags: false, // fix invalid nested <a> HTML tags 5 parseNoneClosedTags: false, // close none closed HTML tags instead of removing them 6 voidTag: { 7 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'] 8 closingSlash: true // optional, default false. void tag serialisation, add a final slash <br/> 9 }, 10 blockTextElements: { 11 script: true, // keep text content when parsing 12 noscript: true, // keep text content when parsing 13 style: true, // keep text content when parsing 14 pre: true // keep text content when parsing 15 } 16}
Parse the data provided, return true if the given data is valid, and return false if not.
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
Trim element from right (in block) after seeing pattern in a TextNode.
Remove whitespaces in this sub tree.
Query CSS selector to find matching nodes.
Note: Full range of CSS3 selectors supported since v3.0.0.
Query CSS Selector to find matching node. null
if not found.
Get all elements with the specified tagName.
Note: Use * for all elements.
Query closest element by css selector. null
if not found.
Insert one or multiple nodes or text before the current element. Does not work on root.
Insert one or multiple nodes or text after the current element. Does not work on root.
Insert one or multiple nodes or text to the first position of an element's child nodes.
Insert one or multiple nodes or text to the last position of an element's child nodes. This is similar to appendChild, but accepts arbitrarily many nodes and converts strings to text nodes.
Append a node to an element's child nodes.
Parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position.
Set value
to key
attribute.
Set attributes of the element.
Remove key
attribute.
Get key
attribute. undefined
if not set.
Exchanges given child with new child.
Remove child node.
Same as outerHTML
Set content. Notice: Do not set content of the root node.
Remove current element.
Replace current element with other node(s).
Add class name.
Replace class name with another one.
Remove class name.
Toggle class. Remove it if it is already included, otherwise add.
Returns true if the classname is already in the classList.
Get class names.
Clone a node.
Get element by it's ID.
Get unescaped text value of current node and its children. Like innerText
.
(slow for the first time)
Get escaped (as-is) text value of current node and its children. May have
&
in it. (fast)
Get or Set tag name of HTMLElement. Note that the returned value is an uppercase string.
Get structured Text.
Get DOM structure.
Get all child nodes. A child node can be a TextNode, a CommentNode and a HTMLElement.
Get all child elements, so all child nodes of type HTMLELement.
Get first child node. undefined
if the node has no children.
Get last child node. undefined
if the node has no children.
Get the first child of type HTMLElement. undefined
if none exists.
Get the first child of type HTMLElement. undefined
if none exists.
Get the number of children that are of type HTMLElement.
Set or Get innerHTML.
Get outerHTML.
Returns a reference to the next child node of the current element's parent. null
if not found.
Returns a reference to the next child element of the current element's parent. null
if not found.
Returns a reference to the previous child node of the current element's parent. null
if not found.
Returns a reference to the previous child element of the current element's parent. null
if not found.
Get or Set textContent of current element, more efficient than set_content.
Get all attributes of current element. Notice: do not try to change the returned value.
Corresponding source code start and end indexes (ie [ 0, 40 ])
No vulnerabilities found.
No security vulnerabilities found.