/usr/share/php/Nette/Utils/Html.php is in php-nette 2.1.0-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 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://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>
*
* @author David Grudl
*/
class Html extends Nette\Object implements \ArrayAccess, \Countable, \IteratorAggregate
{
/** @var string element's name */
private $name;
/** @var bool is element empty? */
private $isEmpty;
/** @var array element's attributes */
public $attrs = array();
/** @var array of Html | string nodes */
protected $children = array();
/** @var bool use XHTML syntax? */
public static $xhtml = FALSE;
/** @var array empty (void) elements */
public static $emptyElements = array('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 textual content)
* @return Html
*/
public static function el($name = NULL, $attrs = NULL)
{
$el = new static;
$parts = explode(' ', $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("Name must be string or NULL, " . gettype($name) ." given.");
}
$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;
}
/**
* 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 self
*/
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];
} elseif ((string) $args[0] === '') {
$tmp = & $this->attrs[$m]; // appending empty value? -> ignore, but ensure it exists
} elseif (!isset($this->attrs[$m]) || is_array($this->attrs[$m])) { // needs array
$this->attrs[$m][$args[0]] = $args[1];
} else {
$this->attrs[$m] = array($this->attrs[$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, NULL, '&');
if ($query !== '') {
$path .= '?' . $query;
}
}
$this->attrs['href'] = $path;
return $this;
}
/**
* Sets element's HTML content.
* @param string
* @return self
* @throws Nette\InvalidArgumentException
*/
public function setHtml($html)
{
if (is_array($html)) {
throw new Nette\InvalidArgumentException("Textual content must be a scalar, " . gettype($html) ." given.");
}
$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);
}
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');
}
/**
* Adds new element's child.
* @param Html|string child node
* @return self
*/
public function add($child)
{
return $this->insert(NULL, $child);
}
/**
* Creates and adds a new Html child.
* @param string elements's name
* @param array|string element's attributes (or textual content)
* @return Html created element
*/
public function create($name, $attrs = NULL)
{
$this->insert(NULL, $child = static::el($name, $attrs));
return $child;
}
/**
* Inserts child node.
* @param int
* @param Html node
* @param bool
* @return self
* @throws \Exception
*/
public function insert($index, $child, $replace = FALSE)
{
if ($child instanceof Html || is_scalar($child)) {
if ($index === NULL) { // append
$this->children[] = $child;
} else { // insert or replace
array_splice($this->children, (int) $index, $replace ? 1 : 0, array($child));
}
} else {
throw new Nette\InvalidArgumentException("Child node must be scalar or Html object, " . (is_object($child) ? get_class($child) : gettype($child)) ." given.");
}
return $this;
}
/**
* Inserts (replaces) child node (\ArrayAccess implementation).
* @param int
* @param Html node
* @return void
*/
public function offsetSet($index, $child)
{
$this->insert($index, $child, TRUE);
}
/**
* Returns child node (\ArrayAccess implementation).
* @param int index
* @return mixed
*/
public function offsetGet($index)
{
return $this->children[$index];
}
/**
* Exists child node? (\ArrayAccess implementation).
* @param int index
* @return bool
*/
public function offsetExists($index)
{
return isset($this->children[$index]);
}
/**
* Removes child node (\ArrayAccess implementation).
* @param int index
* @return void
*/
public function offsetUnset($index)
{
if (isset($this->children[$index])) {
array_splice($this->children, (int) $index, 1);
}
}
/**
* Required by the \Countable interface.
* @return int
*/
public function count()
{
return count($this->children);
}
/**
* Removed all children.
* @return void
*/
public function removeChildren()
{
$this->children = array();
}
/**
* Iterates over a elements.
* @param bool recursive?
* @param string class types filter
* @return \RecursiveIterator
*/
public function getIterator($deep = FALSE)
{
if ($deep) {
$deep = $deep > 0 ? \RecursiveIteratorIterator::SELF_FIRST : \RecursiveIteratorIterator::CHILD_FIRST;
return new \RecursiveIteratorIterator(new Nette\Iterators\Recursor(new \ArrayIterator($this->children)), $deep);
} else {
return new Nette\Iterators\Recursor(new \ArrayIterator($this->children));
}
}
/**
* Returns all of children.
* @return array
*/
public function getChildren()
{
return $this->children;
}
/**
* Renders element's start tag, content and end tag.
* @param int indent
* @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()
{
return $this->render();
}
/**
* 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
*/
public function attributes()
{
if (!is_array($this->attrs)) {
return '';
}
$s = '';
foreach ($this->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 ($key === 'data') {
foreach ($value as $k => $v) {
if ($v !== NULL && $v !== FALSE) {
$q = strpos($v, '"') === FALSE ? '"' : "'";
$s .= ' data-' . $k . '=' . $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $v) . $q;
}
}
continue;
}
$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);
} else {
$value = (string) $value;
}
$q = strpos($value, '"') === FALSE ? '"' : "'";
$s .= ' ' . $key . '=' . $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $value) . $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;
}
}
}
}
|