HTML; $dom->load($str); $e = $dom->find('img'); assert(count($e)==3); // ----------------------------------------------------------------------------- // std selector test $str = << HTML; $dom->load($str); // ----------------------------------------------- // all //$e = $dom->find('*'); //assert(count($e)==3); // ----------------------------------------------- // tag assert(count($dom->find('img'))==3); // ----------------------------------------------- // 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==''); // ----------------------------------------------- // 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==''); $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==''); // ----------------------------------------------- // 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)==0); // ----------------------------------------------- // 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)==0); // ----------------------------------------------- // 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
HTML; $dom->load($str); $es = $dom->find('div'); assert(count($es)==3); assert($es[0]->id=='id0'); assert($es[1]->id=='id1'); assert($es[2]->id=='id2'); $es = $dom->find('div div'); assert(count($es)==2); assert($es[0]->id=='id1'); assert($es[1]->id=='id2'); $es = $dom->find('div div div'); assert(count($es)==1); assert($es[0]->id=='id2'); // ----------------------------------------------------------------------------- // multiple selector test 2 $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_dom($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_dom($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'); // ----------------------------------------------------------------------------- // regular expression syntax escaping $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); // ----------------------------------------------------------------------------- // 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'); // ----------------------------------------------------------------------------- // multiple class test 2 $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'); // ----------------------------------------------------------------------------- // multiple selector test 2 $str = << HTML; $dom->load($str); assert(count($dom->find('a[title], img[title]'))==2); // ----------------------------------------------------------------------------- // tear down $dom->clear(); unset($dom); ?>