The main idea is that we keep a copy of the tree, and then use an array to keep track of matches. To handle a list of selectors (using the comma separator), we have to track both the currently progressing match and the previously matched elements. To use this handler:
Inheritance: implements QueryPath\CSS\EventHandler, implements QueryPath\CSS\Traverser
コード例 #1
0
ファイル: DOMQuery.php プロジェクト: rrmodi88/querypath
 /**
  * This replaces everything that matches the selector with the first value
  * in the current list.
  *
  * This is the reverse of replaceWith.
  *
  * Unlike jQuery, DOMQuery cannot assume a default document. Consequently,
  * you must specify the intended destination document. If it is omitted, the
  * present document is assumed to be tthe document. However, that can result
  * in undefined behavior if the selector and the replacement are not sufficiently
  * distinct.
  *
  * @param string $selector
  *  The selector.
  * @param DOMDocument $document
  *  The destination document.
  * @retval object DOMQuery
  *  The DOMQuery wrapping the modified document.
  * @deprecated Due to the fact that this is not a particularly friendly method,
  *  and that it can be easily replicated using {@see replaceWith()}, it is to be
  *  considered deprecated.
  * @see remove()
  * @see replaceWith()
  */
 public function replaceAll($selector, \DOMDocument $document)
 {
     $replacement = $this->size() > 0 ? $this->getFirstMatch() : $this->document->createTextNode('');
     $c = new QueryPathEventHandler($document);
     $c->find($selector);
     $temp = $c->getMatches();
     foreach ($temp as $item) {
         $node = $replacement->cloneNode();
         $node = $document->importNode($node);
         $item->parentNode->replaceChild($node, $item);
     }
     return QueryPath::with($document, NULL, $this->options);
 }
 public function testAnyDescendant()
 {
     $xml = '<?xml version="1.0" ?>
   <test>
     <li id="one"/><li id="two"/><li id="three">
       <li id="inner-one" class="foo">
         <li id="inner-inner-one" class="foo"/>
         <il id="inner-inner-two"/>
         <li id="dont-match-me"/>
       </li>
       <li id="inner-two"/>
     </li>
     <li id="four"/>
     <li id="five"/>
   </test>';
     $doc = new \DomDocument();
     $doc->loadXML($xml);
     $handler = new QueryPathEventHandler($doc);
     $handler->find('*');
     $matches = $handler->getMatches();
     $this->assertEquals(11, $matches->count());
     $handler = new QueryPathEventHandler($doc);
     $handler->find('*.foo');
     $matches = $handler->getMatches();
     $this->assertEquals(2, $matches->count());
     $this->assertEquals('inner-inner-one', $this->nthMatch($matches, 1)->getAttribute('id'));
     $handler = new QueryPathEventHandler($doc);
     $handler->find('test > li *.foo');
     $matches = $handler->getMatches();
     $this->assertEquals(2, $matches->count());
     $this->assertEquals('inner-inner-one', $this->nthMatch($matches, 1)->getAttribute('id'));
 }