Skip to content

infinity.html

Provides utilities for working with HTML content.

Module: infinity.html

Classes:
Functions:
Interfaces:
Enums:

Classes

parser

Example:

infinity.loadModule('infinity.html');
let myParser = new infinity.html.parser();

// Load HTML content
myParser.html = "<div data='test'>Hello <b>world</b>!</div>";

// Parse the HTML
myParser.parse();

// Access parsed data
let tag = myParser.curTagName;
let attributes = myParser.curAttributes;
console.log(`Current Tag: ${tag}, Attributes: ${JSON.stringify(attributes)}`);
//Current Tag: div, Attributes: [{"name":"data","value":"test"}]

Properties


html

Type: string

Stores the HTML content to be parsed. This property should be set with the HTML string that needs to be processed before calling the parse() method.


normalize

Type: boolean

Specifies whether the parser should normalize the HTML content. When set to true, the parser will standardize the HTML by correcting common errors and inconsistencies, making the parsing process more reliable.


strict

Type: booleanreadonlytext: string

Indicates whether the parser should operate in strict mode. In strict mode, the parser enforces more stringent HTML standards compliance, which can be useful for ensuring the quality and correctness of the HTML content.


code

Type: string

Represents the entire HTML code currently loaded in the parser. This property is read-only and provides access to the original HTML content.


curAttributes

Type: infinity.html.parser.attributesArray

Holds the attributes of the current HTML element being parsed. This read-only property provides an array of attribute names and values for the current element.


curContent

Type: string

Contains the inner content of the current HTML element being parsed.


curPartType

Type: infinity.html.parser.partType

Contains the inner content of the current HTML element being parsed. See partType for further details.


curPosition

Type: string

Indicates the current position of the parser within the HTML content.


curTagName

Type: string

Stores the name of the current HTML tag being parsed.


Methods


constructor()

Creates a new instance of the HTML parser.

Signature:

constructor( html?: string )

Parameters:

  • html: string, optional

    Initial HTML content to load into the parser.

Example:

infinity.loadModule('infinity.html');
let myParser = new infinity.html.parser('<div>Hello World</div>');

clear()

Resets the parser, clearing any loaded HTML content and resetting all internal states.

Signature:

clear(): void

Example:

myParser.clear();

free()

Frees up resources used by the parser. This method should be called to properly dispose of the parser when it's no longer needed.

Signature:

free(): void

Example:

myParser.free();

loadFromFile()

Loads HTML content from a specified file into the parser.

Signature:

loadFromFile( fileName: string, encoding?: infinity.encoding ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to examine.

  • encoding: infinity.encoding, optional

    Specifies the character encoding of the file.

Example:

myParser.loadFromFile('path/to/file.html');

loadFromStream()

Loads HTML content from a provided stream into the parser.

Signature:

loadFromStream( stream: infinity.stream, encoding?: infinity.encoding, fromBeginning?: boolean ): void

Parameters:

  • stream: infinity.stream

    The stream from which to read the HTML content. See infinity.stream

  • encoding: infinity.encoding, optional

    Specifies the character encoding of the stream content.

  • fromBeginning: boolean, optional

    If true, reading starts from the beginning of the stream.

Example:

myParser.loadFromStream(stream);

parse()

Parses the loaded HTML content.

Signature:

parse(): boolean

Return type: boolean

Example:

myParser.parse();

Functions

decode()

Decodes HTML entities in a string.

Signature:

decode( html: string ): string

Parameters:

  • html: string

    The string containing HTML entities to decode.

Return type: string

Example:

infinity.loadModule('infinity.html');
let decodedHtml = infinity.html.decode('&lt;div&gt;Hello World&lt;/div&gt;');

encode()

Encodes special characters in a string into HTML entities.

Signature:

encode( html: string ): string

Parameters:

  • html: string

    The string to encode.

Return type: string

Example:

infinity.loadModule('infinity.html');
let encodedHtml = infinity.html.encode('<div>Hello World</div>');

toText()

Converts HTML content to plain text, stripping out HTML tags and entities.

Signature:

toText( html: string ): string

Parameters:

  • html: string

    The HTML content to convert to plain text.

Return type: string

Example:

infinity.loadModule('infinity.html');
let plainText = infinity.html.toText('<div>Hello <b>World</b></div>');

Interfaces

attributesArray

Extends: Array<{name: string, value: string}>

An array structure used to store and manage HTML tag attributes. Each element of the array is an object representing a single attribute, with name and value properties.

Properties:

  • name

    Type: string. The name of the attribute.

  • value

    Type: string. The value assigned to the attribute.


Enums

infinity.html.parser.partType

Values:

  • emptyTag: 0

    Represents a self-closing tag in HTML, such as img or br.

  • startTag: 1

    Indicates an opening HTML tag.

  • endTag: 2

    Stands for a closing HTML tag.

  • content: 3

    Represents the textual content within HTML tags.

  • comment: 4

    Indicates an HTML comment.

  • dtd: 5

    Denotes the Document Type Declaration in an HTML document.

  • xmlProlog: 6

    Represents the XML prolog in HTML documents, often used in XHTML.

  • script: 7

    Indicates a script tag and its content, e.g.,