npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dom-parser-mini

v2.0.2

Published

A lightweight library for parsing HTML elements.

Downloads

23

Readme

dom-parser-mini

NPM version Build Status Test Coverage License Dependencies

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.
    • Example:
      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.
    • Example:
      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.
    • Example:
      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.
    • Example:
      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.
    • Example:
      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.
    • Example:
      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.
    • Example:
      const node = HTMLNode.create('<div>Hello</div>')[0];
      node.remove();
      console.log(node.html()); // Outputs: ''
  • unRemove(): void: Unmarks the node as removed.
    • Example:
      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.
    • Example:
      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>

Contributing

Feel free to open issues or submit pull requests for improvements and bug fixes.

License

This project is licensed under the MIT License.