1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class Html extends Nette\Object implements \ArrayAccess, \Countable, \IteratorAggregate, IHtmlString
27: {
28:
29: private $name;
30:
31:
32: private $isEmpty;
33:
34:
35: public $attrs = array();
36:
37:
38: protected $children = array();
39:
40:
41: public static $xhtml = FALSE;
42:
43:
44: public static $emptyElements = array(
45: 'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
46: 'source' => 1, 'base' => 1, 'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1,
47: 'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
48: );
49:
50:
51: 52: 53: 54: 55: 56:
57: public static function el($name = NULL, $attrs = NULL)
58: {
59: $el = new static;
60: $parts = explode(' ', $name, 2);
61: $el->setName($parts[0]);
62:
63: if (is_array($attrs)) {
64: $el->attrs = $attrs;
65:
66: } elseif ($attrs !== NULL) {
67: $el->setText($attrs);
68: }
69:
70: if (isset($parts[1])) {
71: foreach (Strings::matchAll($parts[1] . ' ', '#([a-z0-9:-]+)(?:=(["\'])?(.*?)(?(2)\\2|\s))?#i') as $m) {
72: $el->attrs[$m[1]] = isset($m[3]) ? $m[3] : TRUE;
73: }
74: }
75:
76: return $el;
77: }
78:
79:
80: 81: 82: 83: 84: 85: 86:
87: public function setName($name, $isEmpty = NULL)
88: {
89: if ($name !== NULL && !is_string($name)) {
90: throw new Nette\InvalidArgumentException(sprintf('Name must be string or NULL, %s given.', gettype($name)));
91: }
92:
93: $this->name = $name;
94: $this->isEmpty = $isEmpty === NULL ? isset(static::$emptyElements[$name]) : (bool) $isEmpty;
95: return $this;
96: }
97:
98:
99: 100: 101: 102:
103: public function getName()
104: {
105: return $this->name;
106: }
107:
108:
109: 110: 111: 112:
113: public function isEmpty()
114: {
115: return $this->isEmpty;
116: }
117:
118:
119: 120: 121: 122: 123:
124: public function addAttributes(array $attrs)
125: {
126: $this->attrs = array_merge($this->attrs, $attrs);
127: return $this;
128: }
129:
130:
131: 132: 133: 134: 135: 136:
137: public function __set($name, $value)
138: {
139: $this->attrs[$name] = $value;
140: }
141:
142:
143: 144: 145: 146: 147:
148: public function &__get($name)
149: {
150: return $this->attrs[$name];
151: }
152:
153:
154: 155: 156: 157: 158:
159: public function __isset($name)
160: {
161: return isset($this->attrs[$name]);
162: }
163:
164:
165: 166: 167: 168: 169:
170: public function __unset($name)
171: {
172: unset($this->attrs[$name]);
173: }
174:
175:
176: 177: 178: 179: 180: 181:
182: public function __call($m, $args)
183: {
184: $p = substr($m, 0, 3);
185: if ($p === 'get' || $p === 'set' || $p === 'add') {
186: $m = substr($m, 3);
187: $m[0] = $m[0] | "\x20";
188: if ($p === 'get') {
189: return isset($this->attrs[$m]) ? $this->attrs[$m] : NULL;
190:
191: } elseif ($p === 'add') {
192: $args[] = TRUE;
193: }
194: }
195:
196: if (count($args) === 0) {
197:
198: } elseif (count($args) === 1) {
199: $this->attrs[$m] = $args[0];
200:
201: } elseif ((string) $args[0] === '') {
202: $tmp = & $this->attrs[$m];
203:
204: } elseif (!isset($this->attrs[$m]) || is_array($this->attrs[$m])) {
205: $this->attrs[$m][$args[0]] = $args[1];
206:
207: } else {
208: $this->attrs[$m] = array($this->attrs[$m], $args[0] => $args[1]);
209: }
210:
211: return $this;
212: }
213:
214:
215: 216: 217: 218: 219: 220:
221: public function href($path, $query = NULL)
222: {
223: if ($query) {
224: $query = http_build_query($query, NULL, '&');
225: if ($query !== '') {
226: $path .= '?' . $query;
227: }
228: }
229: $this->attrs['href'] = $path;
230: return $this;
231: }
232:
233:
234: 235: 236: 237:
238: public function data($name, $value = NULL)
239: {
240: if (func_num_args() === 1) {
241: $this->attrs['data'] = $name;
242: } else {
243: $this->attrs["data-$name"] = is_bool($value) ? json_encode($value) : $value;
244: }
245: return $this;
246: }
247:
248:
249: 250: 251: 252: 253: 254:
255: public function setHtml($html)
256: {
257: if (is_array($html)) {
258: throw new Nette\InvalidArgumentException(sprintf('Textual content must be a scalar, %s given.', gettype($html)));
259: }
260: $this->removeChildren();
261: $this->children[] = (string) $html;
262: return $this;
263: }
264:
265:
266: 267: 268: 269:
270: public function getHtml()
271: {
272: $s = '';
273: foreach ($this->children as $child) {
274: if (is_object($child)) {
275: $s .= $child->render();
276: } else {
277: $s .= $child;
278: }
279: }
280: return $s;
281: }
282:
283:
284: 285: 286: 287: 288: 289:
290: public function setText($text)
291: {
292: if (!is_array($text) && !$text instanceof self) {
293: $text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
294: }
295: return $this->setHtml($text);
296: }
297:
298:
299: 300: 301: 302:
303: public function getText()
304: {
305: return html_entity_decode(strip_tags($this->getHtml()), ENT_QUOTES, 'UTF-8');
306: }
307:
308:
309: 310: 311: 312: 313:
314: public function add($child)
315: {
316: return $this->insert(NULL, $child);
317: }
318:
319:
320: 321: 322: 323: 324: 325:
326: public function create($name, $attrs = NULL)
327: {
328: $this->insert(NULL, $child = static::el($name, $attrs));
329: return $child;
330: }
331:
332:
333: 334: 335: 336: 337: 338: 339: 340:
341: public function insert($index, $child, $replace = FALSE)
342: {
343: if ($child instanceof Html || is_scalar($child)) {
344: if ($index === NULL) {
345: $this->children[] = $child;
346:
347: } else {
348: array_splice($this->children, (int) $index, $replace ? 1 : 0, array($child));
349: }
350:
351: } else {
352: throw new Nette\InvalidArgumentException(sprintf('Child node must be scalar or Html object, %s given.', is_object($child) ? get_class($child) : gettype($child)));
353: }
354:
355: return $this;
356: }
357:
358:
359: 360: 361: 362: 363: 364:
365: public function offsetSet($index, $child)
366: {
367: $this->insert($index, $child, TRUE);
368: }
369:
370:
371: 372: 373: 374: 375:
376: public function offsetGet($index)
377: {
378: return $this->children[$index];
379: }
380:
381:
382: 383: 384: 385: 386:
387: public function offsetExists($index)
388: {
389: return isset($this->children[$index]);
390: }
391:
392:
393: 394: 395: 396: 397:
398: public function offsetUnset($index)
399: {
400: if (isset($this->children[$index])) {
401: array_splice($this->children, (int) $index, 1);
402: }
403: }
404:
405:
406: 407: 408: 409:
410: public function count()
411: {
412: return count($this->children);
413: }
414:
415:
416: 417: 418: 419:
420: public function removeChildren()
421: {
422: $this->children = array();
423: }
424:
425:
426: 427: 428: 429:
430: public function getIterator()
431: {
432: return new \ArrayIterator($this->children);
433: }
434:
435:
436: 437: 438: 439:
440: public function getChildren()
441: {
442: return $this->children;
443: }
444:
445:
446: 447: 448: 449: 450:
451: public function render($indent = NULL)
452: {
453: $s = $this->startTag();
454:
455: if (!$this->isEmpty) {
456:
457: if ($indent !== NULL) {
458: $indent++;
459: }
460: foreach ($this->children as $child) {
461: if (is_object($child)) {
462: $s .= $child->render($indent);
463: } else {
464: $s .= $child;
465: }
466: }
467:
468:
469: $s .= $this->endTag();
470: }
471:
472: if ($indent !== NULL) {
473: return "\n" . str_repeat("\t", $indent - 1) . $s . "\n" . str_repeat("\t", max(0, $indent - 2));
474: }
475: return $s;
476: }
477:
478:
479: public function __toString()
480: {
481: return $this->render();
482: }
483:
484:
485: 486: 487: 488:
489: public function startTag()
490: {
491: if ($this->name) {
492: return '<' . $this->name . $this->attributes() . (static::$xhtml && $this->isEmpty ? ' />' : '>');
493:
494: } else {
495: return '';
496: }
497: }
498:
499:
500: 501: 502: 503:
504: public function endTag()
505: {
506: return $this->name && !$this->isEmpty ? '</' . $this->name . '>' : '';
507: }
508:
509:
510: 511: 512: 513: 514:
515: public function attributes()
516: {
517: if (!is_array($this->attrs)) {
518: return '';
519: }
520:
521: $s = '';
522: foreach ($this->attrs as $key => $value) {
523: if ($value === NULL || $value === FALSE) {
524: continue;
525:
526: } elseif ($value === TRUE) {
527: if (static::$xhtml) {
528: $s .= ' ' . $key . '="' . $key . '"';
529: } else {
530: $s .= ' ' . $key;
531: }
532: continue;
533:
534: } elseif (is_array($value)) {
535: if ($key === 'data') {
536: foreach ($value as $k => $v) {
537: if ($v !== NULL && $v !== FALSE) {
538: if (is_array($v)) {
539: $v = Json::encode($v);
540: }
541: $q = strpos($v, '"') === FALSE ? '"' : "'";
542: $s .= ' data-' . $k . '='
543: . $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $v)
544: . (strpos($v, '`') !== FALSE && strpbrk($v, ' <>"\'') === FALSE ? ' ' : '')
545: . $q;
546: }
547: }
548: continue;
549:
550: } elseif (strncmp($key, 'data-', 5) === 0) {
551: $value = Json::encode($value);
552:
553: } else {
554: $tmp = NULL;
555: foreach ($value as $k => $v) {
556: if ($v != NULL) {
557:
558: $tmp[] = $v === TRUE ? $k : (is_string($k) ? $k . ':' . $v : $v);
559: }
560: }
561: if ($tmp === NULL) {
562: continue;
563: }
564:
565: $value = implode($key === 'style' || !strncmp($key, 'on', 2) ? ';' : ' ', $tmp);
566: }
567:
568: } elseif (is_float($value)) {
569: $value = rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
570:
571: } else {
572: $value = (string) $value;
573: }
574:
575: $q = strpos($value, '"') === FALSE ? '"' : "'";
576: $s .= ' ' . $key . '='
577: . $q . str_replace(array('&', $q), array('&', $q === '"' ? '"' : '''), $value)
578: . (strpos($value, '`') !== FALSE && strpbrk($value, ' <>"\'') === FALSE ? ' ' : '')
579: . $q;
580: }
581:
582: $s = str_replace('@', '@', $s);
583: return $s;
584: }
585:
586:
587: 588: 589:
590: public function __clone()
591: {
592: foreach ($this->children as $key => $value) {
593: if (is_object($value)) {
594: $this->children[$key] = clone $value;
595: }
596: }
597: }
598:
599: }
600: