You are here

public function QueryPath::closest in QueryPath 6

Same name and namespace in other branches
  1. 7.3 QueryPath/QueryPath.php \QueryPath::closest()
  2. 7.2 QueryPath/QueryPath.php \QueryPath::closest()

File

QueryPath/QueryPath.php, line 1072

Class

QueryPath

Code

public function closest($selector) {
  $found = new SplObjectStorage();
  foreach ($this->matches as $m) {
    if (qp($m, NULL, $this->options)
      ->is($selector) > 0) {
      $found
        ->attach($m);
    }
    else {
      while ($m->parentNode->nodeType !== XML_DOCUMENT_NODE) {
        $m = $m->parentNode;
        if ($m->nodeType === XML_ELEMENT_NODE && qp($m, NULL, $this->options)
          ->is($selector) > 0) {
          $found
            ->attach($m);
          break;
        }
      }
    }
  }
  $this
    ->setMatches($found);
  return $this;
}