Skip to main content

Class: HTMLParser

@divine/uri-x4e-parser.HTMLParser

The text/html parser uses parse5 to convert HTML documents to and from X4E XML objects.

Hierarchy

  • Parser

    HTMLParser

Constructors

constructor

Protected new HTMLParser(contentType)

Constructs a new Parser instance.

Parameters

NameTypeDescription
contentTypeContentTypeThe media type this parser object was instanciated for.

Inherited from

Parser.constructor

Defined in

uri/build/src/parsers.d.ts:126

Properties

contentType

Readonly contentType: ContentType

Inherited from

Parser.contentType

Defined in

uri/build/src/parsers.d.ts:64

Methods

_assertSerializebleData

Protected _assertSerializebleData(condition, data, cause?): asserts condition

A helper method used by parser subclasses to report invalid input.

Parameters

NameTypeDescription
conditionbooleanMust be true, or else a ParserError will be raised.
dataunknownSome kind of extra information that will be provided in ParserError.data.
cause?unknownIf this error was caused by some other kind of failure, the original error will be available as ParserError.cause.

Returns

asserts condition

Inherited from

Parser._assertSerializebleData

Defined in

uri/build/src/parsers.d.ts:155


parse

parse(stream): Promise<XML<Element>>

Parameters

NameType
streamAsyncIterable<Buffer>

Returns

Promise<XML<Element>>

Overrides

Parser.parse

Defined in

uri-x4e-parser/src/html-parser.ts:9


serialize

serialize(data): Buffer

Parameters

NameType
dataNode | XML<Node>

Returns

Buffer

Overrides

Parser.serialize

Defined in

uri-x4e-parser/src/html-parser.ts:13


parse

Static parse<T>(stream, contentType): Promise<T & Finalizable>

Parses a given string, Buffer or byte stream using a parser registered for a specific media type.

NOTE: This method always returns an object. Primitives are never returned. This means that text, for instance will be returned as a String object, null as Object(NULL) and undefined as Object(VOID). You may use toPrimitive to return the original value, or use .valueOf() and test the result against the NULL and VOID symbols.

Throws

ParserError On parser errors or if the media type is not recognized.

Type parameters

NameTypeDescription
Textends objectThe type of the returned object.

Parameters

NameTypeDescription
streamstring | Buffer | AsyncIterable<string | Buffer>The source that should be parsed.
contentTypestring | ContentTypeThe media type that specifies what parser to use.

Returns

Promise<T & Finalizable>

An object (always an object) that represents the original source after parsing. It's possible that the Parser subclass allocated temporary resources as part of the process. These resources may be cleaned up by calling FINALIZE.

Inherited from

Parser.parse

Defined in

uri/build/src/parsers.d.ts:89


register

Static register(type, parser): typeof Parser

Registers a new parser/serializer. All subclasses must register their MIME media type support with this method.

Parameters

NameTypeDescription
typestring | RegExpThe content/media type the parser can handle.
parsertypeof ParserThe Parser subclass to register.

Returns

typeof Parser

The Parser base class (for method chaining).

Inherited from

Parser.register

Defined in

uri/build/src/parsers.d.ts:72


serialize

Static serialize<T>(data, contentType?): [Buffer | Readable & AsyncIterable<Buffer>, ContentType]

Converts a parsed (or manually constructed) object back into a byte stream representation.

Buffers and ReadableStream will be passed through as-is. Strings will just be encoded using the charset param from contentType (or UTF-8 if not present). Everything else is serialized using a Parser subclass.

Throws

ParserError On serialization errors or if the media type is not recognized.

Type parameters

NameTypeDescription
TunknownThe type of the object that is to be serialized.

Parameters

NameTypeDescription
dataTThe object that is to be serialized.
contentType?string | ContentTypeThe media type that specifies what parser to use.

Returns

[Buffer | Readable & AsyncIterable<Buffer>, ContentType]

A tuple containing the Buffer/byte stream and the actual media type. Note that the parser may return a slightly different media type than was given (for instance, MultiPartParser might add a boundary param if none was given).

Inherited from

Parser.serialize

Defined in

uri/build/src/parsers.d.ts:104


serializeToBuffer

Static serializeToBuffer<T>(data, contentType?): Promise<[Buffer, ContentType]>

Converts a parsed (or manually constructed) object into a Buffer.

This is a convenience method that just invokes parse and then converts the byte stream into a single Buffer.

Throws

ParserError On serialization errors or if the media type is not recognized.

Type parameters

NameType
Tunknown

Parameters

NameTypeDescription
dataTThe object that is to be serialized.
contentType?string | ContentTypeThe media type that specifies what parser to use.

Returns

Promise<[Buffer, ContentType]>

A tuple containing the Buffer and the actual media type. Note that the parser may return a slightly different media type than was given (for instance, MultiPartParser might add a boundary param if none was given).

Inherited from

Parser.serializeToBuffer

Defined in

uri/build/src/parsers.d.ts:118