6c06139064a5584d35e05b9bfd22e428de51c08b.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. // "\t" or "\n" in tag test
  10. // std selector test 1
  11. $str = <<<HTML
  12. <img
  13. class="class0" id="id0" src="src0">
  14. <img
  15. class="class1" id="id1" src="src1">
  16. <img class="class2" id="id2" src="src2">
  17. HTML;
  18. $dom->load($str);
  19. $e = $dom->find('img');
  20. assert(count($e)==3);
  21. // -----------------------------------------------------------------------------
  22. // std selector test
  23. $str = <<<HTML
  24. <img class="class0" id="id0" src="src0">
  25. <img class="class1" id="id1" src="src1">
  26. <img class="class2" id="id2" src="src2">
  27. HTML;
  28. $dom->load($str);
  29. // -----------------------------------------------
  30. // all
  31. //$e = $dom->find('*');
  32. //assert(count($e)==3);
  33. // -----------------------------------------------
  34. // tag
  35. assert(count($dom->find('img'))==3);
  36. // -----------------------------------------------
  37. // class
  38. $es = $dom->find('img.class0');
  39. assert(count($es)==1);
  40. assert($es[0]->src=='src0');
  41. assert($es[0]->innertext=='');
  42. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  43. $es = $dom->find('.class0');
  44. assert(count($es)==1);
  45. assert($es[0]->src=='src0');
  46. assert($es[0]->innertext=='');
  47. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  48. // -----------------------------------------------
  49. // id
  50. $es = $dom->find('img#id1');
  51. assert(count($es)==1);
  52. assert($es[0]->src=='src1');
  53. assert($es[0]->innertext=='');
  54. assert($es[0]->outertext=='<img class="class1" id="id1" src="src1">');
  55. $es = $dom->find('#id2');
  56. assert(count($es)==1);
  57. assert($es[0]->src=='src2');
  58. assert($es[0]->innertext=='');
  59. assert($es[0]->outertext=='<img class="class2" id="id2" src="src2">');
  60. // -----------------------------------------------
  61. // attr
  62. $es = $dom->find('img[src="src0"]');
  63. assert(count($es)==1);
  64. assert($es[0]->src=='src0');
  65. assert($es[0]->innertext=='');
  66. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  67. $es = $dom->find('img[src=src0]');
  68. assert(count($es)==1);
  69. assert($es[0]->src=='src0');
  70. assert($es[0]->innertext=='');
  71. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  72. $es = $dom->find('[src=src0]');
  73. assert(count($es)==1);
  74. assert($es[0]->src=='src0');
  75. assert($es[0]->innertext=='');
  76. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  77. $es = $dom->find('[src="src0"]');
  78. assert(count($es)==1);
  79. assert($es[0]->src=='src0');
  80. assert($es[0]->innertext=='');
  81. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  82. // -----------------------------------------------
  83. // text
  84. $str = <<<HTML
  85. <b>text1</b><b>text2</b>
  86. HTML;
  87. $dom->load($str);
  88. $es = $dom->find('text');
  89. assert(count($es)==2);
  90. assert($es[0]->innertext=='text1');
  91. assert($es[0]->outertext=='text1');
  92. assert($es[0]->plaintext=='text1');
  93. assert($es[1]->innertext=='text2');
  94. assert($es[1]->outertext=='text2');
  95. assert($es[1]->plaintext=='text2');
  96. $str = <<<HTML
  97. <b>text1</b><b>text2</b>
  98. HTML;
  99. $dom->load($str);
  100. $es = $dom->find('b text');
  101. assert(count($es)==2);
  102. assert($es[0]->innertext=='text1');
  103. assert($es[0]->outertext=='text1');
  104. assert($es[0]->plaintext=='text1');
  105. assert($es[1]->innertext=='text2');
  106. assert($es[1]->outertext=='text2');
  107. assert($es[1]->plaintext=='text2');
  108. // -----------------------------------------------
  109. // xml namespace test
  110. $str = <<<HTML
  111. <bw:bizy id="date">text</bw:bizy>
  112. HTML;
  113. $dom->load($str);
  114. $es = $dom->find('bw:bizy');
  115. assert(count($es)==0);
  116. // -----------------------------------------------
  117. // user defined tag name test
  118. $str = <<<HTML
  119. <div_test id="1">text</div_test>
  120. HTML;
  121. $dom->load($str);
  122. $es = $dom->find('div_test');
  123. assert(count($es)==1);
  124. assert($es[0]->id=='1');
  125. // -----------------------------------------------
  126. $str = <<<HTML
  127. <div-test id="1">text</div-test>
  128. HTML;
  129. $dom->load($str);
  130. $es = $dom->find('div-test');
  131. assert(count($es)==1);
  132. assert($es[0]->id=='1');
  133. // -----------------------------------------------
  134. $str = <<<HTML
  135. <div::test id="1">text</div::test>
  136. HTML;
  137. $dom->load($str);
  138. $es = $dom->find('div::test');
  139. assert(count($es)==0);
  140. // -----------------------------------------------
  141. // find all occurrences of id="1" regardless of the tag
  142. $str = <<<HTML
  143. <img class="class0" id="1" src="src0">
  144. <img class="class1" id="2" src="src1">
  145. <div class="class2" id="1">ok</div>
  146. HTML;
  147. $dom->load($str);
  148. $es = $dom->find('[id=1]');
  149. assert(count($es)==2);
  150. assert($es[0]->tag=='img');
  151. assert($es[1]->tag=='div');
  152. // -----------------------------------------------------------------------------
  153. // multiple selector test
  154. $str = <<<HTML
  155. <div class="class0" id="id0" >
  156. <div class="class1" id="id1">
  157. <div class="class2" id="id2">ok</div>
  158. </div>
  159. </div>
  160. HTML;
  161. $dom->load($str);
  162. $es = $dom->find('div');
  163. assert(count($es)==3);
  164. assert($es[0]->id=='id0');
  165. assert($es[1]->id=='id1');
  166. assert($es[2]->id=='id2');
  167. $es = $dom->find('div div');
  168. assert(count($es)==2);
  169. assert($es[0]->id=='id1');
  170. assert($es[1]->id=='id2');
  171. $es = $dom->find('div div div');
  172. assert(count($es)==1);
  173. assert($es[0]->id=='id2');
  174. // -----------------------------------------------------------------------------
  175. // multiple selector test 2
  176. $str = <<<HTML
  177. <table>
  178. <tr>
  179. <td>0</td>
  180. <td>1</td>
  181. </tr>
  182. </table>
  183. <table>
  184. <tr>
  185. <td>2</td>
  186. <td>3</td>
  187. </tr>
  188. </table>
  189. HTML;
  190. $dom->load($str);
  191. $es = $dom->find('table td');
  192. assert(count($es)==4);
  193. assert($es[0]->innertext=='0');
  194. assert($es[1]->innertext=='1');
  195. assert($es[2]->innertext=='2');
  196. assert($es[3]->innertext=='3');
  197. // -----------------------------------------------------------------------------
  198. // multiple selector test 3
  199. $str = <<<HTML
  200. <table>
  201. <tr>
  202. <td>
  203. <table class="hello">
  204. <tr>
  205. <td>0</td>
  206. <td>1</td>
  207. </tr>
  208. </table>
  209. </td>
  210. </tr>
  211. </table>
  212. <table class="hello">
  213. <tr>
  214. <td>2</td>
  215. <td>3</td>
  216. </tr>
  217. </table>
  218. HTML;
  219. $dom = str_get_dom($str);
  220. $es = $dom->find('table.hello td');
  221. assert(count($es)==4);
  222. assert($es[0]->innertext=='0');
  223. assert($es[1]->innertext=='1');
  224. assert($es[2]->innertext=='2');
  225. assert($es[3]->innertext=='3');
  226. // -----------------------------------------------------------------------------
  227. // nested selector test
  228. $str = <<<HTML
  229. <ul>
  230. <li>0</li>
  231. <li>1</li>
  232. </ul>
  233. <ul>
  234. <li>2</li>
  235. <li>3</li>
  236. </ul>
  237. HTML;
  238. $dom = str_get_dom($str);
  239. $es= $dom->find('ul');
  240. assert(count($es)==2);
  241. foreach($es as $n) {
  242. $li = $n->find('li');
  243. assert(count($li)==2);
  244. }
  245. $es= $dom->find('li');
  246. assert(count($es)==4);
  247. assert($es[0]->innertext=='0');
  248. assert($es[1]->innertext=='1');
  249. assert($es[2]->innertext=='2');
  250. assert($es[3]->innertext=='3');
  251. assert($es[0]->outertext=='<li>0</li>');
  252. assert($es[1]->outertext=='<li>1</li>');
  253. assert($es[2]->outertext=='<li>2</li>');
  254. assert($es[3]->outertext=='<li>3</li>');
  255. $counter = 0;
  256. foreach($dom->find('ul') as $ul) {
  257. foreach($ul->find('li') as $li) {
  258. assert($li->innertext=="$counter");
  259. assert($li->outertext=="<li>$counter</li>");
  260. ++$counter;
  261. }
  262. }
  263. // -----------------------------------------------------------------------------
  264. // [attribute=value] selector
  265. $str = <<<HTML
  266. <input type="radio" name="newsletter" value="Hot Fuzz" />
  267. <input type="radio" name="newsletters" value="Cold Fusion" />
  268. <input type="radio" name="accept" value="Evil Plans" />
  269. HTML;
  270. $dom->load($str);
  271. $es = $dom->find('[name=newsletter]');
  272. assert(count($es)==1);
  273. assert($es[0]->name=='newsletter');
  274. assert($es[0]->value=='Hot Fuzz');
  275. assert($es[0]->outertext=='<input type="radio" name="newsletter" value="Hot Fuzz">');
  276. $es = $dom->find('[name="newsletter"]');
  277. assert(count($es)==1);
  278. assert($es[0]->name=='newsletter');
  279. assert($es[0]->value=='Hot Fuzz');
  280. assert($es[0]->outertext=='<input type="radio" name="newsletter" value="Hot Fuzz">');
  281. // -----------------------------------------------------------------------------
  282. // [attribute!=value] selector
  283. $str = <<<HTML
  284. <input type="radio" name="newsletter" value="Hot Fuzz" />
  285. <input type="radio" name="newsletter" value="Cold Fusion" />
  286. <input type="radio" name="accept" value="Evil Plans" />
  287. HTML;
  288. $dom->load($str);
  289. $es = $dom->find('[name!=newsletter]');
  290. assert(count($es)==1);
  291. assert($es[0]->name=='accept');
  292. assert($es[0]->value=='Evil Plans');
  293. assert($es[0]->outertext=='<input type="radio" name="accept" value="Evil Plans">');
  294. $es = $dom->find('[name!="newsletter"]');
  295. assert(count($es)==1);
  296. assert($es[0]->name=='accept');
  297. assert($es[0]->value=='Evil Plans');
  298. assert($es[0]->outertext=='<input type="radio" name="accept" value="Evil Plans">');
  299. $es = $dom->find("[name!='newsletter']");
  300. assert(count($es)==1);
  301. assert($es[0]->name=='accept');
  302. assert($es[0]->value=='Evil Plans');
  303. assert($es[0]->outertext=='<input type="radio" name="accept" value="Evil Plans">');
  304. // -----------------------------------------------------------------------------
  305. // [attribute^=value] selector
  306. $str = <<<HTML
  307. <input name="newsletter" />
  308. <input name="milkman" />
  309. <input name="newsboy" />
  310. HTML;
  311. $dom->load($str);
  312. $es = $dom->find('[name^=news]');
  313. assert(count($es)==2);
  314. assert($es[0]->name=='newsletter');
  315. assert($es[0]->outertext=='<input name="newsletter">');
  316. assert($es[1]->name=='newsboy');
  317. assert($es[1]->outertext=='<input name="newsboy">');
  318. $es = $dom->find('[name^="news"]');
  319. assert(count($es)==2);
  320. assert($es[0]->name=='newsletter');
  321. assert($es[0]->outertext=='<input name="newsletter">');
  322. assert($es[1]->name=='newsboy');
  323. assert($es[1]->outertext=='<input name="newsboy">');
  324. // -----------------------------------------------------------------------------
  325. // [attribute$=value] selector
  326. $str = <<<HTML
  327. <input name="newsletter" />
  328. <input name="milkman" />
  329. <input name="jobletter" />
  330. HTML;
  331. $dom->load($str);
  332. $es = $dom->find('[name$=letter]');
  333. assert(count($es)==2);
  334. assert($es[0]->name=='newsletter');
  335. assert($es[0]->outertext=='<input name="newsletter">');
  336. assert($es[1]->name=='jobletter');
  337. assert($es[1]->outertext=='<input name="jobletter">');
  338. $es = $dom->find('[name$="letter"]');
  339. assert(count($es)==2);
  340. assert($es[0]->name=='newsletter');
  341. assert($es[0]->outertext=='<input name="newsletter">');
  342. assert($es[1]->name=='jobletter');
  343. assert($es[1]->outertext=='<input name="jobletter">');
  344. // -----------------------------------------------------------------------------
  345. // [attribute*=value] selector
  346. $str = <<<HTML
  347. <input name="man-news" />
  348. <input name="milkman" />
  349. <input name="letterman2" />
  350. <input name="newmilk" />
  351. <div class="foo hello bar"></div>
  352. <div class="foo bar hello"></div>
  353. <div class="hello foo bar"></div>
  354. HTML;
  355. $dom->load($str);
  356. $es = $dom->find('[name*=man]');
  357. assert(count($es)==3);
  358. assert($es[0]->name=='man-news');
  359. assert($es[0]->outertext=='<input name="man-news">');
  360. assert($es[1]->name=='milkman');
  361. assert($es[1]->outertext=='<input name="milkman">');
  362. assert($es[2]->name=='letterman2');
  363. assert($es[2]->outertext=='<input name="letterman2">');
  364. $es = $dom->find('[name*="man"]');
  365. assert(count($es)==3);
  366. assert($es[0]->name=='man-news');
  367. assert($es[0]->outertext=='<input name="man-news">');
  368. assert($es[1]->name=='milkman');
  369. assert($es[1]->outertext=='<input name="milkman">');
  370. assert($es[2]->name=='letterman2');
  371. assert($es[2]->outertext=='<input name="letterman2">');
  372. $es = $dom->find('[class*=hello]');
  373. assert($es[0]->outertext=='<div class="foo hello bar"></div>');
  374. assert($es[1]->outertext=='<div class="foo bar hello"></div>');
  375. assert($es[2]->outertext=='<div class="hello foo bar"></div>');
  376. // -----------------------------------------------------------------------------
  377. // Testcase for '[]' names element
  378. // normal checkbox
  379. $str = <<<HTML
  380. <input type="checkbox" name="news" value="foo" />
  381. <input type="checkbox" name="news" value="bar">
  382. <input type="checkbox" name="news" value="baz" />
  383. HTML;
  384. $dom->load($str);
  385. $es = $dom->find('[name=news]');
  386. assert(count($es)==3);
  387. assert($es[0]->name=='news');
  388. assert($es[0]->value=='foo');
  389. assert($es[1]->name=='news');
  390. assert($es[1]->value=='bar');
  391. assert($es[2]->name=='news');
  392. assert($es[2]->value=='baz');
  393. // -----------------------------------------------------------------------------
  394. // with '[]' names checkbox
  395. $str = <<<HTML
  396. <input type="checkbox" name="news[]" value="foo" />
  397. <input type="checkbox" name="news[]" value="bar">
  398. <input type="checkbox" name="news[]" value="baz" />
  399. HTML;
  400. $dom->load($str);
  401. $es = $dom->find('[name=news[]]');
  402. assert(count($es)==3);
  403. assert($es[0]->name=='news[]');
  404. assert($es[0]->value=='foo');
  405. assert($es[1]->name=='news[]');
  406. assert($es[1]->value=='bar');
  407. assert($es[2]->name=='news[]');
  408. assert($es[2]->value=='baz');
  409. // -----------------------------------------------------------------------------
  410. // with '[]' names checkbox 2
  411. $str = <<<HTML
  412. <input type="checkbox" name="news[foo]" value="foo" />
  413. <input type="checkbox" name="news[bar]" value="bar">
  414. HTML;
  415. $dom->load($str);
  416. $es = $dom->find('[name=news[foo]]');
  417. assert(count($es)==1);
  418. assert($es[0]->name=='news[foo]');
  419. assert($es[0]->value=='foo');
  420. // -----------------------------------------------------------------------------
  421. // regular expression syntax escaping
  422. $str = <<<HTML
  423. <div>
  424. <a href="image/one.png">one</a>
  425. <a href="image/two.jpg">two</a>
  426. <a href="/favorites/aaa">three (text)</a>
  427. </div>
  428. HTML;
  429. $dom->load($str);
  430. assert(count($dom->find('a[href^="image/"]'))==2);
  431. assert(count($dom->find('a[href*="/favorites/"]'))==1);
  432. // -----------------------------------------------------------------------------
  433. // multiple class test
  434. $str = <<<HTML
  435. <div class="hello">should verify</div>
  436. <div class="foo hello bar">should verify</div>
  437. <div class="foo bar hello">should verify</div>
  438. <div class="hello foo bar">should verify</div>
  439. <div class="helloworld">should not verify</div>
  440. <div class="worldhello">should not verify</div>
  441. <div class="worldhelloworld">should not verify</div>
  442. HTML;
  443. $dom->load($str);
  444. $es = $dom->find('[class="hello"],[class*="hello "],[class*=" hello"]');
  445. assert(count($es)==4);
  446. assert($es[0]->class=='hello');
  447. assert($es[1]->class=='foo hello bar');
  448. assert($es[2]->class=='foo bar hello');
  449. assert($es[3]->class=='hello foo bar');
  450. $es = $dom->find('.hello');
  451. assert(count($es)==4);
  452. assert($es[0]->class=='hello');
  453. assert($es[1]->class=='foo hello bar');
  454. assert($es[2]->class=='foo bar hello');
  455. assert($es[3]->class=='hello foo bar');
  456. // -----------------------------------------------------------------------------
  457. // multiple class test 2
  458. $str = <<<HTML
  459. <div class="aa bb"></div>
  460. HTML;
  461. $dom->load($str);
  462. assert(count($dom->find('[class=aa]'))==1);
  463. assert(count($dom->find('[class=bb]'))==1);
  464. assert(count($dom->find('[class="aa bb"]'))==1);
  465. assert(count($dom->find('[class=aa], [class=bb]'))==1);
  466. // -----------------------------------------------------------------------------
  467. // multiple selector test
  468. $str = <<<HTML
  469. <p>aaa</p>
  470. <b>bbb</b>
  471. <i>ccc</i>
  472. HTML;
  473. $dom->load($str);
  474. $es = $dom->find('p,b,i');
  475. assert(count($es)==3);
  476. assert($es[0]->tag=='p');
  477. assert($es[1]->tag=='b');
  478. assert($es[2]->tag=='i');
  479. $es = $dom->find('p, b, i');
  480. assert(count($es)==3);
  481. assert($es[0]->tag=='p');
  482. assert($es[1]->tag=='b');
  483. assert($es[2]->tag=='i');
  484. $es = $dom->find('p, b , i');
  485. assert(count($es)==3);
  486. assert($es[0]->tag=='p');
  487. assert($es[1]->tag=='b');
  488. assert($es[2]->tag=='i');
  489. $es = $dom->find('p ,b ,i');
  490. assert(count($es)==3);
  491. assert($es[0]->tag=='p');
  492. assert($es[1]->tag=='b');
  493. assert($es[2]->tag=='i');
  494. $es = $dom->find('b,p,i');
  495. assert(count($es)==3);
  496. assert($es[0]->tag=='p');
  497. assert($es[1]->tag=='b');
  498. assert($es[2]->tag=='i');
  499. $es = $dom->find('i,b,p');
  500. assert(count($es)==3);
  501. assert($es[0]->tag=='p');
  502. assert($es[1]->tag=='b');
  503. assert($es[2]->tag=='i');
  504. $es = $dom->find('p,b,i,p,b');
  505. assert(count($es)==3);
  506. assert($es[0]->tag=='p');
  507. assert($es[1]->tag=='b');
  508. assert($es[2]->tag=='i');
  509. // -----------------------------------------------------------------------------
  510. // multiple selector test 2
  511. $str = <<<HTML
  512. <img title="aa" src="src">
  513. <a href="href" title="aa"></a>
  514. HTML;
  515. $dom->load($str);
  516. assert(count($dom->find('a[title], img[title]'))==2);
  517. // -----------------------------------------------------------------------------
  518. // tear down
  519. $dom->clear();
  520. unset($dom);
  521. ?>