Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
dom-parser-mini
Advanced tools
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
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);
Currently, the dom parser mini only handles self closing tags.
const html = `<div><img src="image.jpg" /></div>`;
const nodes = HTMLNode.create(html);
console.log(nodes);
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:
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.html(): string
: Returns the HTML representation of the node.
const node = HTMLNode.create('<div><p>Hello</p></div>')[0];
console.log(node.html()); // Outputs: <div><p>Hello</p></div>
text(): string
: Returns the text content of the node.
const node = HTMLNode.create('<div>Hello <span>world</span></div>')[0];
console.log(node.text()); // Outputs: Hello world
getElementById(id: string): HTMLNodeInterface | null
: Finds a child node by its ID.
const node = HTMLNode.create('<div><p id="para">Hello</p></div>')[0];
console.log(node.getElementById('para')); // Outputs the <p> node with id="para"
getElementsByClass(className: string): HTMLNodeInterface[]
: Finds child nodes by their class name.
const node = HTMLNode.create('<div class="container"><p class="text">Hello</p></div>')[0];
console.log(node.getElementsByClass('text')); // Outputs an array with the <p> node
hidden(): void
: Hides the node by setting display: none;
in its style attribute.
const node = HTMLNode.create('<div>Hello</div>')[0];
node.hidden();
console.log(node.html()); // Outputs: <div style="display: none;">Hello</div>
show(): void
: Shows the node by removing display: none;
from its style attribute.
const node = HTMLNode.create('<div style="display: none;">Hello</div>')[0];
node.show();
console.log(node.html()); // Outputs: <div>Hello</div>
remove(): void
: Marks the node as removed.
const node = HTMLNode.create('<div>Hello</div>')[0];
node.remove();
console.log(node.html()); // Outputs: ''
unRemove(): void
: Unmarks the node as removed.
const node = HTMLNode.create('<div>Hello</div>')[0];
node.remove();
node.unRemove();
console.log(node.html()); // Outputs: <div>Hello</div>
filterAttributes(whitelist: string[]): void
: Filters the node's attributes based on a whitelist.
const node = HTMLNode.create('<div onclick="alert(\'hello\')" class="container">Hello</div>')[0];
node.filterAttributes(['class']);
console.log(node.html()); // Outputs: <div class="container">Hello</div>
Feel free to open issues or submit pull requests for improvements and bug fixes.
This project is licensed under the MIT License.
FAQs
A lightweight library for parsing HTML elements.
The npm package dom-parser-mini receives a total of 1 weekly downloads. As such, dom-parser-mini popularity was classified as not popular.
We found that dom-parser-mini demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.