Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

111 lines (67 sloc) 4.62 KB

parse5

NOTE: By default all functions operate with tree format produced by the default tree adapter. Tree format can be changed by providing custom tree adapter implementation.

Functions

parse

parse(html: string, options?: ParserOptions): Document

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:

Param Type Description
html string Input HTML string.
Optional options ParserOptions Parsing options.

Returns: Document


parseFragment

parseFragment(fragmentContext: Element, html: string, options?: ParserOptions): DocumentFragment

parseFragment(html: string, options?: ParserOptions): DocumentFragment

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:

Param Type Description
Optional 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


serialize

serialize(node: Node, options?: SerializerOptions): string

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:

Param Type Description
node Node Node to serialize.
Optional options SerializerOptions Serialization options.

Returns: string


You can’t perform that action at this time.