4 * This file is part of the Mink package.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Mink\Selector;
13 use Behat\Mink\Selector\Xpath\Escaper;
18 * @author Konstantin Kudryashov <ever.zet@gmail.com>
20 class SelectorsHandler
26 * Initializes selectors handler.
28 * @param SelectorInterface[] $selectors default selectors to register
30 public function __construct(array $selectors = array())
32 $this->escaper = new Escaper();
34 $this->registerSelector('named_partial', new PartialNamedSelector());
35 $this->registerSelector('named_exact', new ExactNamedSelector());
36 $this->registerSelector('css', new CssSelector());
38 foreach ($selectors as $name => $selector) {
39 $this->registerSelector($name, $selector);
44 * Registers new selector engine with specified name.
46 * @param string $name selector engine name
47 * @param SelectorInterface $selector selector engine instance
49 public function registerSelector($name, SelectorInterface $selector)
51 $this->selectors[$name] = $selector;
55 * Checks whether selector with specified name is registered on handler.
57 * @param string $name selector engine name
61 public function isSelectorRegistered($name)
63 return isset($this->selectors[$name]);
67 * Returns selector engine with specified name.
69 * @param string $name selector engine name
71 * @return SelectorInterface
73 * @throws \InvalidArgumentException
75 public function getSelector($name)
77 if ('named' === $name) {
79 'Using the "named" selector directly from the handler is deprecated as of 1.6 and will be removed in 2.0.'
80 .' Use the "named_partial" or use the "named" selector through the Element API instead.',
83 $name = 'named_partial';
86 if (!$this->isSelectorRegistered($name)) {
87 throw new \InvalidArgumentException("Selector \"$name\" is not registered.");
90 return $this->selectors[$name];
94 * Translates selector with specified name to XPath.
96 * @param string $selector selector engine name (registered)
97 * @param string|array $locator selector locator (an array or a string depending of the selector being used)
101 public function selectorToXpath($selector, $locator)
103 if ('xpath' === $selector) {
104 if (!is_string($locator)) {
105 throw new \InvalidArgumentException('The xpath selector expects to get a string as locator');
111 return $this->getSelector($selector)->translateToXPath($locator);
115 * Translates string to XPath literal.
117 * @deprecated since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath
118 * or pass the unescaped value when using the named selector.
124 public function xpathLiteral($s)
127 'The '.__METHOD__.' method is deprecated as of 1.7 and will be removed in 2.0.'
128 .' Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral instead when building Xpath'
129 .' or pass the unescaped value when using the named selector.',
133 return $this->escaper->escapeLiteral($s);