Function parseFragment

  • Parses an HTML fragment.

    Type Parameters

    Parameters

    • fragmentContext: null | T["parentNode"]

      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.

    • options: ParserOptions<T>

      Parsing options.

    Returns T["documentFragment"]

    DocumentFragment

    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 = parse5.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');

    console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'
  • Type Parameters

    Parameters

    Returns T["documentFragment"]