technosophos/querypath — Issue #63: Calling remove() with a selector that doesn't exist results in error.
Author: marktheunissen
Created: 2011-11-17 21:35:56+00:00
State: open

To reproduce, do this:

$qp->top()->remove('blahblah');

You'll see this error: WD php: QueryPathException: Cannot initialize QueryPath from an empty SplObjectStore in QueryPath->__construct() (line 152 of QueryPath.php)

This is because if the selector doesn't result in a match, the remove() function will attempt to initialize and return a QueryPath object from an empty SplObjectStore (see the remove() function). This is new in 2.1.1.

Here is my fixed remove():

```php
public function remove($selector = NULL) {
if(!empty($selector)) {

$query = new QueryPathCssEventHandler($this->matches);
$query->find($selector);
$matches = $query->getMatches();
}
else {
$matches = $this->matches;
}

$found = NULL;
if ($matches->count() > 0) {
$found = new SplObjectStorage();
foreach ($matches as $item) {
$found->attach($item->parentNode->removeChild($item));
}
}

return new QueryPath($found);
}
```