dom-parser-mini
A lightweight, dependency-free HTML parser for Node.js. This package provides a simple way to parse and manipulate HTML
content.
This project is still in beta
You can install the package using npm:
npm install dom-parser-mini
Usage
Basic HTML Parsing
const HTMLNode = require('dom-parser-mini');
const html = `<div><p>Hello, world!</p></div>`;
const nodes = HTMLNode.create(html);
console.log(nodes);
const html = `<div><img src="image.jpg" /></div>`;
const nodes = HTMLNode.create(html);
console.log(nodes);
API
HTMLNode.create(input: string): HTMLNodeInterface[]
Parses the input HTML string and returns an array of HTMLNodeInterface objects representing the DOM structure.
HTMLNodeInterface
An interface representing a parsed HTML node with the following properties and methods:
Properties
tagName: string
: The tag name of the node.attributes: { [key: string]: string }
: The attributes of the node.children: HTMLNodeInterface[]
: The child nodes of the node.content?: string
: The content of the node.
Methods
html(): string
: Returns the HTML representation of the node.
text(): string
: Returns the text content of the node.
getElementById(id: string): HTMLNodeInterface | null
: Finds a child node by its ID.
getElementsByClass(className: string): HTMLNodeInterface[]
: Finds child nodes by their class name.
hidden(): void
: Hides the node by setting display: none;
in its style attribute.
show(): void
: Shows the node by removing display: none;
from its style attribute.
remove(): void
: Marks the node as removed.
unRemove(): void
: Unmarks the node as removed.
filterAttributes(whitelist: string[]): void
: Filters the node's attributes based on a whitelist.
Contributing
Feel free to open issues or submit pull requests for improvements and bug fixes.
License
This project is licensed under the MIT License.