3dad6c01fb8876bf726f6c6a0226bb6471469fec.svn-base 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // $Rev: 130 $
  3. // -----------------------------------------------------------------------------
  4. // setup
  5. error_reporting(E_ALL);
  6. require_once('../../simple_html_dom_reader.php');
  7. $dom = new simple_html_dom;
  8. // -----------------------------------------------------------------------------
  9. // attribute test
  10. $str = <<<HTML
  11. <div onclick="bar('aa')">foo</div>
  12. HTML;
  13. $dom->load($str);
  14. assert($dom->find('div', 0)==$str);
  15. // -----------------------------------------------
  16. $str = <<<HTML
  17. <div onclick='bar("aa")'>foo</div>
  18. HTML;
  19. $dom->load($str);
  20. assert($dom->find('div', 0)==$str);
  21. // -----------------------------------------------------------------------------
  22. // innertext test
  23. $str = <<<HTML
  24. <html><head></head><body><br><span>foo</span></body></html>
  25. HTML;
  26. $dom->load($str);
  27. assert($dom==$str);
  28. // -----------------------------------------------
  29. $str = <<<HTML
  30. <html><head></head><body><br><span>bar</span></body></html>
  31. HTML;
  32. $dom->find('span', 0)->innertext = 'bar';
  33. assert($dom==$str);
  34. // -----------------------------------------------
  35. $str = <<<HTML
  36. <html><head>ok</head><body><br><span>bar</span></body></html>
  37. HTML;
  38. $dom->find('head', 0)->innertext = 'ok';
  39. assert($dom==$str);
  40. // -----------------------------------------------------------------------------
  41. // outertext test
  42. $str = <<<HTML
  43. <table>
  44. <tr><th>Head1</th><th>Head2</th><th>Head3</th></tr>
  45. <tr><td>1</td><td>2</td><td>3</td></tr>
  46. </table>
  47. HTML;
  48. $dom->load($str);
  49. assert($dom->find('tr', 0)->outertext=='<tr><th>Head1</th><th>Head2</th><th>Head3</th></tr>');
  50. assert($dom->find('tr', 1)->outertext=='<tr><td>1</td><td>2</td><td>3</td></tr>');
  51. // -----------------------------------------------
  52. $str = <<<HTML
  53. <table><tr><th>Head1</th><th>Head2</th><th>Head3</th><tr><td>1</td><td>2</td><td>3</td></table>
  54. HTML;
  55. $dom->load($str);
  56. assert($dom->find('tr', 0)->outertext=='<tr><th>Head1</th><th>Head2</th><th>Head3</th></tr>');
  57. assert($dom->find('tr', 1)->outertext=='<tr><td>1</td><td>2</td><td>3</td></tr>');
  58. // -----------------------------------------------
  59. $str = <<<HTML
  60. <ul><li><b>li11</b></li><li><b>li12</b></li></ul><ul><li><b>li21</b></li><li><b>li22</b></li></ul>
  61. HTML;
  62. $dom->load($str);
  63. assert($dom->find('ul', 0)->outertext=='<ul><li><b>li11</b></li><li><b>li12</b></li></ul>');
  64. assert($dom->find('ul', 1)->outertext=='<ul><li><b>li21</b></li><li><b>li22</b></li></ul>');
  65. // -----------------------------------------------
  66. $str = <<<HTML
  67. <ul><li><b>li11</b></li><li><b>li12</b></li><ul><li><b>li21</b></li><li><b>li22</b></li>
  68. HTML;
  69. //$dom->load($str);
  70. //assert($dom->find('ul', 0)->outertext=='<ul><li><b>li11</b></li><li><b>li12</b></li></ul>');
  71. //assert($dom->find('ul', 1)->outertext=='<ul><li><b>li21</b></li><li><b>li22</b></li></ul>');
  72. // -----------------------------------------------
  73. $str = <<<HTML
  74. <ul><li><b>li11</b><li><b>li12</b></li><ul><li><b>li21</b></li><li><b>li22</b>
  75. HTML;
  76. //$dom->load($str);
  77. //assert($dom->find('ul', 0)->outertext=='<ul><li><b>li11</b><li><b>li12</b></li>');
  78. //assert($dom->find('ul', 1)->outertext=='<ul><li><b>li21</b></li><li><b>li22</b>');
  79. // -----------------------------------------------
  80. $str = <<<HTML
  81. <table>
  82. <tr><th>Head1</th><th>Head2</th><th>Head3</th></tr>
  83. <tr><td>1</td><td>2</td><td>3</td></tr>
  84. </table>
  85. HTML;
  86. $dom->load($str);
  87. assert($dom->find('tr', 0)->outertext=='<tr><th>Head1</th><th>Head2</th><th>Head3</th></tr>');
  88. assert($dom->find('tr', 1)->outertext=='<tr><td>1</td><td>2</td><td>3</td></tr>');
  89. // -----------------------------------------------------------------------------
  90. // replacement test
  91. $str = <<<HTML
  92. <div class="class1" id="id2" ><div class="class2">ok</div></div>
  93. HTML;
  94. $dom->load($str);
  95. $es = $dom->find('div');
  96. assert(count($es)==2);
  97. assert($es[0]->innertext=='<div class="class2">ok</div>');
  98. assert($es[0]->outertext=='<div class="class1" id="id2"><div class="class2">ok</div></div>');
  99. // test isset
  100. $es[0]->class = 'class_test';
  101. assert(isset($es[0]->class)===true);
  102. assert(isset($es[0]->okok)===false);
  103. // test replacement
  104. $es[0]->class = 'class_test';
  105. assert($es[0]->outertext=='<div class="class_test" id="id2"><div class="class2">ok</div></div>');
  106. // test replacement
  107. //$es[0]->tag = 'span';
  108. //assert($es[0]->outertext=='<span class="class_test" id="id2"><div class="class2">ok</div></span>');
  109. // test unset (no more support...)
  110. //$dom = str_get_dom($str);
  111. //$es = $dom->find('div');
  112. //unset($es[0]->class);
  113. //assert($es[0]->outertext=='<div id="id2" ><div class="class2">ok</div></div>');
  114. //$dom->load($str);
  115. //$es = $dom->find('div');
  116. //unset($es[0]->attr['class']);
  117. //assert($es[0]->outertext=='<div id="id2"><div class="class2">ok</div></div>');
  118. // -----------------------------------------------
  119. $str = <<<HTML
  120. <select name=something><options>blah</options><options>blah2</options></select>
  121. HTML;
  122. $dom->load($str);
  123. $e = $dom->find('select[name=something]', 0);
  124. $e->innertext = '';
  125. assert($e->outertext =='<select name="something"></select>');
  126. // -----------------------------------------------------------------------------
  127. // nested replacement test
  128. $str = <<<HTML
  129. <div class="class0" id="id0"><div class="class1">ok</div></div>
  130. HTML;
  131. $dom->load($str);
  132. $es = $dom->find('div');
  133. assert(count($es)==2);
  134. assert($es[0]->innertext=='<div class="class1">ok</div>');
  135. assert($es[0]->outertext=='<div class="class0" id="id0"><div class="class1">ok</div></div>');
  136. assert($es[1]->innertext=='ok');
  137. assert($es[1]->outertext=='<div class="class1">ok</div>');
  138. // test replacement
  139. $es[1]->innertext = 'okok';
  140. assert($es[1]->outertext=='<div class="class1">okok</div>');
  141. assert($es[0]->outertext=='<div class="class0" id="id0"><div class="class1">okok</div></div>');
  142. //assert($dom=='<div class="class0" id="id0"><div class="class1">okok</div></div>');
  143. $es[1]->class = 'class_test';
  144. assert($es[1]->outertext=='<div class="class_test">okok</div>');
  145. assert($es[0]->outertext=='<div class="class0" id="id0"><div class="class_test">okok</div></div>');
  146. //assert($dom=='<div class="class0" id="id0"><div class="class_test">okok</div></div>');
  147. $es[0]->class = 'class_test';
  148. assert($es[0]->outertext=='<div class="class_test" id="id0"><div class="class_test">okok</div></div>');
  149. //assert($dom=='<div class="class_test" id="id0"><div class="class_test">okok</div></div>');
  150. $es[0]->innertext = 'okokok';
  151. assert($es[0]->outertext=='<div class="class_test" id="id0">okokok</div>');
  152. //assert($dom=='<div class="class_test" id="id0">okokok</div>');
  153. // -----------------------------------------------------------------------------
  154. // <p> test
  155. $str = <<<HTML
  156. <div class="class0"><p>ok0<a href="#">link0</a></p><div class="class1"><p>ok1<a href="#">link1</a></p></div><div class="class2"></div><p>ok2<a href="#">link2</a></p></div>
  157. HTML;
  158. $dom->load($str);
  159. $es = $dom->find('p');
  160. assert($es[0]->innertext=='ok0<a href="#">link0</a>');
  161. assert($es[1]->innertext=='ok1<a href="#">link1</a>');
  162. assert($es[2]->innertext=='ok2<a href="#">link2</a>');
  163. assert($dom->find('p', 0)->plaintext=='ok0link0');
  164. assert($dom->find('p', 1)->plaintext=='ok1link1');
  165. assert($dom->find('p', 2)->plaintext=='ok2link2');
  166. $count = 0;
  167. foreach($dom->find('p') as $p) {
  168. $a = $p->find('a');
  169. assert($a[0]->innertext=='link'.$count);
  170. ++$count;
  171. }
  172. $es = $dom->find('p a');
  173. assert($es[0]->innertext=='link0');
  174. assert($es[1]->innertext=='link1');
  175. assert($es[2]->innertext=='link2');
  176. assert($dom->find('p a', 0)->plaintext=='link0');
  177. assert($dom->find('p a', 1)->plaintext=='link1');
  178. assert($dom->find('p a', 2)->plaintext=='link2');
  179. // -----------------------------------------------------------------------------
  180. // <embed> test
  181. $str = <<<HTML
  182. <EMBED SRC="../graphics/sounds/1812over.mid" HEIGHT="60" WIDTH="144"></EMBED>
  183. HTML;
  184. $dom->load($str);
  185. $e = $dom->find('embed', 0);
  186. assert($e->src=='../graphics/sounds/1812over.mid');
  187. assert($e->height=='60');
  188. assert($e->width=='144');
  189. assert($e==strtolower($str));
  190. // -----------------------------------------------------------------------------
  191. // <pre> test
  192. $str = <<<HTML
  193. <div class="class0" id="id0" >
  194. <pre>
  195. <input type=submit name="btnG" value="go" onclick='goto("url0")'>
  196. </pre>
  197. </div>
  198. HTML;
  199. $dom->load($str);
  200. //assert(count($dom->find('input'))==0);
  201. // -----------------------------------------------------------------------------
  202. // <code> test
  203. $str = <<<HTML
  204. <div class="class0" id="id0" >
  205. <CODE>
  206. <input type=submit name="btnG" value="go" onclick='goto("url0")'>
  207. </CODE>
  208. </div>
  209. HTML;
  210. $dom->load($str);
  211. assert(count($dom->find('code'))==1);
  212. //assert(count($dom->find('input'))==0);
  213. // -----------------------------------------------------------------------------
  214. // tear down
  215. $dom->clear();
  216. unset($dom);
  217. ?>