{"library":"node-html-better-parser","title":"Node HTML Better Parser","description":"Node HTML Better Parser is a high-performance HTML parser for Node.js and TypeScript, currently at version 1.5.8 and last published 6 months ago. It serves as a fork of `fast-html-parser`, prioritizing speed to process large HTML files efficiently. It generates a simplified Document Object Model (DOM) and includes basic element query support. A key differentiator of this fork is its focus on providing a simpler API for editing HTML and its attributes, which was a primary motivation for its creation. While highly optimized for performance, it may not correctly parse all malformed HTML, though it handles common errors like missing closing tags for `<li>` or `<td>`. The library offers configurable options to retrieve content from `<script>`, `<style>`, `<pre>`, and comments, but users should note these options can slightly impact parsing performance.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-html-better-parser"],"cli":null},"imports":["import { parse } from 'node-html-better-parser';","const HTMLParser = require('node-html-better-parser');\n// then use HTMLParser.parse(data)","import { HTMLElement } from 'node-html-better-parser';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse } from 'node-html-better-parser';\n\nconst htmlString = '<!DOCTYPE html>\\n<html lang=\"en\">\\n<head>\\n  <meta charset=\"UTF-8\">\\n  <title>Sample Page</title>\\n  <style>body { font-family: sans-serif; }</style>\\n</head>\\n<body>\\n  <ul id=\"main-list\">\\n    <li>Hello World</li>\\n    <li class=\"item\">Another item</li>\\n  </ul>\\n  <script>console.log(\"script content\");</script>\\n</body>\\n</html>';\n\n// Parse the HTML, enabling script and style content retrieval\nconst root = parse(htmlString, { script: true, style: true });\n\n// Access the root node's children (the <html> tag in this case)\nconsole.log('Root children structure:', root.firstChild?.structure);\n\n// Query for an element by ID\nconst mainList = root.querySelector('#main-list');\nif (mainList) {\n  console.log('\\nFound #main-list:');\n  console.log('  Tag Name:', mainList.tagName);\n  console.log('  Raw Attributes:', mainList.rawAttrs);\n  console.log('  Text Content:', mainList.text);\n\n  // Edit content of the list\n  mainList.set_content('<li>New first item</li><li>New second item</li>');\n  console.log('\\nList after set_content:', mainList.toString());\n}\n\n// Query for an element by class name\nconst item = root.querySelector('.item'); // Note: This will find 'Another item' before set_content\nif (item) {\n  console.log('\\nFirst element with class \"item\":', item.toString());\n}\n\n// Get the content of the script tag\nconst scriptTag = root.querySelector('script');\nif (scriptTag) {\n  console.log('\\nScript Content:', scriptTag.text);\n}\n\nconsole.log('\\nFull modified HTML:', root.toString());","lang":"typescript","description":"This quickstart demonstrates parsing an HTML string, querying elements by ID and class, accessing their properties, retrieving script and style content using options, and modifying an element's content, then printing the resulting HTML.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}