-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
CombinedSelectorNode.php
134 lines (115 loc) · 3.71 KB
/
CombinedSelectorNode.php
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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Node;
use Symfony\Component\CssSelector\Exception\ParseException;
/**
* CombinedSelectorNode represents a combinator node.
*
* This component is a port of the Python lxml library,
* which is copyright Infrae and distributed under the BSD license.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class CombinedSelectorNode implements NodeInterface
{
static protected $methodMapping = array(
' ' => 'descendant',
'>' => 'child',
'+' => 'direct_adjacent',
'~' => 'indirect_adjacent',
);
protected $selector;
protected $combinator;
protected $subselector;
/**
* The constructor.
*
* @param NodeInterface $selector The XPath selector
* @param string $combinator The combinator
* @param NodeInterface $subselector The sub XPath selector
*/
public function __construct($selector, $combinator, $subselector)
{
$this->selector = $selector;
$this->combinator = $combinator;
$this->subselector = $subselector;
}
/**
* {@inheritDoc}
*/
public function __toString()
{
$comb = $this->combinator == ' ' ? '<followed>' : $this->combinator;
return sprintf('%s[%s %s %s]', __CLASS__, $this->selector, $comb, $this->subselector);
}
/**
* {@inheritDoc}
* @throws ParseException When unknown combinator is found
*/
public function toXpath()
{
if (!isset(self::$methodMapping[$this->combinator])) {
throw new ParseException(sprintf('Unknown combinator: %s', $this->combinator));
}
$method = '_xpath_'.self::$methodMapping[$this->combinator];
$path = $this->selector->toXpath();
return $this->$method($path, $this->subselector);
}
/**
* Joins a NodeInterface into the XPath of this object.
*
* @param XPathExpr $xpath The XPath expression for this object
* @param NodeInterface $sub The NodeInterface object to add
*/
protected function _xpath_descendant($xpath, $sub)
{
// when sub is a descendant in any way of xpath
$xpath->join('/descendant::', $sub->toXpath());
return $xpath;
}
/**
* Joins a NodeInterface as a child of this object.
*
* @param XPathExpr $xpath The parent XPath expression
* @param NodeInterface $sub The NodeInterface object to add
*/
protected function _xpath_child($xpath, $sub)
{
// when sub is an immediate child of xpath
$xpath->join('/', $sub->toXpath());
return $xpath;
}
/**
* Joins an XPath expression as an adjacent of another.
*
* @param XPathExpr $xpath The parent XPath expression
* @param NodeInterface $sub The adjacent XPath expression
*/
protected function _xpath_direct_adjacent($xpath, $sub)
{
// when sub immediately follows xpath
$xpath->join('/following-sibling::', $sub->toXpath());
$xpath->addNameTest();
$xpath->addCondition('position() = 1');
return $xpath;
}
/**
* Joins an XPath expression as an indirect adjacent of another.
*
* @param XPathExpr $xpath The parent XPath expression
* @param NodeInterface $sub The indirect adjacent NodeInterface object
*/
protected function _xpath_indirect_adjacent($xpath, $sub)
{
// when sub comes somewhere after xpath as a sibling
$xpath->join('/following-sibling::', $sub->toXpath());
return $xpath;
}
}