You are here

class Manipulator in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/behat/mink/src/Selector/Xpath/Manipulator.php \Behat\Mink\Selector\Xpath\Manipulator

XPath manipulation utility.

@author Graham Bates @author Christophe Coevoet <stof@notk.org>

Hierarchy

Expanded class hierarchy of Manipulator

2 files declare their use of Manipulator
Element.php in vendor/behat/mink/src/Element/Element.php
ManipulatorTest.php in vendor/behat/mink/tests/Selector/Xpath/ManipulatorTest.php

File

vendor/behat/mink/src/Selector/Xpath/Manipulator.php, line 19

Namespace

Behat\Mink\Selector\Xpath
View source
class Manipulator {

  /**
   * Regex to find union operators not inside brackets.
   */
  const UNION_PATTERN = '/\\|(?![^\\[]*\\])/';

  /**
   * Prepends the XPath prefix to the given XPath.
   *
   * The returned XPath will match elements matching the XPath inside an element
   * matching the prefix.
   *
   * @param string $xpath
   * @param string $prefix
   *
   * @return string
   */
  public function prepend($xpath, $prefix) {
    $expressions = array();

    // If the xpath prefix contains a union we need to wrap it in parentheses.
    if (preg_match(self::UNION_PATTERN, $prefix)) {
      $prefix = '(' . $prefix . ')';
    }

    // Split any unions into individual expressions.
    foreach (preg_split(self::UNION_PATTERN, $xpath) as $expression) {
      $expression = trim($expression);
      $parenthesis = '';

      // If the union is inside some braces, we need to preserve the opening braces and apply
      // the prefix only inside it.
      if (preg_match('/^[\\(\\s*]+/', $expression, $matches)) {
        $parenthesis = $matches[0];
        $expression = substr($expression, strlen($parenthesis));
      }

      // add prefix before element selector
      if (0 === strpos($expression, '/')) {
        $expression = $prefix . $expression;
      }
      else {
        $expression = $prefix . '/' . $expression;
      }
      $expressions[] = $parenthesis . $expression;
    }
    return implode(' | ', $expressions);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Manipulator::prepend public function Prepends the XPath prefix to the given XPath.
Manipulator::UNION_PATTERN constant Regex to find union operators not inside brackets.