//error_reporting(E_ALL); require_once '../PhpQuery/PhpQuery.php'; PhpQuery::$debug = true; PhpQuery::plugin('Scripts'); $testName = 'Scripts/example'; $doc = PhpQuery::newDocumentFile('test.html'); $testResult = 10; if ($doc->script('example', 'p')->length == $testResult) { print "Test '{$testName}' PASSED :)"; } else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; print "<pre>"; var_dump($doc->whois()); print "</pre>\n"; } print "\n"; $testName = 'Scripts/gmail_login'; $testResult = 1; $url = 'http://code.google.com/p/phpquery/w/edit/MultiDocumentSupport'; //PhpQuery::ajaxAllowURL($url); $editor = PhpQuery::newDocument('<div/>')->script('google_login')->location($url); if ($editor->find('textarea#content')->length == $testResult) { print "Test '{$testName}' PASSED :)"; } else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; print "<pre>"; var_dump($doc->whois()); print "</pre>\n"; } print "\n";
} print "\n"; // SLICE2 $testResult = array('li#testID', 'li', 'li#i_have_nested_list', 'li.nested'); $result = PhpQuery::newDocumentFile('test.html')->find('li')->slice(1, -1); if ($result->whois() == $testResult) { print "Test 'Slice2' PASSED :)"; } else { print "Test 'Slice2' <strong>FAILED</strong> !!! "; print "<pre>"; print_r($result->whois()); print "</pre>\n"; } print "\n"; // Multi-insert $result = PhpQuery::newDocument('<li><span class="field1"></span><span class="field1"></span></li>')->find('.field1')->php('longlongtest'); $validResult = '<li><span class="field1"><php>longlongtest</php></span><span class="field1"><php>longlongtest</php></span></li>'; similar_text($result->htmlOuter(), $validResult, $similarity); if ($similarity > 80) { print "Test 'Multi-insert' PASSED :)"; } else { print "Test 'Multi-insert' <strong>FAILED</strong> !!! "; print "<pre>"; var_dump($result->htmlOuter()); print "</pre>\n"; } print "\n"; // INDEX $testResult = 1; $result = PhpQuery::newDocumentFile('test.html')->find('p')->index(pq('p.title:first')); if ($result == $testResult) {
} /** * * @param $html PhpQueryObject * @return unknown_type */ function success2($pq) { print 'success2 callback'; print $pq->find('script')->remove()->end(); } } // Gmail login (not working...) if (0) { PhpQuery::plugin("Scripts"); PhpQuery::newDocument('<div/>')->script('google_login')->location('http://mail.google.com/')->toReference($pq); if ($pq) { print $pq->script('print_websafe'); } } // Gmail login v2 (not working...) if (0) { $browser = null; $browserCallback = new CallbackReference($browser); PhpQuery::browserGet('http://mail.google.com/', $browserCallback); if ($browser) { $browser->WebBrowser($browserCallback)->find('#Email')->val('*****@*****.**')->end()->find('#Passwd')->val('XXX')->parents('form')->submit(); if ($browser) { print $browser->script('print_websafe'); } }
} print_r($result->whois()); print "\n"; $testName = 'Filter with multiplie selectors'; $testResult = array('p.body'); $testDOM = PhpQuery::newDocumentFile('test.html'); $single = $testDOM->find('p')->filter('.body')->add($testDOM->find('p')->filter('.title')); $double = $testDOM->find('p')->filter('.body, .title'); if ($single->length == count($double)) { print "Test '{$testName}' PASSED :)"; } else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; } print "\n"; print_r($single->whois()); print "\n"; print_r($double->whois()); print "\n"; $testName = 'Attributes in HTML element'; $validResult = 'testValue'; $result = PhpQuery::newDocumentFile('test.html')->find('html')->empty()->attr('test', $validResult); $result = PhpQuery::newDocument($result->htmlOuter())->find('html')->attr('test'); //similar_text($result->htmlOuter(), $validResult, $similarity); if ($result == $validResult) { print "Test '{$testName}' PASSED :)"; } else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; print "<pre>"; print $result; print "</pre>\n"; }
<?php require __DIR__ . '/../src/PhpQuery.php'; // INITIALIZE IT // PhpQuery::newDocumentHTML($markup); // PhpQuery::newDocumentXML(); // PhpQuery::newDocumentFileXHTML('test.html'); // PhpQuery::newDocumentFilePHP('test.php'); // PhpQuery::newDocument('test.xml', 'application/rss+xml'); // this one defaults to text/html in utf8 $doc = PhpQuery::newDocument('<div/>'); // FILL IT // array syntax works like ->find() here $doc['div']->append('<ul></ul>'); // array set changes inner html $doc['div ul'] = '<li>1</li> <li>2</li> <li>3</li>'; // MANIPULATE IT $li = null; // almost everything can be a chain $doc['ul > li']->addClass('my-new-class')->filter(':last')->addClass('last-li')->toReference($li); // SELECT DOCUMENT // pq(); is using selected document as default PhpQuery::selectDocument($doc); // documents are selected when created or by above method // query all unordered lists in last selected document $ul = pq('ul')->insertAfter('div'); // ITERATE IT // all direct LIs from $ul foreach ($ul['> li'] as $li) { // iteration returns PLAIN dom nodes, NOT PhpQuery objects $tagName = $li->tagName;
} else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; print $doc->htmlOuter('htmlentities'); } print "\n"; $testName = 'HTML insertion 3'; $doc = PhpQuery::newDocument('<div><p/></div>'); $string = 'Hors paragraphe. <img align="right" src="http://www.stlouisstpierre.com/institution/images/plan.jpg"> <p>Éditorial de l\'institution Saint-Pierre.</p> Hors paragraphe.'; $doc->find('p')->html($string); if (pq('img')->length == 1) { print "Test '{$testName}' PASSED :)"; print $doc->htmlOuter(); } else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; print $doc->htmlOuter('htmlentities'); } print "\n"; $testName = 'Text insertion'; $doc = PhpQuery::newDocument('<div><p/></div>'); $string = "La Thermo-sonde de cuisson vous permet de cuire à la perfection au four comme au bain-marie"; $doc->find('p')->html($string); if (trim(pq('p:first')->html()) == $string) { print "Test '{$testName}' PASSED :)"; } else { print "Test '{$testName}' <strong>FAILED</strong> !!! "; print $doc->htmlOuter('htmlentities'); } print "\n";
// $foo = 'aaa'; // var_dump(mb_ereg_match('^[\w|\||-]+$', $foo) || $foo == '*'); // var_dump(preg_match('@^[\w|\||-]+$@', $foo) || $foo == '*'); // http://code.google.com/p/phpquery/issues/detail?id=67 //$doc = PhpQuery::newDocumentXML("<node1/><node2/>"); //$doc['node1']->data('foo', 'bar'); //var_dump($doc['node1']->data('foo')); //$doc['node1']->removeData('foo'); //var_dump($doc['node1']->data('foo')); //$doc['node1']->data('foo.bar', 'bar'); //var_dump($doc['node1']->data('foo.bar')); //var_dump(PhpQuery::$documents[$doc->getDocumentID()]->data); // xhtml fragments //$doc = PhpQuery::newDocumentXHTML("<p><br/></p>"); //print $doc; $doc = PhpQuery::newDocument('<div id="content"></div><div id="content"></div>'); //$content_string = str_repeat('a', 99988); $content_string = str_repeat(str_repeat('a', 350) . "\n", 350); //var_dump(strlen($content_string)); ?> <pre class='1'><?php //print $content_string; ?> </pre><?php pq('#content')->php('echo $content_string;'); //pq('#content')->php('echo '.var_export($content_string, true)); $doc->dumpTree(); ?> <pre class='2'><?php var_dump($doc->php()); ?>