1.2.2 • Published 11 months ago

@homebots/parse-html v1.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

parse-html

Short and strict HTML parser.

Output format: a tree of objects, starting with a root Document node.

Element and Document nodes can have children, which are any of Text, Comment or Element.

API

import { parse, pack, unpack } from '@homebots/parse-html';
import { serialize, materialize, normalize } from '@homebots/parse-html-runtime';

const document = parse(`
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <div>text<br>text</div>
  </body>
</html>`);

console.log(document);

// remove empty text nodes
normalize(document);

// create HTML from document
console.log(serialize(document));

// create DOM elements from document
console.log(materialize(document, (element, node) => {
  // do something with the new element
  // return element or something new
}));

// to generate a minimal version of a node tree
const packed = pack(document);
const unpacked = unpack(packed);

Node types

Document

{
  "type": "document",
  "docType": "html",
  "children": []
}

Comment

{
  "type": "comment",
  "text": "an html comment"
}

Text

{
  "type": "text",
  "text": "a text\nnode with line breaks"
}

Element

{
  "type": "element",
  "tag": "input",
  "selfClose": true,
  "children": [],
  "attributes": [{ "name": "type", "value": "text" }]
}
1.2.0

11 months ago

1.1.9

11 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.1.11

11 months ago

1.1.10

11 months ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.2

1 year ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago