-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTreeBuilder.php
More file actions
98 lines (81 loc) · 2.89 KB
/
TreeBuilder.php
File metadata and controls
98 lines (81 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace Rowbot\DOM\Parser\HTML;
use Rowbot\DOM\Element\Element;
use Rowbot\DOM\Namespaces;
use Rowbot\DOM\Parser\HTML\InsertionMode\InForeignContentInsertionMode;
use Rowbot\DOM\Parser\Token\CharacterToken;
use Rowbot\DOM\Parser\Token\EOFToken;
use Rowbot\DOM\Parser\Token\StartTagToken;
use Rowbot\DOM\Parser\Token\Token;
class TreeBuilder
{
use IntegrationPointTrait;
/**
* @var \Rowbot\DOM\Parser\HTML\TreeBuilderContext
*/
private TreeBuilderContext $context;
public function __construct(TreeBuilderContext $context)
{
$this->context = $context;
}
/**
* @see https://html.spec.whatwg.org/multipage/syntax.html#tree-construction-dispatcher
*/
public function run(Token $token): void
{
do {
if ($this->context->parser->openElements->isEmpty()) {
break;
}
$adjustedCurrentNode = $this->context->parser->getAdjustedCurrentNode();
if (
$adjustedCurrentNode instanceof Element
&& $adjustedCurrentNode->namespaceURI === Namespaces::HTML
) {
break;
}
if ($this->isMathMLTextIntegrationPoint($adjustedCurrentNode)) {
if (
(
$token instanceof StartTagToken
&& $token->tagName !== 'mglyph'
&& $token->tagName !== 'malignmark'
)
|| $token instanceof CharacterToken
) {
break;
}
}
if (
$adjustedCurrentNode instanceof Element
&& $adjustedCurrentNode->namespaceURI === Namespaces::MATHML
&& $adjustedCurrentNode->localName === 'annotation-xml'
&& $token instanceof StartTagToken
&& $token->tagName === 'svg'
) {
break;
}
if (
$this->isHTMLIntegrationPoint($adjustedCurrentNode, $this->context->elementTokenMap)
&& ($token instanceof StartTagToken || $token instanceof CharacterToken)
) {
break;
}
if ($token instanceof EOFToken) {
break;
}
// Process the token according to the rules given in the section for parsing tokens in
// foreign content.
(new InForeignContentInsertionMode())->processToken($this->context, $token);
return;
} while (false);
// Process the token according to the rules given in the section corresponding to the
// current insertion mode in HTML content.
$this->context->insertionMode->processToken($this->context, $token);
}
public function getContext(): TreeBuilderContext
{
return $this->context;
}
}