PluginProbe
Page Builder: Pagelayer – Drag and Drop website builder / 0.9.2
Page Builder: Pagelayer – Drag and Drop website builder v0.9.2
2.2.1 2.2.0 2.1.9 2.1.8 2.1.7 2.1.6 2.1.5 2.1.4 2.1.3 trunk 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 1.0.0 1.0.2 1.0.3 1.0.4 1.0.5 All 129 releases
pagelayer / lib / pquery / pQuery.php

pQuery.php in Page Builder: Pagelayer – Drag and Drop website builder 0.9.2, at lib/pquery/pQuery.php

280 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @author Niels A.D.
4 * @author Todd Burry <todd@vanillaforums.com>
5 * @copyright 2010 Niels A.D., 2014 Todd Burry
6 * @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
7 * @package pQuery
8 */
9
10 use pQuery\IQuery;
11
12 /**
13 * A jQuery-like object for php.
14 */
15 class pQuery implements ArrayAccess, IteratorAggregate, IQuery {
16 /// Properties ///
17
18 /**
19 * @var IQuery[]
20 */
21 protected $nodes = array();
22
23 /// Methods ///
24
25 public function __construct($nodes = array()) {
26 $this->nodes = $nodes;
27 }
28
29 public function addClass($classname) {
30 foreach ($this->nodes as $node) {
31 $node->addClass($classname);
32 }
33 return $this;
34 }
35
36 public function after($content) {
37 foreach ($this->nodes as $node) {
38 $node->after($content);
39 }
40 return $this;
41 }
42
43 public function append($content) {
44 foreach ($this->nodes as $node) {
45 $node->append($content);
46 }
47 return $this;
48 }
49
50 public function attr($name, $value = null) {
51 if (empty($this->nodes) && $value === null)
52 return '';
53
54 foreach ($this->nodes as $node) {
55 if ($value === null)
56 return $node->attr($name);
57 $node->attr($name, $value);
58 }
59 return $this;
60 }
61
62 public function before($content) {
63 foreach ($this->nodes as $node) {
64 $node->before($content);
65 }
66 return $this;
67 }
68
69 public function clear() {
70 foreach ($this->nodes as $node) {
71 $node->clear();
72 }
73 return $this;
74 }
75
76 /**
77 * Get the count of matched elements.
78 *
79 * @return int Returns the count of matched elements.
80 */
81 public function count() {
82 return count($this->nodes);
83 }
84
85 /**
86 * Format/beautify a DOM.
87 *
88 * @param pQuery\DomNode $dom The dom to format.
89 * @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}.
90 * @return bool Returns `true` on sucess and `false` on failure.
91 */
92 // public static function format($dom, $options = array()) {
93 // $formatter = new pQuery\HtmlFormatter($options);
94 // return $formatter->format($dom);
95 // }
96
97 public function getIterator() {
98 return new ArrayIterator($this->nodes);
99 }
100
101 public function hasClass($classname) {
102 foreach ($this->nodes as $node) {
103 if ($node->hasClass($classname))
104 return true;
105 }
106 return false;
107 }
108
109 public function html($value = null) {
110 if (empty($this->nodes) && $value === null)
111 return '';
112
113 foreach ($this->nodes as $node) {
114 if ($value === null)
115 return $node->html();
116 $node->html($value);
117 }
118 return $this;
119 }
120
121 public function offsetExists($offset) {
122 return isset($this->nodes[$offset]);
123 }
124
125 public function offsetGet($offset) {
126 return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
127 }
128
129 public function offsetSet($offset, $value) {
130
131 if (is_null($offset) || !isset($this->nodes[$offset])) {
132 throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
133 } else {
134 $this->nodes[$offset]->replaceWith($value);
135 }
136 }
137
138 public function offsetUnset($offset) {
139 if (isset($this->nodes[$offset])) {
140 $this->nodes[$offset]->remove();
141 unset($this->nodes[$offset]);
142 }
143 }
144
145 /**
146 * Query a file or url.
147 *
148 * @param string $path The path to the url.
149 * @param resource $context A context suitable to be passed into {@link file_get_contents}
150 * @return pQuery\DomNode Returns the root dom node for the html file.
151 */
152 public static function parseFile($path, $context = null) {
153 $html_str = file_get_contents($path, false, $context);
154 return static::parseStr($html_str);
155 }
156
157 /**
158 * Query a string of html.
159 *
160 * @param string $html
161 * @return pQuery\DomNode Returns the root dom node for the html string.
162 */
163 public static function parseStr($html) {
164 $parser = new pQuery\Html5Parser($html);
165 return $parser->root;
166 }
167
168 public function prepend($content = null) {
169 foreach ($this->nodes as $node) {
170 $node->prepend($content);
171 }
172 return $this;
173 }
174
175 public function prop($name, $value = null) {
176 if (empty($this->nodes) && $value === null)
177 return '';
178
179 foreach ($this->nodes as $node) {
180 if ($value === null)
181 return $node->prop($name);
182 $node->prop($name, $value);
183 }
184 return $this;
185 }
186
187 public function remove($selector = null) {
188 foreach ($this->nodes as $node) {
189 $node->remove($selector);
190 }
191 if ($selector === null)
192 $this->nodes = array();
193
194 return $this;
195 }
196
197 public function removeAttr($name) {
198 foreach ($this->nodes as $node) {
199 $node->removeAttr($name);
200 }
201 return $this;
202 }
203
204 public function removeClass($classname) {
205 foreach ($this->nodes as $node) {
206 $node->removeClass($classname);
207 }
208 return $this;
209 }
210
211 public function replaceWith($content) {
212 foreach ($this->nodes as &$node) {
213 $node = $node->replaceWith($content);
214 }
215 return $this;
216 }
217
218 public function tagName($value = null) {
219 foreach ($this->nodes as $node) {
220 if ($value === null)
221 return $node->tagName();
222 $node->tagName($value);
223 }
224 return $this;
225 }
226
227 public function text($value = null) {
228 if (empty($this->nodes) && $value === null)
229 return '';
230
231 foreach ($this->nodes as $node) {
232 if ($value === null)
233 return $node->text();
234 $node->text($value);
235 }
236 return $this;
237 }
238
239 public function toggleClass($classname, $switch = null) {
240 foreach ($this->nodes as $node) {
241 $node->toggleClass($classname, $switch);
242 }
243
244 return $this;
245 }
246
247 public function unwrap() {
248 foreach ($this->nodes as $node) {
249 $node->unwrap();
250 }
251 return $this;
252 }
253
254 public function val($value = null) {
255 if (empty($this->nodes) && $value === null)
256 return '';
257
258 foreach ($this->nodes as $node) {
259 if ($value === null)
260 return $node->val();
261 $node->val($value);
262 }
263 return $this;
264 }
265
266 public function wrap($wrapping_element) {
267 foreach ($this->nodes as $node) {
268 $node->wrap($wrapping_element);
269 }
270 return $this;
271 }
272
273 public function wrapInner($wrapping_element) {
274 foreach ($this->nodes as $node) {
275 $node->wrapInner($wrapping_element);
276 }
277 return $this;
278 }
279 }
280