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

use HTML5\Elements;

require_once __DIR__ . '/../TestCase.php';

/**
 * 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();
  }

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

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

  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 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><mathml><![CDATA[test]]></mathml></html>";
    $doc = $this->parse($html);

    $wrapper = $doc->getElementsByTagName('mathml')->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><mathml>test</mathml></body></html>";
    $doc = $this->parse($html);

    $wrapper = $doc->getElementsByTagName('mathml')->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);
  }

  public function testParseErrors() {
    $html = "<!DOCTYPE html><html><mathml><![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() {
    $this->markTestIncomplete("Incomplete.");
  }

  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 testMathML() {
    $this->markTestIncomplete("Incomplete.");
  }

  public function testSVG() {
    $this->markTestIncomplete("Incomplete.");
  }
}