MastermindsHTML5TestsParserTreeBuildingRulesTest

These tests are functional, not necessarily unit tests.

Defined (1)

The class is defined in the following location(s).

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