HtmlLoader

The HtmlLoader class in UssElement is designed to parse HTML strings into a structure compatible with UssElement's node-based architecture. This utility provides a seamless way to convert raw HTML into a manipulatable set of nodes, aligning with the UssElement framework's principles.

Constructor

public HtmlLoader::__construct(string $html5, bool $preserveWhiteSpaces = false)

Parameters

html5

The raw HTML string to be parsed.

preserveWhiteSpaces

Determines whether whitespace should be preserved when parsing. By default, this is set to false.


Core Functionality

The primary role of the HtmlLoader is to parse an HTML string and convert it into nodes compatible with UssElement's classes, such as ElementNode, TextNode, and DocumentTypeNode. The parsed nodes are then stored in a NodeList object for easy access and manipulation.

Example

$html = <<<HTML
<!doctype html>
<html>
    <head>...</head>
    <body>...</body>
</html>
HTML;

$loader = new HtmlLoader($html);
$nodeList = $loader->getNodeList();

// Iterate through nodes
foreach ($nodeList as $node) {
    echo $node->getNodeName(); // Outputs: "#DOCTYPE", "HTML"
}

This demonstrates how the HtmlLoader simplifies parsing and provides an intuitive interface for interacting with parsed HTML nodes.

The HtmlLoader does not mandate that the input HTML string starts with a doctype declaration. Any valid HTML structure, even without the <!DOCTYPE> declaration, can be parsed.


Methods

Source Code
If you find this project useful, consider leaving a on GitHub! Thank you!