4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Utils;
21
* $el = Html::el('a')->href($link)->setText('Nette');
22
* $el->class = 'myclass';
25
* echo $el->startTag(), $el->endTag();
30
class Html extends Nette\Object implements \ArrayAccess, \Countable, \IteratorAggregate
32
/** @var string element's name */
35
/** @var bool is element empty? */
38
/** @var array element's attributes */
39
public $attrs = array();
41
/** @var array of Html | string nodes */
42
protected $children = array();
44
/** @var bool use XHTML syntax? */
45
public static $xhtml = FALSE;
47
/** @var array empty (void) elements */
48
public static $emptyElements = array('img'=>1,'hr'=>1,'br'=>1,'input'=>1,'meta'=>1,'area'=>1,'embed'=>1,'keygen'=>1,
49
'source'=>1,'base'=>1,'col'=>1,'link'=>1,'param'=>1,'basefont'=>1,'frame'=>1,'isindex'=>1,'wbr'=>1,'command'=>1,'track'=>1);
54
* @param string element name (or NULL)
55
* @param array|string element's attributes (or textual content)
58
public static function el($name = NULL, $attrs = NULL)
61
$parts = explode(' ', $name, 2);
62
$el->setName($parts[0]);
64
if (is_array($attrs)) {
67
} elseif ($attrs !== NULL) {
71
if (isset($parts[1])) {
72
foreach (Strings::matchAll($parts[1] . ' ', '#([a-z0-9:-]+)(?:=(["\'])?(.*?)(?(2)\\2|\s))?#i') as $m) {
73
$el->attrs[$m[1]] = isset($m[3]) ? $m[3] : TRUE;
82
* Changes element's name.
84
* @param bool Is element empty?
86
* @throws Nette\InvalidArgumentException
88
final public function setName($name, $isEmpty = NULL)
90
if ($name !== NULL && !is_string($name)) {
91
throw new Nette\InvalidArgumentException("Name must be string or NULL, " . gettype($name) ." given.");
95
$this->isEmpty = $isEmpty === NULL ? isset(static::$emptyElements[$name]) : (bool) $isEmpty;
101
* Returns element's name.
104
final public function getName()
114
final public function isEmpty()
116
return $this->isEmpty;
121
* Sets multiple attributes.
125
public function addAttributes(array $attrs)
127
$this->attrs = array_merge($this->attrs, $attrs);
133
* Overloaded setter for element's attribute.
134
* @param string HTML attribute name
135
* @param mixed HTML attribute value
138
final public function __set($name, $value)
140
$this->attrs[$name] = $value;
145
* Overloaded getter for element's attribute.
146
* @param string HTML attribute name
147
* @return mixed HTML attribute value
149
final public function &__get($name)
151
return $this->attrs[$name];
156
* Overloaded tester for element's attribute.
157
* @param string HTML attribute name
160
final public function __isset($name)
162
return isset($this->attrs[$name]);
167
* Overloaded unsetter for element's attribute.
168
* @param string HTML attribute name
171
final public function __unset($name)
173
unset($this->attrs[$name]);
178
* Overloaded setter for element's attribute.
179
* @param string HTML attribute name
180
* @param array (string) HTML attribute value or pair?
183
final public function __call($m, $args)
185
$p = substr($m, 0, 3);
186
if ($p === 'get' || $p === 'set' || $p === 'add') {
188
$m[0] = $m[0] | "\x20";
190
return isset($this->attrs[$m]) ? $this->attrs[$m] : NULL;
192
} elseif ($p === 'add') {
197
if (count($args) === 0) { // invalid
199
} elseif (count($args) === 1) { // set
200
$this->attrs[$m] = $args[0];
202
} elseif ((string) $args[0] === '') {
203
$tmp = & $this->attrs[$m]; // appending empty value? -> ignore, but ensure it exists
205
} elseif (!isset($this->attrs[$m]) || is_array($this->attrs[$m])) { // needs array
206
$this->attrs[$m][$args[0]] = $args[1];
209
$this->attrs[$m] = array($this->attrs[$m], $args[0] => $args[1]);
217
* Special setter for element's attribute.
222
final public function href($path, $query = NULL)
225
$query = http_build_query($query, NULL, '&');
227
$path .= '?' . $query;
230
$this->attrs['href'] = $path;
236
* Sets element's HTML content.
239
* @throws Nette\InvalidArgumentException
241
final public function setHtml($html)
243
if (is_array($html)) {
244
throw new Nette\InvalidArgumentException("Textual content must be a scalar, " . gettype($html) ." given.");
246
$this->removeChildren();
247
$this->children[] = (string) $html;
253
* Returns element's HTML content.
256
final public function getHtml()
259
foreach ($this->children as $child) {
260
if (is_object($child)) {
261
$s .= $child->render();
271
* Sets element's textual content.
274
* @throws Nette\InvalidArgumentException
276
final public function setText($text)
278
if (!is_array($text) && !$text instanceof self) {
279
$text = htmlspecialchars((string) $text, ENT_NOQUOTES);
281
return $this->setHtml($text);
286
* Returns element's textual content.
289
final public function getText()
291
return html_entity_decode(strip_tags($this->getHtml()), ENT_QUOTES, 'UTF-8');
296
* Adds new element's child.
297
* @param Html|string child node
300
final public function add($child)
302
return $this->insert(NULL, $child);
307
* Creates and adds a new Html child.
308
* @param string elements's name
309
* @param array|string element's attributes (or textual content)
310
* @return Html created element
312
final public function create($name, $attrs = NULL)
314
$this->insert(NULL, $child = static::el($name, $attrs));
320
* Inserts child node.
327
public function insert($index, $child, $replace = FALSE)
329
if ($child instanceof Html || is_scalar($child)) {
330
if ($index === NULL) { // append
331
$this->children[] = $child;
333
} else { // insert or replace
334
array_splice($this->children, (int) $index, $replace ? 1 : 0, array($child));
338
throw new Nette\InvalidArgumentException("Child node must be scalar or Html object, " . (is_object($child) ? get_class($child) : gettype($child)) ." given.");
346
* Inserts (replaces) child node (\ArrayAccess implementation).
351
final public function offsetSet($index, $child)
353
$this->insert($index, $child, TRUE);
358
* Returns child node (\ArrayAccess implementation).
362
final public function offsetGet($index)
364
return $this->children[$index];
369
* Exists child node? (\ArrayAccess implementation).
373
final public function offsetExists($index)
375
return isset($this->children[$index]);
380
* Removes child node (\ArrayAccess implementation).
384
public function offsetUnset($index)
386
if (isset($this->children[$index])) {
387
array_splice($this->children, (int) $index, 1);
393
* Required by the \Countable interface.
396
final public function count()
398
return count($this->children);
403
* Removed all children.
406
public function removeChildren()
408
$this->children = array();
413
* Iterates over a elements.
414
* @param bool recursive?
415
* @param string class types filter
416
* @return \RecursiveIterator
418
final public function getIterator($deep = FALSE)
421
$deep = $deep > 0 ? \RecursiveIteratorIterator::SELF_FIRST : \RecursiveIteratorIterator::CHILD_FIRST;
422
return new \RecursiveIteratorIterator(new Nette\Iterators\Recursor(new \ArrayIterator($this->children)), $deep);
425
return new Nette\Iterators\Recursor(new \ArrayIterator($this->children));
431
* Returns all of children.
434
final public function getChildren()
436
return $this->children;
441
* Renders element's start tag, content and end tag.
445
final public function render($indent = NULL)
447
$s = $this->startTag();
449
if (!$this->isEmpty) {
451
if ($indent !== NULL) {
454
foreach ($this->children as $child) {
455
if (is_object($child)) {
456
$s .= $child->render($indent);
463
$s .= $this->endTag();
466
if ($indent !== NULL) {
467
return "\n" . str_repeat("\t", $indent - 1) . $s . "\n" . str_repeat("\t", max(0, $indent - 2));
473
final public function __toString()
475
return $this->render();
480
* Returns element's start tag.
483
final public function startTag()
486
return '<' . $this->name . $this->attributes() . (static::$xhtml && $this->isEmpty ? ' />' : '>');
495
* Returns element's end tag.
498
final public function endTag()
500
return $this->name && !$this->isEmpty ? '</' . $this->name . '>' : '';
505
* Returns element's attributes.
508
final public function attributes()
510
if (!is_array($this->attrs)) {
515
foreach ($this->attrs as $key => $value) {
516
if ($value === NULL || $value === FALSE) {
519
} elseif ($value === TRUE) {
520
if (static::$xhtml) {
521
$s .= ' ' . $key . '="' . $key . '"';
527
} elseif (is_array($value)) {
528
if ($key === 'data') {
529
foreach ($value as $k => $v) {
530
if ($v !== NULL && $v !== FALSE) {
531
$q = strpos($v, '"') === FALSE ? '"' : "'";
532
$s .= ' data-' . $k . '=' . $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $v) . $q;
539
foreach ($value as $k => $v) {
540
if ($v != NULL) { // intentionally ==, skip NULLs & empty string
541
// composite 'style' vs. 'others'
542
$tmp[] = $v === TRUE ? $k : (is_string($k) ? $k . ':' . $v : $v);
549
$value = implode($key === 'style' || !strncmp($key, 'on', 2) ? ';' : ' ', $tmp);
552
$value = (string) $value;
555
$q = strpos($value, '"') === FALSE ? '"' : "'";
556
$s .= ' ' . $key . '=' . $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $value) . $q;
559
$s = str_replace('@', '@', $s);
565
* Clones all children too.
567
public function __clone()
569
foreach ($this->children as $key => $value) {
570
if (is_object($value)) {
571
$this->children[$key] = clone $value;