You are here

protected function simple_html_dom_node::parse_selector in simplehtmldom API 7

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

File

simplehtmldom/simple_html_dom.php, line 384

Class

simple_html_dom_node

Code

protected function parse_selector($selector_string) {

  // pattern of CSS selectors, modified from mootools
  $pattern = "/([\\w-:\\*]*)(?:\\#([\\w-]+)|\\.([\\w-]+))?(?:\\[@?(!?[\\w-]+)(?:([!*^\$]?=)[\"']?(.*?)[\"']?)?\\])?([\\/, ]+)/is";
  preg_match_all($pattern, trim($selector_string) . ' ', $matches, PREG_SET_ORDER);
  $selectors = array();
  $result = array();

  //print_r($matches);
  foreach ($matches as $m) {
    $m[0] = trim($m[0]);
    if ($m[0] === '' || $m[0] === '/' || $m[0] === '//') {
      continue;
    }

    // for borwser grnreated xpath
    if ($m[1] === 'tbody') {
      continue;
    }
    list($tag, $key, $val, $exp, $no_key) = array(
      $m[1],
      null,
      null,
      '=',
      false,
    );
    if (!empty($m[2])) {
      $key = 'id';
      $val = $m[2];
    }
    if (!empty($m[3])) {
      $key = 'class';
      $val = $m[3];
    }
    if (!empty($m[4])) {
      $key = $m[4];
    }
    if (!empty($m[5])) {
      $exp = $m[5];
    }
    if (!empty($m[6])) {
      $val = $m[6];
    }

    // convert to lowercase
    if ($this->dom->lowercase) {
      $tag = strtolower($tag);
      $key = strtolower($key);
    }

    //elements that do NOT have the specified attribute
    if (isset($key[0]) && $key[0] === '!') {
      $key = substr($key, 1);
      $no_key = true;
    }
    $result[] = array(
      $tag,
      $key,
      $val,
      $exp,
      $no_key,
    );
    if (trim($m[7]) === ',') {
      $selectors[] = $result;
      $result = array();
    }
  }
  if (count($result) > 0) {
    $selectors[] = $result;
  }
  return $selectors;
}