/
AttribNode.php
131 lines (118 loc) · 4.37 KB
/
AttribNode.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
<?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\XPathExpr;
use Symfony\Component\CssSelector\Exception\ParseException;
/**
* AttribNode represents a "selector[namespace|attrib operator value]" 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 AttribNode implements NodeInterface
{
protected $selector;
protected $namespace;
protected $attrib;
protected $operator;
protected $value;
/**
* Constructor.
*
* @param NodeInterface $selector The XPath selector
* @param string $namespace The namespace
* @param string $attrib The attribute
* @param string $operator The operator
* @param string $value The value
*/
public function __construct($selector, $namespace, $attrib, $operator, $value)
{
$this->selector = $selector;
$this->namespace = $namespace;
$this->attrib = $attrib;
$this->operator = $operator;
$this->value = $value;
}
/**
* {@inheritDoc}
*/
public function __toString()
{
if ($this->operator == 'exists') {
return sprintf('%s[%s[%s]]', __CLASS__, $this->selector, $this->formatAttrib());
}
return sprintf('%s[%s[%s %s %s]]', __CLASS__, $this->selector, $this->formatAttrib(), $this->operator, $this->value);
}
/**
* {@inheritDoc}
*/
public function toXpath()
{
$path = $this->selector->toXpath();
$attrib = $this->xpathAttrib();
$value = $this->value;
if ($this->operator == 'exists') {
$path->addCondition($attrib);
} elseif ($this->operator == '=') {
$path->addCondition(sprintf('%s = %s', $attrib, XPathExpr::xpathLiteral($value)));
} elseif ($this->operator == '!=') {
// FIXME: this seems like a weird hack...
if ($value) {
$path->addCondition(sprintf('not(%s) or %s != %s', $attrib, $attrib, XPathExpr::xpathLiteral($value)));
} else {
$path->addCondition(sprintf('%s != %s', $attrib, XPathExpr::xpathLiteral($value)));
}
// path.addCondition('%s != %s' % (attrib, xpathLiteral(value)))
} elseif ($this->operator == '~=') {
$path->addCondition(sprintf("contains(concat(' ', normalize-space(%s), ' '), %s)", $attrib, XPathExpr::xpathLiteral(' '.$value.' ')));
} elseif ($this->operator == '|=') {
// Weird, but true...
$path->addCondition(sprintf('%s = %s or starts-with(%s, %s)', $attrib, XPathExpr::xpathLiteral($value), $attrib, XPathExpr::xpathLiteral($value.'-')));
} elseif ($this->operator == '^=') {
$path->addCondition(sprintf('starts-with(%s, %s)', $attrib, XPathExpr::xpathLiteral($value)));
} elseif ($this->operator == '$=') {
// Oddly there is a starts-with in XPath 1.0, but not ends-with
$path->addCondition(sprintf('substring(%s, string-length(%s)-%s) = %s', $attrib, $attrib, strlen($value) - 1, XPathExpr::xpathLiteral($value)));
} elseif ($this->operator == '*=') {
// FIXME: case sensitive?
$path->addCondition(sprintf('contains(%s, %s)', $attrib, XPathExpr::xpathLiteral($value)));
} else {
throw new ParseException(sprintf('Unknown operator: %s', $this->operator));
}
return $path;
}
/**
* Returns the XPath Attribute
*
* @return string The XPath attribute
*/
protected function xpathAttrib()
{
// FIXME: if attrib is *?
if ($this->namespace == '*') {
return '@'.$this->attrib;
}
return sprintf('@%s:%s', $this->namespace, $this->attrib);
}
/**
* Returns a formatted attribute
*
* @return string The formatted attribute
*/
protected function formatAttrib()
{
if ($this->namespace == '*') {
return $this->attrib;
}
return sprintf('%s|%s', $this->namespace, $this->attrib);
}
}