You are here

protected function simple_html_dom_node::seek in simplehtmldom API 7

Same name and namespace in other branches
  1. 5.2 simplehtmldom/simple_html_dom.php \simple_html_dom_node::seek()
  2. 6 simplehtmldom/simple_html_dom.php \simple_html_dom_node::seek()

File

simplehtmldom/simple_html_dom.php, line 303

Class

simple_html_dom_node

Code

protected function seek($selector, &$ret) {
  list($tag, $key, $val, $exp, $no_key) = $selector;

  // xpath index
  if ($tag && $key && is_numeric($key)) {
    $count = 0;
    foreach ($this->children as $c) {
      if ($tag === '*' || $tag === $c->tag) {
        if (++$count == $key) {
          $ret[$c->_[HDOM_INFO_BEGIN]] = 1;
          return;
        }
      }
    }
    return;
  }
  $end = !empty($this->_[HDOM_INFO_END]) ? $this->_[HDOM_INFO_END] : 0;
  if ($end == 0) {
    $parent = $this->parent;
    while (!isset($parent->_[HDOM_INFO_END]) && $parent !== null) {
      $end -= 1;
      $parent = $parent->parent;
    }
    $end += $parent->_[HDOM_INFO_END];
  }
  for ($i = $this->_[HDOM_INFO_BEGIN] + 1; $i < $end; ++$i) {
    $node = $this->dom->nodes[$i];
    $pass = true;
    if ($tag === '*' && !$key) {
      if (in_array($node, $this->children, true)) {
        $ret[$i] = 1;
      }
      continue;
    }

    // compare tag
    if ($tag && $tag != $node->tag && $tag !== '*') {
      $pass = false;
    }

    // compare key
    if ($pass && $key) {
      if ($no_key) {
        if (isset($node->attr[$key])) {
          $pass = false;
        }
      }
      else {
        if (!isset($node->attr[$key])) {
          $pass = false;
        }
      }
    }

    // compare value
    if ($pass && $key && $val && $val !== '*') {
      $check = $this
        ->match($exp, $val, $node->attr[$key]);

      // handle multiple class
      if (!$check && strcasecmp($key, 'class') === 0) {
        foreach (explode(' ', $node->attr[$key]) as $k) {
          $check = $this
            ->match($exp, $val, $k);
          if ($check) {
            break;
          }
        }
      }
      if (!$check) {
        $pass = false;
      }
    }
    if ($pass) {
      $ret[$i] = 1;
    }
    unset($node);
  }
}