PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / libs / HTML / Common2.php
matomo / app / libs / HTML Last commit date
QuickForm2 1 year ago Common2.php 2 years ago QuickForm2.php 1 year ago
Common2.php
447 lines
1 <?php
2
3 namespace {
4 /**
5 * HTML_Common2: port of HTML_Common package to PHP5
6 *
7 * PHP version 5
8 *
9 * LICENSE:
10 *
11 * Copyright (c) 2004-2009, Alexey Borzov <avb@php.net>
12 *
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * The names of the authors may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
35 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * @category HTML
40 * @package HTML_Common2
41 * @author Alexey Borzov <avb@php.net>
42 * @license http://opensource.org/licenses/bsd-license.php New BSD License
43 * @version CVS: $Id: Common2.php 295050 2010-02-14 05:01:19Z clockwerx $
44 * @link http://pear.php.net/package/HTML_Common2
45 */
46 /**
47 * Base class for HTML classes
48 *
49 * Implements methods for working with HTML attributes, parsing and generating
50 * attribute strings. Port of HTML_Common class for PHP4 originally written by
51 * Adam Daniel with contributions from numerous other developers.
52 *
53 * @category HTML
54 * @package HTML_Common2
55 * @author Alexey Borzov <avb@php.net>
56 * @version Release: @package_version@
57 */
58 abstract class HTML_Common2
59 {
60 /**
61 * Associative array of attributes
62 * @var array
63 */
64 protected $attributes = array();
65 /**
66 * List of attribites changes to which will be announced via onAttributeChange()
67 * method rather than performed by HTML_Common2 class itself
68 * @var array
69 * @see onAttributeChange()
70 */
71 protected $watchedAttributes = array();
72 /**
73 * Indentation level of the element
74 * @var int
75 */
76 private $_indentLevel = 0;
77 /**
78 * Comment associated with the element
79 * @var string
80 */
81 private $_comment = null;
82 /**
83 * Global options for all elements generated by subclasses of HTML_Common2
84 *
85 * Preset options are
86 * - 'charset': charset parameter used in htmlspecialchars() calls,
87 * defaults to 'ISO-8859-1'
88 * - 'indent': string used to indent HTML elements, defaults to "\11"
89 * - 'linebreak': string used to indicate linebreak, defaults to "\12"
90 *
91 * @var array
92 */
93 private static $_options = array('charset' => 'ISO-8859-1', 'indent' => "\t", 'linebreak' => "\n");
94 /**
95 * Sets global option(s)
96 *
97 * @param string|array Option name or array ('option name' => 'option value')
98 * @param mixed Option value, if first argument is not an array
99 */
100 public static function setOption($nameOrOptions, $value = null)
101 {
102 if (\is_array($nameOrOptions)) {
103 foreach ($nameOrOptions as $k => $v) {
104 self::setOption($k, $v);
105 }
106 } else {
107 $linebreaks = array('win' => "\r\n", 'unix' => "\n", 'mac' => "\r");
108 if ('linebreak' == $nameOrOptions && isset($linebreaks[$value])) {
109 $value = $linebreaks[$value];
110 }
111 self::$_options[$nameOrOptions] = $value;
112 }
113 }
114 /**
115 * Returns global option(s)
116 *
117 * @param string Option name
118 * @return mixed Option value, null if option does not exist,
119 * array of all options if $name is not given
120 */
121 public static function getOption($name = null)
122 {
123 if (null === $name) {
124 return self::$_options;
125 } else {
126 return isset(self::$_options[$name]) ? self::$_options[$name] : null;
127 }
128 }
129 /**
130 * Parses the HTML attributes given as string
131 *
132 * @param string HTML attribute string
133 * @return array An associative aray of attributes
134 */
135 protected static function parseAttributes($attrString)
136 {
137 $attributes = array();
138 if (\preg_match_all("/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" . "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/", $attrString, $regs)) {
139 for ($i = 0; $i < \count($regs[1]); $i++) {
140 $name = \trim($regs[1][$i]);
141 $check = \trim($regs[0][$i]);
142 $value = \trim($regs[7][$i]);
143 if ($name == $check) {
144 $attributes[\strtolower($name)] = \strtolower($name);
145 } else {
146 if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
147 $value = \substr($value, 1, -1);
148 }
149 $attributes[\strtolower($name)] = $value;
150 }
151 }
152 }
153 return $attributes;
154 }
155 /**
156 * Creates a valid attribute array from either a string or an array
157 *
158 * @param mixed Array of attributes or HTML attribute string
159 * @return array An associative aray of attributes
160 */
161 protected static function prepareAttributes($attributes)
162 {
163 $prepared = array();
164 if (\is_string($attributes)) {
165 return self::parseAttributes($attributes);
166 } elseif (\is_array($attributes)) {
167 foreach ($attributes as $key => $value) {
168 if (\is_int($key)) {
169 $key = \strtolower($value);
170 $prepared[$key] = $key;
171 } else {
172 $prepared[\strtolower($key)] = (string) $value;
173 }
174 }
175 }
176 return $prepared;
177 }
178 /**
179 * Removes an attribute from an attribute array
180 *
181 * @param array Attribute array
182 * @param string Name of attribute to remove
183 */
184 protected static function removeAttributeArray(&$attributes, $name)
185 {
186 unset($attributes[\strtolower($name)]);
187 }
188 /**
189 * Creates HTML attribute string from array
190 *
191 * @param array Attribute array
192 * @return string Attribute string
193 */
194 protected static function getAttributesString($attributes)
195 {
196 $str = '';
197 if (\is_array($attributes)) {
198 $charset = self::getOption('charset');
199 foreach ($attributes as $key => $value) {
200 $str .= ' ' . $key . '="' . \htmlspecialchars($value, \ENT_QUOTES, $charset) . '"';
201 }
202 }
203 return $str;
204 }
205 /**
206 * Class constructor, sets default attributes
207 *
208 * @param mixed Array of attribute 'name' => 'value' pairs or HTML attribute string
209 */
210 public function __construct($attributes = null)
211 {
212 $this->mergeAttributes($attributes);
213 }
214 /**
215 * Sets the value of the attribute
216 *
217 * @param string Attribute name
218 * @param string Attribute value (will be set to $name if omitted)
219 * @return HTML_Common2
220 */
221 public function setAttribute($name, $value = null)
222 {
223 $name = \strtolower($name);
224 if (\is_null($value)) {
225 $value = $name;
226 }
227 if (\in_array($name, $this->watchedAttributes)) {
228 $this->onAttributeChange($name, $value);
229 } else {
230 $this->attributes[$name] = (string) $value;
231 }
232 return $this;
233 }
234 /**
235 * Returns the value of an attribute
236 *
237 * @param string Attribute name
238 * @return string Attribute value, null if attribute does not exist
239 */
240 public function getAttribute($name)
241 {
242 $name = \strtolower($name);
243 return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
244 }
245 /**
246 * Sets the attributes
247 *
248 * @param mixed Array of attribute 'name' => 'value' pairs or HTML attribute string
249 * @return HTML_Common2
250 */
251 public function setAttributes($attributes)
252 {
253 $attributes = self::prepareAttributes($attributes);
254 $watched = array();
255 foreach ($this->watchedAttributes as $watchedKey) {
256 if (isset($attributes[$watchedKey])) {
257 $this->setAttribute($watchedKey, $attributes[$watchedKey]);
258 unset($attributes[$watchedKey]);
259 } else {
260 $this->removeAttribute($watchedKey);
261 }
262 if (isset($this->attributes[$watchedKey])) {
263 $watched[$watchedKey] = $this->attributes[$watchedKey];
264 }
265 }
266 $this->attributes = \array_merge($watched, $attributes);
267 return $this;
268 }
269 /**
270 * Returns the attribute array or string
271 *
272 * @param bool Whether to return attributes as string
273 * @return mixed Either an array or string of attributes
274 */
275 public function getAttributes($asString = \false)
276 {
277 if ($asString) {
278 return self::getAttributesString($this->attributes);
279 } else {
280 return $this->attributes;
281 }
282 }
283 /**
284 * Merges the existing attributes with the new ones
285 *
286 * @param mixed Array of attribute 'name' => 'value' pairs or HTML attribute string
287 * @return HTML_Common2
288 */
289 public function mergeAttributes($attributes)
290 {
291 $attributes = self::prepareAttributes($attributes);
292 foreach ($this->watchedAttributes as $watchedKey) {
293 if (isset($attributes[$watchedKey])) {
294 $this->onAttributeChange($watchedKey, $attributes[$watchedKey]);
295 unset($attributes[$watchedKey]);
296 }
297 }
298 $this->attributes = \array_merge($this->attributes, $attributes);
299 return $this;
300 }
301 /**
302 * Removes an attribute
303 *
304 * @param string Name of attribute to remove
305 * @return HTML_Common2
306 */
307 public function removeAttribute($attribute)
308 {
309 if (\in_array(\strtolower($attribute), $this->watchedAttributes)) {
310 $this->onAttributeChange(\strtolower($attribute), null);
311 } else {
312 self::removeAttributeArray($this->attributes, $attribute);
313 }
314 return $this;
315 }
316 /**
317 * Sets the indentation level
318 *
319 * @param int
320 * @return HTML_Common2
321 */
322 public function setIndentLevel($level)
323 {
324 $level = \intval($level);
325 if (0 <= $level) {
326 $this->_indentLevel = $level;
327 }
328 return $this;
329 }
330 /**
331 * Gets the indentation level
332 *
333 * @return int
334 */
335 public function getIndentLevel()
336 {
337 return $this->_indentLevel;
338 }
339 /**
340 * Returns the string to indent the element
341 *
342 * @return string
343 */
344 protected function getIndent()
345 {
346 return \str_repeat(self::getOption('indent'), $this->getIndentLevel());
347 }
348 /**
349 * Sets the comment for the element
350 *
351 * @param string
352 * @return HTML_Common2
353 */
354 public function setComment($comment)
355 {
356 $this->_comment = $comment;
357 return $this;
358 }
359 /**
360 * Returns the comment associated with the element
361 *
362 * @return string
363 */
364 public function getComment()
365 {
366 return $this->_comment;
367 }
368 /**
369 * Checks whether the element has given CSS class
370 *
371 * @param string Class name
372 * @return bool
373 */
374 public function hasClass($class)
375 {
376 $regex = '/(^|\\s)' . \preg_quote($class, '/') . '(\\s|$)/';
377 return (bool) \preg_match($regex, $this->getAttribute('class'));
378 }
379 /**
380 * Adds the given CSS class(es) to the element
381 *
382 * @param string|array Class name, multiple class names separated by
383 * whitespace, array of class names
384 * @return HTML_Common2
385 */
386 public function addClass($class)
387 {
388 if (!\is_array($class)) {
389 $class = \preg_split('/\\s+/', $class, null, \PREG_SPLIT_NO_EMPTY);
390 }
391 $curClass = \preg_split('/\\s+/', $this->getAttribute('class'), null, \PREG_SPLIT_NO_EMPTY);
392 foreach ($class as $c) {
393 if (!\in_array($c, $curClass)) {
394 $curClass[] = $c;
395 }
396 }
397 $this->setAttribute('class', \implode(' ', $curClass));
398 return $this;
399 }
400 /**
401 * Removes the given CSS class(es) from the element
402 *
403 * @param string|array Class name, multiple class names separated by
404 * whitespace, array of class names
405 * @return HTML_Common2
406 */
407 public function removeClass($class)
408 {
409 if (!\is_array($class)) {
410 $class = \preg_split('/\\s+/', $class, null, \PREG_SPLIT_NO_EMPTY);
411 }
412 $curClass = \array_diff(\preg_split('/\\s+/', $this->getAttribute('class'), null, \PREG_SPLIT_NO_EMPTY), $class);
413 if (0 == \count($curClass)) {
414 $this->removeAttribute('class');
415 } else {
416 $this->setAttribute('class', \implode(' ', $curClass));
417 }
418 return $this;
419 }
420 /**
421 * Returns the HTML representation of the element
422 *
423 * This magic method allows using the instances of HTML_Common2 in string
424 * contexts
425 *
426 * @return string
427 */
428 public abstract function __toString();
429 /**
430 * Called if trying to change an attribute with name in $watchedAttributes
431 *
432 * This method is called for each attribute whose name is in the
433 * $watchedAttributes array and which is being changed by setAttribute(),
434 * setAttributes() or mergeAttributes() or removed via removeAttribute().
435 * Note that the operation for the attribute is not carried on after calling
436 * this method, it is the responsibility of this method to change or remove
437 * (or not) the attribute.
438 *
439 * @param string Attribute name
440 * @param string Attribute value, null if attribute is being removed
441 */
442 protected function onAttributeChange($name, $value = null)
443 {
444 }
445 }
446 }
447