forked from chris900/phpCSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpCSS.php
136 lines (123 loc) · 3.14 KB
/
phpCSS.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
<?php
require_once 'Selector.php';
require_once 'Element.php';
class phpCSS {
private $dom;
public function __construct($htmlfile) {
if ($path = realpath($htmlfile)) {
$htmlfile = file_get_contents($path);
}
$this->dom = new DomDocument();
@$this->dom->loadHTML($htmlfile);
$this->root_elm = new Element($this->dom->documentElement);
}
private function findAllBySelector($elms, $sel, $trans) {
$ret_elms = array();
foreach ($elms as $elm) {
switch ($trans) {
case 'end':
break;
case 'child':
if ($elm->elm->hasChildNodes()) {
foreach($elm->elm->childNodes as $item) {
if (is_a($item, 'DOMElement')) {
$e = new Element($item);
if ($e->matchesSingleSelector($sel)) {
$ret_elms[] = $e;
}
}
}
}
break;
case 'sibling':
if ($sib = $elm->elm->nextSibling) {
$e = new Element($sib);
if (is_a($sib, 'DOMElement')) {
if ($e->matchesSingleSelector($sel)) {
$ret_elms[] = $e;
}
}
$ret_elms = array_merge($ret_elms, $this->findAllBySelector(array($e), $sel, 'sibling'));
}
break;
case 'immediate_sibling':
if ($sib = $elm->elm->nextSibling) {
$e = new Element($sib);
if (is_a($sib, 'DOMElement')) {
if ($e->matchesSingleSelector($sel)) {
$ret_elms[] = $e;
}
}
else {
$ret_elms = array_merge($ret_elms, $this->findAllBySelector(array($e), $sel, 'immediate_sibling'));
}
}
break;
default: //for descend & start
if ($trans == 'start' && ($sel['name'] === '*' || $sel['name'] === 'html')) {
if ($elm->matchesSingleSelector($sel)) {
$ret_elms[] = $elm;
}
break;
}
if ($elm->elm->hasChildNodes()) {
foreach($elm->elm->childNodes as $item) {
if (is_a($item, 'DOMElement')) {
$e = new Element($item);
if ($e->matchesSingleSelector($sel)) {
$ret_elms[] = $e;
}
$ret_elms = array_merge($ret_elms, $this->findAllBySelector(array($e), $sel, 'descend'));
}
}
}
break;
}
}
return $ret_elms;
}
public function find($sel, $selms = false, $limit = false) {
$selUtil = new SelectorUtil();
$selectors = $selUtil->format($sel);
if ($selms === false) {
$selms = array($this->root_elm);
}
$parent_trans = 'start';
foreach ($selectors as $selector) {
$elms = $selms;
foreach ($selector as $bit) {
$elms = $this->findAllBySelector($elms, $bit, $parent_trans);
$parent_trans = $bit['trans'];
if (empty($elms) || $bit['trans'] == 'end') {
break;
}
}
if ($limit !== false) {
$limit--;
if ($limit <= 0) {
return $elms;
}
}
}
if (empty($elms)) {
return false;
}
return $elms;
}
public function selector_used($selector) {
$elms = array($this->root_elm);
$new_elms = false;
$parent_trans = 'start';
foreach ($selector as $bit) {
$elms = $this->findAllBySelector($elms, $bit, $parent_trans);
$parent_trans = $bit['trans'];
if (empty($elms)) {
return false;
}
else if ($bit['trans'] == 'end') {
return true;
}
}
return true;
}
}