-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathElement.php
274 lines (247 loc) · 5.1 KB
/
Element.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
//removes multiple delimiters if they are next to each other
function multi_explode ($d, $s) {
$t = explode($d, $s);
$r = array();
foreach ($t as $k => $v) {
if ($v !== '') {
$r[] = $v;
}
}
return $r;
}
class Element {
public $elm;
public $parent;
public function __construct($elm) {
$this->elm = $elm;
$this->parent = $this->elm->parentNode;
}
public function getName() {
return $this->elm->nodeName;
}
public function isCalled($name) {
if ($name === '*' || $name === 'html' || $name === 'body') {
return true; // quick fix for * html
}
return ($this->getName() === $name);
}
public function getValue() {
return $this->elm->nodeValue;
}
public function getId() {
if ($this->elm->hasAttributes()) {
if ($ret = $this->elm->attributes->getNamedItem('id')) {
return $ret->nodeValue;
}
}
return false;
}
public function isId($id) {
if ($ret = $this->getId()) {
return ($ret === $id);
}
return false;
}
public function getClasses() {
if ($this->elm->hasAttributes()) {
if ($ret = $this->elm->attributes->getNamedItem('class')) {
return multi_explode(' ', $ret->nodeValue);
}
}
return false;
}
public function hasClass($class) {
if ($ret = $this->getClasses()) {
return (in_array($class, $ret));
}
return false;
}
public function getAttributes() {
if ($this->elm->hasAttributes()) {
$ret = array();
foreach ($this->elm->attributes as $attr) {
$ret[$attr->nodeName] = trim($attr->nodeValue);
}
return $ret;
}
return false;
}
public function getAttributeValue($attr) {
if ($ret = $this->getAttributes()) {
if (isset($ret[$attr])) {
return $ret[$attr];
}
}
return false;
}
public function AttributeMatches($attr, $opp, $val) {
if ($ret = $this->getAttributeValue($attr)) {
if ($opp == '~=') {
$list = multi_explode(' ', $ret);
return (in_array($val, $list));
}
else if ($opp == '^=') {
return (substr($ret, 0, strlen($val)) == $val);
}
else if ($opp == '$=') {
return (substr($ret, -strlen($val)) == $val);
}
else if ($opp == '*=') {
retrun (strpos($ret, $val) !== false);
}
else if ($opp == '|=') {
$list = multi_explode('-', $ret);
return ($val == $list[0]);
}
else { // if $opp is '='
return ($ret == $val);
}
}
return false;
}
public function matchesSingleSelector($sel) {
if ($sel['name'] !== false) {
if ($sel['name'] == '*') {
return true;
}
else if (!$this->isCalled($sel['name'])) {
return false;
}
}
if ($sel['id'] !== false) {
if (!$this->isId($sel['id'])) {
return false;
}
}
if (!empty($sel['classes'])) {
foreach ($sel['classes'] as $class) {
if (!$this->hasClass($class)) {
return false;
}
}
}
if (!empty($sel['attr'])) {
foreach ($sel['attr'] as $attr) {
if (!$this->attributeMatches($attr['name'], $attr['opp'], $attr['value'])) {
return false;
}
}
}
if (!empty($sel['pseudos'])) {
foreach ($sel['pseudos'] as $pseudo => $pval) {
if (!$this->is($pseudo, $pval)) {
return false;
}
}
}
return true;
}
public function is($value, $data = false) {
switch (strtolower($value)) {
case 'root':
if (!$this->isCalled('html')) {
return false;
}
break;
case 'nth-child':
break;
case 'nth-last-child':
break;
case 'nth-of-type':
break;
case 'nth-last-of-type':
break;
case 'first-child':
if (!$this->parent->firstChild->isSameNode($this->elm)) {
return false;
}
break;
case 'last-child':
if (!$this->parent->lastChild->isSameNode($this->elm)) {
return false;
}
break;
case 'first-of-type':
break;
case 'last-of-type':
break;
case 'only-child':
if (!$this->parent->lastChild->isSameNode($this->parent->firstChild)) {
return false;
}
break;
case 'only-of-type':
break;
case 'empty':
if (!$this->elm->hasChildNodes()) {
return false;
}
else {
foreach($elm->elm->childNodes as $item) {
if (is_a($item, 'DOMElement')) {
return false;
}
}
}
break;
case 'link':
if (!$this->isCalled('a')) {
return false;
}
break;
case 'visited':
if (!$this->isCalled('a')) {
return false;
}
break;
case 'active':
return true;
break;
case 'hover':
return true;
break;
case 'focus':
return true;
break;
case 'target':
return true;
break;
case 'lang':
return true;
break;
case 'enabled':
return true;
break;
case 'disabled':
return true;
break;
case 'checked':
return true;
break;
case 'first-line':
if (!$this->elm->hasChildNodes()) {
return false;
}
break;
case 'first-letter':
if (!$this->elm->hasChildNodes()) {
return false;
}
break;
case 'before':
return true;
break;
case 'after':
return true;
break;
case 'not':
$su = new SelectorUtil($data);
if ($e->matchesSingleSelector($su->Data[0][0])) {
return false;
}
break;
}
return true;
}
}