You are here

public function QueryPath::find in QueryPath 6

Same name and namespace in other branches
  1. 7.3 QueryPath/QueryPath.php \QueryPath::find()
  2. 7.2 QueryPath/QueryPath.php \QueryPath::find()
4 calls to QueryPath::find()
QueryPath::detach in QueryPath/QueryPath.php
QueryPath::remove in QueryPath/QueryPath.php
QueryPath::top in QueryPath/QueryPath.php
QueryPath::__construct in QueryPath/QueryPath.php

File

QueryPath/QueryPath.php, line 211

Class

QueryPath

Code

public function find($selector) {
  $ids = array();
  $regex = '/^#([\\w-]+)$|^\\.([\\w-]+)$/';
  if (preg_match($regex, $selector, $ids) === 1) {
    if (!empty($ids[1])) {
      $xpath = new DOMXPath($this->document);
      foreach ($this->matches as $item) {
        if ($item
          ->isSameNode($this->document->documentElement)) {
          $xpathQuery = "//*[@id='{$ids[1]}']";
        }
        else {
          $xpathQuery = ".//*[@id='{$ids[1]}']";
        }
        $nl = $xpath
          ->query($xpathQuery, $item);
        if ($nl->length > 0) {
          $this
            ->setMatches($nl
            ->item(0));
          break;
        }
        else {
          $this
            ->noMatches();
        }
      }
    }
    else {
      $xpath = new DOMXPath($this->document);
      $found = new SplObjectStorage();
      foreach ($this->matches as $item) {
        if ($item
          ->isSameNode($this->document->documentElement)) {
          $xpathQuery = "//*[@class]";
        }
        else {
          $xpathQuery = ".//*[@class]";
        }
        $nl = $xpath
          ->query($xpathQuery, $item);
        for ($i = 0; $i < $nl->length; ++$i) {
          $vals = explode(' ', $nl
            ->item($i)
            ->getAttribute('class'));
          if (in_array($ids[2], $vals)) {
            $found
              ->attach($nl
              ->item($i));
          }
        }
      }
      $this
        ->setMatches($found);
    }
    return $this;
  }
  $query = new QueryPathCssEventHandler($this->matches);
  $query
    ->find($selector);
  $this
    ->setMatches($query
    ->getMatches());
  return $this;
}