function PseudoIndex::check
Returns true if the selector part matches the given node, false otherwise
Parameters
Node $parent Parent node:
Return value
bool
Overrides Part::check
File
-
vendor/
mck89/ peast/ lib/ Peast/ Selector/ Node/ Part/ PseudoIndex.php, line 78
Class
- PseudoIndex
- Selector part index pseudo class
Namespace
Peast\Selector\Node\PartCode
public function check(Node $node, $parent = null) {
$props = Utils::getExpandedNodeProperties($parent);
$count = count($props);
$reverse = $this->name === "nth-last-child";
if ($reverse) {
$start = $count - 1 - ($this->offset - 1);
$step = $this->step * -1;
if ($step > 0) {
$reverse = false;
}
}
else {
$start = $this->offset - 1;
$step = $this->step;
if ($step < 0) {
$reverse = true;
}
}
//Step 0 will cause an infinite loop, so it must be set to the
//number of props so that it will execute only one iteration
if (!$step) {
$step = $reverse ? -$count : $count;
}
for ($i = $start; $reverse && $i >= 0 || !$reverse && $i < $count; $i += $step) {
if (isset($props[$i]) && $props[$i] === $node) {
return true;
}
}
return false;
}