0b1bd516b7adf72da57202dc86bd3dfc18b05136.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. // $Rev: 180 $
  3. // -----------------------------------------------------------------------------
  4. // setup
  5. error_reporting(E_ALL);
  6. require_once('../simple_html_dom.php');
  7. $html = new simple_html_dom;
  8. // -----------------------------------------------------------------------------
  9. // DOM tree test
  10. $html->load('');
  11. $e = $html->root;
  12. assert($e->first_child()==null);
  13. assert($e->last_child()==null);
  14. assert($e->next_sibling()==null);
  15. assert($e->prev_sibling()==null);
  16. // -----------------------------------------------
  17. $str = '<div id="div1"></div>';
  18. $html->load($str);
  19. $e = $html->root;
  20. assert($e->first_child()->id=='div1');
  21. assert($e->last_child()->id=='div1');
  22. assert($e->next_sibling()==null);
  23. assert($e->prev_sibling()==null);
  24. assert($e->plaintext=='');
  25. assert($e->innertext=='<div id="div1"></div>');
  26. assert($e->outertext==$str);
  27. // -----------------------------------------------
  28. $str = <<<HTML
  29. <div id="div1">
  30. <div id="div10"></div>
  31. <div id="div11"></div>
  32. <div id="div12"></div>
  33. </div>
  34. HTML;
  35. $html->load($str);
  36. assert($html==$str);
  37. $e = $html->find('div#div1', 0);
  38. assert(isset($e->id)==true);
  39. assert(isset($e->_not_exist)==false);
  40. assert($e->first_child()->id=='div10');
  41. assert($e->last_child()->id=='div12');
  42. assert($e->next_sibling()==null);
  43. assert($e->prev_sibling()==null);
  44. // -----------------------------------------------
  45. $str = <<<HTML
  46. <div id="div0">
  47. <div id="div00"></div>
  48. </div>
  49. <div id="div1">
  50. <div id="div10"></div>
  51. <div id="div11"></div>
  52. <div id="div12"></div>
  53. </div>
  54. <div id="div2"></div>
  55. HTML;
  56. $html->load($str);
  57. assert($html==$str);
  58. $e = $html->find('div#div1', 0);
  59. assert($e->first_child()->id=='div10');
  60. assert($e->last_child()->id=='div12');
  61. assert($e->next_sibling()->id=='div2');
  62. assert($e->prev_sibling()->id=='div0');
  63. $e = $html->find('div#div2', 0);
  64. assert($e->first_child()==null);
  65. assert($e->last_child()==null);
  66. $e = $html->find('div#div0 div#div00', 0);
  67. assert($e->first_child()==null);
  68. assert($e->last_child()==null);
  69. assert($e->next_sibling()==null);
  70. assert($e->prev_sibling()==null);
  71. // -----------------------------------------------
  72. $str = <<<HTML
  73. <div id="div0">
  74. <div id="div00"></div>
  75. </div>
  76. <div id="div1">
  77. <div id="div10"></div>
  78. <div id="div11">
  79. <div id="div110"></div>
  80. <div id="div111">
  81. <div id="div1110"></div>
  82. <div id="div1111"></div>
  83. <div id="div1112"></div>
  84. </div>
  85. <div id="div112"></div>
  86. </div>
  87. <div id="div12"></div>
  88. </div>
  89. <div id="div2"></div>
  90. HTML;
  91. $html->load($str);
  92. assert($html==$str);
  93. assert($html->find("#div1", 0)->id=='div1');
  94. assert($html->find("#div1", 0)->children(0)->id=='div10');
  95. assert($html->find("#div1", 0)->children(1)->children(1)->id=='div111');
  96. assert($html->find("#div1", 0)->children(1)->children(1)->children(2)->id=='div1112');
  97. // -----------------------------------------------------------------------------
  98. // no value attr test
  99. $str = <<<HTML
  100. <form name="form1" method="post" action="">
  101. <input type="checkbox" name="checkbox0" checked value="checkbox0">aaa<br>
  102. <input type="checkbox" name="checkbox1" value="checkbox1">bbb<br>
  103. <input type="checkbox" name="checkbox2" value="checkbox2" checked>ccc<br>
  104. </form>
  105. HTML;
  106. $html->load($str);
  107. assert($html==$str);
  108. $counter = 0;
  109. foreach($html->find('input[type=checkbox]') as $checkbox) {
  110. if (isset($checkbox->checked)) {
  111. assert($checkbox->value=="checkbox$counter");
  112. $counter += 2;
  113. }
  114. }
  115. $counter = 0;
  116. foreach($html->find('input[type=checkbox]') as $checkbox) {
  117. if ($checkbox->checked) {
  118. assert($checkbox->value=="checkbox$counter");
  119. $counter += 2;
  120. }
  121. }
  122. $es = $html->find('input[type=checkbox]');
  123. $es[1]->checked = true;
  124. assert($es[1]->outertext=='<input type="checkbox" name="checkbox1" value="checkbox1" checked>');
  125. $es[0]->checked = false;
  126. assert($es[0]=='<input type="checkbox" name="checkbox0" value="checkbox0">');
  127. $es[0]->checked = true;
  128. assert($es[0]->outertext=='<input type="checkbox" name="checkbox0" checked value="checkbox0">');
  129. // -----------------------------------------------------------------------------
  130. // remove attr test
  131. $str = <<<HTML
  132. <input type="checkbox" name="checkbox0">
  133. <input type = "checkbox" name = 'checkbox1' value = "checkbox1">
  134. HTML;
  135. $html->load($str);
  136. assert($html==$str);
  137. $e = $html->find('[name=checkbox0]', 0);
  138. $e->name = null;
  139. assert($e=='<input type="checkbox">');
  140. $e->type = null;
  141. assert($e=='<input>');
  142. // -----------------------------------------------
  143. $html->load($str);
  144. assert($html==$str);
  145. $e = $html->find('[name=checkbox0]', 0);
  146. $e->name = null;
  147. assert($e=='<input type="checkbox">');
  148. $e->type = null;
  149. assert($e=='<input>');
  150. // -----------------------------------------------
  151. $html->load($str);
  152. assert($html==$str);
  153. $e = $html->find('[name=checkbox1]', 0);
  154. $e->value = null;
  155. assert($e=="<input type = \"checkbox\" name = 'checkbox1'>");
  156. $e->type = null;
  157. assert($e=="<input name = 'checkbox1'>");
  158. $e->name = null;
  159. assert($e=='<input>');
  160. $html->load($str);
  161. assert($html==$str);
  162. $e = $html->find('[name=checkbox1]', 0);
  163. $e->type = null;
  164. assert($e=="<input name = 'checkbox1' value = \"checkbox1\">");
  165. $e->name = null;
  166. assert($e=='<input value = "checkbox1">');
  167. $e->value = null;
  168. assert($e=='<input>');
  169. // -----------------------------------------------------------------------------
  170. // remove no value attr test
  171. $str = <<<HTML
  172. <input type="checkbox" checked name='checkbox0'>
  173. <input type="checkbox" name='checkbox1' checked>
  174. HTML;
  175. $html->load($str);
  176. assert($html==$str);
  177. $e = $html->find('[name=checkbox1]', 0);
  178. $e->type = NULL;
  179. assert($e=="<input name='checkbox1' checked>");
  180. $e->name = null;
  181. assert($e=="<input checked>");
  182. $e->checked = NULL;
  183. assert($e=="<input>");
  184. // -----------------------------------------------
  185. $html->load($str);
  186. assert($html==$str);
  187. $e = $html->find('[name=checkbox0]', 0);
  188. $e->type = NULL;
  189. assert($e=="<input checked name='checkbox0'>");
  190. $e->name = NULL;
  191. assert($e=='<input checked>');
  192. $e->checked = NULL;
  193. assert($e=='<input>');
  194. $html->load($str);
  195. assert($html==$str);
  196. $e = $html->find('[name=checkbox0]', 0);
  197. $e->checked = NULL;
  198. assert($e=="<input type=\"checkbox\" name='checkbox0'>");
  199. $e->name = NULL;
  200. assert($e=='<input type="checkbox">');
  201. $e->type = NULL;
  202. assert($e=="<input>");
  203. // -----------------------------------------------------------------------------
  204. // extract text
  205. $str = <<<HTML
  206. <b>okok</b>
  207. HTML;
  208. $html->load($str);
  209. assert($html==$str);
  210. assert($html->plaintext=='okok');
  211. $str = <<<HTML
  212. <div><b>okok</b></div>
  213. HTML;
  214. $html->load($str);
  215. assert($html==$str);
  216. assert($html->plaintext=='okok');
  217. $str = <<<HTML
  218. <div><b>okok</b>
  219. HTML;
  220. $html->load($str);
  221. assert($html==$str);
  222. assert($html->plaintext=='okok');
  223. $str = <<<HTML
  224. <b>okok</b></div>
  225. HTML;
  226. $html->load($str);
  227. assert($html==$str);
  228. assert($html->plaintext=='okok</div>');
  229. // -----------------------------------------------------------------------------
  230. // old fashion camel naming conventions test
  231. $str = <<<HTML
  232. <input type="checkbox" id="checkbox" name="checkbox" value="checkbox" checked>
  233. <input type="checkbox" id="checkbox1" name="checkbox1" value="checkbox1">
  234. <input type="checkbox" id="checkbox2" name="checkbox2" value="checkbox2" checked>
  235. HTML;
  236. $html->load($str);
  237. assert($html==$str);
  238. assert($html->getElementByTagName('input')->hasAttribute('checked')==true);
  239. assert($html->getElementsByTagName('input', 1)->hasAttribute('checked')==false);
  240. assert($html->getElementsByTagName('input', 1)->hasAttribute('not_exist')==false);
  241. assert($html->find('input', 0)->value==$html->getElementByTagName('input')->getAttribute('value'));
  242. assert($html->find('input', 1)->value==$html->getElementsByTagName('input', 1)->getAttribute('value'));
  243. assert($html->find('#checkbox1', 0)->value==$html->getElementById('checkbox1')->getAttribute('value'));
  244. assert($html->find('#checkbox2', 0)->value==$html->getElementsById('checkbox2', 0)->getAttribute('value'));
  245. $e = $html->find('[name=checkbox]', 0);
  246. assert($e->getAttribute('value')=='checkbox');
  247. assert($e->getAttribute('checked')==true);
  248. assert($e->getAttribute('not_exist')=='');
  249. $e->setAttribute('value', 'okok');
  250. assert($e=='<input type="checkbox" id="checkbox" name="checkbox" value="okok" checked>');
  251. $e->setAttribute('checked', false);
  252. assert($e=='<input type="checkbox" id="checkbox" name="checkbox" value="okok">');
  253. $e->setAttribute('checked', true);
  254. assert($e=='<input type="checkbox" id="checkbox" name="checkbox" value="okok" checked>');
  255. $e->removeAttribute('value');
  256. assert($e=='<input type="checkbox" id="checkbox" name="checkbox" checked>');
  257. $e->removeAttribute('checked');
  258. assert($e=='<input type="checkbox" id="checkbox" name="checkbox">');
  259. // -----------------------------------------------
  260. $str = <<<HTML
  261. <div id="div1">
  262. <div id="div10"></div>
  263. <div id="div11"></div>
  264. <div id="div12"></div>
  265. </div>
  266. HTML;
  267. $html->load($str);
  268. assert($html==$str);
  269. $e = $html->find('div#div1', 0);
  270. assert($e->firstChild()->getAttribute('id')=='div10');
  271. assert($e->lastChild()->getAttribute('id')=='div12');
  272. assert($e->nextSibling()==null);
  273. assert($e->previousSibling()==null);
  274. // -----------------------------------------------
  275. $str = <<<HTML
  276. <div id="div0">
  277. <div id="div00"></div>
  278. </div>
  279. <div id="div1">
  280. <div id="div10"></div>
  281. <div id="div11">
  282. <div id="div110"></div>
  283. <div id="div111">
  284. <div id="div1110"></div>
  285. <div id="div1111"></div>
  286. <div id="div1112"></div>
  287. </div>
  288. <div id="div112"></div>
  289. </div>
  290. <div id="div12"></div>
  291. </div>
  292. <div id="div2"></div>
  293. HTML;
  294. $html->load($str);
  295. assert($html==$str);
  296. assert($html->getElementById("div1")->hasAttribute('id')==true);
  297. assert($html->getElementById("div1")->hasAttribute('not_exist')==false);
  298. assert($html->getElementById("div1")->getAttribute('id')=='div1');
  299. assert($html->getElementById("div1")->childNodes(0)->getAttribute('id')=='div10');
  300. assert($html->getElementById("div1")->childNodes(1)->childNodes(1)->getAttribute('id')=='div111');
  301. assert($html->getElementById("div1")->childNodes(1)->childNodes(1)->childNodes(2)->getAttribute('id')=='div1112');
  302. assert($html->getElementsById("div1", 0)->childNodes(1)->id=='div11');
  303. assert($html->getElementsById("div1", 0)->childNodes(1)->childNodes(1)->getAttribute('id')=='div111');
  304. assert($html->getElementsById("div1", 0)->childNodes(1)->childNodes(1)->childNodes(1)->getAttribute('id')=='div1111');
  305. // -----------------------------------------------
  306. $str = <<<HTML
  307. <ul class="menublock">
  308. </li>
  309. <ul>
  310. <li>
  311. <a href="http://www.cyberciti.biz/tips/pollsarchive">Polls Archive</a>
  312. </li>
  313. </ul>
  314. </li>
  315. </ul>
  316. HTML;
  317. $html->load($str);
  318. $ul = $html->find('ul', 0);
  319. assert($ul->first_child()->tag==='ul');
  320. // -----------------------------------------------
  321. $str = <<<HTML
  322. <ul>
  323. <li>Item 1
  324. <ul>
  325. <li>Sub Item 1 </li>
  326. <li>Sub Item 2 </li>
  327. </ul>
  328. </li>
  329. <li>Item 2 </li>
  330. </ul>
  331. HTML;
  332. $html->load($str);
  333. assert($html==$str);
  334. $ul = $html->find('ul', 0);
  335. assert($ul->first_child()->tag==='li');
  336. assert($ul->first_child()->next_sibling()->tag==='li');
  337. // -----------------------------------------------------------------------------
  338. // tear down
  339. $html->clear();
  340. unset($html);
  341. ?>