summaryrefslogtreecommitdiff
path: root/test/HTML5/Parser/TreeBuildingRulesTest.php
blob: b285073bab0ddb2345151223c504ab35b17c55ac (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
<?php
/**
 * @file
 * Test the Tree Builder's special-case rules.
 */
namespace HTML5\Tests\Parser;

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



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

  const HTML_STUB = '<!DOCTYPE html><html><head><title>test</title></head><body>%s</body></html>';

  /**
   * 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 testHasRules() {
    $doc = new \DOMDocument('1.0');
    $engine = new TreeBuildingRules($doc);

    $this->assertTrue($engine->hasRules('li'));
    $this->assertFalse($engine->hasRules('imaginary'));
  }

  public function testHandleLI() {
    $html = sprintf(self::HTML_STUB, '<ul id="a"><li>test<li>test2</ul><a></a>');
    $doc = $this->parse($html);

    $list = $doc->getElementById('a');

    $this->assertEquals(2, $list->childNodes->length);
    foreach($list->childNodes as $ele) {
      $this->assertEquals('li', $ele->tagName);
    }

  }

  public function testHandleDT() {
    $html = sprintf(self::HTML_STUB, '<dl id="a"><dt>Hello<dd>Hi</dl><a></a>');
    $doc = $this->parse($html);

    $list = $doc->getElementById('a');

    $this->assertEquals(2, $list->childNodes->length);
    $this->assertEquals('dt', $list->firstChild->tagName);
    $this->assertEquals('dd', $list->lastChild->tagName);
  }

  public function testTable() {
    $html = sprintf(self::HTML_STUB, '<table><thead id="a"><th>foo<td>bar<td>baz');
    $doc = $this->parse($html);

    $list = $doc->getElementById('a');

    $this->assertEquals(3, $list->childNodes->length);
    $this->assertEquals('th', $list->firstChild->tagName);
    $this->assertEquals('td', $list->lastChild->tagName);
  }

}