-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTMLParser.php
More file actions
130 lines (104 loc) · 3.11 KB
/
Copy pathHTMLParser.php
File metadata and controls
130 lines (104 loc) · 3.11 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Diffyne\VirtualDOM;
use DOMComment;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMText;
/**
* Parser to convert HTML strings to Virtual DOM nodes.
*/
class HTMLParser
{
/**
* Parse HTML string to VNode tree.
*/
public function parse(string $html): VNode
{
// Wrap in a root element to handle fragments
$html = '<div>'.$html.'</div>';
$dom = new DOMDocument('1.0', 'UTF-8');
// Suppress warnings for malformed HTML
libxml_use_internal_errors(true);
$dom->loadHTML(
'<?xml encoding="UTF-8">'.$html,
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
);
libxml_clear_errors();
$root = $this->convertDOMNode($dom->documentElement);
// Unwrap the root div we added
if ($root->isElement() && $root->tag === 'div' && count($root->children) === 1) {
return $root->children[0];
}
return $root;
}
/**
* Convert a DOMNode to a VNode.
*/
protected function convertDOMNode(DOMNode $node): VNode
{
if ($node instanceof DOMText) {
return VNode::text($node->textContent);
}
if ($node instanceof DOMComment) {
return VNode::comment($node->textContent);
}
if ($node instanceof DOMElement) {
$attributes = $this->extractAttributes($node);
$children = $this->convertChildren($node);
$vnode = VNode::element($node->nodeName, $attributes, $children);
if (isset($attributes['diff:key'])) {
$vnode->key = $attributes['diff:key'];
} elseif (isset($attributes['key'])) {
$vnode->key = $attributes['key'];
}
return $vnode;
}
// Default to text node for unknown types
return VNode::text('');
}
/**
* Extract attributes from a DOM element.
*
* @return array<string, string>
*/
protected function extractAttributes(DOMElement $element): array
{
$attributes = [];
if ($element->hasAttributes()) {
/** @var \DOMAttr $attr */
foreach ($element->attributes as $attr) {
$attributes[$attr->name] = $attr->value;
}
}
return $attributes;
}
/**
* Convert child nodes to VNode array.
*
* @return array<int, VNode>
*/
protected function convertChildren(DOMElement $element): array
{
$children = [];
foreach ($element->childNodes as $child) {
$vnode = $this->convertDOMNode($child);
// Skip empty text nodes (whitespace only)
if ($vnode->isText() && trim($vnode->text) === '') {
continue;
}
$children[] = $vnode;
}
return $children;
}
/**
* Parse multiple HTML fragments.
*
* @param array<int, string> $htmlStrings
* @return array<int, VNode>
*/
public function parseFragments(array $htmlStrings): array
{
return array_map(fn ($html) => $this->parse($html), $htmlStrings);
}
}