Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. PseudoIndex.php

class PseudoIndex

Selector part index pseudo class

@author Marco Marchiò <marco.mm89@gmail.com>

Hierarchy

  • class \Peast\Selector\Node\Part\Part
    • class \Peast\Selector\Node\Part\Pseudo extends \Peast\Selector\Node\Part\Part
      • class \Peast\Selector\Node\Part\PseudoIndex extends \Peast\Selector\Node\Part\Pseudo

Expanded class hierarchy of PseudoIndex

File

vendor/mck89/peast/lib/Peast/Selector/Node/Part/PseudoIndex.php, line 20

Namespace

Peast\Selector\Node\Part
View source
class PseudoIndex extends Pseudo {
    
    /**
     * Priority
     *
     * @var int
     */
    protected $priority = 2;
    
    /**
     * Step
     *
     * @var int
     */
    protected $step = 0;
    
    /**
     * Offset
     *
     * @var int
     */
    protected $offset = 0;
    
    /**
     * Sets the step
     *
     * @param int $step Step
     *
     * @return $this
     */
    public function setStep($step) {
        $this->step = $step;
        return $this;
    }
    
    /**
     * Sets the offset
     *
     * @param int $offset Offset
     *
     * @return $this
     */
    public function setOffset($offset) {
        $this->offset = $offset;
        return $this;
    }
    
    /**
     * Returns true if the selector part matches the given node,
     * false otherwise
     *
     * @param Node $node    Node
     * @param Node $parent  Parent node
     *
     * @return bool
     */
    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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Part::getPriority public function
Pseudo::$name protected property Selector name
Pseudo::setName public function Sets the name
PseudoIndex::$offset protected property Offset
PseudoIndex::$priority protected property Priority Overrides Part::$priority
PseudoIndex::$step protected property Step
PseudoIndex::check public function Returns true if the selector part matches the given node,
false otherwise
Overrides Part::check
PseudoIndex::setOffset public function Sets the offset
PseudoIndex::setStep public function Sets the step

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal