-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetHtmlParserTest.php
More file actions
258 lines (214 loc) · 9.67 KB
/
SubsetHtmlParserTest.php
File metadata and controls
258 lines (214 loc) · 9.67 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
// SPDX-FileCopyrightText: 2026 LibreSign
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreSign\XObjectTemplate\Tests\Unit\Html;
use DOMDocument;
use LibreSign\XObjectTemplate\Exception\UnsupportedSubsetException;
use LibreSign\XObjectTemplate\Html\SubsetHtmlParser;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
final class SubsetHtmlParserTest extends TestCase
{
/**
* @return iterable<string, array{0: string, 1: string}>
*/
public static function unsupportedTagProvider(): iterable
{
yield 'table is outside the supported subset' => [
'<table><tr><td>x</td></tr></table>',
'Tag <table> is not supported.',
];
yield 'unordered lists are outside the supported subset' => [
'<ul><li>x</li></ul>',
'Tag <ul> is not supported.',
];
yield 'semantic strong tags are outside the supported subset' => [
'<strong>x</strong>',
'Tag <strong> is not supported.',
];
}
/**
* @return iterable<string, array{0: string, 1: string, 2: string, 3: string, 4: list<string>}>
*/
public static function inheritableStyleProvider(): iterable
{
yield 'layout-only styles stay on parent while text styles inherit' => [
'<div style="width:58%;height:100%;padding:18 24;font-size:20;color:#123456">'
. '<div style="font-weight:700">Title</div>'
. '</div>',
'width:58%;height:100%;padding:18 24;font-size:20;color:#123456',
'font-size:20;color:#123456;font-weight:700',
'font-size:20;color:#123456;font-weight:700',
['width:58%', 'height:100%', 'padding:18 24'],
];
yield 'text alignment white-space and color inherit together' => [
'<div style="text-align:right;white-space:nowrap;color:#222222;font-size:11">'
. '<span>Aligned</span>'
. '</div>',
'text-align:right;white-space:nowrap;color:#222222;font-size:11',
'text-align:right;white-space:nowrap;color:#222222;font-size:11',
'text-align:right;white-space:nowrap;color:#222222;font-size:11',
[],
];
yield 'malformed declarations preserve last inheritable values and colon values' => [
'<div style=" ; COLOR : #fff ; broken ; font-family : Times:Bold ; invalid: ; '
. 'white-space : nowrap ; hyphens : auto ; color : #abc ; line-height : 12 ; ">'
. '<span style="font-weight:bold">Hello</span>'
. '</div>',
'; COLOR : #fff ; broken ; font-family : Times:Bold ; invalid: ; white-space : nowrap ; '
. 'hyphens : auto ; color : #abc ; line-height : 12 ;',
'color:#abc;font-family:Times:Bold;white-space:nowrap;hyphens:auto;line-height:12;font-weight:bold',
'color:#abc;font-family:Times:Bold;white-space:nowrap;hyphens:auto;line-height:12;font-weight:bold',
[],
];
}
#[DataProvider('unsupportedTagProvider')]
public function testUnsupportedTagThrowsException(string $html, string $expectedMessage): void
{
$parser = new SubsetHtmlParser();
$this->expectException(UnsupportedSubsetException::class);
$this->expectExceptionMessage($expectedMessage);
$parser->parse($html);
}
public function testParseNormalizesAttributesAndTrimsTextNodes(): void
{
$parser = new SubsetHtmlParser();
$nodes = $parser->parse('<span STYLE=" color:#fff "> Hello </span>');
self::assertCount(1, $nodes);
self::assertSame('span', $nodes[0]->tag);
self::assertSame('color:#fff', $nodes[0]->attributes['style']);
self::assertSame('Hello', $nodes[0]->children[0]->text);
}
public function testParseMergesInheritedStylesAndKeepsAllowedTags(): void
{
$parser = new SubsetHtmlParser();
$nodes = $parser->parse(
'<div style="font-size:10; margin:2">'
. '<span style="font-weight:bold">Hello</span>'
. '<br />'
. 'World'
. '</div>',
);
self::assertCount(1, $nodes);
self::assertSame('div', $nodes[0]->tag);
self::assertSame('font-size:10; margin:2', $nodes[0]->attributes['style']);
self::assertCount(3, $nodes[0]->children);
self::assertSame('span', $nodes[0]->children[0]->tag);
self::assertSame('Hello', $nodes[0]->children[0]->children[0]->text);
self::assertSame('font-size:10;font-weight:bold', $nodes[0]->children[0]->attributes['style']);
self::assertSame(
'font-size:10;font-weight:bold',
$nodes[0]->children[0]->children[0]->attributes['style'],
);
self::assertSame('br', $nodes[0]->children[1]->tag);
self::assertSame('World', $nodes[0]->children[2]->text);
self::assertSame('font-size:10; margin:2', $nodes[0]->children[2]->attributes['style']);
}
#[DataProvider('inheritableStyleProvider')]
public function testParseOnlyInheritsTextualStylesToDescendants(
string $html,
string $expectedRootStyle,
string $expectedChildStyle,
string $expectedTextStyle,
array $excludedFragments,
): void {
$parser = new SubsetHtmlParser();
$nodes = $parser->parse($html);
self::assertSame($expectedRootStyle, $nodes[0]->attributes['style']);
self::assertSame($expectedChildStyle, $nodes[0]->children[0]->attributes['style']);
self::assertSame($expectedTextStyle, $nodes[0]->children[0]->children[0]->attributes['style']);
foreach ($excludedFragments as $excludedFragment) {
self::assertStringNotContainsString($excludedFragment, $nodes[0]->children[0]->attributes['style']);
}
}
public function testParseNormalizesTagAndAttributeNamesAndKeepsAllAttributes(): void
{
$parser = new SubsetHtmlParser();
$nodes = $parser->parse('<DIV STYLE="font-size:10" DATA-ID=" 42 ">Hi</DIV>');
self::assertCount(1, $nodes);
self::assertSame('div', $nodes[0]->tag);
self::assertArrayHasKey('style', $nodes[0]->attributes);
self::assertArrayHasKey('data-id', $nodes[0]->attributes);
self::assertSame('font-size:10', $nodes[0]->attributes['style']);
self::assertSame('42', $nodes[0]->attributes['data-id']);
}
public function testParseDoesNotKeepEmptyStyleAttributeWhenNoStyleIsResolved(): void
{
$parser = new SubsetHtmlParser();
$nodes = $parser->parse('<div><span style=" ">Hello</span></div>');
self::assertCount(1, $nodes);
self::assertArrayNotHasKey('style', $nodes[0]->attributes);
self::assertArrayNotHasKey('style', $nodes[0]->children[0]->attributes);
self::assertArrayNotHasKey('style', $nodes[0]->children[0]->children[0]->attributes);
}
public function testParseTrimsInheritedStyleAndRestoresLibxmlInternalErrorFlag(): void
{
$parser = new SubsetHtmlParser();
$previous = libxml_use_internal_errors(false);
try {
$nodes = $parser->parse('<div style=" font-size:10 "><span style=" font-weight:bold ">Hi</span></div>');
} finally {
$current = libxml_use_internal_errors(false);
libxml_use_internal_errors($previous);
}
self::assertFalse($current);
self::assertSame('font-size:10', $nodes[0]->attributes['style']);
self::assertSame(
'font-size:10;font-weight:bold',
$nodes[0]->children[0]->children[0]->attributes['style'],
);
}
public function testParseClearsLibxmlErrorsAfterMalformedHtmlAndRestoresPreviousFlag(): void
{
$parser = new SubsetHtmlParser();
libxml_clear_errors();
$previous = libxml_use_internal_errors(true);
try {
$nodes = $parser->parse('<div style="font-size:10"><span>Broken');
$current = libxml_use_internal_errors(true);
$errors = libxml_get_errors();
} finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
self::assertTrue($current);
self::assertSame([], $errors);
self::assertCount(1, $nodes);
self::assertSame('div', $nodes[0]->tag);
self::assertSame('Broken', $nodes[0]->children[0]->children[0]->text);
}
public function testParsePreservesUtf8CharactersFromHtmlFragment(): void
{
$parser = new SubsetHtmlParser();
$nodes = $parser->parse('<span>ação € 😀</span>');
self::assertCount(1, $nodes);
self::assertSame('ação € 😀', $nodes[0]->children[0]->text);
}
public function testParseKeepsAllTopLevelNodesInOrder(): void
{
$parser = new SubsetHtmlParser();
$nodes = $parser->parse('<span>First</span><span>Second</span>');
$this->assertCount(2, $nodes);
$this->assertSame('First', $nodes[0]->children[0]->text);
$this->assertSame('Second', $nodes[1]->children[0]->text);
}
public function testParseClearsPreExistingLibxmlErrorBuffer(): void
{
$parser = new SubsetHtmlParser();
libxml_clear_errors();
$previous = libxml_use_internal_errors(true);
try {
$probe = new DOMDocument('1.0', 'UTF-8');
$probe->loadXML('<root><broken></root>');
$errorsBefore = libxml_get_errors();
$parser->parse('<div>ok</div>');
$errorsAfter = libxml_get_errors();
} finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
self::assertNotSame([], $errorsBefore);
self::assertSame([], $errorsAfter);
}
}