SelectorsHandler.php
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*
* This file is part of the Mink package.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\Mink\Selector;
use Behat\Mink\Selector\Xpath\Escaper;
/**
* Selectors handler.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class SelectorsHandler
{
private $selectors;
private $escaper;
/**
* Initializes selectors handler.
*
* @param SelectorInterface[] $selectors default selectors to register
*/
public function __construct(array $selectors = array())
{
$this->escaper = new Escaper();
$this->registerSelector('named_partial', new PartialNamedSelector());
$this->registerSelector('named_exact', new ExactNamedSelector());
$this->registerSelector('css', new CssSelector());
foreach ($selectors as $name => $selector) {
$this->registerSelector($name, $selector);
}
}
/**
* Registers new selector engine with specified name.
*
* @param string $name selector engine name
* @param SelectorInterface $selector selector engine instance
*/
public function registerSelector($name, SelectorInterface $selector)
{
$this->selectors[$name] = $selector;
}
/**
* Checks whether selector with specified name is registered on handler.
*
* @param string $name selector engine name
*
* @return Boolean
*/
public function isSelectorRegistered($name)
{
return isset($this->selectors[$name]);
}
/**
* Returns selector engine with specified name.
*
* @param string $name selector engine name
*
* @return SelectorInterface
*
* @throws \InvalidArgumentException
*/
public function getSelector($name)
{
if ('named' === $name) {
@trigger_error(
'Using the "named" selector directly from the handler is deprecated as of 1.6 and will be removed in 2.0.'
.' Use the "named_partial" or use the "named" selector through the Element API instead.',
E_USER_DEPRECATED
);
$name = 'named_partial';
}
if (!$this->isSelectorRegistered($name)) {
throw new \InvalidArgumentException("Selector \"$name\" is not registered.");
}
return $this->selectors[$name];
}
/**
* Translates selector with specified name to XPath.
*
* @param string $selector selector engine name (registered)
* @param string|array $locator selector locator (an array or a string depending of the selector being used)
*
* @return string
*/
public function selectorToXpath($selector, $locator)
{
if ('xpath' === $selector) {
if (!is_string($locator)) {
throw new \InvalidArgumentException('The xpath selector expects to get a string as locator');
}
return $locator;
}
return $this->getSelector($selector)->translateToXPath($locator);
}
/**
* Translates string to XPath literal.
*
* @deprecated since Mink 1.7. Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral when building Xpath
* or pass the unescaped value when using the named selector.
*
* @param string $s
*
* @return string
*/
public function xpathLiteral($s)
{
@trigger_error(
'The '.__METHOD__.' method is deprecated as of 1.7 and will be removed in 2.0.'
.' Use \Behat\Mink\Selector\Xpath\Escaper::escapeLiteral instead when building Xpath'
.' or pass the unescaped value when using the named selector.',
E_USER_DEPRECATED
);
return $this->escaper->escapeLiteral($s);
}
}