TreeBuildingRulesTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @file
  4. * Test the Tree Builder's special-case rules.
  5. */
  6. namespace Masterminds\HTML5\Tests\Parser;
  7. use Masterminds\HTML5\Parser\TreeBuildingRules;
  8. use Masterminds\HTML5\Parser\Tokenizer;
  9. use Masterminds\HTML5\Parser\Scanner;
  10. use Masterminds\HTML5\Parser\DOMTreeBuilder;
  11. /**
  12. * These tests are functional, not necessarily unit tests.
  13. */
  14. class TreeBuildingRulesTest extends \Masterminds\HTML5\Tests\TestCase
  15. {
  16. const HTML_STUB = '<!DOCTYPE html><html><head><title>test</title></head><body>%s</body></html>';
  17. /**
  18. * Convenience function for parsing.
  19. */
  20. protected function parse($string)
  21. {
  22. $treeBuilder = new DOMTreeBuilder();
  23. $scanner = new Scanner($string);
  24. $parser = new Tokenizer($scanner, $treeBuilder);
  25. $parser->parse();
  26. return $treeBuilder->document();
  27. }
  28. /**
  29. * Convenience function for parsing fragments.
  30. */
  31. protected function parseFragment($string)
  32. {
  33. $events = new DOMTreeBuilder(true);
  34. $scanner = new Scanner($string);
  35. $parser = new Tokenizer($scanner, $events);
  36. $parser->parse();
  37. return $events->fragment();
  38. }
  39. public function testTDFragment()
  40. {
  41. $frag = $this->parseFragment('<td>This is a test of the HTML5 parser</td>');
  42. $td = $frag->childNodes->item(0);
  43. $this->assertEquals(1, $frag->childNodes->length);
  44. $this->assertEquals('td', $td->tagName);
  45. $this->assertEquals('This is a test of the HTML5 parser', $td->nodeValue);
  46. }
  47. public function testHasRules()
  48. {
  49. $doc = new \DOMDocument('1.0');
  50. $engine = new TreeBuildingRules($doc);
  51. $this->assertTrue($engine->hasRules('li'));
  52. $this->assertFalse($engine->hasRules('imaginary'));
  53. }
  54. public function testHandleLI()
  55. {
  56. $html = sprintf(self::HTML_STUB, '<ul id="a"><li>test<li>test2</ul><a></a>');
  57. $doc = $this->parse($html);
  58. $list = $doc->getElementById('a');
  59. $this->assertEquals(2, $list->childNodes->length);
  60. foreach ($list->childNodes as $ele) {
  61. $this->assertEquals('li', $ele->tagName);
  62. }
  63. }
  64. public function testHandleDT()
  65. {
  66. $html = sprintf(self::HTML_STUB, '<dl id="a"><dt>Hello<dd>Hi</dl><a></a>');
  67. $doc = $this->parse($html);
  68. $list = $doc->getElementById('a');
  69. $this->assertEquals(2, $list->childNodes->length);
  70. $this->assertEquals('dt', $list->firstChild->tagName);
  71. $this->assertEquals('dd', $list->lastChild->tagName);
  72. }
  73. public function testHandleOptionGroupAndOption()
  74. {
  75. $html = sprintf(self::HTML_STUB, '<optgroup id="foo" label="foo" ><option value="foo">bar</option></optgroup>');
  76. $doc = $this->parse($html);
  77. $list = $doc->getElementById('foo');
  78. $this->assertEquals(1, $list->childNodes->length);
  79. $option = $list->childNodes->item(0);
  80. $this->assertEquals('option', $option->tagName);
  81. }
  82. public function testTable()
  83. {
  84. $html = sprintf(self::HTML_STUB, '<table><thead id="a"><th>foo<td>bar<td>baz');
  85. $doc = $this->parse($html);
  86. $list = $doc->getElementById('a');
  87. $this->assertEquals(3, $list->childNodes->length);
  88. $this->assertEquals('th', $list->firstChild->tagName);
  89. $this->assertEquals('td', $list->lastChild->tagName);
  90. }
  91. }