d38e274dbdc5421f4668c5ae90de14bd74c87f02.svn-base 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?php
  2. // $Rev: 172 $
  3. // -----------------------------------------------------------------------------
  4. // setup
  5. error_reporting(E_ALL);
  6. require_once('../simple_html_dom.php');
  7. $dom = new simple_html_dom;
  8. // -----------------------------------------------------------------------------
  9. // "\t" or "\n" in tag test
  10. $str = <<<HTML
  11. <img
  12. class="class0" id="id0" src="src0">
  13. <img
  14. class="class1" id="id1" src="src1">
  15. <img class="class2" id="id2" src="src2">
  16. HTML;
  17. $dom->load($str);
  18. $e = $dom->find('img');
  19. assert(count($e)==3);
  20. // -----------------------------------------------------------------------------
  21. // std selector test
  22. $str = <<<HTML
  23. <div>
  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. </div>
  28. HTML;
  29. $dom->load($str);
  30. // -----------------------------------------------
  31. // wildcard
  32. assert(count($dom->find('*'))==1);
  33. assert(count($dom->find('div *'))==3);
  34. assert(count($dom->find('div img *'))==0);
  35. assert(count($dom->find(' * '))==1);
  36. assert(count($dom->find(' div * '))==3);
  37. assert(count($dom->find(' div img *'))==0);
  38. // -----------------------------------------------
  39. // tag
  40. assert(count($dom->find('img'))==3);
  41. assert(count($dom->find('text'))==4);
  42. // -----------------------------------------------
  43. // class
  44. $es = $dom->find('img.class0');
  45. assert(count($es)==1);
  46. assert($es[0]->src=='src0');
  47. assert($es[0]->innertext=='');
  48. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  49. $es = $dom->find('.class0');
  50. assert(count($es)==1);
  51. assert($es[0]->src=='src0');
  52. assert($es[0]->innertext=='');
  53. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  54. // -----------------------------------------------
  55. // index
  56. assert($dom->find('img', 0)->src=='src0');
  57. assert($dom->find('img', 1)->src=='src1');
  58. assert($dom->find('img', 2)->src=='src2');
  59. assert($dom->find('img', -3)->src=='src0');
  60. assert($dom->find('img', -2)->src=='src1');
  61. assert($dom->find('img', -1)->src=='src2');
  62. // -----------------------------------------------
  63. // id
  64. $es = $dom->find('img#id1');
  65. assert(count($es)==1);
  66. assert($es[0]->src=='src1');
  67. assert($es[0]->innertext=='');
  68. assert($es[0]->outertext=='<img class="class1" id="id1" src="src1">');
  69. $es = $dom->find('#id2');
  70. assert(count($es)==1);
  71. assert($es[0]->src=='src2');
  72. assert($es[0]->innertext=='');
  73. assert($es[0]->outertext=='<img class="class2" id="id2" src="src2">');
  74. // -----------------------------------------------
  75. // attr
  76. $es = $dom->find('img[src="src0"]');
  77. assert(count($es)==1);
  78. assert($es[0]->src=='src0');
  79. assert($es[0]->innertext=='');
  80. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  81. $es = $dom->find('img[src=src0]');
  82. assert(count($es)==1);
  83. assert($es[0]->src=='src0');
  84. assert($es[0]->innertext=='');
  85. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  86. // -----------------------------------------------------------------------------
  87. // wildcard
  88. $es = $dom->find('*[src]');
  89. assert(count($es)==3);
  90. $es = $dom->find('*[src=*]');
  91. assert(count($es)==3);
  92. $es = $dom->find('*[alt=*]');
  93. assert(count($es)==0);
  94. $es = $dom->find('*[src="src0"]');
  95. assert(count($es)==1);
  96. assert($es[0]->src=='src0');
  97. assert($es[0]->innertext=='');
  98. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  99. $es = $dom->find('*[src=src0]');
  100. assert(count($es)==1);
  101. assert($es[0]->src=='src0');
  102. assert($es[0]->innertext=='');
  103. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  104. $es = $dom->find('[src=src0]');
  105. assert(count($es)==1);
  106. assert($es[0]->src=='src0');
  107. assert($es[0]->innertext=='');
  108. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  109. $es = $dom->find('[src="src0"]');
  110. assert(count($es)==1);
  111. assert($es[0]->src=='src0');
  112. assert($es[0]->innertext=='');
  113. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  114. $es = $dom->find('*#id1');
  115. assert(count($es)==1);
  116. assert($es[0]->src=='src1');
  117. assert($es[0]->innertext=='');
  118. assert($es[0]->outertext=='<img class="class1" id="id1" src="src1">');
  119. $es = $dom->find('*.class0');
  120. assert(count($es)==1);
  121. assert($es[0]->src=='src0');
  122. assert($es[0]->innertext=='');
  123. assert($es[0]->outertext=='<img class="class0" id="id0" src="src0">');
  124. // -----------------------------------------------------------------------------
  125. // text
  126. $str = <<<HTML
  127. <b>text1</b><b>text2</b>
  128. HTML;
  129. $dom->load($str);
  130. $es = $dom->find('text');
  131. assert(count($es)==2);
  132. assert($es[0]->innertext=='text1');
  133. assert($es[0]->outertext=='text1');
  134. assert($es[0]->plaintext=='text1');
  135. assert($es[1]->innertext=='text2');
  136. assert($es[1]->outertext=='text2');
  137. assert($es[1]->plaintext=='text2');
  138. $str = <<<HTML
  139. <b>text1</b><b>text2</b>
  140. HTML;
  141. $dom->load($str);
  142. $es = $dom->find('b text');
  143. assert(count($es)==2);
  144. assert($es[0]->innertext=='text1');
  145. assert($es[0]->outertext=='text1');
  146. assert($es[0]->plaintext=='text1');
  147. assert($es[1]->innertext=='text2');
  148. assert($es[1]->outertext=='text2');
  149. assert($es[1]->plaintext=='text2');
  150. // -----------------------------------------------------------------------------
  151. // xml namespace test
  152. $str = <<<HTML
  153. <bw:bizy id="date">text</bw:bizy>
  154. HTML;
  155. $dom->load($str);
  156. $es = $dom->find('bw:bizy');
  157. assert(count($es)==1);
  158. assert($es[0]->id=='date');
  159. // -----------------------------------------------------------------------------
  160. // user defined tag name test
  161. $str = <<<HTML
  162. <div_test id="1">text</div_test>
  163. HTML;
  164. $dom->load($str);
  165. $es = $dom->find('div_test');
  166. assert(count($es)==1);
  167. assert($es[0]->id=='1');
  168. // -----------------------------------------------
  169. $str = <<<HTML
  170. <div-test id="1">text</div-test>
  171. HTML;
  172. $dom->load($str);
  173. $es = $dom->find('div-test');
  174. assert(count($es)==1);
  175. assert($es[0]->id=='1');
  176. // -----------------------------------------------
  177. $str = <<<HTML
  178. <div::test id="1">text</div::test>
  179. HTML;
  180. $dom->load($str);
  181. $es = $dom->find('div::test');
  182. assert(count($es)==1);
  183. assert($es[0]->id=='1');
  184. // -----------------------------------------------
  185. // find all occurrences of id="1" regardless of the tag
  186. $str = <<<HTML
  187. <img class="class0" id="1" src="src0">
  188. <img class="class1" id="2" src="src1">
  189. <div class="class2" id="1">ok</div>
  190. HTML;
  191. $dom->load($str);
  192. $es = $dom->find('[id=1]');
  193. assert(count($es)==2);
  194. assert($es[0]->tag=='img');
  195. assert($es[1]->tag=='div');
  196. // -----------------------------------------------------------------------------
  197. // multiple selector test
  198. $str = <<<HTML
  199. <div class="class0" id="id0" ><div class="class1" id="id1"><div class="class2" id="id2">ok</div><div style="st1 st2" id="id3"><span class="id4">ok</span></div></div></div>
  200. HTML;
  201. $dom->load($str);
  202. $es = $dom->find('div');
  203. assert(count($es)==4);
  204. assert($es[0]->id=='id0');
  205. assert($es[1]->id=='id1');
  206. assert($es[2]->id=='id2');
  207. $es = $dom->find('div div');
  208. assert(count($es)==3);
  209. assert($es[0]->id=='id1');
  210. assert($es[1]->id=='id2');
  211. $es = $dom->find('div div div');
  212. assert(count($es)==2);
  213. assert($es[0]->id=='id2');
  214. $es = $dom->find('.class0 .class1 .class2');
  215. assert(count($es)==1);
  216. assert($es[0]->id=='id2');
  217. $es = $dom->find('#id0 #id1 #id2');
  218. assert(count($es)==1);
  219. assert($es[0]->id=='id2');
  220. $es = $dom->find('div[id=id0] div[id=id1] div[id=id2]');
  221. assert(count($es)==1);
  222. assert($es[0]->id=='id2');
  223. $es = $dom->find('div[id="id0"] div[id="id1"] div[id="id2"]');
  224. assert(count($es)==1);
  225. assert($es[0]->id=='id2');
  226. $es = $dom->find('div[id=id0] div[id="id1"] div[id="id2"]');
  227. assert(count($es)==1);
  228. assert($es[0]->id=='id2');
  229. $es = $dom->find('div[id="id0"] div[id=id1] div[id="id2"]');
  230. assert(count($es)==1);
  231. assert($es[0]->id=='id2');
  232. $es = $dom->find('div[id="id0"] div[id="id1"] div[id=id2]');
  233. assert(count($es)==1);
  234. assert($es[0]->id=='id2');
  235. $es = $dom->find("div[id='id0'] div[id='id1'] div[id='id2']");
  236. assert(count($es)==1);
  237. assert($es[0]->id=='id2');
  238. $es = $dom->find('[id=id0] [id=id1] [id=id2]');
  239. assert(count($es)==1);
  240. assert($es[0]->id=='id2');
  241. $es = $dom->find('[id] [id] [id]');
  242. assert(count($es)==2);
  243. assert($es[0]->id=='id2');
  244. assert($es[1]->id=='id3');
  245. $es = $dom->find('[id=id0] [id=id1] [id=id3]');
  246. assert(count($es)==1);
  247. assert($es[0]->id=='id3');
  248. $es = $dom->find('[id=id0] [id=id1] [style="st1 st2"]');
  249. assert(count($es)==1);
  250. assert($es[0]->id=='id3');
  251. $es = $dom->find('[id=id0] [id=id1] [style=st1 st2]');
  252. assert(count($es)==1);
  253. assert($es[0]->id=='id3');
  254. $es = $dom->find('[id=id0] [id=id1] [style=st1 st2] span[class=id4]');
  255. assert(count($es)==1);
  256. assert($es[0]->innertext=='ok');
  257. $es = $dom->find('[id=id0] [id=id1] [style="st1 st2"] span[class="id4"]');
  258. assert(count($es)==1);
  259. assert($es[0]->innertext=='ok');
  260. // -----------------------------------------------
  261. $str = <<<HTML
  262. <table>
  263. <tr>
  264. <td>0</td>
  265. <td>1</td>
  266. </tr>
  267. </table>
  268. <table>
  269. <tr>
  270. <td>2</td>
  271. <td>3</td>
  272. </tr>
  273. </table>
  274. HTML;
  275. $dom->load($str);
  276. $es = $dom->find('table td');
  277. assert(count($es)==4);
  278. assert($es[0]->innertext=='0');
  279. assert($es[1]->innertext=='1');
  280. assert($es[2]->innertext=='2');
  281. assert($es[3]->innertext=='3');
  282. // -----------------------------------------------------------------------------
  283. // multiple selector test 3
  284. $str = <<<HTML
  285. <table>
  286. <tr>
  287. <td>
  288. <table class="hello">
  289. <tr>
  290. <td>0</td>
  291. <td>1</td>
  292. </tr>
  293. </table>
  294. </td>
  295. </tr>
  296. </table>
  297. <table class="hello">
  298. <tr>
  299. <td>2</td>
  300. <td>3</td>
  301. </tr>
  302. </table>
  303. HTML;
  304. $dom = str_get_html($str);
  305. $es = $dom->find('table.hello td');
  306. assert(count($es)==4);
  307. assert($es[0]->innertext=='0');
  308. assert($es[1]->innertext=='1');
  309. assert($es[2]->innertext=='2');
  310. assert($es[3]->innertext=='3');
  311. // -----------------------------------------------------------------------------
  312. // nested selector test
  313. $str = <<<HTML
  314. <ul>
  315. <li>0</li>
  316. <li>1</li>
  317. </ul>
  318. <ul>
  319. <li>2</li>
  320. <li>3</li>
  321. </ul>
  322. HTML;
  323. $dom = str_get_html($str);
  324. $es= $dom->find('ul');
  325. assert(count($es)==2);
  326. foreach($es as $n) {
  327. $li = $n->find('li');
  328. assert(count($li)==2);
  329. }
  330. $es= $dom->find('li');
  331. assert(count($es)==4);
  332. assert($es[0]->innertext=='0');
  333. assert($es[1]->innertext=='1');
  334. assert($es[2]->innertext=='2');
  335. assert($es[3]->innertext=='3');
  336. assert($es[0]->outertext=='<li>0</li>');
  337. assert($es[1]->outertext=='<li>1</li>');
  338. assert($es[2]->outertext=='<li>2</li>');
  339. assert($es[3]->outertext=='<li>3</li>');
  340. $counter = 0;
  341. foreach($dom->find('ul') as $ul) {
  342. foreach($ul->find('li') as $li) {
  343. assert($li->innertext=="$counter");
  344. assert($li->outertext=="<li>$counter</li>");
  345. ++$counter;
  346. }
  347. }
  348. // -----------------------------------------------------------------------------
  349. // [attribute=value] selector
  350. $str = <<<HTML
  351. <input type="radio" name="newsletter" value="Hot Fuzz" />
  352. <input type="radio" name="newsletters" value="Cold Fusion" />
  353. <input type="radio" name="accept" value="Evil Plans" />
  354. HTML;
  355. $dom->load($str);
  356. $es = $dom->find('[name=newsletter]');
  357. assert(count($es)==1);
  358. assert($es[0]->name=='newsletter');
  359. assert($es[0]->value=='Hot Fuzz');
  360. assert($es[0]->outertext=='<input type="radio" name="newsletter" value="Hot Fuzz" />');
  361. $es = $dom->find('[name="newsletter"]');
  362. assert(count($es)==1);
  363. assert($es[0]->name=='newsletter');
  364. assert($es[0]->value=='Hot Fuzz');
  365. assert($es[0]->outertext=='<input type="radio" name="newsletter" value="Hot Fuzz" />');
  366. // -----------------------------------------------------------------------------
  367. // [attribute!=value] selector
  368. $str = <<<HTML
  369. <input type="radio" name="newsletter" value="Hot Fuzz" />
  370. <input type="radio" name="newsletter" value="Cold Fusion" />
  371. <input type="radio" name="accept" value="Evil Plans" />
  372. HTML;
  373. $dom->load($str);
  374. $es = $dom->find('[name!=newsletter]');
  375. assert(count($es)==1);
  376. assert($es[0]->name=='accept');
  377. assert($es[0]->value=='Evil Plans');
  378. assert($es[0]->outertext=='<input type="radio" name="accept" value="Evil Plans" />');
  379. $es = $dom->find('[name!="newsletter"]');
  380. assert(count($es)==1);
  381. assert($es[0]->name=='accept');
  382. assert($es[0]->value=='Evil Plans');
  383. assert($es[0]->outertext=='<input type="radio" name="accept" value="Evil Plans" />');
  384. $es = $dom->find("[name!='newsletter']");
  385. assert(count($es)==1);
  386. assert($es[0]->name=='accept');
  387. assert($es[0]->value=='Evil Plans');
  388. assert($es[0]->outertext=='<input type="radio" name="accept" value="Evil Plans" />');
  389. // -----------------------------------------------------------------------------
  390. // [attribute^=value] selector
  391. $str = <<<HTML
  392. <input name="newsletter" />
  393. <input name="milkman" />
  394. <input name="newsboy" />
  395. HTML;
  396. $dom->load($str);
  397. $es = $dom->find('[name^=news]');
  398. assert(count($es)==2);
  399. assert($es[0]->name=='newsletter');
  400. assert($es[0]->outertext=='<input name="newsletter" />');
  401. assert($es[1]->name=='newsboy');
  402. assert($es[1]->outertext=='<input name="newsboy" />');
  403. $es = $dom->find('[name^="news"]');
  404. assert(count($es)==2);
  405. assert($es[0]->name=='newsletter');
  406. assert($es[0]->outertext=='<input name="newsletter" />');
  407. assert($es[1]->name=='newsboy');
  408. assert($es[1]->outertext=='<input name="newsboy" />');
  409. // -----------------------------------------------------------------------------
  410. // [attribute$=value] selector
  411. $str = <<<HTML
  412. <input name="newsletter" />
  413. <input name="milkman" />
  414. <input name="jobletter" />
  415. HTML;
  416. $dom->load($str);
  417. $es = $dom->find('[name$=letter]');
  418. assert(count($es)==2);
  419. assert($es[0]->name=='newsletter');
  420. assert($es[0]->outertext=='<input name="newsletter" />');
  421. assert($es[1]->name=='jobletter');
  422. assert($es[1]->outertext=='<input name="jobletter" />');
  423. $es = $dom->find('[name$="letter"]');
  424. assert(count($es)==2);
  425. assert($es[0]->name=='newsletter');
  426. assert($es[0]->outertext=='<input name="newsletter" />');
  427. assert($es[1]->name=='jobletter');
  428. assert($es[1]->outertext=='<input name="jobletter" />');
  429. // -----------------------------------------------------------------------------
  430. // [attribute*=value] selector
  431. $str = <<<HTML
  432. <input name="man-news" />
  433. <input name="milkman" />
  434. <input name="letterman2" />
  435. <input name="newmilk" />
  436. <div class="foo hello bar"></div>
  437. <div class="foo bar hello"></div>
  438. <div class="hello foo bar"></div>
  439. HTML;
  440. $dom->load($str);
  441. $es = $dom->find('[name*=man]');
  442. assert(count($es)==3);
  443. assert($es[0]->name=='man-news');
  444. assert($es[0]->outertext=='<input name="man-news" />');
  445. assert($es[1]->name=='milkman');
  446. assert($es[1]->outertext=='<input name="milkman" />');
  447. assert($es[2]->name=='letterman2');
  448. assert($es[2]->outertext=='<input name="letterman2" />');
  449. $es = $dom->find('[name*="man"]');
  450. assert(count($es)==3);
  451. assert($es[0]->name=='man-news');
  452. assert($es[0]->outertext=='<input name="man-news" />');
  453. assert($es[1]->name=='milkman');
  454. assert($es[1]->outertext=='<input name="milkman" />');
  455. assert($es[2]->name=='letterman2');
  456. assert($es[2]->outertext=='<input name="letterman2" />');
  457. $es = $dom->find('[class*=hello]');
  458. assert($es[0]->outertext=='<div class="foo hello bar"></div>');
  459. assert($es[1]->outertext=='<div class="foo bar hello"></div>');
  460. assert($es[2]->outertext=='<div class="hello foo bar"></div>');
  461. // -----------------------------------------------------------------------------
  462. // Testcase for '[]' names element
  463. // normal checkbox
  464. $str = <<<HTML
  465. <input type="checkbox" name="news" value="foo" />
  466. <input type="checkbox" name="news" value="bar">
  467. <input type="checkbox" name="news" value="baz" />
  468. HTML;
  469. $dom->load($str);
  470. $es = $dom->find('[name=news]');
  471. assert(count($es)==3);
  472. assert($es[0]->name=='news');
  473. assert($es[0]->value=='foo');
  474. assert($es[1]->name=='news');
  475. assert($es[1]->value=='bar');
  476. assert($es[2]->name=='news');
  477. assert($es[2]->value=='baz');
  478. // -----------------------------------------------------------------------------
  479. // with '[]' names checkbox
  480. $str = <<<HTML
  481. <input type="checkbox" name="news[]" value="foo" />
  482. <input type="checkbox" name="news[]" value="bar">
  483. <input type="checkbox" name="news[]" value="baz" />
  484. HTML;
  485. $dom->load($str);
  486. $es = $dom->find('[name=news[]]');
  487. assert(count($es)==3);
  488. assert($es[0]->name=='news[]');
  489. assert($es[0]->value=='foo');
  490. assert($es[1]->name=='news[]');
  491. assert($es[1]->value=='bar');
  492. assert($es[2]->name=='news[]');
  493. assert($es[2]->value=='baz');
  494. // -----------------------------------------------------------------------------
  495. // with '[]' names checkbox 2
  496. $str = <<<HTML
  497. <input type="checkbox" name="news[foo]" value="foo" />
  498. <input type="checkbox" name="news[bar]" value="bar">
  499. HTML;
  500. $dom->load($str);
  501. $es = $dom->find('[name=news[foo]]');
  502. assert(count($es)==1);
  503. assert($es[0]->name=='news[foo]');
  504. assert($es[0]->value=='foo');
  505. // -----------------------------------------------------------------------------
  506. // with '[]' names 3
  507. $str = <<<HTML
  508. <div name="div[]">
  509. <input type="checkbox" name="checkbox[]" value="foo" />
  510. </div>
  511. HTML;
  512. $dom->load($str);
  513. $es = $dom->find('div[name=div[]] input[name=checkbox[]]');
  514. assert(count($es)==1);
  515. assert($es[0]->value=='foo');
  516. // -----------------------------------------------------------------------------
  517. // regular expression
  518. $str = <<<HTML
  519. <div>
  520. <a href="image/one.png">one</a>
  521. <a href="image/two.jpg">two</a>
  522. <a href="/favorites/aaa">three (text)</a>
  523. </div>
  524. HTML;
  525. $dom->load($str);
  526. assert(count($dom->find('a[href^="image/"]'))==2);
  527. assert(count($dom->find('a[href*="/favorites/"]'))==1);
  528. $str = <<<HTML
  529. <html>
  530. <body>
  531. <div id="news-id-123">okok</div>
  532. </bod>
  533. </html>
  534. HTML;
  535. $dom->load($str);
  536. assert(count($dom->find('div[id*=news-id-[0-9]+]'))==1);
  537. assert(count($dom->find('div[id*=/news-id-[0-9]+/i]'))==1);
  538. // -----------------------------------------------------------------------------
  539. // multiple class test
  540. $str = <<<HTML
  541. <div class="hello">should verify</div>
  542. <div class="foo hello bar">should verify</div>
  543. <div class="foo bar hello">should verify</div>
  544. <div class="hello foo bar">should verify</div>
  545. <div class="helloworld">should not verify</div>
  546. <div class="worldhello">should not verify</div>
  547. <div class="worldhelloworld">should not verify</div>
  548. HTML;
  549. $dom->load($str);
  550. $es = $dom->find('[class="hello"],[class*="hello "],[class*=" hello"]');
  551. assert(count($es)==4);
  552. assert($es[0]->class=='hello');
  553. assert($es[1]->class=='foo hello bar');
  554. assert($es[2]->class=='foo bar hello');
  555. assert($es[3]->class=='hello foo bar');
  556. $es = $dom->find('.hello');
  557. assert(count($es)==4);
  558. assert($es[0]->class=='hello');
  559. assert($es[1]->class=='foo hello bar');
  560. assert($es[2]->class=='foo bar hello');
  561. assert($es[3]->class=='hello foo bar');
  562. // -----------------------------------------------
  563. $str = <<<HTML
  564. <div class="aa bb"></div>
  565. HTML;
  566. $dom->load($str);
  567. assert(count($dom->find('[class=aa]'))==1);
  568. assert(count($dom->find('[class=bb]'))==1);
  569. assert(count($dom->find('[class="aa bb"]'))==1);
  570. assert(count($dom->find('[class=aa], [class=bb]'))==1);
  571. // -----------------------------------------------------------------------------
  572. // multiple selector test
  573. $str = <<<HTML
  574. <p>aaa</p>
  575. <b>bbb</b>
  576. <i>ccc</i>
  577. HTML;
  578. $dom->load($str);
  579. $es = $dom->find('p,b,i');
  580. assert(count($es)==3);
  581. assert($es[0]->tag=='p');
  582. assert($es[1]->tag=='b');
  583. assert($es[2]->tag=='i');
  584. $es = $dom->find('p, b, i');
  585. assert(count($es)==3);
  586. assert($es[0]->tag=='p');
  587. assert($es[1]->tag=='b');
  588. assert($es[2]->tag=='i');
  589. $es = $dom->find('p, b , i');
  590. assert(count($es)==3);
  591. assert($es[0]->tag=='p');
  592. assert($es[1]->tag=='b');
  593. assert($es[2]->tag=='i');
  594. $es = $dom->find('p ,b ,i');
  595. assert(count($es)==3);
  596. assert($es[0]->tag=='p');
  597. assert($es[1]->tag=='b');
  598. assert($es[2]->tag=='i');
  599. $es = $dom->find('b,p,i');
  600. assert(count($es)==3);
  601. assert($es[0]->tag=='p');
  602. assert($es[1]->tag=='b');
  603. assert($es[2]->tag=='i');
  604. $es = $dom->find('i,b,p');
  605. assert(count($es)==3);
  606. assert($es[0]->tag=='p');
  607. assert($es[1]->tag=='b');
  608. assert($es[2]->tag=='i');
  609. $es = $dom->find('p,b,i,p,b');
  610. assert(count($es)==3);
  611. assert($es[0]->tag=='p');
  612. assert($es[1]->tag=='b');
  613. assert($es[2]->tag=='i');
  614. // -----------------------------------------------
  615. $str = <<<HTML
  616. <img title="aa" src="src">
  617. <a href="href" title="aa"></a>
  618. HTML;
  619. $dom->load($str);
  620. assert(count($dom->find('a[title], img[title]'))==2);
  621. // -----------------------------------------------------------------------------
  622. // elements that do NOT have the specified attribute
  623. $str = <<<HTML
  624. <img id="aa" src="src">
  625. <img src="src">
  626. HTML;
  627. $dom->load($str);
  628. assert(count($dom->find('img[!id]'))==1);
  629. // -----------------------------------------------------------------------------
  630. //js test
  631. $str = <<<HTML
  632. <a onMouseover="dropdownmenu(this, event, 'messagesmenu')" class="n" href="messagecenter.cfm?key=972489434">foo</a>
  633. HTML;
  634. $dom->load($str);
  635. assert($dom->find('a[onMouseover="dropdownmenu(this, event, \'messagesmenu\')"]',0)->innertext=='foo');
  636. assert($dom->find("a[onMouseover=dropdownmenu(this, event, 'messagesmenu')]",0)->innertext=='foo');
  637. // -----------------------------------------------------------------------------
  638. //dash test
  639. $str = '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
  640. $dom->load($str);
  641. assert($dom->find('meta[http-equiv=content-type]', 0)->content==='text/html; charset=utf-8');
  642. // -----------------------------------------------------------------------------
  643. // tear down
  644. $dom->clear();
  645. unset($dom);
  646. ?>