API / Minn\Html\Tree / Builder

Builder final class

Minn\Html\Tree\Builder · public/minn/src/Minn/Html/Tree/Builder.php · 551 lines

The HTML tree construction algorithm over the engine's tokenizer, as the reference's HTML processor runs it: tokens go through the insertion modes (HeadRules, BodyRules, TableRules) or the foreign content rules, and every node opened or closed becomes an Event with its breadcrumbs. Text is read one kind at a time (NUL bytes, white space, the rest). At the end of the input every open element closes; nothing implied is added. Where the reference does not build a tree (Unsupported), the walk stops with that error.

Constants and properties

constFRAGMENT = 'fragment'
constDOCUMENT = 'document'
privateMinn\Html\Tree\OpenElements $stack
privateMinn\Html\Tree\Formatting $formatting
privatestring $mode
privatearray $templateModes
private?Minn\Html\Tree\Node $headPointer
private?Minn\Html\Tree\Node $formPointer
privatebool $framesetOk
privatestring $compat
privatearray $queue
privatearray $pending
private?Minn\Html\Tree\Token $token
privatebool $done
privatebool $skipNext
privatebool $skipping
private?Minn\Html\Tree\Unsupported $error
privateMinn\Html\Tree\Node $context
privatereadonly Minn\Html\Tags $tags

Promoted from the constructor.

privatereadonly string $kind

Promoted from the constructor.

Public

new__construct(Minn\Html\Tags $tags, string $kind)#
methodreset(): void#

Back to the state before the first token.

methodnext(): ?Minn\Html\Tree\Event#

The next opened or closed node, or null at the end, at an incomplete token, or once unsupported markup stopped the walk.

methodhalt(): void#

Stops the walk for good (a failed seek leaves the processor like this).

methoderror(): ?Minn\Html\Tree\Unsupported#

Why the walk stopped early, if it did.

methodbreadcrumbs(): array#

The breadcrumbs of the open elements now.

@return list<string>
methodtoken(): Minn\Html\Tree\Token#

The token being processed.

methodstack(): Minn\Html\Tree\OpenElements#

The stack of open elements.

methodformatting(): Minn\Html\Tree\Formatting#

The list of active formatting elements.

methodisFragment(): bool#

Whether this builder parses a fragment (in a BODY context) rather than a document.

methodmode(): string#

The insertion mode.

methodswitchTo(string $mode): void#

Switches the insertion mode.

methodreprocessIn(string $mode): void#

Switches the insertion mode and processes the current token again under it.

methodprocess(Minn\Html\Tree\Token $token): void#

Processes a token under the current insertion mode, or the foreign content rules when they apply.

methodprocessIn(string $mode, Minn\Html\Tree\Token $token): void#

Processes a token using the rules of an insertion mode other than the current one.

methodframesetOk(): bool#

Whether the frameset-ok flag is still set.

methodframesetNotOk(): void#

Clears the frameset-ok flag.

methodcompat(): string#

The document's compatibility mode ("no-quirks", "limited-quirks", "quirks").

methodsetCompat(string $compat): void#

Sets the compatibility mode the doctype indicated.

methodheadPointer(): ?Minn\Html\Tree\Node#

The head element pointer.

methodformPointer(): ?Minn\Html\Tree\Node#

The form element pointer.

methodsetFormPointer(?Minn\Html\Tree\Node $node): void#

Sets (or clears) the form element pointer.

methodtemplateModes(): array#

The stack of template insertion modes.

@return list<string>
methodpushTemplateMode(string $mode): void#

Pushes a template insertion mode.

methodpopTemplateMode(): void#

Pops the current template insertion mode.

methodadjustedCurrent(): ?Minn\Html\Tree\Node#

The adjusted current node: the context element when only the fragment's root is open.

methodinsert(string $namespace = 'html'): Minn\Html\Tree\Node#

Inserts an element for the current start tag, real, in the namespace.

methodinsertImplied(string $name): Minn\Html\Tree\Node#

Inserts an element the markup implied.

methodinsertLeafElement(string $namespace = 'html'): Minn\Html\Tree\Node#

Inserts and at once closes an element for the current start tag: a void element, one read whole, or self-closed foreign content.

methodinsertLeaf(string $namespace = 'html'): void#

Inserts a leaf (text, comment, CDATA, processing instruction) for the current token, in the namespace.

methodinsertDocumentLeaf(): void#

Inserts a leaf at the top of the document (a doctype or a comment before or around the html element).

methodpop(): ?Minn\Html\Tree\Node#

Closes the current node as implied.

methodpopUntil(string ...$names): void#

Pops until an HTML element of one of the names closes; when the current end tag names it, that last close is real.

methodpopUntilNode(Minn\Html\Tree\Node $target): void#

Pops until this very node closes.

methodgenerateImpliedEndTags(string $except = ''): void#

Generates implied end tags, leaving an element of the name open.

methodgenerateAllImpliedEndTags(): void#

Generates all implied end tags thoroughly (table parts as well).

methodcloseP(): void#

Closes a P element: implied end tags but P's, then pops through the P.

methodclosePInButtonScope(): void#

Closes a P element when one is in button scope.

methodreconstructFormatting(): void#

Reconstructs the active formatting elements; the reference stops when any would have to be reopened.

methodskipNextNewline(): void#

Asks the parser to drop one leading line feed from the next token (after PRE, LISTING).

methodresetInsertionMode(): void#

Resets the insertion mode from the stack of open elements.

methodunsupported(string $message): void#

Stops: the reference's processor does not build a tree for this markup.

methodattribute(string $name): string|true|null#

The current tag's attribute, decoded; null when absent.

methoddoctype(): array#

The doctype's name and identifiers, for the compatibility mode. @return array{name: ?string, public: ?string, system: ?string}

@return array{name: ?string, public: ?string, system: ?string}

Internals 9

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privatestep(): void#
privateread(): array#
@return list<Token> the next token from the tokenizer (text in runs), an end-of-input token, or nothing when paused
privatetextRuns(): array#
@return list<Token>
privatetextValue(Minn\Html\Tree\Token $token, string $namespace): string#
privateusesHtmlRules(Minn\Html\Tree\Token $token): bool#
privatefinish(): void#

Closes everything still open at the end of the input; nothing implied is added.

privatepushNode(Minn\Html\Tree\Node $node, ?int $offset): void#
privateevent(string $op, Minn\Html\Tree\Node $node, ?int $offset): void#
privatekeptAttributes(string $name, string $namespace): array#

The attributes an element keeps for later comparison: all of a formatting element's (Noah's Ark), and encoding on MathML annotation-xml.

@return array<string, string|true>