Options
All
  • Public
  • Public/Protected
  • All
Menu

parse5

Index

Variables

treeAdapters

treeAdapters: object

Provides built-in tree adapters that can be used for parsing and serialization.

example

const parse5 = require('parse5');

// Uses the default tree adapter for parsing.
const document = parse5.parse('<div></div>', {
    treeAdapter: parse5.treeAdapters.default
});

// Uses the htmlparser2 tree adapter with the SerializerStream.
const serializer = new parse5.SerializerStream(node, {
    treeAdapter: parse5.treeAdapters.htmlparser2
});

Type declaration

Functions

parse

  • Parses an HTML string.

    example
    
    const parse5 = require('parse5');
    
    const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
    
    console.log(document.childNodes[1].tagName); //> 'html'
    

    Parameters

    • html: string

      Input HTML string.

    • Optional options: ParserOptions

      Parsing options.

    Returns Document

parseFragment

  • Parses an HTML fragment.

    example
    
    const parse5 = require('parse5');
    
    const documentFragment = parse5.parseFragment('<table></table>');
    
    console.log(documentFragment.childNodes[0].tagName); //> 'table'
    
    // Parses the html fragment in the context of the parsed <table> element.
    const trFragment = parser.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
    
    console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'
    

    Parameters

    • fragmentContext: Element

      Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's innerHTML property.

    • html: string

      Input HTML fragment string.

    • Optional options: ParserOptions

      Parsing options.

    Returns DocumentFragment

  • Parameters

    Returns DocumentFragment

serialize

  • Serializes an AST node to an HTML string.

    example
    
    const parse5 = require('parse5');
    
    const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
    
    // Serializes a document.
    const html = parse5.serialize(document);
    
    // Serializes the <html> element content.
    const str = parse5.serialize(document.childNodes[1]);
    
    console.log(str); //> '<head></head><body>Hi there!</body>'
    

    Parameters

    Returns string

Generated using TypeDoc