infinity.html¶
Provides utilities for working with HTML content.
Module: infinity.html
parser¶
A sequential HTML parser. Each call of parse() reads the next part of the HTML code (tag, text content, comment, etc.) and provides its data in the cur... properties.
Properties:
Methods:
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 part by part
while (myParser.parse()) {
if (myParser.curPartType == infinity.html.parser.partType.startTag)
console.log('Tag: ' + myParser.curTagName + ', Attributes: ' + JSON.stringify(myParser.curAttributes));
else if (myParser.curPartType == infinity.html.parser.partType.content)
console.log('Content: ' + myParser.curContent);
}
//Tag: div, Attributes: [{"name":"data","value":"test"}]
//Content: Hello
//Tag: b, Attributes: []
//...
myParser.free();
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. Setting it resets the parse position.
normalize¶
Type: boolean
If set to true, whitespace is trimmed from text content and attribute values. Default: false.
strict¶
Type: boolean
If set to true (the default), parse() throws an error on malformed HTML (e.g. missing closing characters, quotes or closing script/style tags, or an invalid XML prolog). If set to false, the parser tolerates these errors.
code¶
Type: string
Returns the HTML code of the current part (e.g. <div class="a"> for a start tag), rebuilt from the parsed data.
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 text of the current part, if it is of the type content.
curPartType¶
Type: infinity.html.parser.partType
Contains the type of the current part. See partType for further details.
curPosition¶
Type: string
Indicates the current position of the parser within the HTML content in the format '(row:column)'.
curTagName¶
Type: string
Stores the name of the current HTML tag being parsed.
text¶
Type: string
Returns the plain text of the complete HTML content (tags removed, entities decoded, line breaks for br, p, tr and div tags). Reading this property parses the whole content and changes the parse position.
Methods
constructor()¶
Creates a new instance of the HTML parser.
Signature:
constructor( html?: string )
Parameters:
- html:
string, optionalInitial 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. The instance must not be used afterwards.
Signature:
free(): void
Example:
myParser.free();
loadFromFile()¶
Loads HTML content from a specified file into the parser.
Note: Up to version 3.10 this method was only registered under the name readString; since 3.11 it is available as loadFromFile, readString remains as an alias.
Signature:
loadFromFile( fileName: string, encoding?: infinity.encoding ): void
Parameters:
-
fileName:
stringFilename, 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, optionalSpecifies the character encoding of the file. Default: UTF-8.
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.streamThe stream from which to read the HTML content. See infinity.stream
-
encoding:
infinity.encoding, optionalSpecifies the character encoding of the stream content. Default: UTF-8.
-
fromBeginning:
boolean, optionalIf
true(the default), reading starts from the beginning of the stream, otherwise from the current position.
Example:
myParser.loadFromStream(stream);
parse()¶
Parses the next part of the loaded HTML content and updates the cur... properties. Returns false if the end of the content has been reached.
Signature:
parse(): boolean
Return type: boolean
Example:
myParser.parse();
decode()¶
Decodes HTML entities in a string.
Signature:
decode( html: string ): string
Parameters:
- html:
stringThe string containing HTML entities to decode.
Return type: string
Example:
infinity.loadModule('infinity.html');
let decodedHtml = infinity.html.decode('<div>Hello World</div>');
encode()¶
Encodes the special characters &, <, >, " and ' in a string into HTML entities.
Signature:
encode( html: string ): string
Parameters:
- html:
stringThe string to encode.
Return type: string
Example:
infinity.loadModule('infinity.html');
let encodedHtml = infinity.html.encode('<div>Hello World</div>');
toMarkdown()¶
Converts HTML content to Markdown text.
Signature:
toMarkdown( html: string ): string
Parameters:
- html:
stringThe HTML content to convert.
Return type: string
Example:
infinity.loadModule('infinity.html');
let markdown = infinity.html.toMarkdown('<h1>Title</h1><p>Hello <a href="https://example.com">World</a></p>');
toStructuredText()¶
Converts HTML content to plain text while keeping the text structure (paragraphs, line breaks, lists, quotes, tables and links).
Signature:
toStructuredText( html: string ): string
Parameters:
- html:
stringThe HTML content to convert.
Return type: string
Example:
infinity.loadModule('infinity.html');
let text = infinity.html.toStructuredText('<ul><li>One</li><li>Two</li></ul>');
toText()¶
Converts HTML content to plain text, stripping out HTML tags and decoding entities. Line breaks are inserted for br, p, tr and div tags. Malformed HTML is tolerated.
Signature:
toText( html: string ): string
Parameters:
- html:
stringThe 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>');
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.
infinity.html.parser.partType¶
Values:
-
emptyTag:
0Represents a self-closing tag in HTML, such as
imgorbr. -
startTag:
1Indicates an opening HTML tag.
-
endTag:
2Stands for a closing HTML tag.
-
content:
3Represents the textual content within HTML tags.
-
comment:
4Indicates an HTML comment.
-
dtd:
5Denotes the Document Type Declaration in an HTML document.
-
xmlProlog:
6Represents the XML prolog in HTML documents, often used in XHTML.
-
script:
7Indicates a script tag and its content, e.g.,