❀Nocturnal Parakeet Monitor
    Wondering what’s next for npm?Check out our public roadmap! Β»

    hast-util-from-parse5
    TypeScript icon, indicating that this package has built-in type declarations

    7.0.0Β β€’Β PublicΒ β€’Β Published

    hast-util-from-parse5

    Build Coverage Downloads Size Sponsors Backers Chat

    hast utility to transform Parse5’s AST to a hast tree.

    Install

    This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

    npm:

    npm install hast-util-from-parse5

    Use

    Say we have the following file, example.html:

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

    And our script, example.js, looks as follows:

    import vfile from 'to-vfile'
    import parse5 from 'parse5'
    import {inspect} from 'unist-util-inspect'
    import {fromParse5} from 'hast-util-from-parse5'
    
    var file = vfile.readSync('example.html')
    var p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true})
    var hast = fromParse5(p5ast, file)
    
    console.log(inspect(hast))

    Now, running node example 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)
    

    API

    This package exports the following identifiers: fromParse5. There is no default export.

    fromParse5(ast[, file|options])

    Transform Parse5’s AST to a hast tree.

    options

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

    options.space

    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.

    options.file

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

    options.verbose

    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}
      }
    }

    Security

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

    Related

    Contribute

    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.

    License

    MIT Β© Titus Wormer

    Install

    npm i hast-util-from-parse5

    DownloadsWeekly Downloads

    2,029,392

    Version

    7.0.0

    License

    MIT

    Unpacked Size

    21 kB

    Total Files

    7

    Last publish

    Collaborators

    • avatar
    • avatar