Example #1
1
<?php

require_once '../PhpQuery/PhpQuery.php';
PhpQuery::$debug = 2;
$testName = 'HTML insertion';
$doc = PhpQuery::newDocumentFile('document-types/document-utf8.xhtml');
//$doc = PhpQuery::newDocumentFile('document-types/document-utf8.html');
//$doc = PhpQuery::newDocumentFile('document-types/document-utf8.xml');
//print $doc->find('step');
print $doc->find('p');
$markup = "test<br />test<p>test p</p>";
$doc['body > p:last']->append($markup);
if ($doc['body > p:last p']->length == 1) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
    print $doc->htmlOuter('htmlspecialchars');
}
print "\n";
Example #2
0
 public function testExtend()
 {
     $newMethods = array('newMethod1' => array($this, 'callback1'), 'newMethod2' => array($this, 'callback2'));
     PhpQuery::extend('PhpQueryObject', $newMethods);
     $doc = PhpQuery::newDocumentXML("<div/>");
     $this->assertTrue($doc->newMethod1() == $doc, '$doc->newMethod1 == $doc');
     $this->assertTrue($doc->newMethod2() == "callback2", '$doc->newMethod1 == "callback2"');
 }
Example #3
0
// CALLBACKS
class callbackClass
{
    static function staticMethodCallback($node)
    {
        pq($node)->addClass('newClass');
    }
    function methodCallback($node)
    {
        pq($node)->addClass('newClass');
    }
}
function functionCallback($node)
{
    pq($node)->addClass('newClass');
}
$testResult = array('li.newClass', 'li#testID.newClass', 'li.newClass', 'li#i_have_nested_list.newClass', 'li.nested.newClass', 'li.second.newClass');
$tests = array('functionCallback', array('callbackClass', 'staticMethodCallback'), array(new callbackClass(), 'methodCallback'));
foreach ($tests as $test) {
    $result = PhpQuery::newDocumentFile('test.html')->find('li')->each($test);
    $testName = is_array($test) ? $test[1] : $test;
    if ($result->whois() == $testResult) {
        print "Test '{$testName}' PASSED :)";
    } else {
        print "Test '{$testName}' <strong>FAILED</strong> !!! ";
        print "<pre>";
        print_r($result->whois());
        print "</pre>\n";
    }
    print "\n";
}
Example #4
0
require_once '../PhpQuery/PhpQuery.php';
// PhpQuery::$debug = true;
$form = <<<EOF
<form>
  <input name='input-example'>
  <input name='array[array-example]'>
  <textarea name='textarea-example'></textarea>
\t<select name='select-example'>
    <option value='first'></option>
\t</select>
  <input type='radio' name='radio-example' value='foo'>
  <input type='checkbox' name='checkbox-example' value='foo'>
</form>
EOF;
$doc = PhpQuery::newDocumentHTML($form);
$inputs = $doc['form > *'];
// creates array from input names
// $results = $inputs->get(null,
// 	create_function('$node', 'return $node->getAttribute("name");')
// );
$results = array();
foreach ($inputs as $node) {
    $node = pq($node);
    $name = $node->attr('name');
    $results[$name] = false;
    $node->change(new CallbackReference($results[$name]));
}
$inputs->not('select,:checkbox,:radio')->val('new value')->end()->filter('select')->val('first')->end()->filter(':checkbox')->val(array('foo'))->end()->filter(':radio')->val(array('foo'))->end();
foreach ($results as $name => $result) {
    print $result ? "Test for '{$name}' PASSED :)<br />\n" : "Test for '{$name}' <strong>FAILED</strong> !!!<br />\n";
Example #5
0
 */
class PhpQuery
{
    static $defaultDocumentID;
    static $debug = 0;
    static $documents = array();
    static $defaultCharset = 'utf-8';
    static function debug($text)
    {
        if (self::$debug) {
            print var_dump($text);
        }
    }
}
require_once __DIR__ . '/../../Dom/DOMDocumentWrapper.php';
PhpQuery::$debug = 2;
/* ENCODINGS */
//print '<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-2">';
print '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">';
/* HTML */
//$htmlIso = new DOMDocumentWrapper(
//	file_get_contents('document-types/document-iso88592.html')
//);
//$htmlIsoNoCharset = new DOMDocumentWrapper(
//	file_get_contents('document-types/document-iso88592-nocharset.html'),
//	'text/html;charset=iso-8859-2'
//);
$htmlUtf = new DOMDocumentWrapper(file_get_contents('document-types/document-utf8.html'));
var_dump($htmlUtf->markup());
//$htmlUtfNoCharset = new DOMDocumentWrapper(
//	file_get_contents('document-types/document-utf8-nocharset.html'),
Example #6
0
$testName = 'Append and move';
$result = PhpQuery::newDocumentFile('test.html');
$li = $result->find('li:first');
$result->find('div')->_empty();
$li->html('test1-&eacute;-test1')->append('test2-é-test2')->appendTo($result->find('div:first'));
$result = $result->find('div:first li:first');
$expected = 'test1-é-test1test2-é-test2';
if (trim(str_replace("\n", '', $result->html())) == $expected) {
    print "Test '{$testName}' passed :)<br />\n";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!!<br />\n";
    print "'" . trim($result->html()) . "'";
}
print "\n";
$testName = 'Attr charset';
$result = PhpQuery::newDocumentFile('test.html')->find('li:first')->attr('test', 'foo &eacute; żźć bar');
if (trim($result->attr('test')) == 'foo &eacute; żźć bar') {
    print "Test '{$testName}' passed :)<br />\n";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!!<br />\n";
    print $result->attr('test');
}
print "\n";
//$testName = 'Loading document without meta charset';
//$result = PhpQuery::newDocumentFile('test.html')
//	->_empty();
////var_dump((string)$result->htmlOuter());
//$result = PhpQuery::newDocument($result->htmlOuter());
//$validResult = <<<EOF
//<!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" xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /></head></html>
Example #7
0
$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;
    $childNodes = $li->childNodes;
    // so you NEED to wrap it within PhpQuery, using pq();
    pq($li)->addClass('my-second-new-class');
}
// PRINT OUTPUT
// 1st way
print PhpQuery::getDocument($doc->getDocumentID());
// 2nd way
print PhpQuery::getDocument(pq('div')->getDocumentID());
// 3rd way
print pq('div')->getDocument();
// 4th way
print $doc->htmlOuter();
// 5th way
print $doc;
// another...
print $doc['ul'];
Example #8
0
} 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";
Example #9
0
$p->dump();
print "\n";
$testName = 'WrapAll';
$testResult = 1;
PhpQuery::newDocumentFile('test.html')->find('p')->slice(1, 3)->wrapAll('<div class="wrapper">');
$result = pq('.wrapper');
if ($result->size() == $testResult) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
$result->dump();
print "\n";
$testName = 'WrapInner';
$testResult = 3;
PhpQuery::newDocumentFile('test.html')->find('li:first')->wrapInner('<div class="wrapper">');
$result = pq('.wrapper p');
if ($result->size() == $testResult) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
print $result->dump();
print "\n";
// TODO !
$testName = 'WrapAllTest';
/*
$doc = PhpQuery::newDocumentHTML('<div id="myDiv"></div>');
$doc['#myDiv']->append('hors paragraphe<p>Test</p>hors paragraphe')
	->contents()
		->not('[nodeType=1]')
Example #10
0
// $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());
?>
Example #11
0
}
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";
}
Example #12
0
 /**
  *
  * @link http://docs.jquery.com/Utilities/jQuery.map
  */
 public static function map($array, $callback, $param1 = null, $param2 = null, $param3 = null)
 {
     $result = array();
     $paramStructure = null;
     if (func_num_args() > 2) {
         $paramStructure = func_get_args();
         $paramStructure = array_slice($paramStructure, 2);
     }
     foreach ($array as $v) {
         $vv = PhpQuery::callbackRun($callback, array($v), $paramStructure);
         //			$callbackArgs = $args;
         //			foreach($args as $i => $arg) {
         //				$callbackArgs[$i] = $arg instanceof CallbackParam
         //					? $v
         //					: $arg;
         //			}
         //			$vv = call_user_func_array($callback, $callbackArgs);
         if (is_array($vv)) {
             foreach ($vv as $vvv) {
                 $result[] = $vvv;
             }
         } else {
             if ($vv !== null) {
                 $result[] = $vv;
             }
         }
     }
     return $result;
 }
Example #13
0
<?php

require_once '../PhpQuery/PhpQuery.php';
PhpQuery::$debug = true;
$testName = 'ReplaceWith';
PhpQuery::newDocumentFile('test.html')->find('p:eq(1)')->replaceWith("<p class='newTitle'>\n                        this is example title\n                    </p>");
$result = pq('p:eq(1)');
if ($result->hasClass('newTitle')) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
$result->dump();
print "\n";
$testName = 'ReplaceAll';
$testResult = 3;
PhpQuery::newDocumentFile('test.html');
pq('<div class="replacer">')->replaceAll('li:first p');
$result = pq('.replacer');
if ($result->size() == $testResult) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
$result->dump();
print "\n";
Example #14
0
print "\n";
// CLONE
$testName = 'Clone';
$testResult = 3;
$document;
$p = PhpQuery::newDocumentFile('test.html')->toReference($document)->find('p:first');
foreach (array(0, 1, 2) as $i) {
    $p->clone()->addClass("clone-test")->addClass("class-{$i}")->insertBefore($p);
}
if (pq('.clone-test')->size() == $testResult) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
print "\n";
// SIBLINGS
$testName = 'Next';
$testResult = 3;
$document;
$result = PhpQuery::newDocumentFile('test.html')->find('li:first')->next()->next()->prev()->is('#testID');
if ($result) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
print "\n";
?>


<?php 
die;
Example #15
0
<?php

namespace PhpQuery\Tests;

use PhpQuery\PhpQuery as phpQuery;
PhpQuery::use_function(__NAMESPACE__);
class BasicTest extends \PHPUnit_Framework_TestCase
{
    function provider()
    {
        // TODO change filename
        return array(array(phpQuery::newDocumentFile(__DIR__ . '/test.html')));
    }
    /**
     * @param phpQueryObject $pq
     * @dataProvider provider
     * @return void
     */
    function testFilterWithPseudoclass($pq)
    {
        $pq = $pq->find('p')->filter('.body:gt(1)');
        $result = array('p.body');
        $this->assertTrue($pq->whois() == $result);
    }
    /**
     * @param phpQueryObject $pq
     * @dataProvider provider
     * @return void
     */
    function testSlice($pq)
    {
Example #16
0
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<?php 
require_once '../PhpQuery/PhpQuery.php';
PhpQuery::$debug = true;
$testName = 'PHP Code output';
$expected = <<<EOF
<?php  print \$r  ?><a href="<?php print \$array['key']; if ("abc'd'") {}; ?>"></a>
EOF;
$result = PhpQuery::newDocumentPHP(null, 'text/html;charset=utf-8')->appendPHP('print $r')->append('<a/>')->find('a')->attrPHP('href', 'print $array[\'key\']; if ("abc\'d\'") {};')->end();
if (trim($result->php()) == $expected) {
    print "Test '{$testName}' passed :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!!";
}
print "\n";
$testName = 'PHP file open';
$result = PhpQuery::newDocumentFilePHP('document-types/document-utf8.php');
var_dump($result->php());
/*
	->appendPHP('print $r')
	->append('<a/>')
		->find('a')
			->attrPHP('href', 'print $array[\'key\']; if ("abc\'d\'") {};')
		->end();
if (trim($result->php()) == $expected)
	print "Test '{$testName}' passed :)";
else
	print "Test '{$testName}' <strong>FAILED</strong> !!!";
print "\n";
*/
Example #17
0
//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";
Example #18
0
        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');
        }
    }
}
//	if ( $result->whois() == $testResult )
//		print "Test '$testName' PASSED :)";
//	else {
//		print "Test '$testName' <strong>FAILED</strong> !!! ";
//		print "<pre>";
//		print_r($result->whois());
//		print "</pre>\n";
//	}
Example #19
0
//		print "</pre>\n";
//	}
//	print "\n";
//}
$testName = 'Load';
$test = PhpQuery::newDocumentFile('test.html')->find('div:first')->load('http://wikipedia.org/ div[lang]');
if (pq('div[lang]')->size()) {
    print "Test '{$testName}' PASSED :)";
} else {
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
    print "<pre>";
    print "</pre>\n";
}
print "\n";
// http://code.google.com/p/phpquery/issues/detail?id=130
$pq = PhpQuery::ajax(array('url' => 'http://' . $_SERVER['SERVER_NAME'] . preg_replace('@/[^/]+$@', '/test_ajax_data_1', $_SERVER['REQUEST_URI']), 'success' => 'a789fhasdui3124', 'error' => 'jhdbg786213u8dsfg7y'));
function a789fhasdui3124($html)
{
    $testName = 'AJAX request text node';
    if ($html == 'hello world') {
        print "Test '{$testName}' PASSED :)";
    } else {
        print "Test '{$testName}' <strong>FAILED</strong> !!! ";
    }
    print "\n";
}
function jhdbg786213u8dsfg7y()
{
    $testName = 'AJAX request text node';
    print "Test '{$testName}' <strong>FAILED</strong> !!! ";
}
 function LingvoTranslate($Word, $Options = array())
 {
     LoadPhpQuery();
     static $Result, $LanguageCode;
     if (is_null($LanguageCode)) {
         $LanguageCode = LocaleLanguageCode();
     }
     $ResetCache = ArrayValue('ResetCache', $Options, False);
     $From = ArrayValue('From', $Options, $LanguageCode);
     $To = ArrayValue('To', $Options, $LanguageCode);
     if (!isset($Result[$Word]) || $ResetCache) {
         $Direction = $From . '-' . $To;
         $Url = 'http://lingvo.abbyyonline.com/en/' . $Direction . '/' . rawurlencode($Word);
         $Doc = PhpQuery::NewDocumentFile($Url);
         /*$Translation = Pq('div.card-short > span.Bold')->Text();
         		if($Translation != ''){
         			$Translation = preg_replace('/\n/', '', $Translation);
         			if(!preg_match('/^[а-я ]+$/iu', $Translation)){
         				$Translation = Pq('span.translation:eq(0)')->Text();
         			}
         		}*/
         Pq('span.translation:eq(0)')->Children()->Remove();
         $Translation = Pq('span.translation:eq(0)')->Text();
         $Translation = trim($Translation);
         $Result[$Word] = $Translation;
     }
     return $Result[$Word];
 }