-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaterGlass.php
204 lines (175 loc) · 4.19 KB
/
WaterGlass.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
<?php
class WaterGlass extends DOMDocument
{
private $varTagName;
private $customVars = array();
function __construct( $content, $var_tag_name = 'wg' )
{
parent::__construct('1.0', 'utf-8');
$this->strictErrorChecking = false;
$this->preserveWhiteSpace = false;
$this->formatOutput = true;
if( is_file($content) )
{
$this->loadFromFile($content);
}
else
{
$this->loadFromString($content);
}
$this->setVariableTag( $var_tag_name );
}
private function loadFromFile( $filename )
{
@$this->loadHTMLFile( $filename );
}
private function loadFromString( $string )
{
@$this->loadHTML( $string );
}
private function fetchVars()
{
if( $list = $this->find( sprintf('%s[id]', $this->varTagName) ) )
{
foreach ($list as $element)
{
$this->customVars[ $element->getAttribute('id') ] = $element;
}
}
}
private function find( $expression )
{
if( array_key_exists( $expression, $this->customVars ) )
{
return array($this->customVars[$expression]->parentNode);
}
else
{
$xpath = new DOMXPath($this);
if(!preg_match('/\//', $expression))
{
$sel = new DOMSelector($expression);
$expression = $sel->toXpath();
}
$nodelist = $xpath->query($expression, $this);
$list = array();
foreach($nodelist as $node)
{
$list[] = $node;
}
return ( count($list) ) ? $list : false;
}
}
private function inject($expression, $value, $replace_content = false, $duplicate_element = false)
{
if( is_array($value) )
{
foreach( $value as $iter )
{
$this->inject($expression, $iter, $replace_content, $duplicate_element);
}
}
else
{
$elements = $this->find( $expression );
if( !$elements )
{
throw new Exception( sprintf('%s element not found', $expression) );
}
if( $duplicate_element && count($elements) )
{
$source = $elements[count($elements) - 1];
if( count($elements) == 1 )
{
$this->customVars[uniqid()] = $source;
}
$clone = $source->cloneNode( true );
$clone->removeAttribute('id');
$source->parentNode->appendChild( $clone );
$elements = array($clone);
}
foreach ($elements as $element)
{
$tmpDoc = new DOMDocument($this->xmlVersion, $this->xmlEncoding);
@$tmpDoc->loadHTML( utf8_decode($value) );
if( $replace_content && $element->hasChildNodes() )
{
foreach( $element->childNodes as $child)
{
$element->removeChild( $child );
}
}
foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $nodes)
{
$children = ( !$nodes->hasChildNodes() ) ? array($nodes) : $nodes->childNodes;
foreach ($children as $child)
{
$element->appendChild( $this->importNode($child, true) );
}
}
}
}
}
function setVariableTag( $name )
{
$this->varTagName = $name;
$this->fetchVars();
}
function getElement( $expression )
{
$list = $this->find( $expression );
return ( $list && is_array($list) && count($list) == 1) ? $list[0] : $list;
}
function set($expression, $value)
{
return $this->inject($expression, $value, true);
}
function setAll($array)
{
foreach ($array as $expression => $value) {
$this->set($expression, $value);
}
}
function add($expression, $value)
{
return $this->inject($expression, $value);
}
function addAll($array)
{
foreach ($array as $expression => $value) {
$this->add($expression, $value);
}
}
function loop($expression, $value)
{
return $this->inject($expression, $value, true, true);
}
function __toString()
{
foreach($this->customVars as $node)
{
$node->parentNode->removeChild( $node );
}
return $this->saveHTML();
}
}
class DOMSelector
{
private $expression;
function __construct($expression)
{
$this->expression = $expression;
}
function toXpath()
{
$xpath = './/'.$this->expression;
$xpath = preg_replace("/\[(?!\w+\()/", '[@', $xpath);
$xpath = preg_replace("/(\#([a-z][a-z0-9_]+))/i", '[@id="$2"]', $xpath);
$xpath = preg_replace("/(\.([a-z][a-z0-9_]+))/i", '[contains(@class,"$2")]', $xpath);
$xpath = preg_replace("/\s(\+|\s)+/", '/following-sibling::', $xpath);
$xpath = preg_replace("/\s(>|\s)*/", '/', $xpath);
$xpath = preg_replace("/\/\[/", '/*[', $xpath);
return $xpath;
}
}
?>