You are here

protected function QueryPathCssEventHandler::nthChild in QueryPath 7.2

Same name and namespace in other branches
  1. 6 QueryPath/CssEventHandler.php \QueryPathCssEventHandler::nthChild()
  2. 7.3 QueryPath/CssEventHandler.php \QueryPathCssEventHandler::nthChild()
2 calls to QueryPathCssEventHandler::nthChild()
QueryPathCssEventHandler::nthLastChild in QueryPath/CssEventHandler.php
QueryPathCssEventHandler::pseudoClass in QueryPath/CssEventHandler.php

File

QueryPath/CssEventHandler.php, line 602

Class

QueryPathCssEventHandler

Code

protected function nthChild($groupSize, $elementInGroup, $lastChild = FALSE) {
  $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 && ($this->findAnyElement || $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;
}