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
|
<?php
namespace HTML5\Parser;
require __DIR__ . '/../TestCase.php';
require 'EventStack.php';
class TokenizerTest extends \HTML5\Tests\TestCase {
// ================================================================
// Additional assertions.
// ================================================================
/**
* Tests that an event matches both the event type and the expected value.
*
* @param string $type
* Expected event type.
* @param string $expects
* The value expected in $event['data'][0].
*/
public function assertEventEquals($type, $expects, $event) {
$this->assertEquals($type, $event['name'], "Event $type for " . print_r($event, TRUE));
if (is_array($expects)) {
$this->assertEquals($expects, $event['data'], "Event $type should equal $expects: " . print_r($event, TRUE));
}
else {
$this->assertEquals($expects, $event['data'][0], "Event $type should equal $expects: " . print_r($event, TRUE));
}
}
/**
* Assert that a given event is 'error'.
*/
public function assertEventError($event) {
$this->assertEquals('error', $event['name'], "Expected error for event: " . print_r($event, TRUE));
}
// ================================================================
// Utility functions.
// ================================================================
public function testParse() {
list($tok, $events) = $this->createTokenizer('');
$tok->parse();
$e1 = $events->get(0);
$this->assertEquals(1, $events->Depth());
$this->assertEquals('eof', $e1['name']);
}
public function testWhitespace() {
$spaces = ' ';
list($tok, $events) = $this->createTokenizer($spaces);
$tok->parse();
$this->assertEquals(2, $events->depth());
$e1 = $events->get(0);
$this->assertEquals('text', $e1['name']);
$this->assertEquals($spaces, $e1['data'][0]);
}
public function testCharacterReference() {
$str = '&';
$events = $this->parse($str);
$this->assertEquals(2, $events->depth());
$e1 = $events->get(0);
$this->assertEquals('&', $e1['data'][0]);
// Test with hex charref
$str = '<';
$events = $this->parse($str);
$e1 = $events->get(0);
$this->assertEquals('<', $e1['data'][0]);
// Test with decimal charref
$str = '&';
$events = $this->parse($str);
$e1 = $events->get(0);
$this->assertEquals('&', $e1['data'][0]);
// Test with stand-alone ampersand
$str = '& ';
$events = $this->parse($str);
$e1 = $events->get(0);
$this->assertEquals('&', $e1['data'][0][0], "Stand-alone &");
// Test with broken charref
$str = '&foo';
$events = $this->parse($str);
$e1 = $events->get(0);
$this->assertEquals('error', $e1['name']);
}
public function testBogusComment() {
$bogus = array(
'</+this is a bogus comment. +>',
'<!+this is a bogus comment. !>',
'<!D OCTYPE foo bar>',
'<!DOCTYEP foo bar>',
'<![CADATA[ TEST ]]>',
'<![CDATA Hello ]]>',
'<![CDATA[ Hello [[>',
'<!CDATA[[ test ]]>',
'<![CDATA[',
'<![CDATA[hellooooo hello',
);
foreach ($bogus as $str) {
$events = $this->parse($str);
$this->assertEventError($events->get(0));
$this->assertEventEquals('comment', $str, $events->get(1));
}
}
public function testEndTag() {
$succeed = array(
'</a>' => 'a',
'</test>' => 'test',
'</test
>' => 'test',
'</thisIsTheTagThatDoesntEndItJustGoesOnAndOnMyFriend>' =>
'thisIsTheTagThatDoesntEndItJustGoesOnAndOnMyFriend',
// See 8.2.4.10, which requires this and does not say error.
'</a<b>' => 'a<b',
);
foreach ($succeed as $test => $result) {
$events = $this->parse($test);
$this->assertEquals(2, $events->depth());
$this->assertEventEquals('endTag', $result, $events->get(0));
}
// Recoverable failures
$fail = array(
'</a class="monkey">' => 'a',
'</a <b>' => 'a',
'</a <b <c>' => 'a',
'</a is the loneliest letter>' => 'a',
'</a' => 'a',
);
foreach ($fail as $test => $result) {
$events = $this->parse($test);
$this->assertEquals(3, $events->depth());
// Should have triggered an error.
$this->assertEventError($events->get(0));
// Should have tried to parse anyway.
$this->assertEventEquals('endTag', $result, $events->get(1));
}
// BogoComments
$comments = array(
'</>' => '</>',
'</ >' => '</ >',
'</ a>' => '</ a>',
);
foreach ($comments as $test => $result) {
$events = $this->parse($test);
$this->assertEquals(3, $events->depth());
// Should have triggered an error.
$this->assertEventError($events->get(0));
// Should have tried to parse anyway.
$this->assertEventEquals('comment', $result, $events->get(1));
}
}
public function testComment() {
$good = array(
'<!--easy-->' => 'easy',
'<!-- 1 > 0 -->' => ' 1 > 0 ',
'<!-- --$i -->' => ' --$i ',
'<!----$i-->' => '--$i',
'<!-- 1 > 0 -->' => ' 1 > 0 ',
"<!--\nHello World.\na-->" => "\nHello World.\na",
'<!-- <!-- -->' => ' <!-- ',
);
foreach ($good as $test => $expected) {
$events = $this->parse($test);
$this->assertEventEquals('comment', $expected, $events->get(0));
}
$fail = array(
'<!-->' => '',
'<!--Hello' => 'Hello',
"<!--\0Hello" => UTF8Utils::FFFD . 'Hello',
'<!--' => '',
);
foreach ($fail as $test => $expected) {
$events = $this->parse($test);
$this->assertEquals(3, $events->depth());
$this->assertEventError($events->get(0));
$this->assertEventEquals('comment', $expected, $events->get(1));
}
}
public function testCDATASection() {
$good = array(
'<![CDATA[ This is a test. ]]>' => ' This is a test. ',
'<![CDATA[CDATA]]>' => 'CDATA',
'<![CDATA[ ]] > ]]>' => ' ]] > ',
'<![CDATA[ ]]>' => ' ',
);
foreach ($good as $test => $expects) {
$events = $this->parse($test);
$this->assertEquals(2, $events->depth(), "Counting events for '$test': " . print_r($events, TRUE));
$this->assertEventEquals('cdata', $expects, $events->get(0));
}
}
public function testDoctype() {
$good = array(
'<!DOCTYPE html>' => array('html', 0, NULL, FALSE),
"<!DOCTYPE\nhtml>" => array('html', 0, NULL, FALSE),
"<!DOCTYPE\fhtml>" => array('html', 0, NULL, FALSE),
'<!DOCTYPE html PUBLIC "foo bar">' => array('html', EventStack::DOCTYPE_PUBLIC, 'foo bar', FALSE),
"<!DOCTYPE html PUBLIC 'foo bar'>" => array('html', EventStack::DOCTYPE_PUBLIC, 'foo bar', FALSE),
'<!DOCTYPE html PUBLIC "foo bar" >' => array('html', EventStack::DOCTYPE_PUBLIC, 'foo bar', FALSE),
"<!DOCTYPE html \nPUBLIC\n'foo bar'>" => array('html', EventStack::DOCTYPE_PUBLIC, 'foo bar', FALSE),
'<!DOCTYPE html SYSTEM "foo bar">' => array('html', EventStack::DOCTYPE_SYSTEM, 'foo bar', FALSE),
"<!DOCTYPE html SYSTEM 'foo bar'>" => array('html', EventStack::DOCTYPE_SYSTEM, 'foo bar', FALSE),
'<!DOCTYPE html SYSTEM "foo/bar" >' => array('html', EventStack::DOCTYPE_SYSTEM, 'foo/bar', FALSE),
"<!DOCTYPE html \nSYSTEM\n'foo bar'>" => array('html', EventStack::DOCTYPE_SYSTEM, 'foo bar', FALSE),
);
foreach ($good as $test => $expects) {
$events = $this->parse($test);
$this->assertEquals(2, $events->depth(), "Counting events for '$test': " . print_r($events, TRUE));
$this->assertEventEquals('doctype', $expects, $events->get(0));
}
$bad = array(
'<!DOCTYPE>' => array(NULL, EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE >' => array(NULL, EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo PUB' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo PUB>' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo PUB "Looks good">' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo SYSTME "Looks good"' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
// Can't tell whether these are ids or ID types, since the context is chopped.
'<!DOCTYPE foo PUBLIC' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo PUBLIC>' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo SYSTEM' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE foo SYSTEM>' => array('foo', EventStack::DOCTYPE_NONE, NULL, TRUE),
'<!DOCTYPE html SYSTEM "foo bar"' => array('html', EventStack::DOCTYPE_SYSTEM, 'foo bar', TRUE),
'<!DOCTYPE html SYSTEM "foo bar" more stuff>' => array('html', EventStack::DOCTYPE_SYSTEM, 'foo bar', TRUE),
);
foreach ($bad as $test => $expects) {
$events = $this->parse($test);
//fprintf(STDOUT, $test . PHP_EOL);
$this->assertEquals(3, $events->depth(), "Counting events for '$test': " . print_r($events, TRUE));
$this->assertEventError($events->get(0));
$this->assertEventEquals('doctype', $expects, $events->get(1));
}
}
public function testText() {
$good = array(
'a<br>b',
'<a>test</a>',
'a<![[ test ]]>b',
'a&b',
);
$this->markTestIncomplete("Need tag parsing first.");
}
// ================================================================
// Utility functions.
// ================================================================
protected function createTokenizer($string, $debug = FALSE) {
$eventHandler = new EventStack();
$stream = new StringInputStream($string);
$scanner = new Scanner($stream);
$scanner->debug = $debug;
return array(
new Tokenizer($scanner, $eventHandler),
$eventHandler,
);
}
public function parse($string, $debug = FALSE) {
list($tok, $events) = $this->createTokenizer($string, $debug);
$tok->parse();
return $events;
}
}
|