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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jikurata/html-parser

v0.0.9

Published

Parse html into a HTMLDocument

Readme

html-parser v0.0.9

Synchronously parse html into a HTML Document object

Install


npm install @jikurata/html-parser

Usage


const parser = require('@jikurata/html-parser');

const html = `
    <section id="sampleDoc">
        <header class="text">This is a sample</header>
        <div>
            <p>With a nested Element</p>
            And text node support
            <div>Another div</div>
        </div>
    </section>
`;

const doc = parser(html); // Returns a ParsedHTMLDocument object

    // Returns a ParsedElement containing information about the section tag
    const sectionElement = doc.getElementById('sampleDoc')

    // Search an element's children. Returns a ParsedElement for the header tag
    const header = sectionElement.getElementsByClassName('text')[0]
    
    // Search a document. Returns two ParsedElements for the div tags
    const divs = doc.getElementsByTagName('div') 

Modify the ParsedHTMLDocument or any of its ParsedElements with familiar methods

    sectionElement.setAttribute('data-attr', 'foo');
    header.innerHTML += ' and a header too';
    doc.removeChildren(divs);

    const modifiedHtml = doc.stringify()

    console.log(modifiedHtml); 
    // <section id="sampleDoc" data-attr="foo">
    //    <header class="text">This is a sample and a header too</header>
    // </section>

Documentation


class ParsedElement

constructor(options)

  • options {ParsedElementOptions}

Properties

  • referenceId {String}

Methods

  • getElementById(id)
    • id {String}
  • getElementsByTagName(tag)
    • tag {String}
  • getElementsByClassName(name)
    • name {String}
  • hasAttribute(attr)
    • attr {String}
  • getAttribute(attr)
    • attr {String}
  • setAttribute(attr, value)
    • attr {String}
    • value {String}
  • appendChild(element)
    • element {ParsedElement}
  • prependChild(element)
    • element {ParsedElement}
  • replaceChild(child, elements)
    • child {ParsedElement}
    • elements {ParsedElement|Array[ParsedElement]}
  • remove()
  • removeChildren(elements)
    • elements {ParsedElement|Array[ParsedElement]}
  • getDescendants()
  • stringify()

class ParsedHTMLElement extends ParsedElement

constructor(options)

  • options {ParsedElementOptions}

Properties

  • textContent {String}
  • innerHTML {String}
  • outerHTML {String}

Methods

  • stringifyChildren()

object ConfigOptions

  • config {Object}
    • voidTags {Array}
    • trimWhitespace {Boolean}

object ParsedElementOptions

  • tagName: {String}
  • nodeType: {String},
  • mode: {String}
  • attributes {Object},
  • content: {String},
  • parent: {ParsedElement},
  • children: {Array[ParsedElement]}

class ParsedFragmentElement extends ParsedHTMLElement

Version Log


v0.0.9

  • Parser now recognizes comments. Comment elements are set to mode = 'comment'

v0.0.8

  • Fixed a bug that only allowed retrieval of config, instead of modifying it through the export

v0.0.7

  • Refactored ParsedHTMLDocument to ParsedFragmentElement
  • Refactored data structure behind the parser to utilize a tree instead of an array

v0.0.6

  • Fixed a bug that prevented void elements from parsing null attributes into implicit attributes

v0.0.5

  • Attributes with a null value are parsed as implicit attributes

v0.0.4

  • Fixed an issue that prevented htmldocument from updating its contents
  • Refactored appendChild and prependChild to be available in ParsedElement
  • htmldocument's replaceChild, appendChild, and prependChild methods now invoke its fragment's methods
  • TODO: Implement a clone method for ParsedElement

v0.0.3

  • Fixed an issue that caused textContent and innerHTML to return an empty string when multiple distinct tags were involved
  • !doctype is now recognized as a void tag
  • TODO: Implement a clone method for ParsedElement

v0.0.2

  • Implement replaceChild method for ParsedElement
  • Fixed outerHTML setter to properly overwrite the parent element's children

v0.0.1

  • ParsedElement emits update events and propagate the updates to its ancestors
  • Implement textContent, innerHTML, outerHTML
  • The document or any section of a document can be converted back into a string with the stringify() method
  • Fixed a bug that caused the parser to miss an element's closing tag when the element has no content

v0.0.0

  • Plans:
    • Expand upon available methods for ParsedElement and its derivatives