Project: syntax-tree/hast-util-from-parse5

Package: hast-util-from-parse5@7.1.0

  1. Dependents: 0
  2. hast utility to transform from Parse5’s AST
  1. util 136
  2. utility 132
  3. unist 128
  4. hast 72
  5. hast-util 47
  6. ast 29
  7. transform 6
  8. change 4


Build Coverage Downloads Size Sponsors Backers Chat

hast utility to transform from parse5s AST.


What is this?

This package is a utility that can turn a parse5 tree into a hast tree.

When should I use this?

You can use this package when using parse5 as an HTML parser and wanting to work with hast.

The utility hast-util-to-parse5 does the inverse of this utility. It generates parse5s AST again.

The utility hast-util-from-html wraps this utility and parse5 to both parse HTML and generate hast from it.


This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with npm:

npm install hast-util-from-parse5

In Deno with esm.sh:

import {fromParse5} from "https://esm.sh/hast-util-from-parse5@7"

In browsers with esm.sh:

<script type="module">
  import {fromParse5} from "https://esm.sh/hast-util-from-parse5@7?bundle"


Say our document example.html contains:

<!doctype html><title>Hello!</title><h1 id="world">World!<!--after-->

…and our module example.js looks as follows:

import {parse} from 'parse5'
import {read} from 'to-vfile'
import {inspect} from 'unist-util-inspect'
import {fromParse5} from 'hast-util-from-parse5'

const file = await read('example.html')
const p5ast = parse(String(file), {sourceCodeLocationInfo: true})
const hast = fromParse5(p5ast, file)


…now running node example.js yields:

root[2] (1:1-2:1, 0-70)
│ data: {"quirksMode":false}
├─0 doctype<html> (1:1-1:16, 0-15)
│     public: null
│     system: null
└─1 element<html>[2]
    │ properties: {}
    ├─0 element<head>[1]
    │   │ properties: {}
    │   └─0 element<title>[1] (1:16-1:37, 15-36)
    │       │ properties: {}
    │       └─0 text "Hello!" (1:23-1:29, 22-28)
    └─1 element<body>[1]
        │ properties: {}
        └─0 element<h1>[3] (1:37-2:1, 36-70)
            │ properties: {"id":"world"}
            ├─0 text "World!" (1:52-1:58, 51-57)
            ├─1 comment "after" (1:58-1:70, 57-69)
            └─2 text "\n" (1:70-2:1, 69-70)


This package exports the identifier fromParse5. There is no default export.

fromParse5(ast[, file|options])

Transform from parse5s AST.


If options is a VFile, it’s treated as {file: options}.


Whether the root of the tree is in the HTML or SVG space (enum, 'svg' or 'html', default: 'html').

If an element in with the SVG namespace is found in ast, fromParse5 automatically switches to the SVG space when entering the element, and switches back when leaving.


VFile, used to add positional information to nodes. If given, the file should have the original HTML source as its contents.


Whether to add extra positional information about starting tags, closing tags, and attributes to elements (boolean, default: false). Note: not used without file.

For the following HTML:

<img src="http://example.com/fav.ico" alt="foo" title="bar">

The verbose info would looks as follows:

  type: 'element',
  tagName: 'img',
  properties: {src: 'http://example.com/fav.ico', alt: 'foo', title: 'bar'},
  children: [],
  data: {
    position: {
      opening: {
        start: {line: 1, column: 1, offset: 0},
        end: {line: 1, column: 61, offset: 60}
      closing: null,
      properties: {
        src: {
          start: {line: 1, column: 6, offset: 5},
          end: {line: 1, column: 38, offset: 37}
        alt: {
          start: {line: 1, column: 39, offset: 38},
          end: {line: 1, column: 48, offset: 47}
        title: {
          start: {line: 1, column: 49, offset: 48},
          end: {line: 1, column: 60, offset: 59}
  position: {
    start: {line: 1, column: 1, offset: 0},
    end: {line: 1, column: 61, offset: 60}


This package is fully typed with TypeScript. It exports the additional type Options.


Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.


Use of hast-util-from-parse5 can open you up to a cross-site scripting (XSS) attack if Parse5’s AST is unsafe.


See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.


MIT © Titus Wormer