summaryrefslogtreecommitdiff
path: root/test/HTML5/Parser/DOMTreeBuilderTest.php
blob: ab5378a2001413f77a226dc3f259a836e707ca76 (plain)
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
<?php
/**
 * @file
 * Test the Tree Builder.
 */
namespace HTML5\Tests\Parser;

use HTML5\Elements;
use HTML5\Parser\StringInputStream;
use HTML5\Parser\Scanner;
use HTML5\Parser\Tokenizer;
use HTML5\Parser\DOMTreeBuilder;

/**
 * These tests are functional, not necessarily unit tests.
 */
class DOMTreeBuilderTest extends \HTML5\Tests\TestCase {

  /**
   * Convenience function for parsing.
   */
  protected function parse($string) {
    $treeBuilder = new DOMTreeBuilder();
    $input = new StringInputStream($string);
    $scanner = new Scanner($input);
    $parser = new Tokenizer($scanner, $treeBuilder);

    $parser->parse();

    return $treeBuilder->document();
  }

  /**
   * Utility function for parsing a fragment of HTML5.
   */
  protected function parseFragment($string) {
    $treeBuilder = new DOMTreeBuilder(TRUE);
    $input = new StringInputStream($string);
    $scanner = new Scanner($input);
    $parser = new Tokenizer($scanner, $treeBuilder);

    $parser->parse();

    return $treeBuilder->fragment();
  }

  public function testDocument() {
    $html = "<!DOCTYPE html><html></html>";
    $doc = $this->parse($html);

    $this->assertInstanceOf('\DOMDocument', $doc);
    $this->assertEquals('html', $doc->documentElement->tagName);
  }

  public function testFragment() {
    $html = "<div>test</div><span>test2</span>";
    $doc = $this->parseFragment($html);

    $this->assertInstanceOf('\DOMDocumentFragment', $doc);
    $this->assertTrue($doc->hasChildNodes());
    $this->assertEquals('div', $doc->childNodes->item(0)->tagName);
    $this->assertEquals('test', $doc->childNodes->item(0)->textContent);
    $this->assertEquals('span', $doc->childNodes->item(1)->tagName);
    $this->assertEquals('test2', $doc->childNodes->item(1)->textContent);
  }

  public function testElements() {
    $html = "<!DOCTYPE html><html><head><title></title></head><body></body></html>";
    $doc = $this->parse($html);
    $root = $doc->documentElement;

    $this->assertEquals('html', $root->tagName);
    $this->assertEquals('html', $root->localName);
    $this->assertEquals('html', $root->nodeName);

    $this->assertEquals(2, $root->childNodes->length);
    $kids = $root->childNodes;

    $this->assertEquals('head', $kids->item(0)->tagName);
    $this->assertEquals('body', $kids->item(1)->tagName);

    $head = $kids->item(0);
    $this->assertEquals(1, $head->childNodes->length);
    $this->assertEquals('title', $head->childNodes->item(0)->tagName);
  }

  public function testAttributes() {
    $html = "<!DOCTYPE html>
      <html>
      <head><title></title></head>
      <body id='a' class='b c'></body>
      </html>";
    $doc = $this->parse($html);
    $root = $doc->documentElement;

    $body = $root->GetElementsByTagName('body')->item(0);
    $this->assertEquals('body', $body->tagName);
    $this->assertTrue($body->hasAttributes());
    $this->assertEquals('a', $body->getAttribute('id'));
    $this->assertEquals('b c', $body->getAttribute('class'));

    $body2 = $doc->getElementById('a');
    $this->assertEquals('body', $body2->tagName);
    $this->assertEquals('a', $body2->getAttribute('id'));
  }

  public function testSVGAttributes() {
    $html = "<!DOCTYPE html>
      <html><body>
      <svg width='150' viewbox='2'>
      <rect textlength='2'/>
      <animatecolor>foo</animatecolor>
      </svg>
      </body></html>";
    $doc = $this->parse($html);
    $root = $doc->documentElement;

    $svg = $root->getElementsByTagName('svg')->item(0);
    $this->assertTrue($svg->hasAttribute('viewBox'));

    $rect = $root->getElementsByTagName('rect')->item(0);
    $this->assertTrue($rect->hasAttribute('textLength'));

    $ac = $root->getElementsByTagName('animateColor');
    $this->assertEquals(1, $ac->length);
  }

  public function testMathMLAttribute() {
    $html = '<!doctype html>
      <html lang="en">
        <body>
          <math>
            <mi>x</mi>
            <csymbol definitionurl="http://www.example.com/mathops/multiops.html#plusminus">
              <mo>&PlusMinus;</mo>
            </csymbol>
            <mi>y</mi>
          </math>
        </body>
      </html>';

    $doc = $this->parse($html);
    $root = $doc->documentElement;

    $csymbol = $root->getElementsByTagName('csymbol')->item(0);
    $this->assertTrue($csymbol->hasAttribute('definitionURL'));
  }

  public function testMissingHtmlTag() {
    $html = "<!DOCTYPE html><title>test</title>";
    $doc = $this->parse($html);

    $this->assertEquals('html', $doc->documentElement->tagName);
    $this->assertEquals('title', $doc->documentElement->childNodes->item(0)->tagName);
  }

  public function testComment() {
    $html = '<html><!--Hello World.--></html>';

    $doc = $this->parse($html);

    $comment = $doc->documentElement->childNodes->item(0);
    $this->assertEquals(XML_COMMENT_NODE, $comment->nodeType);
    $this->assertEquals("Hello World.", $comment->data);


    $html = '<!--Hello World.--><html></html>';
    $doc = $this->parse($html);

    $comment = $doc->childNodes->item(1);
    $this->assertEquals(XML_COMMENT_NODE, $comment->nodeType);
    $this->assertEquals("Hello World.", $comment->data);

    $comment = $doc->childNodes->item(2);
    $this->assertEquals(XML_ELEMENT_NODE, $comment->nodeType);
    $this->assertEquals("html", $comment->tagName);
  }

  public function testCDATA() {
    $html = "<!DOCTYPE html><html><math><![CDATA[test]]></math></html>";
    $doc = $this->parse($html);

    $wrapper = $doc->getElementsByTagName('math')->item(0);
    $this->assertEquals(1, $wrapper->childNodes->length);
    $cdata = $wrapper->childNodes->item(0);
    $this->assertEquals(XML_CDATA_SECTION_NODE, $cdata->nodeType);
    $this->assertEquals('test', $cdata->data);
  }

  public function testText() {
    $html = "<!DOCTYPE html><html><head></head><body><math>test</math></body></html>";
    $doc = $this->parse($html);

    $wrapper = $doc->getElementsByTagName('math')->item(0);
    $this->assertEquals(1, $wrapper->childNodes->length);
    $data = $wrapper->childNodes->item(0);
    $this->assertEquals(XML_TEXT_NODE, $data->nodeType);
    $this->assertEquals('test', $data->data);

    // The DomTreeBuilder has special handling for text when in before head mode.
    $html = "<!DOCTYPE html><html>
    Foo<head></head><body></body></html>";
    $doc = $this->parse($html);
    $this->assertEquals('Line 0, Col 0: Unexpected text. Ignoring: Foo', $doc->errors[0]);
    $headElement = $doc->documentElement->firstChild;
    $this->assertEquals('head', $headElement->tagName);
  }

  public function testParseErrors() {
    $html = "<!DOCTYPE html><html><math><![CDATA[test";
    $doc = $this->parse($html);

    // We're JUST testing that we can access errors. Actual testing of
    // error messages happen in the Tokenizer's tests.
    $this->assertGreaterThan(0,  count($doc->errors));
    $this->assertTrue(is_string($doc->errors[0]));
  }

  public function testProcessingInstruction() {
    // Test the simple case, which is where PIs are inserted into the DOM.
    $doc = $this->parse('<!DOCTYPE html><html><?foo bar?>');
    $this->assertEquals(1, $doc->documentElement->childNodes->length);
    $pi = $doc->documentElement->firstChild;
    $this->assertInstanceOf('\DOMProcessingInstruction', $pi);
    $this->assertEquals('foo', $pi->nodeName);
    $this->assertEquals('bar', $pi->data);

    // Leading xml PIs should be ignored.
    $doc = $this->parse('<?xml version="1.0"?><!DOCTYPE html><html><head></head></html>');

    $this->assertEquals(2, $doc->childNodes->length);
    $this->assertInstanceOf('\DOMDocumentType', $doc->childNodes->item(0));
    $this->assertInstanceOf('\DOMElement', $doc->childNodes->item(1));
  }

  public function testAutocloseP() {
    $html = "<!DOCTYPE html><html><body><p><figure></body></html>";
    $doc = $this->parse($html);

    $p = $doc->getElementsByTagName('p')->item(0);
    $this->assertEquals(0, $p->childNodes->length);
    $this->assertEquals('figure', $p->nextSibling->tagName);
  }

  public function testAutocloseLI() {
    $html = '<!doctype html>
      <html lang="en">
        <body>
          <ul><li>Foo<li>Bar<li>Baz</ul>
        </body>
      </html>';

    $doc = $this->parse($html);
    $length = $doc->getElementsByTagName('ul')->item(0)->childNodes->length;
    $this->assertEquals(3, $length);
  }

  public function testMathML() {
    $html = '<!doctype html>
      <html lang="en">
        <body>
          <math xmlns="http://www.w3.org/1998/Math/MathML">
            <mi>x</mi>
            <csymbol definitionurl="http://www.example.com/mathops/multiops.html#plusminus">
              <mo>&PlusMinus;</mo>
            </csymbol>
            <mi>y</mi>
          </math>
        </body>
      </html>';

    $doc = $this->parse($html);
    $math = $doc->getElementsByTagName('math')->item(0);
    $this->assertEquals('math', $math->tagName);
    $this->assertEquals('math', $math->nodeName);
    $this->assertEquals('math', $math->localName);
    $this->assertEmpty($math->namespaceURI);
  }

  public function testSVG() {
    $html = '<!doctype html>
      <html lang="en">
        <body>
          <svg width="150" height="100" viewBox="0 0 3 2" xmlns="http://www.w3.org/2000/svg">
            <rect width="1" height="2" x="2" fill="#d2232c" />
            <text font-family="Verdana" font-size="32">
              <textpath xlink:href="#Foo">
                Test Text.
              </textPath>
            </text>
          </svg>
        </body>
      </html>';

    $doc = $this->parse($html);
    $svg = $doc->getElementsByTagName('svg')->item(0);
    $this->assertEquals('svg', $svg->tagName);
    $this->assertEquals('svg', $svg->nodeName);
    $this->assertEquals('svg', $svg->localName);
    $this->assertEmpty($svg->namespaceURI);

    $textPath = $doc->getElementsByTagName('textPath')->item(0);
    $this->assertEquals('textPath', $textPath->tagName);
  }

  public function testNoScript() {
    $html = '<!DOCTYPE html><html><head><noscript>No JS</noscript></head></html>';
    $doc = $this->parse($html);
    $this->assertEmpty($doc->errors);
    $noscript = $doc->getElementsByTagName('noscript')->item(0);
    $this->assertEquals('noscript', $noscript->tagName);
  }

  /**
   * Regression for issue #13
   */
  public function testRegressionHTMLNoBody() {
    $html = '<!DOCTYPE html><html><span id="test">Test</span></html>';
    $doc = $this->parse($html);
    $span = $doc->getElementById('test');

    $this->assertEmpty($doc->errors);

    $this->assertEquals('span', $span->tagName);
    $this->assertEquals('Test', $span->textContent);
  }

  public function testInstructionProcessor() {
    $string = '<!DOCTYPE html><html><?foo bar ?></html>';

    $treeBuilder = new DOMTreeBuilder();
    $is = new InstructionProcessorMock();
    $treeBuilder->setInstructionProcessor($is);

    $input = new StringInputStream($string);
    $scanner = new Scanner($input);
    $parser = new Tokenizer($scanner, $treeBuilder);

    $parser->parse();
    $dom = $treeBuilder->document();
    $div = $dom->getElementsByTagName('div')->item(0);

    $this->assertEquals(1, $is->count);
    $this->assertEquals('foo', $is->name);
    $this->assertEquals('bar ', $is->data);
    $this->assertEquals('div', $div->tagName);
    $this->assertEquals('foo', $div->textContent);
  }
}