forked from tj/php-selector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselector.inc
128 lines (115 loc) · 4.09 KB
/
selector.inc
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
<?php
// --- Selector.inc - (c) Copyright TJ Holowaychuk <tj@vision-media.ca> MIT Licensed
define('SELECTOR_VERSION', '1.1.3');
/**
* SelectorDOM.
*
* Persitant object for selecting elements.
*
* $dom = new SelectorDOM($html);
* $links = $dom->select('a');
* $list_links = $dom->select('ul li a');
*
*/
class SelectorDOM {
public function SelectorDOM($html) {
$this->html = $html;
$this->dom = new DOMDocument();
@$this->dom->loadHTML($html);
$this->xpath = new DOMXpath($this->dom);
}
public function select($selector, $as_array = true) {
$elements = $this->xpath->evaluate(selector_to_xpath($selector));
return $as_array ? elements_to_array($elements) : $element;
}
}
/**
* Select elements from $html using the css $selector.
* When $as_array is true elements and their children will
* be converted to array's containing the following keys (defaults to true):
*
* - name : element name
* - text : element text
* - children : array of children elements
* - attributes : attributes array
*
* Otherwise regular DOMElement's will be returned.
*/
function select_elements($selector, $html, $as_array = true) {
$dom = new SelectorDOM($html);
return $dom->select($selector, $as_array);
}
/**
* Convert $elements to an array.
*/
function elements_to_array($elements) {
$array = array();
for ($i = 0, $length = $elements->length; $i < $length; ++$i)
if ($elements->item($i)->nodeType == XML_ELEMENT_NODE)
array_push($array, element_to_array($elements->item($i)));
return $array;
}
/**
* Convert $element to an array.
*/
function element_to_array($element) {
$array = array(
'name' => $element->nodeName,
'attributes' => array(),
'text' => $element->textContent,
'children' =>elements_to_array($element->childNodes)
);
if ($element->attributes->length)
foreach($element->attributes as $key => $attr)
$array['attributes'][$key] = $attr->value;
return $array;
}
/**
* Convert $selector into an XPath string.
*/
function selector_to_xpath($selector) {
$selector = 'descendant-or-self::' . $selector;
// ,
$selector = preg_replace('/\s*,\s*/', '|descendant-or-self::', $selector);
// :button, :submit, etc
$selector = preg_replace('/:(button|submit|file|checkbox|radio|image|reset|text|password)/', 'input[@type="\1"]', $selector);
// [id]
$selector = preg_replace('/\[(\w+)\]/', '*[@\1]', $selector);
// foo[id=foo]
$selector = preg_replace('/\[(\w+)=[\'"]?(.*?)[\'"]?\]/', '[@\1="\2"]', $selector);
// [id=foo]
$selector = str_replace(':[', ':*[', $selector);
// div#foo
$selector = preg_replace('/([\w\-]+)\#([\w\-]+)/', '\1[@id="\2"]', $selector);
// #foo
$selector = preg_replace('/\#([\w\-]+)/', '*[@id="\1"]', $selector);
// div.foo
$selector = preg_replace('/([\w\-]+)\.([\w\-]+)/', '\1[contains(@class,"\2")]', $selector);
// .foo
$selector = preg_replace('/\.([\w\-]+)/', '*[contains(@class,"\1")]', $selector);
// div:first-child
$selector = preg_replace('/([\w\-]+):first-child/', '*/\1[position()=1]', $selector);
// div:last-child
$selector = preg_replace('/([\w\-]+):last-child/', '*/\1[position()=last()]', $selector);
// :first-child
$selector = str_replace(':first-child', '*/*[position()=1]', $selector);
// :last-child
$selector = str_replace(':last-child', '*/*[position()=last()]', $selector);
// div:nth-child
$selector = preg_replace('/([\w\-]+):nth-child\((\d+)\)/', '*/\1[position()=\2]', $selector);
// :nth-child
$selector = preg_replace('/:nth-child\((\d+)\)/', '*/*[position()=\1]', $selector);
// :contains(Foo)
$selector = preg_replace('/([\w\-]+):contains\((.*?)\)/', '\1[contains(string(.),"\2")]', $selector);
// >
$selector = preg_replace('/\s*>\s*/', '/', $selector);
// ~
$selector = preg_replace('/\s*~\s*/', '/following-sibling::', $selector);
// +
$selector = preg_replace('/\s*\+\s*([\w\-]+)/', '/following-sibling::\1[position()=1]', $selector);
// ' '
$selector = preg_replace('/\s+/', '/descendant::', $selector);
$selector = str_replace(']*', ']', $selector);
$selector = str_replace(']/*', ']', $selector);
return $selector;
}