HTML; $dom->load($str); $e = $dom->find('img'); assert(count($e)==3); // ----------------------------------------------------------------------------- // std selector test $str = << HTML; $dom->load($str); // ----------------------------------------------- // wildcard assert(count($dom->find('*'))==1); assert(count($dom->find('div *'))==3); assert(count($dom->find('div img *'))==0); assert(count($dom->find(' * '))==1); assert(count($dom->find(' div * '))==3); assert(count($dom->find(' div img *'))==0); // ----------------------------------------------- // tag assert(count($dom->find('img'))==3); assert(count($dom->find('text'))==4); // ----------------------------------------------- // class $es = $dom->find('img.class0'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('.class0'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); // ----------------------------------------------- // index assert($dom->find('img', 0)->src=='src0'); assert($dom->find('img', 1)->src=='src1'); assert($dom->find('img', 2)->src=='src2'); assert($dom->find('img', -3)->src=='src0'); assert($dom->find('img', -2)->src=='src1'); assert($dom->find('img', -1)->src=='src2'); // ----------------------------------------------- // id $es = $dom->find('img#id1'); assert(count($es)==1); assert($es[0]->src=='src1'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('#id2'); assert(count($es)==1); assert($es[0]->src=='src2'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); // ----------------------------------------------- // attr $es = $dom->find('img[src="src0"]'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('img[src=src0]'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); // ----------------------------------------------------------------------------- // wildcard $es = $dom->find('*[src]'); assert(count($es)==3); $es = $dom->find('*[src=*]'); assert(count($es)==3); $es = $dom->find('*[alt=*]'); assert(count($es)==0); $es = $dom->find('*[src="src0"]'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('*[src=src0]'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('[src=src0]'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('[src="src0"]'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('*#id1'); assert(count($es)==1); assert($es[0]->src=='src1'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); $es = $dom->find('*.class0'); assert(count($es)==1); assert($es[0]->src=='src0'); assert($es[0]->innertext==''); assert($es[0]->outertext==''); // ----------------------------------------------------------------------------- // text $str = <<text1text2 HTML; $dom->load($str); $es = $dom->find('text'); assert(count($es)==2); assert($es[0]->innertext=='text1'); assert($es[0]->outertext=='text1'); assert($es[0]->plaintext=='text1'); assert($es[1]->innertext=='text2'); assert($es[1]->outertext=='text2'); assert($es[1]->plaintext=='text2'); $str = <<text1text2 HTML; $dom->load($str); $es = $dom->find('b text'); assert(count($es)==2); assert($es[0]->innertext=='text1'); assert($es[0]->outertext=='text1'); assert($es[0]->plaintext=='text1'); assert($es[1]->innertext=='text2'); assert($es[1]->outertext=='text2'); assert($es[1]->plaintext=='text2'); // ----------------------------------------------------------------------------- // xml namespace test $str = <<text HTML; $dom->load($str); $es = $dom->find('bw:bizy'); assert(count($es)==1); assert($es[0]->id=='date'); // ----------------------------------------------------------------------------- // user defined tag name test $str = <<text HTML; $dom->load($str); $es = $dom->find('div_test'); assert(count($es)==1); assert($es[0]->id=='1'); // ----------------------------------------------- $str = <<text HTML; $dom->load($str); $es = $dom->find('div-test'); assert(count($es)==1); assert($es[0]->id=='1'); // ----------------------------------------------- $str = <<text HTML; $dom->load($str); $es = $dom->find('div::test'); assert(count($es)==1); assert($es[0]->id=='1'); // ----------------------------------------------- // find all occurrences of id="1" regardless of the tag $str = <<
ok
HTML; $dom->load($str); $es = $dom->find('[id=1]'); assert(count($es)==2); assert($es[0]->tag=='img'); assert($es[1]->tag=='div'); // ----------------------------------------------------------------------------- // multiple selector test $str = <<
ok
ok
HTML; $dom->load($str); $es = $dom->find('div'); assert(count($es)==4); assert($es[0]->id=='id0'); assert($es[1]->id=='id1'); assert($es[2]->id=='id2'); $es = $dom->find('div div'); assert(count($es)==3); assert($es[0]->id=='id1'); assert($es[1]->id=='id2'); $es = $dom->find('div div div'); assert(count($es)==2); assert($es[0]->id=='id2'); $es = $dom->find('.class0 .class1 .class2'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('#id0 #id1 #id2'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('div[id=id0] div[id=id1] div[id=id2]'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('div[id="id0"] div[id="id1"] div[id="id2"]'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('div[id=id0] div[id="id1"] div[id="id2"]'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('div[id="id0"] div[id=id1] div[id="id2"]'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('div[id="id0"] div[id="id1"] div[id=id2]'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find("div[id='id0'] div[id='id1'] div[id='id2']"); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('[id=id0] [id=id1] [id=id2]'); assert(count($es)==1); assert($es[0]->id=='id2'); $es = $dom->find('[id] [id] [id]'); assert(count($es)==2); assert($es[0]->id=='id2'); assert($es[1]->id=='id3'); $es = $dom->find('[id=id0] [id=id1] [id=id3]'); assert(count($es)==1); assert($es[0]->id=='id3'); $es = $dom->find('[id=id0] [id=id1] [style="st1 st2"]'); assert(count($es)==1); assert($es[0]->id=='id3'); $es = $dom->find('[id=id0] [id=id1] [style=st1 st2]'); assert(count($es)==1); assert($es[0]->id=='id3'); $es = $dom->find('[id=id0] [id=id1] [style=st1 st2] span[class=id4]'); assert(count($es)==1); assert($es[0]->innertext=='ok'); $es = $dom->find('[id=id0] [id=id1] [style="st1 st2"] span[class="id4"]'); assert(count($es)==1); assert($es[0]->innertext=='ok'); // ----------------------------------------------- $str = << 0 1
2 3
HTML; $dom->load($str); $es = $dom->find('table td'); assert(count($es)==4); assert($es[0]->innertext=='0'); assert($es[1]->innertext=='1'); assert($es[2]->innertext=='2'); assert($es[3]->innertext=='3'); // ----------------------------------------------------------------------------- // multiple selector test 3 $str = <<
0 1
2 3
HTML; $dom = str_get_html($str); $es = $dom->find('table.hello td'); assert(count($es)==4); assert($es[0]->innertext=='0'); assert($es[1]->innertext=='1'); assert($es[2]->innertext=='2'); assert($es[3]->innertext=='3'); // ----------------------------------------------------------------------------- // nested selector test $str = <<
  • 0
  • 1
  • HTML; $dom = str_get_html($str); $es= $dom->find('ul'); assert(count($es)==2); foreach($es as $n) { $li = $n->find('li'); assert(count($li)==2); } $es= $dom->find('li'); assert(count($es)==4); assert($es[0]->innertext=='0'); assert($es[1]->innertext=='1'); assert($es[2]->innertext=='2'); assert($es[3]->innertext=='3'); assert($es[0]->outertext=='
  • 0
  • '); assert($es[1]->outertext=='
  • 1
  • '); assert($es[2]->outertext=='
  • 2
  • '); assert($es[3]->outertext=='
  • 3
  • '); $counter = 0; foreach($dom->find('ul') as $ul) { foreach($ul->find('li') as $li) { assert($li->innertext=="$counter"); assert($li->outertext=="
  • $counter
  • "); ++$counter; } } // ----------------------------------------------------------------------------- // [attribute=value] selector $str = << HTML; $dom->load($str); $es = $dom->find('[name=newsletter]'); assert(count($es)==1); assert($es[0]->name=='newsletter'); assert($es[0]->value=='Hot Fuzz'); assert($es[0]->outertext==''); $es = $dom->find('[name="newsletter"]'); assert(count($es)==1); assert($es[0]->name=='newsletter'); assert($es[0]->value=='Hot Fuzz'); assert($es[0]->outertext==''); // ----------------------------------------------------------------------------- // [attribute!=value] selector $str = << HTML; $dom->load($str); $es = $dom->find('[name!=newsletter]'); assert(count($es)==1); assert($es[0]->name=='accept'); assert($es[0]->value=='Evil Plans'); assert($es[0]->outertext==''); $es = $dom->find('[name!="newsletter"]'); assert(count($es)==1); assert($es[0]->name=='accept'); assert($es[0]->value=='Evil Plans'); assert($es[0]->outertext==''); $es = $dom->find("[name!='newsletter']"); assert(count($es)==1); assert($es[0]->name=='accept'); assert($es[0]->value=='Evil Plans'); assert($es[0]->outertext==''); // ----------------------------------------------------------------------------- // [attribute^=value] selector $str = << HTML; $dom->load($str); $es = $dom->find('[name^=news]'); assert(count($es)==2); assert($es[0]->name=='newsletter'); assert($es[0]->outertext==''); assert($es[1]->name=='newsboy'); assert($es[1]->outertext==''); $es = $dom->find('[name^="news"]'); assert(count($es)==2); assert($es[0]->name=='newsletter'); assert($es[0]->outertext==''); assert($es[1]->name=='newsboy'); assert($es[1]->outertext==''); // ----------------------------------------------------------------------------- // [attribute$=value] selector $str = << HTML; $dom->load($str); $es = $dom->find('[name$=letter]'); assert(count($es)==2); assert($es[0]->name=='newsletter'); assert($es[0]->outertext==''); assert($es[1]->name=='jobletter'); assert($es[1]->outertext==''); $es = $dom->find('[name$="letter"]'); assert(count($es)==2); assert($es[0]->name=='newsletter'); assert($es[0]->outertext==''); assert($es[1]->name=='jobletter'); assert($es[1]->outertext==''); // ----------------------------------------------------------------------------- // [attribute*=value] selector $str = <<
    HTML; $dom->load($str); $es = $dom->find('[name*=man]'); assert(count($es)==3); assert($es[0]->name=='man-news'); assert($es[0]->outertext==''); assert($es[1]->name=='milkman'); assert($es[1]->outertext==''); assert($es[2]->name=='letterman2'); assert($es[2]->outertext==''); $es = $dom->find('[name*="man"]'); assert(count($es)==3); assert($es[0]->name=='man-news'); assert($es[0]->outertext==''); assert($es[1]->name=='milkman'); assert($es[1]->outertext==''); assert($es[2]->name=='letterman2'); assert($es[2]->outertext==''); $es = $dom->find('[class*=hello]'); assert($es[0]->outertext=='
    '); assert($es[1]->outertext=='
    '); assert($es[2]->outertext=='
    '); // ----------------------------------------------------------------------------- // Testcase for '[]' names element // normal checkbox $str = << HTML; $dom->load($str); $es = $dom->find('[name=news]'); assert(count($es)==3); assert($es[0]->name=='news'); assert($es[0]->value=='foo'); assert($es[1]->name=='news'); assert($es[1]->value=='bar'); assert($es[2]->name=='news'); assert($es[2]->value=='baz'); // ----------------------------------------------------------------------------- // with '[]' names checkbox $str = << HTML; $dom->load($str); $es = $dom->find('[name=news[]]'); assert(count($es)==3); assert($es[0]->name=='news[]'); assert($es[0]->value=='foo'); assert($es[1]->name=='news[]'); assert($es[1]->value=='bar'); assert($es[2]->name=='news[]'); assert($es[2]->value=='baz'); // ----------------------------------------------------------------------------- // with '[]' names checkbox 2 $str = << HTML; $dom->load($str); $es = $dom->find('[name=news[foo]]'); assert(count($es)==1); assert($es[0]->name=='news[foo]'); assert($es[0]->value=='foo'); // ----------------------------------------------------------------------------- // with '[]' names 3 $str = << HTML; $dom->load($str); $es = $dom->find('div[name=div[]] input[name=checkbox[]]'); assert(count($es)==1); assert($es[0]->value=='foo'); // ----------------------------------------------------------------------------- // regular expression $str = << one two three (text) HTML; $dom->load($str); assert(count($dom->find('a[href^="image/"]'))==2); assert(count($dom->find('a[href*="/favorites/"]'))==1); $str = <<
    okok
    HTML; $dom->load($str); assert(count($dom->find('div[id*=news-id-[0-9]+]'))==1); assert(count($dom->find('div[id*=/news-id-[0-9]+/i]'))==1); // ----------------------------------------------------------------------------- // multiple class test $str = <<should verify
    should verify
    should verify
    should verify
    should not verify
    should not verify
    should not verify
    HTML; $dom->load($str); $es = $dom->find('[class="hello"],[class*="hello "],[class*=" hello"]'); assert(count($es)==4); assert($es[0]->class=='hello'); assert($es[1]->class=='foo hello bar'); assert($es[2]->class=='foo bar hello'); assert($es[3]->class=='hello foo bar'); $es = $dom->find('.hello'); assert(count($es)==4); assert($es[0]->class=='hello'); assert($es[1]->class=='foo hello bar'); assert($es[2]->class=='foo bar hello'); assert($es[3]->class=='hello foo bar'); // ----------------------------------------------- $str = << HTML; $dom->load($str); assert(count($dom->find('[class=aa]'))==1); assert(count($dom->find('[class=bb]'))==1); assert(count($dom->find('[class="aa bb"]'))==1); assert(count($dom->find('[class=aa], [class=bb]'))==1); // ----------------------------------------------------------------------------- // multiple selector test $str = <<aaa

    bbb ccc HTML; $dom->load($str); $es = $dom->find('p,b,i'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); $es = $dom->find('p, b, i'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); $es = $dom->find('p, b , i'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); $es = $dom->find('p ,b ,i'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); $es = $dom->find('b,p,i'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); $es = $dom->find('i,b,p'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); $es = $dom->find('p,b,i,p,b'); assert(count($es)==3); assert($es[0]->tag=='p'); assert($es[1]->tag=='b'); assert($es[2]->tag=='i'); // ----------------------------------------------- $str = << HTML; $dom->load($str); assert(count($dom->find('a[title], img[title]'))==2); // ----------------------------------------------------------------------------- // elements that do NOT have the specified attribute $str = << HTML; $dom->load($str); assert(count($dom->find('img[!id]'))==1); // ----------------------------------------------------------------------------- //js test $str = <<foo HTML; $dom->load($str); assert($dom->find('a[onMouseover="dropdownmenu(this, event, \'messagesmenu\')"]',0)->innertext=='foo'); assert($dom->find("a[onMouseover=dropdownmenu(this, event, 'messagesmenu')]",0)->innertext=='foo'); // ----------------------------------------------------------------------------- //dash test $str = ''; $dom->load($str); assert($dom->find('meta[http-equiv=content-type]', 0)->content==='text/html; charset=utf-8'); // ----------------------------------------------------------------------------- // tear down $dom->clear(); unset($dom); ?>