You are here

protected function QueryPathCssEventHandler::nthOfTypeChild in QueryPath 6

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

File

QueryPath/CssEventHandler.php, line 678

Class

QueryPathCssEventHandler

Code

protected function nthOfTypeChild($groupSize, $elementInGroup, $lastChild) {
  $parents = new SplObjectStorage();
  $matches = new SplObjectStorage();
  $i = 0;
  foreach ($this->matches as $item) {
    $parent = $item->parentNode;
    if (!$parents
      ->contains($parent)) {
      $c = 0;
      foreach ($parent->childNodes as $child) {
        if ($child->nodeType == XML_ELEMENT_NODE && $child->tagName == $item->tagName) {
          $child->nodeIndex = ++$c;
        }
      }
      $parent->numElements = $c;
      $parents
        ->attach($parent);
    }
    if ($lastChild) {
      $indexToMatch = $item->parentNode->numElements - $item->nodeIndex + 1;
    }
    else {
      $indexToMatch = $item->nodeIndex;
    }
    if ($groupSize == 0) {
      if ($indexToMatch == $elementInGroup) {
        $matches
          ->attach($item);
      }
    }
    else {
      if (($indexToMatch - $elementInGroup) % $groupSize == 0 && ($indexToMatch - $elementInGroup) / $groupSize >= 0) {
        $matches
          ->attach($item);
      }
    }
    ++$i;
  }
  $this->matches = $matches;
}