1<?php 2 3final class PhutilHTMLParserTestCase 4 extends PhutilTestCase { 5 6 public function testHTMLParser() { 7 8 $root = dirname(__FILE__).'/data/'; 9 $tests = Filesystem::listDirectory($root, $include_hidden = false); 10 11 foreach ($tests as $test) { 12 $path = $root.$test; 13 $data = Filesystem::readFile($path); 14 15 $parts = explode("\n~~~~~~~~~~\n", $data); 16 if (count($parts) !== 2) { 17 throw new Exception( 18 pht( 19 'Expected "~~~~~~~~~~" delimiter in test "%s".', 20 $test)); 21 } 22 23 $input = $parts[0]; 24 25 $expect = $parts[1]; 26 $expect = phutil_json_decode($parts[1]); 27 28 $document = id(new PhutilHTMLParser()) 29 ->parseDocument($input); 30 31 // We're just testing the child list of the root node since this 32 // reduces the amount of boilerplate in the test cases. 33 $list = array(); 34 foreach ($document->getChildren() as $child) { 35 $list[] = $child->toDictionary(); 36 } 37 38 $this->assertEqual( 39 $expect, 40 $list, 41 pht('DOM tree for "%s".', $test)); 42 } 43 } 44 45 public function testSelectChildrenWithTags() { 46 $input = '<a></a><b></b><a /><c /><a /><x></x><y /><a /><d>x</d><a /><e>'; 47 $document = id(new PhutilHTMLParser()) 48 ->parseDocument($input); 49 50 $children = $document->selectChildrenWithTags(array('a')); 51 52 $list = array(); 53 foreach ($children as $child) { 54 $list[] = $child->toDictionary(); 55 } 56 57 $this->assertEqual( 58 array( 59 array( 60 'tag' => 'a', 61 'attributes' => array(), 62 'children' => array(), 63 ), 64 array( 65 'content' => '<b></b>', 66 ), 67 array( 68 'tag' => 'a', 69 'attributes' => array(), 70 'children' => array(), 71 ), 72 array( 73 'content' => '<c />', 74 ), 75 array( 76 'tag' => 'a', 77 'attributes' => array(), 78 'children' => array(), 79 ), 80 array( 81 'content' => '<x></x><y />', 82 ), 83 array( 84 'tag' => 'a', 85 'attributes' => array(), 86 'children' => array(), 87 ), 88 array( 89 'content' => '<d>x</d>', 90 ), 91 array( 92 'tag' => 'a', 93 'attributes' => array(), 94 'children' => array(), 95 ), 96 array( 97 'content' => '<e>', 98 ), 99 ), 100 $list, 101 pht('Child selection of: %s.', $input)); 102 } 103 104} 105