You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

html5parser

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html5parser

A simple HTML5 parser, for walk and manipulate attributes and nodes.

Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
55K
5.39%
Maintainers
1
Weekly downloads
 
Created
Source

html5parser

A simple and fast html5 parser, the result could be manipulated like ECMAScript ESTree, especially about the attributes.

Install

# var npm
npm install html5parser -S

# var yarn
yarn add html5parser

Quick Start

import * as html from 'html5parser'

const input = `
<!DOCTYPE html>
<html>
  <body>
    <h1 id="hello">Hello world</h1>
  </body>
</html>
`

const ast = html.parse()

html.walk(ast, {
  enter: (node) => {
    if (node.type === html.SyntaxKind.Tag) {
      for (const attr of node.attributes) {
        if (attr.value !== void 0) {
          console.log(input.substring(attr.value.start, attr.value.end))
        }
      }
    }
  }
})

// Should output:
// hello

API

// Top level API, parse html to ast tree
export function parse(input: string): INode[];

// Low level API, get tokens
export function tokenize(input: string): IToken[];

Spec

The AST tree structure: types.ts

Warning

This is use for HTML5, that means:

  • All tags like <? ... ?>, <! ... > (except for <!doctype ...>, case insensitive) is treated as Comment, that means CDATASection is treated as comment.
  • Special tag names:
  • "!doctype" (case insensitive), the doctype declaration
  • "!": short comment
  • "!--": normal comment
  • ""(empty string): short comment, for <? ... >, the leading ? is treated as comment content

License

MIT

Keywords

html5

FAQs

Package last updated on 18 Aug 2017

Did you know?

Socket

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.

Install

Related posts