Core\Html::parse
Core\Html::parse(string $document): Core\Xml\NodeCore\Html::parse( string $document, ): Core\Xml\Node
Reads a text of HTML and gives you its tree of elements, the same tree a web browser builds.
Core\Html::parse follows the parsing rules that every browser follows. It never throws an error. HTML that is broken or incomplete is also parsed, and the rules say what the result is: a missing </p> or </li> tag is added, and a missing html, head or body element is added too.
The result is a Core\Xml\Node for the whole document. You read it with the same methods as a tree from Core\Xml::parse: kind(), name(), attributes(), children(), text() and source().
Good to know: the text in the tree is tainted, because it comes from outside your program. Escape it before you write it into a page.
The examples show the tags that parsing adds, the items of a list, and every link on a page.
Parameters
$documentstringThe document text. Any text is a valid document, so this method never throws an error.
Return value
Returns Core\Xml\Node. The Core\Xml\Node of kind Document. Its children are the top level of the parsed page. For any input that is not empty, this is one html element with head and body in it. That is true even when the text does not contain these tags. Missing tags, wrongly nested tags and stray content go exactly where a browser puts them. So the tree is the page a browser would show. A <!DOCTYPE …> gives no node, because the tree has no kind for it. Names are lower case in HTML. In SVG and MathML they get the correct case, as the algorithm says. Elements nested deeper than 1000 levels are placed at level 1000, so walking the tree can never run out of stack.
Examples
The tags a fragment leaves out
<?nvs// Shows that `Core\Html::parse` adds the tags that a short piece of HTML leaves out.
// This text has no `html`, `head` or `body` tag, and the `p` tag is never closed.var $tree = Core\Html::parse('<p>Hello, <b>world</b>');
// `source()` writes the whole tree back as text. The missing tags are now there.echo $tree->source(), "\n";<html><head></head><body><p>Hello, <b>world</b></p></body></html>List items that are not closed
<?nvs// Shows how to read the items of a list, even when the `</li>` tags are missing.
class Items{ // Returns the text of every `li` element under `$node`. public static function of(Core\Xml\Node $node): array<tainted string> { array<tainted string> $found = []; foreach ($node->children() as Core\Xml\Node $child) { if ($child->kind() == Core\Xml\Node::Kind::Element && $child->name() == 'li') { // The text of an element is in its children, so this reads the first child. foreach ($child->children() as Core\Xml\Node $text) { $found[] = $text->text(); break; } } foreach (Items::of($child) as tainted string $item) { $found[] = $item; } } return $found; }}
// No item is closed. Each new `<li>` ends the item before it.var $tree = Core\Html::parse('<ul><li>Apples<li>Pears<li>Plums</ul>');
// This prints the three items, one on each line.foreach (Items::of($tree) as tainted string $item) { echo $item, "\n";}ApplesPearsPlumsEvery link on a page
<?nvs// Shows how to list every link on a web page, as a link checker does.
class Links{ // Prints the `href` of every `a` element under `$node`, and the text of the link. public static function show(Core\Xml\Node $node): void { foreach ($node->children() as Core\Xml\Node $child) { if ($child->kind() != Core\Xml\Node::Kind::Element) { continue; } array<tainted string> $attributes = $child->attributes(); if ($child->name() == 'a' && Core\Arr::hasKey($attributes, 'href')) { echo $attributes['href'], ' (', Links::text($child), ")\n"; } Links::show($child); } }
// Returns all the text under `$node`, with the tags left out. public static function text(Core\Xml\Node $node): tainted string { tainted string $text = $node->text(); foreach ($node->children() as Core\Xml\Node $child) { $text = $text . Links::text($child); } return $text; }}
// A page as a server sends it. The second link has no `href`, so it is not printed.string $page = <<<HTML<!DOCTYPE html><title>Shop</title><nav><a href="/">Home</a> <a name="top">Top</a></nav><p>See our <a href="/offers"><b>new</b> offers</a> and <a href=/help>help pages</a>.HTML;
Links::show(Core\Html::parse($page));/ (Home)/offers (new offers)/help (help pages)