PluginProbe
Page Builder: Pagelayer – Drag and Drop website builder / 2.2.0
Page Builder: Pagelayer – Drag and Drop website builder v2.2.0
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 2.2.0, at lib/pquery/pQuery.php

289 lines 7.3 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 pagelayerQuery\IQuery;
11
12 /**
13 * A jQuery-like object for php.
14 */
15 class pagelayerQuery 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 $value = str_replace('<', '&lt;', $value);
58 $value = str_replace('>', '&gt;', $value);
59 $node->attr($name, $value);
60 }
61 return $this;
62 }
63
64 public function before($content) {
65 foreach ($this->nodes as $node) {
66 $node->before($content);
67 }
68 return $this;
69 }
70
71 public function clear() {
72 foreach ($this->nodes as $node) {
73 $node->clear();
74 }
75 return $this;
76 }
77
78 /**
79 * Get the count of matched elements.
80 *
81 * @return int Returns the count of matched elements.
82 */
83
84 #[\ReturnTypeWillChange]
85 public function count() {
86 return count($this->nodes);
87 }
88
89 /**
90 * Format/beautify a DOM.
91 *
92 * @param pagelayerQuery\DomNode $dom The dom to format.
93 * @param array $options Extra formatting options. See {@link pagelayerQuery\HtmlFormatter::$options}.
94 * @return bool Returns `true` on sucess and `false` on failure.
95 */
96 // public static function format($dom, $options = array()) {
97 // $formatter = new pagelayerQuery\HtmlFormatter($options);
98 // return $formatter->format($dom);
99 // }
100
101 #[\ReturnTypeWillChange]
102 public function getIterator() {
103 return new ArrayIterator($this->nodes);
104 }
105
106 public function hasClass($classname) {
107 foreach ($this->nodes as $node) {
108 if ($node->hasClass($classname))
109 return true;
110 }
111 return false;
112 }
113
114 public function html($value = null) {
115 if (empty($this->nodes) && $value === null)
116 return '';
117
118 foreach ($this->nodes as $node) {
119 if ($value === null)
120 return $node->html();
121 $node->html($value);
122 }
123 return $this;
124 }
125
126 #[\ReturnTypeWillChange]
127 public function offsetExists($offset){
128 return isset($this->nodes[$offset]);
129 }
130
131 #[\ReturnTypeWillChange]
132 public function offsetGet($offset) {
133 return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
134 }
135
136 #[\ReturnTypeWillChange]
137 public function offsetSet($offset, $value) {
138
139 if (is_null($offset) || !isset($this->nodes[$offset])) {
140 throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
141 } else {
142 $this->nodes[$offset]->replaceWith($value);
143 }
144 }
145
146 #[\ReturnTypeWillChange]
147 public function offsetUnset($offset) {
148 if (isset($this->nodes[$offset])) {
149 $this->nodes[$offset]->remove();
150 unset($this->nodes[$offset]);
151 }
152 }
153
154 /**
155 * Query a file or url.
156 *
157 * @param string $path The path to the url.
158 * @param resource $context A context suitable to be passed into {@link file_get_contents}
159 * @return pagelayerQuery\DomNode Returns the root dom node for the html file.
160 */
161 public static function parseFile($path, $context = null) {
162 $html_str = file_get_contents($path, false, $context);
163 return static::parseStr($html_str);
164 }
165
166 /**
167 * Query a string of html.
168 *
169 * @param string $html
170 * @return pagelayerQuery\DomNode Returns the root dom node for the html string.
171 */
172 public static function parseStr($html) {
173 $parser = new pagelayerQuery\Html5Parser($html);
174 return $parser->root;
175 }
176
177 public function prepend($content = null) {
178 foreach ($this->nodes as $node) {
179 $node->prepend($content);
180 }
181 return $this;
182 }
183
184 public function prop($name, $value = null) {
185 if (empty($this->nodes) && $value === null)
186 return '';
187
188 foreach ($this->nodes as $node) {
189 if ($value === null)
190 return $node->prop($name);
191 $node->prop($name, $value);
192 }
193 return $this;
194 }
195
196 public function remove($selector = null) {
197 foreach ($this->nodes as $node) {
198 $node->remove($selector);
199 }
200 if ($selector === null)
201 $this->nodes = array();
202
203 return $this;
204 }
205
206 public function removeAttr($name) {
207 foreach ($this->nodes as $node) {
208 $node->removeAttr($name);
209 }
210 return $this;
211 }
212
213 public function removeClass($classname) {
214 foreach ($this->nodes as $node) {
215 $node->removeClass($classname);
216 }
217 return $this;
218 }
219
220 public function replaceWith($content) {
221 foreach ($this->nodes as &$node) {
222 $node = $node->replaceWith($content);
223 }
224 return $this;
225 }
226
227 public function tagName($value = null) {
228 foreach ($this->nodes as $node) {
229 if ($value === null)
230 return $node->tagName();
231 $node->tagName($value);
232 }
233 return $this;
234 }
235
236 public function text($value = null) {
237 if (empty($this->nodes) && $value === null)
238 return '';
239
240 foreach ($this->nodes as $node) {
241 if ($value === null)
242 return $node->text();
243 $node->text($value);
244 }
245 return $this;
246 }
247
248 public function toggleClass($classname, $switch = null) {
249 foreach ($this->nodes as $node) {
250 $node->toggleClass($classname, $switch);
251 }
252
253 return $this;
254 }
255
256 public function unwrap() {
257 foreach ($this->nodes as $node) {
258 $node->unwrap();
259 }
260 return $this;
261 }
262
263 public function val($value = null) {
264 if (empty($this->nodes) && $value === null)
265 return '';
266
267 foreach ($this->nodes as $node) {
268 if ($value === null)
269 return $node->val();
270 $node->val($value);
271 }
272 return $this;
273 }
274
275 public function wrap($wrapping_element) {
276 foreach ($this->nodes as $node) {
277 $node->wrap($wrapping_element);
278 }
279 return $this;
280 }
281
282 public function wrapInner($wrapping_element) {
283 foreach ($this->nodes as $node) {
284 $node->wrapInner($wrapping_element);
285 }
286 return $this;
287 }
288 }
289