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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
<?php
namespace HTML5\Tests;
use \HTML5\Serializer\OutputRules;
use \HTML5\Serializer\Traverser;
use \HTML5\Parser;
require_once __DIR__ . '/../TestCase.php';
class OutputRulesTest extends \HTML5\Tests\TestCase {
protected $markup = '<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<p>This is a test.</p>
</body>
</html>';
/**
* Using reflection we make a protected method accessible for testing.
*
* @param string $name
* The name of the method on the Traverser class to test.
*
* @return \ReflectionMethod
* \ReflectionMethod for the specified method
*/
function getProtectedMethod($name) {
$class = new \ReflectionClass('\HTML5\Serializer\OutputRules');
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
function getTraverserProtectedProperty($name) {
$class = new \ReflectionClass('\HTML5\Serializer\Traverser');
$property = $class->getProperty($name);
$property->setAccessible(true);
return $property;
}
function getOutputRules($options = array()) {
$options = $options + \HTML5::options();
$stream = fopen('php://temp', 'w');
$dom = \HTML5::loadHTML($this->markup);
$r = new OutputRules($stream, $options);
$t = new Traverser($dom, $stream, $r, $options);
return array($r, $stream);
}
function testDocument() {
$dom = \HTML5::loadHTML('<!doctype html><html lang="en"><body>foo</body></html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$r->document($dom);
$this->assertEquals("<!DOCTYPE html>\n<html lang=\"en\"><body>foo</body></html>\n", stream_get_contents($stream, -1, 0));
}
function testDoctype() {
$dom = \HTML5::loadHTML('<!doctype html><html lang="en"><body>foo</body></html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$m = $this->getProtectedMethod('doctype');
$m->invoke($r, 'foo');
$this->assertEquals("<!DOCTYPE html>\n", stream_get_contents($stream, -1, 0));
}
function testElement() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo" class="bar baz">foo bar baz</div>
<svg width="150" height="100" viewBox="0 0 3 2">
<rect width="1" height="2" x="0" fill="#008d46" />
<rect width="1" height="2" x="1" fill="#ffffff" />
<rect width="1" height="2" x="2" fill="#d2232c" />
</svg>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$r->element($list->item(0));
$this->assertEquals('<div id="foo" class="bar baz">foo bar baz</div>', stream_get_contents($stream, -1, 0));
}
function testOpenTag() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo" class="bar baz">foo bar baz</div>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$m = $this->getProtectedMethod('openTag');
$m->invoke($r, $list->item(0));
$this->assertEquals('<div id="foo" class="bar baz">', stream_get_contents($stream, -1, 0));
}
function testCData() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div><![CDATA[bar]]></div>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$r->cdata($list->item(0)->childNodes->item(0));
$this->assertEquals('<![CDATA[bar]]>', stream_get_contents($stream, -1, 0));
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo"></div>
</body>
</html>');
$dom->getElementById('foo')->appendChild(new \DOMCdataSection("]]>Foo<[![CDATA test ]]>"));
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$r->cdata($list->item(0)->childNodes->item(0));
$this->assertEquals('<![CDATA[]]]]><![CDATA[>Foo<[![CDATA test ]]]]><![CDATA[>]]>', stream_get_contents($stream, -1, 0));
}
function testComment() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div><!-- foo --></div>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$r->comment($list->item(0)->childNodes->item(0));
$this->assertEquals('<!-- foo -->', stream_get_contents($stream, -1, 0));
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo"></div>
</body>
</html>');
$dom->getElementById('foo')->appendChild(new \DOMComment('<!-- --> --> Foo -->'));
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$r->comment($list->item(0)->childNodes->item(0));
// Could not find more definitive guidelines on what this should be. Went with
// what the HTML5 spec says and what \DOMDocument::saveXML() produces.
$this->assertEquals('<!--<!-- --> --> Foo -->-->', stream_get_contents($stream, -1, 0));
}
function testText() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<head>
<script>baz();</script>
</head>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('script');
$r->text($list->item(0)->childNodes->item(0));
$this->assertEquals('baz();', stream_get_contents($stream, -1, 0));
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<head id="foo"></head>
</html>');
$dom->getElementById('foo')->appendChild(new \DOMText('<script>alert("hi");</script>'));
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$item = $dom->getElementById('foo');
$r->text($item->firstChild);
$this->assertEquals('<script>alert("hi");</script>', stream_get_contents($stream, -1, 0));
}
function testNl() {
list($o, $s) = $this->getOutputRules();
$m = $this->getProtectedMethod('nl');
$m->invoke($o);
$this->assertEquals(PHP_EOL, stream_get_contents($s, -1, 0));
}
function testWr() {
list($o, $s) = $this->getOutputRules();
$m = $this->getProtectedMethod('wr');
$m->invoke($o, 'foo');
$this->assertEquals('foo', stream_get_contents($s, -1, 0));
}
function getEncData(){
return array(
array('&\'<>"', '&\'<>"'),
array('This + is. a < test', 'This + is. a < test'),
array('.+#', '.+#'),
);
}
function getEncWithEntiyesData(){
return array(
array('.+#', '.+#'),
);
}
/**
* Test basic escaping of text.
* @dataProvider getEncData
*/
function testEnc($test, $expected) {
list($o, $s) = $this->getOutputRules();
$m = $this->getProtectedMethod('enc');
$this->assertEquals($expected, $m->invoke($o, $test));
}
/**
* Test basic escaping of text.
* @dataProvider getEncWithEntiyesData
*/
function testEncWithEntities($test, $expected) {
list($o, $s) = $this->getOutputRules(array('encode_entities' => TRUE));
$m = $this->getProtectedMethod('enc');
$this->assertEquals($expected, $m->invoke($o, $test));
}
function testAttrs() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo" class="bar baz" disabled>foo bar baz</div>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('div');
$m = $this->getProtectedMethod('attrs');
$m->invoke($r, $list->item(0));
$content = stream_get_contents($stream, -1, 0);
$this->assertEquals(' id="foo" class="bar baz" disabled', $content);
}
function testSvg() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo" class="bar baz">foo bar baz</div>
<svg width="150" height="100" viewBox="0 0 3 2">
<rect width="1" height="2" x="0" fill="#008d46" />
<rect width="1" height="2" x="1" fill="#ffffff" />
<rect width="1" height="2" x="2" fill="#d2232c" />
<rect id="Bar" x="300" y="100" width="300" height="100" fill="rgb(255,255,0)">
<animate attributeName="x" attributeType="XML" begin="0s" dur="9s" fill="freeze" from="300" to="0" />
</rect>
</svg>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('svg');
$r->element($list->item(0));
$contents = stream_get_contents($stream, -1, 0);
$this->assertRegExp('|<svg width="150" height="100" viewBox="0 0 3 2">|', $contents);
$this->assertRegExp('|<rect width="1" height="2" x="0" fill="#008d46" />|', $contents);
$this->assertRegExp('|<rect id="Bar" x="300" y="100" width="300" height="100" fill="rgb\(255,255,0\)">|', $contents);
}
function testMath() {
$dom = \HTML5::loadHTML('<!doctype html>
<html lang="en">
<body>
<div id="foo" class="bar baz">foo bar baz</div>
<math>
<mi>x</mi>
<csymbol definitionURL="http://www.example.com/mathops/multiops.html#plusminus">
<mo>±</mo>
</csymbol>
<mi>y</mi>
</math>
</body>
</html>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$list = $dom->getElementsByTagName('math');
$r->element($list->item(0));
$content = stream_get_contents($stream, -1, 0);
$this->assertRegExp('|<math>|', $content);
$this->assertRegExp('|<csymbol definitionURL="http://www.example.com/mathops/multiops.html#plusminus">|', $content);
}
function testProcessorInstruction() {
$dom = \HTML5::loadHTMLFragment('<?foo bar ?>');
$stream = fopen('php://temp', 'w');
$r = new OutputRules($stream, \HTML5::options());
$t = new Traverser($dom, $stream, $r, \HTML5::options());
$r->processorInstruction($dom->firstChild);
$content = stream_get_contents($stream, -1, 0);
$this->assertRegExp('|<\?foo bar \?>|', $content);
}
}
|