find() public method

..
public find ( $selectors, $context = null, $noHistory = false ) : phpQueryObject | QueryTemplatesSource | QueryTemplatesParse | QueryTemplatesSourceQuery
return phpQueryObject | QueryTemplatesSource | QueryTemplatesParse | QueryTemplatesSourceQuery
コード例 #1
0
ファイル: TestResponse.php プロジェクト: neosunchess/dokuwiki
 /**
  * Query the response for a JQuery compatible CSS selector
  *
  * @link https://code.google.com/p/phpquery/wiki/Selectors
  * @param $selector string
  * @return phpQueryObject
  */
 public function queryHTML($selector)
 {
     if (is_null($this->pq)) {
         $this->pq = phpQuery::newDocument($this->content);
     }
     return $this->pq->find($selector);
 }
コード例 #2
0
ファイル: Html2Csv.php プロジェクト: bariew/html2csv
 public function toArray()
 {
     foreach ($this->dom->find($this->rowSelector) as $rowKey => $row) {
         foreach (pq($row)->find($this->cellSelector) as $colKey => $cell) {
             $this->addEl(pq($cell), $rowKey, $colKey);
         }
     }
     foreach ($this->data as $key => $values) {
         ksort($values);
         $this->data[$key] = $values;
     }
     return $this;
 }
コード例 #3
0
 private function createPost(\phpQueryObject $form)
 {
     $result = [];
     foreach ($form->find('input, select, checkbox, textarea') as $input) {
         $el = pq($input);
         if (($value = $el->attr('example')) || ($value = $el->attr('value'))) {
             $result[$el->attr('name')] = $value;
             continue;
         }
         $value = $el->attr('example');
         switch ($input->tagName) {
             case 'input':
                 $this->addInput($el, $result, $value);
                 break;
             case 'select':
                 $this->addSelect($el, $result, $value);
                 break;
             case 'checkbox':
                 $this->addCheckbox($el, $result, $value);
                 break;
             case 'textarea':
                 $this->addTextarea($el, $result, $value);
                 break;
         }
     }
     return $result;
 }
コード例 #4
0
ファイル: example.php プロジェクト: mnfjorge/moip-php
 /**
  * Enter description here...
  *
  * @param phpQueryObject $self
  */
 public static function example($self, $arg1)
 {
     // this method can be called on any phpQuery object, like this:
     // pq('div')->example('$arg1 Value')
     // do something
     $self->append('Im just an example !');
     // change stack of result object
     return $self->find('div');
 }
コード例 #5
0
 /**
  * Multi-purpose function.
  * Use pq() as shortcut.
  *
  * In below examples, $pq is any result of pq(); function.
  *
  * 1. Import markup into existing document (without any attaching):
  * - Import into selected document:
  *   pq('<div/>')				// DOESNT accept text nodes at beginning of input string !
  * - Import into document with ID from $pq->getDocumentID():
  *   pq('<div/>', $pq->getDocumentID())
  * - Import into same document as DOMNode belongs to:
  *   pq('<div/>', DOMNode)
  * - Import into document from phpQuery object:
  *   pq('<div/>', $pq)
  *
  * 2. Run query:
  * - Run query on last selected document:
  *   pq('div.myClass')
  * - Run query on document with ID from $pq->getDocumentID():
  *   pq('div.myClass', $pq->getDocumentID())
  * - Run query on same document as DOMNode belongs to and use node(s)as root for query:
  *   pq('div.myClass', DOMNode)
  * - Run query on document from phpQuery object
  *   and use object's stack as root node(s) for query:
  *   pq('div.myClass', $pq)
  *
  * @param string|DOMNode|DOMNodeList|array	$arg1	HTML markup, CSS Selector, DOMNode or array of DOMNodes
  * @param string|phpQueryObject|DOMNode	$context	DOM ID from $pq->getDocumentID(), phpQuery object (determines also query root) or DOMNode (determines also query root)
  *
  * @return phpQueryObject|QueryTemplatesSource|QueryTemplatesParse|QueryTemplatesSourceQuery|QueryTemplatesPhpQuery|false
  * phpQuery object or false in case of error.
  */
 public static function pq($arg1, $context = null)
 {
     if ($arg1 instanceof DOMNODE && !isset($context)) {
         foreach (phpQuery::$documents as $documentWrapper) {
             $compare = $arg1 instanceof DOMDocument ? $arg1 : $arg1->ownerDocument;
             if ($documentWrapper->document->isSameNode($compare)) {
                 $context = $documentWrapper->id;
             }
         }
     }
     if (!$context) {
         $domId = self::$defaultDocumentID;
         if (!$domId) {
             throw new Exception("Can't use last created DOM, because there isn't any. Use phpQuery::newDocument() first.");
         }
         //		} else if (is_object($context) && ($context instanceof PHPQUERY || is_subclass_of($context, 'phpQueryObject')))
     } else {
         if (is_object($context) && $context instanceof phpQueryObject) {
             $domId = $context->getDocumentID();
         } else {
             if ($context instanceof DOMDOCUMENT) {
                 $domId = self::getDocumentID($context);
                 if (!$domId) {
                     //throw new Exception('Orphaned DOMDocument');
                     $domId = self::newDocument($context)->getDocumentID();
                 }
             } else {
                 if ($context instanceof DOMNODE) {
                     $domId = self::getDocumentID($context);
                     if (!$domId) {
                         throw new Exception('Orphaned DOMNode');
                         //				$domId = self::newDocument($context->ownerDocument);
                     }
                 } else {
                     $domId = $context;
                 }
             }
         }
     }
     if ($arg1 instanceof phpQueryObject) {
         //		if (is_object($arg1) && (get_class($arg1) == 'phpQueryObject' || $arg1 instanceof PHPQUERY || is_subclass_of($arg1, 'phpQueryObject'))) {
         /**
          * Return $arg1 or import $arg1 stack if document differs:
          * pq(pq('<div/>'))
          */
         if ($arg1->getDocumentID() == $domId) {
             return $arg1;
         }
         $class = get_class($arg1);
         // support inheritance by passing old object to overloaded constructor
         $phpQuery = $class != 'phpQuery' ? new $class($arg1, $domId) : new phpQueryObject($domId);
         $phpQuery->elements = array();
         foreach ($arg1->elements as $node) {
             $phpQuery->elements[] = $phpQuery->document->importNode($node, true);
         }
         return $phpQuery;
     } else {
         if ($arg1 instanceof DOMNODE || is_array($arg1) && isset($arg1[0]) && $arg1[0] instanceof DOMNODE) {
             /*
              * Wrap DOM nodes with phpQuery object, import into document when needed:
              * pq(array($domNode1, $domNode2))
              */
             $phpQuery = new phpQueryObject($domId);
             if (!$arg1 instanceof DOMNODELIST && !is_array($arg1)) {
                 $arg1 = array($arg1);
             }
             $phpQuery->elements = array();
             foreach ($arg1 as $node) {
                 $sameDocument = $node->ownerDocument instanceof DOMDOCUMENT && !$node->ownerDocument->isSameNode($phpQuery->document);
                 $phpQuery->elements[] = $sameDocument ? $phpQuery->document->importNode($node, true) : $node;
             }
             return $phpQuery;
         } else {
             if (self::isMarkup($arg1)) {
                 /**
                  * Import HTML:
                  * pq('<div/>')
                  */
                 $phpQuery = new phpQueryObject($domId);
                 return $phpQuery->newInstance($phpQuery->documentWrapper->import($arg1));
             } else {
                 /**
                  * Run CSS query:
                  * pq('div.myClass')
                  */
                 $phpQuery = new phpQueryObject($domId);
                 //			if ($context && ($context instanceof PHPQUERY || is_subclass_of($context, 'phpQueryObject')))
                 if ($context && $context instanceof phpQueryObject) {
                     $phpQuery->elements = $context->elements;
                 } else {
                     if ($context && $context instanceof DOMNODELIST) {
                         $phpQuery->elements = array();
                         foreach ($context as $node) {
                             $phpQuery->elements[] = $node;
                         }
                     } else {
                         if ($context && $context instanceof DOMNODE) {
                             $phpQuery->elements = array($context);
                         }
                     }
                 }
                 return $phpQuery->find($arg1);
             }
         }
     }
 }
コード例 #6
0
ファイル: test.php プロジェクト: limi58/fetchJD
    /**
     * @param phpQueryObject $pq
     * @dataProvider provider
     * @return void
     */
    function testSimpleDataInsertion($pq)
    {
        $testName = 'Simple data insertion';
        $testResult = <<<EOF
<div class="articles">
        div.articles text node
        <ul>

        <li>
                <p>This is paragraph of first LI</p>
                <p class="title">News 1 title</p>
                <p class="body">News 1 body</p>
            </li>

<li>
                <p>This is paragraph of first LI</p>
                <p class="title">News 2 title</p>
                <p class="body">News 2 body</p>
            </li>
<li>
                <p>This is paragraph of first LI</p>
                <p class="title">News 3</p>
                <p class="body">News 3 body</p>
            </li>
</ul>
<p>paragraph after UL</p>
    </div>
EOF;
        $rows = array(array('title' => 'News 1 title', 'body' => 'News 1 body'), array('title' => 'News 2 title', 'body' => 'News 2 body'), array('title' => 'News 3', 'body' => 'News 3 body'));
        $articles = $pq->find('.articles ul');
        $rowSrc = $articles->find('li')->remove()->eq(0);
        foreach ($rows as $r) {
            $row = $rowSrc->_clone();
            foreach ($r as $field => $value) {
                $row->find(".{$field}")->html($value);
                //		die($row->htmlOuter());
            }
            $row->appendTo($articles);
        }
        $result = $pq->find('.articles')->htmlOuter();
        //print htmlspecialchars("<pre>{$result}</pre>").'<br />';
        $similarity = 0.0;
        similar_text($testResult, $result, $similarity);
        $this->assertGreaterThan(90, $similarity);
    }
コード例 #7
0
 protected function checkField(\phpQueryObject $pq, $schema, $name, $value)
 {
     $this->assertEquals(1, $pq->find("span.label:contains('{$name}')")->length, "Field {$schema}.{$name} not found");
     $this->assertEquals($value, $pq->find("input[name='struct_schema_data[{$schema}][{$name}]']")->val(), "Field {$schema}.{$name} has wrong value");
 }
コード例 #8
0
ファイル: Kontur.php プロジェクト: carono/company-info
 /**
  * @param \phpQueryObject $phpQuery
  *
  * @return string
  */
 private static function extractFound($phpQuery)
 {
     foreach ($phpQuery->find('div.noMargin') as $node) {
         $node = pq($node);
         if (mb_stripos($node->html(), "Уставный капитал", null, 'utf-8')) {
             $arr = explode(":", current(array_filter(explode("\r\n", $node->html()))));
             return trim($arr[1]);
         }
     }
     return '';
 }
コード例 #9
0
ファイル: BasicTest.php プロジェクト: cagartner/phpquery
    /**
     * @param phpQueryObject $pq
     * @dataProvider provider
     * @return void
     */
    function testSimpleDataInsertion($pq)
    {
        $testName = 'Simple data insertion';
        $testResult = <<<EOF
<div class="articles">
        div.articles text node
        <ul>

        <li>
                <p>This is paragraph of first LI</p>
                <p class="title">News 1 title</p>
                <p class="body">News 1 body</p>
            </li>

<li>
                <p>This is paragraph of first LI</p>
                <p class="title">News 2 title</p>
                <p class="body">News 2 body</p>
            </li>
<li>
                <p>This is paragraph of first LI</p>
                <p class="title">News 3</p>
                <p class="body">News 3 body</p>
            </li>
</ul>
<p class="after">paragraph after UL</p>
    </div>
EOF;
        $expected_pq = phpQuery::newDocumentHTML($testResult);
        $rows = array(array('title' => 'News 1 title', 'body' => 'News 1 body'), array('title' => 'News 2 title', 'body' => 'News 2 body'), array('title' => 'News 3', 'body' => 'News 3 body'));
        $articles = $pq->find('.articles ul');
        $rowSrc = $articles->find('li')->remove()->eq(0);
        foreach ($rows as $r) {
            $row = $rowSrc->_clone();
            foreach ($r as $field => $value) {
                $row->find(".{$field}")->html($value);
                //		die($row->htmlOuter());
            }
            $row->appendTo($articles);
        }
        $result = $pq->find('.articles')->htmlOuter();
        //         print htmlspecialchars("<pre>{$result}</pre>").'<br />';
        $this->assertEqualXMLStructure($expected_pq->find('.articles')->elements[0], $pq->find('.articles')->elements[0]);
        //         $this->assertEqualXMLStructure(DOMDocument::loadHTML($testResult)->documentElement, DOMDocument::loadHTML($result)->documentElement);
    }