123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- // $Rev: 115 $
- // -----------------------------------------------------------------------------
- // setup
- error_reporting(E_ALL);
- require_once('../simple_html_dom.php');
- $dom = new simple_html_dom;
- // -----------------------------------------------------------------------------
- // empty test
- $str = '';
- $dom->load($str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = null;
- $dom->load($str);
- assert($dom->save()==$str);
- // -----------------------------------------------------------------------------
- // text test
- $str = <<<HTML
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"></html>
- HTML;
- $dom->load($str);
- assert(count($dom->find('unknown'))==1);
- assert(count($dom->find('text'))==1);
- // -----------------------------------------------------------------------------
- // string quote test
- $str = <<<HTML
- <div class="class0" id="id0" >
- okok<br>
- <input type=submit name="btnG" value="go" onclick='goto("url0")'>
- <br/>
- <div><input type=submit name="btnG2" value="go" onclick="goto('url1'+'\'')"/></div>
- <input type=submit name="btnG2" value="go" onclick="goto('url2')"/>
- <div><input type=submit name="btnG2" value="go" onclick='goto("url4"+"\"")'></div>
- <br/>
- </div>
- HTML;
- $dom->load($str);
- $es = $dom->find('input');
- assert(count($es)==4);
- assert($es[0]->onclick=='goto("url0")');
- assert($es[1]->onclick=="goto('url1'+'\'')");
- assert($es[2]->onclick=="goto('url2')");
- assert($es[3]->onclick=='goto("url4"+"\"")');
- // -----------------------------------------------------------------------------
- // clone test
- $str = <<<HTML
- <div class="class0" id="id0" >
- okok<br>
- <input type=submit name="btnG" value="go" onclick='goto("url0")'>
- <br/>
- <div><input type=submit name="btnG2" value="go" onclick="goto('url1'+'\'')"/></div>
- <input type=submit name="btnG2" value="go" onclick="goto('url2')"/>
- <div><input type=submit name="btnG2" value="go" onclick='goto("url4"+"\"")'></div>
- <br/>
- </div>
- HTML;
- $dom->load($str);
- $es = $dom->find('input');
- assert(count($es)==4);
- assert($es[0]->onclick=='goto("url0")');
- assert($es[1]->onclick=="goto('url1'+'\'')");
- assert($es[2]->onclick=="goto('url2')");
- assert($es[3]->onclick=='goto("url4"+"\"")');
- unset($es);
- $dom2 = clone($dom);
- $es = $dom2->find('input');
- assert(count($es)==4);
- assert($es[0]->onclick=='goto("url0")');
- assert($es[1]->onclick=="goto('url1'+'\'')");
- assert($es[2]->onclick=="goto('url2')");
- assert($es[3]->onclick=='goto("url4"+"\"")');
- // -----------------------------------------------
- $str = <<<HTML
- <div class='class0' id="id0" aa='aa' bb="bb" cc='"cc"' dd="'dd'"></div>
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------------------------------------
- // monkey test
- $str = <<<HTML
- <
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <a
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <a<
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <<<<ab
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <<<<ab
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <<><<>ab
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <abc
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- >
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------
- // $str = <<<HTML
- // <abc
- // (<1 mol%)
- // HTML;
- // $dom->load($str);
- // echo $dom;
- // assert($dom==$str);
- // assert($dom->save()==$str);
- // -----------------------------------------------
- $str = <<<HTML
- (<1 mol%)
- HTML;
- $dom->load($str);
- assert($dom==$str);
- assert($dom->save()==$str);
- // -----------------------------------------------------------------------------
- // rnadom string test
- function str_random($length)
- {
- $str = "";
- srand((double)microtime()*1000000);
- $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $char_list .= "abcdefghijklmnopqrstuvwxyz";
- $char_list .= "1234567890";
- $char_list .= "<>!?[]%^&*()";
- for($i=0; $i<$length; ++$i)
- $str .= substr($char_list,(rand()%(strlen($char_list))), 1);
- return $str;
- }
- for($i=0; $i<60; ++$i) {
- $str = str_random($i);
- //echo $str."\n<br>";
- $dom->load($str, false);
- //echo $dom->save()."\n<br>";
- assert($dom==$str);
- }
- // -----------------------------------------------------------------------------
- // lowercase test
- $str = <<<HTML
- <img class="class0" id="id0" src="src0">
- HTML;
- $dom->load($str);
- assert(count($dom->find('img'))==1);
- assert(count($dom->find('IMG'))==1);
- assert(isset($dom->find('img', 0)->class));
- assert(!isset($dom->find('img', 0)->CLASS));
- assert($dom->find('img', 0)->class=='class0');
- assert($dom==$str);
- // -----------------------------------------------
- $str = <<<HTML
- <IMG CLASS="class0" ID="id0" SRC="src0">
- HTML;
- $dom->load($str);
- assert(count($dom->find('img'))==1);
- assert(count($dom->find('IMG'))==1);
- assert(isset($dom->find('img', 0)->class));
- assert(!isset($dom->find('img', 0)->CLASS));
- assert($dom->find('img', 0)->class=='class0');
- assert($dom==strtolower($str));
- // -----------------------------------------------
- $str = <<<HTML
- <IMG CLASS="class0" ID="id0" SRC="src0">
- HTML;
- $dom->load($str, false);
- assert(count($dom->find('img'))==0);
- assert(count($dom->find('IMG'))==1);
- assert(isset($dom->find('IMG', 0)->CLASS));
- assert(!isset($dom->find('IMG', 0)->class));
- assert($dom->find('IMG', 0)->CLASS=='class0');
- assert($dom==$str);
- // -----------------------------------------------------------------------------
- // tear down
- $dom->clear();
- unset($dom);
- ?>
|