-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjQuery.php
More file actions
303 lines (282 loc) · 7.96 KB
/
jQuery.php
File metadata and controls
303 lines (282 loc) · 7.96 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/*
Made in the likeness of http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.js
- handles prop() and attr() properly
$dom = new DOMDocument;
$dom->load('doc.html');
$jQuery = new jQuery($dom);
Run utility functions with $jQuery->parseHTML($data, $context, $keep_scripts);
Query the DOM with CSS3 and manipulate the selection: $jQuery('.class-name')->attr('rel', 'external');
Query the DOM with XPATH and manipulate the selection: $jQuery('descendant::select')->val('');
Get the <body> content html: $jQuery('body')->html();
Example plugins:
http://learn.jquery.com/plugins/basic-plugin-creation/
$jQuery->fn->greenify = function($that) use($jQuery){
$that->css( "color", "green" );
};
$jQuery->fn->showLinkLocation = function($that) use($jQuery){
return $that->each(function($i, $el) use($jQuery) {
$jQuery( $el )->append( " (" . $jQuery( $el )->attr( "href" ) . ")" );
});
};
$q->sum = function() use($jQuery){
return array_sum(func_get_args());
};
*/
namespace ThinkHTML\jQuery;
use \stdClass,\Exception;
use \DOMDocument,\DOMXPath,\DOMNodeList,\DOMNode;
use ThinkHTML\jQuery\jQueryObject;
use ThinkHTML\jQuery\SymfonyCSSSelector;
use HTML5_Parser as HTML5_Parser;
// The jQuery object
class jQuery
{
public $dom, $fn, $options=array(), $version = '1.0.0';
public static $instance;
public function __construct(DOMDocument $dom, $options=array())
{
$this->options = array_replace(array(
'data_camel_case'=>true,
'use_css'=>true,
), $options);
$this->fn = new stdClass;
$this->dom = $dom;
$this->error = function($msg){
throw new Exception($msg);
};
$jquery = $this;
$this->parseHTML = function($html, $context=null, $keep_scripts=false, $encoding=null) use($jquery) {
$encoding = $encoding ? $encoding : $jquery->dom->actualEncoding;
$context = $context ? $context : $jquery->dom;
$html = HTML5Parser::setEncoding($html, $encoding);
$dom = HTML5_Parser::parse($html);
/*
TODO: figure out and test
if(!$keep_scripts){
$scripts = $dom->getElementsByTagName('script');
if($scripts && $scripts->length){
$i = $scripts->length;
while($i){
$el = $scripts->item($i);
$el->parentNode->removeChild($el);
$i--;
}
}
}
*/
$children = $dom->getElementsByTagName('body')->item(0)->childNodes;
//$children = HTML5_Parser::parseFragment($html);
$nodes = array();
if($children && $children->length){
for($i=0; $i<$children->length; $i++){
$node = $children->item($i);
$nodes[] = $node;
}
}
return $nodes;
};
$this->merge = function($first, $second) use($jquery){
$len = count($second);
$j = 0;
$i = count($first);
for ( ; $j < $len; $j++ ) {
$first[ $i++ ] = $second[ $j ];
}
return $first;
};
$this->grep = function( $elems, $callback, $invert=false ) use($jquery){
$callbackInverse;
$matches = array();
$i = 0;
$length = count($elems);
$callbackExpect = !$invert;
// Go through the array, only saving the items
// that pass the validator function
for ( ; $i < $length; $i++ ) {
$callbackInverse = !$callback( $elems[ $i ], $i );
if ( $callbackInverse !== $callbackExpect ) {
$matches[] = $elems[ $i ];
}
}
return $matches;
};
$this->filter = function( $expr, $elems, $not=false ) use($jquery) {
$elem = $elems[ 0 ];
if ( $not ) {
$expr = ":not(" . $expr . ")";
}
return count($elems) === 1 && $elem->nodeType === 1 ?
($jquery->findMatchesSelector( $elem, $expr ) ? array( $elem ) : array()) :
($jquery->findMatches( $expr, $jquery->grep( $elems, function( $elem ) {
return $elem->nodeType === 1;
})));
};
}
public function findMatchesSelector($elem, $expr){
$matches = $this->getElementsFromSelector($expr);
return $this->arrayHasNode($elem, $matches);
}
public function findMatches($expr, $elems){
$collection = array();
$matches = $this->getElementsFromSelector($expr);
foreach($matches as $node){
foreach($elems as $elem){
if($node->isSameNode($elem)){
$collection[] = $node;
}
}
}
return $collection;
}
public static function firstSlug($str){
$parts = explode('/', $str);
return array_shift($parts);
}
public static function isXPath($str){
if(strpos($str,'/')===0){
return true;
}
$first = self::firstSlug($str);
$parts = explode('::', $first);
$first = array_shift($parts);
return in_array($first, array(
'body','child',
'ancestor',
'ancestor-or-self',
'attribute','@',
'descendant',
'descendant-or-self',
'following',
'following-sibling',
'namespace',
'parent',
'preceding',
'preceding-sibling',
'self'));
}
public static function toXPath($css3)
{
return SymfonyCSSSelector::toXPath($css3);
}
public function getElementsFromSelector($content, $context=null)
{
$jquery = $this;
$nodes = array();
if(is_string($content) && strpos($content,'<')===false){
$xpath = (!$this->options['use_css'] || self::isXPath($content))? $content : self::toXPath($content);
$domxpath = new DOMXPath($this->dom);
if($context===null){
$nodelist = $domxpath->query($xpath);
}else{
$context = new jQueryObject($jquery, $context);
$context = $context->get(0);
$nodelist = $domxpath->query($xpath, $context);
}
if(!$nodelist){
return array();
}
if(!$nodelist->length){
return array();
}
foreach($nodelist as $node){
$nodes[] = $node;
}
}else{
$nodes = $this->getElementsFromContent($content, $context);
}
return $nodes;
}
public function getElementsFromContent($content){
$jquery = $this;
$nodes = array();
if(is_string($content)){
$nodes = $jquery->parseHTML($content);
}else
if(is_array($content)){
$nodes = $content;
}else
if($content instanceof jQueryObject){
$nodes = $content->stack;
}else
if($content instanceof DOMNodeList){
foreach($content as $node){
$nodes[] = $node;
}
}else
if($content instanceof DOMDocument){
$nodes = array($content);
}else
if($content instanceof DOMNode){
$nodes = array($content);
}
return $nodes;
}
// Implement the identical functionality for filter and not
public function winnow( $elements, $qualifier, $not=false ){
$jquery = $this;
$risSimple = '/^.[^:#\[\.,]*$/';
if ( is_callable( $qualifier ) ) {
return $jquery->grep( $elements, function( $elem, $i ) use($qualifier, $not) {
return call_user_func($qualifier, $elem, $i, $elem ) !== $not;
});
}
if ( $qualifier instanceof DOMNode ) {
return $jquery->grep( $elements, function( $elem ) use($qualifier, $not) {
return $elem->isSameNode($qualifier) !== $not;
});
}
if ( is_string($qualifier) ) {
if ( preg_match( $risSimple, $qualifier ) ) {
return $jquery->filter( $qualifier, $elements, $not );
}
$qualifier = $jquery->filter( $qualifier, $elements );
}
return $jquery->grep( $elements, function( $elem ) use($qualifier, $not, $jquery) {
return ( $jquery->arrayHasNode($elem, $qualifier) ) !== $not;
});
}
public function arrayHasNode($elem, $elems){
foreach($elems as $node){
if($node->isSameNode($elem)){
return true;
}
}
return false;
}
public function __invoke($selector, $context=null){
return new jQueryObject($this, $selector, $context);
}
public function __call($name, $args){
if(isset($this->$name)){
$callback = $this->$name;
if(is_callable($callback)){
if(method_exists($callback, 'bindTo')){
$callback = $callback->bindTo($this);
}
return call_user_func_array($callback, $args);
}
}else{
throw new Exception('Method '.$name.' does not exist in jQuery');
//trigger_error('Method '.$name.' does not exist in jQuery', E_USER_ERROR);
}
}
/*
public static function nodeInnerHTML($node)
{
$html = '';
if($node instanceof DOMNode && $node->childNodes instanceof DOMNodeList)
{
foreach ($node->childNodes as $child)
{
$html .= $child->ownerDocument->saveHTML($child);
}
}
return $html;
}
public static function DOMouterHTML($dom, $node)
{
return $dom->saveHtml($node);
}
*/
}