Translates provided locator into XPath.

@param string|array $locator selector name or array of (selector_name, locator)

@return string

@throws \InvalidArgumentException
498911
public function translateToXPath($locator)
    {
        if (\is_array($locator)) {
            if (2 !== \count($locator)) {
                throw new \InvalidArgumentException('NamedSelector expects array(name, locator) as argument');
            }

            $selector = $locator[0];
            $locator = $locator[1];
        } else {
            $selector = (string) $locator;
            $locator = null;
        }

        if (!isset($this->selectors[$selector])) {
            throw new \InvalidArgumentException(sprintf(
                'Unknown named selector provided: "%s". Expected one of (%s)',
                $selector,
                implode(', ', array_keys($this->selectors))
            ));
        }

        $xpath = $this->selectors[$selector];

        if (null !== $locator) {
            $xpath = strtr($xpath, array('%locator%' => $this->escapeLocator($locator)));
        }

        return $xpath;
    }
7074
private function relativize(string $xpath): string
    {
        $expressions = [];

        // An expression which will never match to replace expressions which cannot match in the crawler
        // We cannot drop
        $nonMatchingExpression = 'a[name() = "b"]';

        $xpathLen = \strlen($xpath);
        $openedBrackets = 0;
        $startPosition = strspn($xpath, " \t\n\r\0\x0B");

        for ($i = $startPosition; $i <= $xpathLen; ++$i) {
            $i += strcspn($xpath, '"\'[]|', $i);

            if ($i < $xpathLen) {
                switch ($xpath[$i]) {
                    case '"':
                    case "'":
                        if (false === $i = strpos($xpath, $xpath[$i], $i + 1)) {
                            return $xpath; // The XPath expression is invalid
                        }
                        continue 2;
                    case '[':
                        ++$openedBrackets;
                        continue 2;
                    case ']':
                        --$openedBrackets;
                        continue 2;
                }
            }
            if ($openedBrackets) {
                continue;
            }

            if ($startPosition < $xpathLen && '(' === $xpath[$startPosition]) {
                // If the union is inside some braces, we need to preserve the opening braces and apply
                // the change only inside it.
                $j = 1 + strspn($xpath, "( \t\n\r\0\x0B", $startPosition + 1);
                $parenthesis = substr($xpath, $startPosition, $j);
                $startPosition += $j;
            } else {
                $parenthesis = '';
            }
            $expression = rtrim(substr($xpath, $startPosition, $i - $startPosition));

            if (0 === strpos($expression, 'self::*/')) {
                $expression = './'.substr($expression, 8);
            }

            // add prefix before absolute element selector
            if ('' === $expression) {
                $expression = $nonMatchingExpression;
            } elseif (0 === strpos($expression, '//')) {
                $expression = 'descendant-or-self::'.substr($expression, 2);
            } elseif (0 === strpos($expression, './/')) {
                $expression = 'descendant-or-self::'.substr($expression, 3);
            } elseif (0 === strpos($expression, './')) {
                $expression = 'self::'.substr($expression, 2);
            } elseif (0 === strpos($expression, 'child::')) {
                $expression = 'self::'.substr($expression, 7);
            } elseif ('/' === $expression[0] || '.' === $expression[0] || 0 === strpos($expression, 'self::')) {
                $expression = $nonMatchingExpression;
            } elseif (0 === strpos($expression, 'descendant::')) {
                $expression = 'descendant-or-self::'.substr($expression, 12);
            } elseif (preg_match('/^(ancestor|ancestor-or-self|attribute|following|following-sibling|namespace|parent|preceding|preceding-sibling)::/', $expression)) {
                // the fake root has no parent, preceding or following nodes and also no attributes (even no namespace attributes)
                $expression = $nonMatchingExpression;
            } elseif (0 !== strpos($expression, 'descendant-or-self::')) {
                $expression = 'self::'.$expression;
            }
            $expressions[] = $parenthesis.$expression;

            if ($i === $xpathLen) {
                return implode(' | ', $expressions);
            }

            $i += strspn($xpath, " \t\n\r\0\x0B", $i + 1);
            $startPosition = $i + 1;
        }

        return $xpath; // The XPath expression is invalid
    }
1311594
private function getPathFromUri($uri)
    {
        $uriParser = new UriParser($uri);
        // Prepare uri
        $uriParser->encode();
        $uri = $uriParser->stripFragment();
        if (strpos($uri, '/') === 0) {
            // URI is already an path
            return $uri;
        }
        if (!$uriParser->validate()) {
            throw new \InvalidArgumentException('Invalid URI');
        }
        $path = (($path = parse_url($uri, PHP_URL_PATH)) === null) ? '/' : $path;
        $query = (($query = parse_url($uri, PHP_URL_QUERY)) === null) ? '' : '?' . $query;
        return $path . $query;
    }
1048874
public function querySelector($selector)
    {
        $converter = new CssSelectorConverter();

        $xpathQuery = $converter->toXPath($selector);

        $result = $this->xpath->query($xpathQuery);

        if (!$result->item(0)) {
            return null;
        }
        return new Node($result->item(0));
    }
550147
public function findOrFail($selector)
    {
        if (! is_null($element = $this->findById($selector))) {
            return $element;
        }

        return $this->driver->findElement(
            WebDriverBy::cssSelector($this->format($selector))
        );
    }
1193729
public function getDescendant(
        SimpleXMLElement $element,
        array $path
    ) {
        foreach ($path as $childProperties) {
            if (!is_array($childProperties)
                || empty($childProperties)
            ) {
                throw new InvalidArgumentException('Invalid descendant path');
            }

            $name       = array_shift($childProperties);
            $attributes = count($childProperties)
                ? array_shift($childProperties)
                : [];

            $element = $this->getChild($element, $name, $attributes);
        }

        return $element;
    }
1227041
public function typeKeys($locator, $value)
    {
        try {
            return $this->getMinkSession()->getDriver()->getBrowser()->typeKeys($locator, $value);
        } catch (\Selenium\Exception $e) {
            sleep(1);
            return $this->getMinkSession()->getDriver()->getBrowser()->typeKeys($locator, $value);
        }
    }
678981
public function selectLink($value)
    {
        $xpath = sprintf('descendant-or-self::a[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) ', static::xpathLiteral(' '.$value.' ')).
                            sprintf('or ./img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)]]', static::xpathLiteral(' '.$value.' '));

        return $this->filterRelativeXPath($xpath);
    }
1176890
private function getSelectorAsXpath(SelectorsHandler $selectorsHandler)
    {
        $selectorType = is_array($this->selector) ? key($this->selector) : 'css';
        $locator = is_array($this->selector) ? $this->selector[$selectorType] : $this->selector;

        return $selectorsHandler->selectorToXpath($selectorType, $locator);
    }
1092260
public function getValidatorsLocator() {
		if ($this->_validatorsLocator === null) {
			if ($this->hasLocator()) {
				$this->_validatorsLocator = $this->getLocator()->get('validatorsLocator');
				
				if (! $this->_validatorsLocator instanceof LocatorByArgumentsInterface) {
					throw new Exception('Validators\' locator must implements "LocatorByArgumentsInterface" interface');
				}
			} else {
				throw new Exception('Unable to provide validators\' locator');
			}
		}
		
		return $this->_validatorsLocator;
	}
1345438
public function findByXpath($locator)
    {
        $locator = \Normalizer::normalize($locator, \Normalizer::FORM_D);
        return parent::find($locator);
    }
161184
public static function toArray($value)
    {
        if (is_array($value)) {
            return $value;
        }

        if ($value === null) {
            return [];
        }

        if (!$value instanceof Traversable) {
            throw new Exception\InvalidArgumentException(
                'An invalid value was provided, should either be an null, array or Traversable'
            );
        }

        return iterator_to_array($value);
    }
750341
public function uncheckField($locator)
    {
        $field = $this->findField($locator);

        if (null === $field) {
            throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
        }

        $field->uncheck();
    }
1099073
public static function getSegments($selector)
    {
        $selector = trim($selector);

        if ($selector === '') {
            throw new InvalidSelectorException('The selector must not be empty');
        }

        $tag = '(?P<tag>[\*|\w|\-]+)?';
        $id = '(?:#(?P<id>[\w|\-]+))?';
        $classes = '(?P<classes>\.[\w|\-|\.]+)*';
        $attrs = '(?P<attrs>(?:\[.+?\])*)?';
        $name = '(?P<pseudo>[\w\-]+)';
        $expr = '(?:\((?P<expr>[^\)]+)\))';
        $pseudo = '(?::'.$name.$expr.'?)?';
        $rel = '\s*(?P<rel>>)?';

        $regexp = '/'.$tag.$id.$classes.$attrs.$pseudo.$rel.'/is';

        if (preg_match($regexp, $selector, $segments)) {
            if ($segments[0] === '') {
                throw new InvalidSelectorException(sprintf('Invalid selector "%s"', $selector));
            }

            $result['selector'] = $segments[0];

            if (isset($segments['tag']) && $segments['tag'] !== '') {
                $result['tag'] = $segments['tag'];
            }

            // if the id attribute specified
            if (isset($segments['id']) && $segments['id'] !== '') {
                $result['id'] = $segments['id'];
            }

            // if the attributes specified
            if (isset($segments['attrs'])) {
                $attributes = trim($segments['attrs'], '[]');
                $attributes = explode('][', $attributes);

                foreach ($attributes as $attribute) {
                    if ($attribute !== '') {
                        list($name, $value) = array_pad(explode('=', $attribute, 2), 2, null);

                        if ($name === '') {
                            throw new InvalidSelectorException(sprintf('Invalid selector "%s": attribute name must not be empty', $selector));
                        }

                        // equal null if specified only the attribute name
                        $result['attributes'][$name] = is_string($value) ? trim($value, '\'"') : null;
                    }
                }
            }

            // if the class attribute specified
            if (isset($segments['classes'])) {
                $classes = trim($segments['classes'], '.');
                $classes = explode('.', $classes);

                foreach ($classes as $class) {
                    if ($class !== '') {
                        $result['classes'][] = $class;
                    }
                }
            }

            // if the pseudo class specified
            if (isset($segments['pseudo']) && $segments['pseudo'] !== '') {
                $result['pseudo'] = $segments['pseudo'];

                if (isset($segments['expr']) && $segments['expr'] !== '') {
                    $result['expr'] = $segments['expr'];
                }
            }

            // if it is a direct descendant
            if (isset($segments['rel'])) {
                $result['rel'] = $segments['rel'];
            }

            return $result;
        }

        throw new InvalidSelectorException(sprintf('Invalid selector "%s"', $selector));
    }
1041557
private static function AtomizeIterator( $iter )
	{
		/**
		 * @var XPathNavigator $item
		 */
		foreach ( $iter as $item )
		{
			if ( $item->getIsNode() )
			{
				if ( $item->getNodeType() == XPathNodeType::Comment )
				{
					continue;
				}

				if ( $item->getNodeType() == XPathNodeType::Element )
				{
					$child = $item->CloneInstance();
					$result = $child->MoveToFirstChild();

					$items = array();

					if ( $child->getNodeType() == XPathNodeType::Text )
					{
						$items[] = $item->getTypedValue();
					}
					else
					{
						while ( $result )
						{
							$data = ExtFuncs::GetData( XPath2NodeIterator::Create( $child->CloneInstance() ) );
							if ( $data->MoveNext() )
							{
								$items[] = $data->getCurrent(); // ->getValue();
							}

							$result = $child->MoveToNext();
						}
					}

					if ( ! count( $items ) )
					{
						yield Undefined::getValue();
					}
					else if ( count( $items ) == 1 )
					{
						yield $items[0];
					}
					else
					{
						yield XPath2Item::fromValueAndType( implode( "", $items ), XmlSchema::$String );
					}

					return false;
				}
				else
				if ( is_null( $item->getTypedValue() ) )
				{
			    	if ( $item instanceof XPathNavigator )
					{
						/**
						 * @var XPathNavigator $nav
						 */
						$nav = $item;
						if ( ! is_null( $nav->getSchemaInfo() ) &&
							 ! is_null( $nav->getSchemaInfo()->getSchemaType() ) &&
							 ! ( $nav->getSchemaInfo()->getSchemaType() instanceof XmlSchemaSimpleType ) )
						{
							throw XPath2Exception::withErrorCodeAndParam( "FOTY0012", Resources::FOTY0012, new XmlQualifiedName( $nav->LocalName, $nav->NamespaceURI, false ) );
						}
					}
				}
			}

		    yield $item instanceof XPath2Item
		    	? $item
		    	: XPath2Item::fromValue( $item->GetTypedValue() );
		}
	}
1112204
public function getLocatorAccessPolicy($locator)
    {
        $locatorId = Utilities::getEntityId(
            $locator,
            'WindowsAzure\MediaServices\Models\Locator'
        );

        return AccessPolicy::createFromOptions(
            $this->_getEntity("Locators('{$locatorId}')/AccessPolicy")
        );
    }
1142310
private function getControllerLocator($controller)
    {
        $class = null;
        $prefix = null;

        if (0 === strpos($controller, '@')) {
            $file = $this->locator->locate($controller);
            $controllerClass = ClassUtils::findClassInFile($file);

            if (false === $controllerClass) {
                throw new \InvalidArgumentException(sprintf('Can\'t find class for controller "%s"', $controller));
            }

            $controller = $controllerClass;
        }

        if ($this->container->has($controller)) {
            // service_id
            $prefix = $controller.':';

            if (Kernel::VERSION_ID >= 40100) {
                $prefix .= ':';
            }

            $useScope = method_exists($this->container, 'enterScope') && $this->container->hasScope('request');
            if ($useScope) {
                $this->container->enterScope('request');
                $this->container->set('request', new Request());
            }
            $class = get_class($this->container->get($controller));
            if ($useScope) {
                $this->container->leaveScope('request');
            }
        } elseif (class_exists($controller)) {
            // full class name
            $class = $controller;
            $prefix = $class.'::';
        } elseif (false !== strpos($controller, ':')) {
            // bundle:controller notation
            try {
                $notation = $this->controllerParser->parse($controller.':method');
                list($class) = explode('::', $notation);
                $prefix = $class.'::';
            } catch (\Exception $e) {
                throw new \InvalidArgumentException(
                    sprintf('Can\'t locate "%s" controller.', $controller)
                );
            }
        }

        if (empty($class)) {
            throw new \InvalidArgumentException(sprintf(
                'Class could not be determined for Controller identified by "%s".', $controller
            ));
        }

        return [$prefix, $class];
    }