You are here

public function QueryPathCssEventHandler::pseudoClass in QueryPath 6

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

Overrides CssEventHandler::pseudoClass

File

QueryPath/CssEventHandler.php, line 290

Class

QueryPathCssEventHandler

Code

public function pseudoClass($name, $value = NULL) {
  $name = strtolower($name);
  switch ($name) {
    case 'visited':
    case 'hover':
    case 'active':
    case 'focus':
    case 'animated':
    case 'visible':
    case 'hidden':
    case 'target':
      $this->matches = new SplObjectStorage();
      break;
    case 'indeterminate':
      throw new NotImplementedException(":indeterminate is not implemented.");
      break;
    case 'lang':
      if (!isset($value)) {
        throw new NotImplementedException("No handler for lang pseudoclass without value.");
      }
      $this
        ->lang($value);
      break;
    case 'link':
      $this
        ->searchForAttr('href');
      break;
    case 'root':
      $found = new SplObjectStorage();
      if (empty($this->dom)) {
        $this->matches = $found;
      }
      elseif (is_array($this->dom)) {
        $found
          ->attach($this->dom[0]->ownerDocument->documentElement);
        $this->matches = $found;
      }
      elseif ($this->dom instanceof DOMNode) {
        $found
          ->attach($this->dom->ownerDocument->documentElement);
        $this->matches = $found;
      }
      elseif ($this->dom instanceof DOMNodeList && $this->dom->length > 0) {
        $found
          ->attach($this->dom
          ->item(0)->ownerDocument->documentElement);
        $this->matches = $found;
      }
      else {
        $found
          ->attach($this->dom);
        $this->matches = $found;
      }
      break;
    case 'x-root':
    case 'x-reset':
      $this->matches = new SplObjectStorage();
      $this->matches
        ->attach($this->dom);
      break;
    case 'even':
      $this
        ->nthChild(2, 0);
      break;
    case 'odd':
      $this
        ->nthChild(2, 1);
      break;
    case 'nth-child':
      list($aVal, $bVal) = $this
        ->parseAnB($value);
      $this
        ->nthChild($aVal, $bVal);
      break;
    case 'nth-last-child':
      list($aVal, $bVal) = $this
        ->parseAnB($value);
      $this
        ->nthLastChild($aVal, $bVal);
      break;
    case 'nth-of-type':
      list($aVal, $bVal) = $this
        ->parseAnB($value);
      $this
        ->nthOfTypeChild($aVal, $bVal);
      break;
    case 'nth-last-of-type':
      list($aVal, $bVal) = $this
        ->parseAnB($value);
      $this
        ->nthLastOfTypeChild($aVal, $bVal);
      break;
    case 'first-child':
      $this
        ->nthChild(0, 1);
      break;
    case 'last-child':
      $this
        ->nthLastChild(0, 1);
      break;
    case 'first-of-type':
      $this
        ->firstOfType();
      break;
    case 'last-of-type':
      $this
        ->lastOfType();
      break;
    case 'only-child':
      $this
        ->onlyChild();
      break;
    case 'only-of-type':
      $this
        ->onlyOfType();
      break;
    case 'empty':
      $this
        ->emptyElement();
      break;
    case 'not':
      if (empty($value)) {
        throw new CssParseException(":not() requires a value.");
      }
      $this
        ->not($value);
      break;
    case 'lt':
    case 'gt':
    case 'nth':
    case 'eq':
    case 'first':
    case 'last':
      $this
        ->getByPosition($name, $value);
      break;
    case 'parent':
      $matches = $this
        ->candidateList();
      $found = new SplObjectStorage();
      foreach ($matches as $match) {
        if (!empty($match->firstChild)) {
          $found
            ->attach($match);
        }
      }
      $this->matches = $found;
      break;
    case 'enabled':
    case 'disabled':
    case 'checked':
      $this
        ->attribute($name);
      break;
    case 'text':
    case 'radio':
    case 'checkbox':
    case 'file':
    case 'password':
    case 'submit':
    case 'image':
    case 'reset':
    case 'button':
    case 'submit':
      $this
        ->attribute('type', $name);
      break;
    case 'header':
      $matches = $this
        ->candidateList();
      $found = new SplObjectStorage();
      foreach ($matches as $item) {
        $tag = $item->tagName;
        $f = strtolower(substr($tag, 0, 1));
        if ($f == 'h' && strlen($tag) == 2 && ctype_digit(substr($tag, 1, 1))) {
          $found
            ->attach($item);
        }
      }
      $this->matches = $found;
      break;
    case 'has':
      $this
        ->has($value);
      break;
    case 'contains':
      $value = $this
        ->removeQuotes($value);
      $matches = $this
        ->candidateList();
      $found = new SplObjectStorage();
      foreach ($matches as $item) {
        if (strpos($item->textContent, $value) !== FALSE) {
          $found
            ->attach($item);
        }
      }
      $this->matches = $found;
      break;
    case 'contains-exactly':
      $value = $this
        ->removeQuotes($value);
      $matches = $this
        ->candidateList();
      $found = new SplObjectStorage();
      foreach ($matches as $item) {
        if ($item->textContent == $value) {
          $found
            ->attach($item);
        }
      }
      $this->matches = $found;
      break;
    default:
      throw new CssParseException("Unknown Pseudo-Class: " . $name);
  }
  $this->findAnyElement = FALSE;
}