/usr/share/php/Nette/Utils/Html.php is in php-nette 2.4-20160731-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Utils;
use Nette;
/**
* HTML helper.
*
* <code>
* $el = Html::el('a')->href($link)->setText('Nette');
* $el->class = 'myclass';
* echo $el;
*
* echo $el->startTag(), $el->endTag();
* </code>
*/
class Html implements \ArrayAccess, \Countable, \IteratorAggregate, IHtmlString
{
use Nette\SmartObject;
/** @var string element's name */
private $name;
/** @var bool is element empty? */
private $isEmpty;
/** @var array element's attributes */
public $attrs = [];
/** @var array of Html | string nodes */
protected $children = [];
/** @var bool use XHTML syntax? */
public static $xhtml = FALSE;
/** @var array empty (void) elements */
public static $emptyElements = [
'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
'source' => 1, 'base' => 1, 'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1,
'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
];
/**
* Static factory.
* @param string element name (or NULL)
* @param array|string element's attributes or plain text content
* @return self
*/
public static function el($name = NULL, $attrs = NULL)
{
$el = new static;
$parts = explode(' ', (string) $name, 2);
$el->setName($parts[0]);
if (is_array($attrs)) {
$el->attrs = $attrs;
} elseif ($attrs !== NULL) {
$el->setText($attrs);
}
if (isset($parts[1])) {
foreach (Strings::matchAll($parts[1] . ' ', '#([a-z0-9:-]+)(?:=(["\'])?(.*?)(?(2)\\2|\s))?#i') as $m) {
$el->attrs[$m[1]] = isset($m[3]) ? $m[3] : TRUE;
}
}
return $el;
}
/**
* Changes element's name.
* @param string
* @param bool Is element empty?
* @return self
* @throws Nette\InvalidArgumentException
*/
public function setName($name, $isEmpty = NULL)
{
if ($name !== NULL && !is_string($name)) {
throw new Nette\InvalidArgumentException(sprintf('Name must be string or NULL, %s given.', gettype($name)));
}
$this->name = $name;
$this->isEmpty = $isEmpty === NULL ? isset(static::$emptyElements[$name]) : (bool) $isEmpty;
return $this;
}
/**
* Returns element's name.
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Is element empty?
* @return bool
*/
public function isEmpty()
{
return $this->isEmpty;
}
/**
* Sets multiple attributes.
* @param array
* @return self
*/
public function addAttributes(array $attrs)
{
$this->attrs = array_merge($this->attrs, $attrs);
return $this;
}
/**
* Appends value to element's attribute.
* @param string
* @param string|array value to append
* @param string|bool value option
* @return self
*/
public function appendAttribute($name, $value, $option = TRUE)
{
if (is_array($value)) {
$prev = isset($this->attrs[$name]) ? (array) $this->attrs[$name] : [];
$this->attrs[$name] = $value + $prev;
} elseif ((string) $value === '') {
$tmp = & $this->attrs[$name]; // appending empty value? -> ignore, but ensure it exists
} elseif (!isset($this->attrs[$name]) || is_array($this->attrs[$name])) { // needs array
$this->attrs[$name][$value] = $option;
} else {
$this->attrs[$name] = [$this->attrs[$name] => TRUE, $value => $option];
}
return $this;
}
/**
* Sets element's attribute.
* @param string
* @param mixed
* @return self
*/
public function setAttribute($name, $value)
{
$this->attrs[$name] = $value;
return $this;
}
/**
* Returns element's attribute.
* @param string
* @return mixed
*/
public function getAttribute($name)
{
return isset($this->attrs[$name]) ? $this->attrs[$name] : NULL;
}
/**
* Unsets element's attribute.
* @param string
* @return self
*/
public function removeAttribute($name)
{
unset($this->attrs[$name]);
return $this;
}
/**
* Overloaded setter for element's attribute.
* @param string HTML attribute name
* @param mixed HTML attribute value
* @return void
*/
public function __set($name, $value)
{
$this->attrs[$name] = $value;
}
/**
* Overloaded getter for element's attribute.
* @param string HTML attribute name
* @return mixed HTML attribute value
*/
public function &__get($name)
{
return $this->attrs[$name];
}
/**
* Overloaded tester for element's attribute.
* @param string HTML attribute name
* @return bool
*/
public function __isset($name)
{
return isset($this->attrs[$name]);
}
/**
* Overloaded unsetter for element's attribute.
* @param string HTML attribute name
* @return void
*/
public function __unset($name)
{
unset($this->attrs[$name]);
}
/**
* Overloaded setter for element's attribute.
* @param string HTML attribute name
* @param array (string) HTML attribute value or pair?
* @return mixed
*/
public function __call($m, $args)
{
$p = substr($m, 0, 3);
if ($p === 'get' || $p === 'set' || $p === 'add') {
$m = substr($m, 3);
$m[0] = $m[0] | "\x20";
if ($p === 'get') {
return isset($this->attrs[$m]) ? $this->attrs[$m] : NULL;
} elseif ($p === 'add') {
$args[] = TRUE;
}
}
if (count($args) === 0) { // invalid
} elseif (count($args) === 1) { // set
$this->attrs[$m] = $args[0];
} else { // add
$this->appendAttribute($m, $args[0], $args[1]);
}
return $this;
}
/**
* Special setter for element's attribute.
* @param string path
* @param array query
* @return self
*/
public function href($path, $query = NULL)
{
if ($query) {
$query = http_build_query($query, '', '&');
if ($query !== '') {
$path .= '?' . $query;
}
}
$this->attrs['href'] = $path;
return $this;
}
/**
* Setter for data-* attributes. Booleans are converted to 'true' resp. 'false'.
* @return self
*/
public function data($name, $value = NULL)
{
if (func_num_args() === 1) {
$this->attrs['data'] = $name;
} else {
$this->attrs["data-$name"] = is_bool($value) ? json_encode($value) : $value;
}
return $this;
}
/**
* Sets element's HTML content.
* @param string raw HTML string
* @return self
* @throws Nette\InvalidArgumentException
*/
public function setHtml($html)
{
if (is_array($html)) {
throw new Nette\InvalidArgumentException(sprintf('Textual content must be a scalar, %s given.', gettype($html)));
}
$this->removeChildren();
$this->children[] = (string) $html;
return $this;
}
/**
* Returns element's HTML content.
* @return string
*/
public function getHtml()
{
$s = '';
foreach ($this->children as $child) {
if (is_object($child)) {
$s .= $child->render();
} else {
$s .= $child;
}
}
return $s;
}
/**
* Sets element's textual content.
* @param string
* @return self
* @throws Nette\InvalidArgumentException
*/
public function setText($text)
{
if (!is_array($text) && !$text instanceof self) {
$text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
}
return $this->setHtml($text);
}
/**
* Returns element's textual content.
* @return string
*/
public function getText()
{
return html_entity_decode(strip_tags($this->getHtml()), ENT_QUOTES, 'UTF-8');
}
/**
* @deprecated
*/
public function add($child)
{
trigger_error(__METHOD__ . '() is deprecated, use addHtml() or addText() instead.', E_USER_DEPRECATED);
return $this->addHtml($child);
}
/**
* Adds new element's child.
* @param Html|string Html node or raw HTML string
* @return self
*/
public function addHtml($child)
{
return $this->insert(NULL, $child);
}
/**
* Appends plain-text string to element content.
* @param string plain-text string
* @return self
*/
public function addText($text)
{
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
return $this->insert(NULL, $text);
}
/**
* Creates and adds a new Html child.
* @param string elements's name
* @param array|string element's attributes or raw HTML string
* @return self created element
*/
public function create($name, $attrs = NULL)
{
$this->insert(NULL, $child = static::el($name, $attrs));
return $child;
}
/**
* Inserts child node.
* @param int|NULL position or NULL for appending
* @param Html|string Html node or raw HTML string
* @param bool
* @return self
* @throws Nette\InvalidArgumentException
*/
public function insert($index, $child, $replace = FALSE)
{
if ($child instanceof self || is_scalar($child)) {
if ($index === NULL) { // append
$this->children[] = $child;
} else { // insert or replace
array_splice($this->children, (int) $index, $replace ? 1 : 0, [$child]);
}
} else {
throw new Nette\InvalidArgumentException(sprintf('Child node must be scalar or Html object, %s given.', is_object($child) ? get_class($child) : gettype($child)));
}
return $this;
}
/**
* Inserts (replaces) child node (\ArrayAccess implementation).
* @param int|NULL position or NULL for appending
* @param Html|string Html node or raw HTML string
* @return void
*/
public function offsetSet($index, $child)
{
$this->insert($index, $child, TRUE);
}
/**
* Returns child node (\ArrayAccess implementation).
* @param int
* @return self|string
*/
public function offsetGet($index)
{
return $this->children[$index];
}
/**
* Exists child node? (\ArrayAccess implementation).
* @param int
* @return bool
*/
public function offsetExists($index)
{
return isset($this->children[$index]);
}
/**
* Removes child node (\ArrayAccess implementation).
* @param int
* @return void
*/
public function offsetUnset($index)
{
if (isset($this->children[$index])) {
array_splice($this->children, (int) $index, 1);
}
}
/**
* Returns children count.
* @return int
*/
public function count()
{
return count($this->children);
}
/**
* Removes all children.
* @return void
*/
public function removeChildren()
{
$this->children = [];
}
/**
* Iterates over elements.
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->children);
}
/**
* Returns all children.
* @return array
*/
public function getChildren()
{
return $this->children;
}
/**
* Renders element's start tag, content and end tag.
* @param int
* @return string
*/
public function render($indent = NULL)
{
$s = $this->startTag();
if (!$this->isEmpty) {
// add content
if ($indent !== NULL) {
$indent++;
}
foreach ($this->children as $child) {
if (is_object($child)) {
$s .= $child->render($indent);
} else {
$s .= $child;
}
}
// add end tag
$s .= $this->endTag();
}
if ($indent !== NULL) {
return "\n" . str_repeat("\t", $indent - 1) . $s . "\n" . str_repeat("\t", max(0, $indent - 2));
}
return $s;
}
public function __toString()
{
try {
return $this->render();
} catch (\Throwable $e) {
} catch (\Exception $e) {
}
trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
}
/**
* Returns element's start tag.
* @return string
*/
public function startTag()
{
if ($this->name) {
return '<' . $this->name . $this->attributes() . (static::$xhtml && $this->isEmpty ? ' />' : '>');
} else {
return '';
}
}
/**
* Returns element's end tag.
* @return string
*/
public function endTag()
{
return $this->name && !$this->isEmpty ? '</' . $this->name . '>' : '';
}
/**
* Returns element's attributes.
* @return string
* @internal
*/
public function attributes()
{
if (!is_array($this->attrs)) {
return '';
}
$s = '';
$attrs = $this->attrs;
if (isset($attrs['data']) && is_array($attrs['data'])) { // deprecated
trigger_error('Expanded attribute "data" is deprecated.', E_USER_DEPRECATED);
foreach ($attrs['data'] as $key => $value) {
$attrs['data-' . $key] = $value;
}
unset($attrs['data']);
}
foreach ($attrs as $key => $value) {
if ($value === NULL || $value === FALSE) {
continue;
} elseif ($value === TRUE) {
if (static::$xhtml) {
$s .= ' ' . $key . '="' . $key . '"';
} else {
$s .= ' ' . $key;
}
continue;
} elseif (is_array($value)) {
if (strncmp($key, 'data-', 5) === 0) {
$value = Json::encode($value);
} else {
$tmp = NULL;
foreach ($value as $k => $v) {
if ($v != NULL) { // intentionally ==, skip NULLs & empty string
// composite 'style' vs. 'others'
$tmp[] = $v === TRUE ? $k : (is_string($k) ? $k . ':' . $v : $v);
}
}
if ($tmp === NULL) {
continue;
}
$value = implode($key === 'style' || !strncmp($key, 'on', 2) ? ';' : ' ', $tmp);
}
} elseif (is_float($value)) {
$value = rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
} else {
$value = (string) $value;
}
$q = strpos($value, '"') === FALSE ? '"' : "'";
$s .= ' ' . $key . '=' . $q
. str_replace(
['&', $q, '<'],
['&', $q === '"' ? '"' : ''', self::$xhtml ? '<' : '<'],
$value
)
. (strpos($value, '`') !== FALSE && strpbrk($value, ' <>"\'') === FALSE ? ' ' : '')
. $q;
}
$s = str_replace('@', '@', $s);
return $s;
}
/**
* Clones all children too.
*/
public function __clone()
{
foreach ($this->children as $key => $value) {
if (is_object($value)) {
$this->children[$key] = clone $value;
}
}
}
}
|