Skip to content

Commit

Permalink
Added options to internal qp() calls.
Browse files Browse the repository at this point in the history
With the abstract factory added, it is necessary to pass options
from the base qp() call to any other qp() calls within QueryPath.
Unit tests also added.
  • Loading branch information
M Butcher committed Jul 24, 2009
1 parent 688d26a commit 50de0bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/QueryPath/QueryPath.php
Expand Up @@ -749,7 +749,7 @@ public function is($selector) {
*/
public function filter($selector) {
$found = new SplObjectStorage();
foreach ($this->matches as $m) if (qp($m)->is($selector)) $found->attach($m);
foreach ($this->matches as $m) if (qp($m, NULL, $this->options)->is($selector)) $found->attach($m);
$this->setMatches($found);
return $this;
}
Expand Down Expand Up @@ -850,7 +850,7 @@ public function not($selector) {
foreach ($this->matches as $m) if ($selector->contains($m)) $found->attach($m);
}
else {
foreach ($this->matches as $m) if (!qp($m)->is($selector)) $found->attach($m);
foreach ($this->matches as $m) if (!qp($m, NULL, $this->options)->is($selector)) $found->attach($m);
}
$this->setMatches($found);
return $this;
Expand Down Expand Up @@ -1623,7 +1623,7 @@ public function replaceAll($selector, DOMDocument $document) {
$node = $document->importNode($node);
$item->parentNode->replaceChild($node, $item);
}
return qp($document);
return qp($document, NULL, $this->options);
}
/**
* Add more elements to the current set of matches.
Expand All @@ -1646,7 +1646,7 @@ public function add($selector) {
// This is destructive, so we need to set $last:
$this->last = $this->matches;

foreach (qp($this->document, $selector)->get() as $item)
foreach (qp($this->document, $selector, $this->options)->get() as $item)
$this->matches->attach($item);
return $this;
}
Expand Down Expand Up @@ -1853,14 +1853,14 @@ public function closest($selector) {
$found = new SplObjectStorage();
foreach ($this->matches as $m) {

if (qp($m)->is($selector) > 0) {
if (qp($m, NULL, $this->options)->is($selector) > 0) {
$found->attach($m);
}
else {
while ($m->parentNode->nodeType !== XML_DOCUMENT_NODE) {
$m = $m->parentNode;
// Is there any case where parent node is not an element?
if ($m->nodeType === XML_ELEMENT_NODE && qp($m)->is($selector) > 0) {
if ($m->nodeType === XML_ELEMENT_NODE && qp($m, NULL, $this->options)->is($selector) > 0) {
$found->attach($m);
break;
}
Expand Down Expand Up @@ -1893,7 +1893,7 @@ public function parent($selector = NULL) {
// Is there any case where parent node is not an element?
if ($m->nodeType === XML_ELEMENT_NODE) {
if (!empty($selector)) {
if (qp($m)->is($selector) > 0) {
if (qp($m, NULL, $this->options)->is($selector) > 0) {
$found->attach($m);
break;
}
Expand Down Expand Up @@ -1929,7 +1929,7 @@ public function parents($selector = NULL) {
// Is there any case where parent node is not an element?
if ($m->nodeType === XML_ELEMENT_NODE) {
if (!empty($selector)) {
if (qp($m)->is($selector) > 0)
if (qp($m, NULL, $this->options)->is($selector) > 0)
$found->attach($m);
}
else
Expand Down Expand Up @@ -2365,7 +2365,7 @@ public function next($selector = NULL) {
$m = $m->nextSibling;
if ($m->nodeType === XML_ELEMENT_NODE) {
if (!empty($selector)) {
if (qp($m)->is($selector) > 0) {
if (qp($m, NULL, $this->options)->is($selector) > 0) {
$found->attach($m);
break;
}
Expand Down Expand Up @@ -2403,7 +2403,7 @@ public function nextAll($selector = NULL) {
$m = $m->nextSibling;
if ($m->nodeType === XML_ELEMENT_NODE) {
if (!empty($selector)) {
if (qp($m)->is($selector) > 0) {
if (qp($m, NULL, $this->options)->is($selector) > 0) {
$found->attach($m);
}
}
Expand Down Expand Up @@ -2440,7 +2440,7 @@ public function prev($selector = NULL) {
$m = $m->previousSibling;
if ($m->nodeType === XML_ELEMENT_NODE) {
if (!empty($selector)) {
if (qp($m)->is($selector)) {
if (qp($m, NULL, $this->options)->is($selector)) {
$found->attach($m);
break;
}
Expand Down Expand Up @@ -2478,7 +2478,7 @@ public function prevAll($selector = NULL) {
$m = $m->previousSibling;
if ($m->nodeType === XML_ELEMENT_NODE) {
if (!empty($selector)) {
if (qp($m)->is($selector)) {
if (qp($m, NULL, $this->options)->is($selector)) {
$found->attach($m);
}
}
Expand All @@ -2500,7 +2500,7 @@ public function peers($selector = NULL) {
foreach ($m->parentNode->childNodes as $kid) {
if ($kid->nodeType == XML_ELEMENT_NODE && $m !== $kid) {
if (!empty($selector)) {
if (qp($kid)->is($selector)) {
if (qp($kid, NULL, $this->options)->is($selector)) {
$found->attach($kid);
}
}
Expand Down Expand Up @@ -2676,7 +2676,7 @@ public function hasClass($class) {
* @see find()
*/
public function branch($selector = NULL) {
$temp = qp($this->matches);
$temp = qp($this->matches, NULL, $this->options);
if (isset($selector)) $temp->find($selector);
return $temp;
}
Expand Down Expand Up @@ -3008,7 +3008,9 @@ public function __call($name, $arguments) {
* Returns an iterator.
*/
public function getIterator() {
return new QueryPathIterator($this->matches);
$i = new QueryPathIterator($this->matches);
$i->options = $this->options;
return $i;
}
}

Expand Down Expand Up @@ -3168,8 +3170,10 @@ public static function replaceEntity($entity) {
* method is called.
*/
class QueryPathIterator extends IteratorIterator {
public $options = array();

public function current() {
return qp(parent::current());
return qp(parent::current(), NULL, $this->options);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/Tests/QueryPath/QueryPathTest.php
Expand Up @@ -84,6 +84,15 @@ public function testQPAbstractFactory() {
$this->assertTrue($qp->foonator(), 'Has special foonator() function.');
}

public function testQPAbstractFactoryIterating() {
$xml = '<?xml version="1.0"?><l><i/><i/><i/><i/><i/></l>';
$options = array('QueryPath_class' => 'QueryPathExtended');
foreach(qp($xml, 'i', $options) as $item) {
$this->assertTrue($item instanceof QueryPathExtended, 'Is instance of extending class.');
}

}

/**
* @expectedException QueryPathException
*/
Expand Down

0 comments on commit 50de0bf

Please sign in to comment.