Html.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (https://nette.org)
  4. * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  5. */
  6. declare(strict_types=1);
  7. namespace Nette\Utils;
  8. use Nette;
  9. use Nette\HtmlStringable;
  10. use function is_array, is_float, is_object, is_string;
  11. /**
  12. * HTML helper.
  13. *
  14. * @property string|null $accept
  15. * @property string|null $accesskey
  16. * @property string|null $action
  17. * @property string|null $align
  18. * @property string|null $allow
  19. * @property string|null $alt
  20. * @property bool|null $async
  21. * @property string|null $autocapitalize
  22. * @property string|null $autocomplete
  23. * @property bool|null $autofocus
  24. * @property bool|null $autoplay
  25. * @property string|null $charset
  26. * @property bool|null $checked
  27. * @property string|null $cite
  28. * @property string|null $class
  29. * @property int|null $cols
  30. * @property int|null $colspan
  31. * @property string|null $content
  32. * @property bool|null $contenteditable
  33. * @property bool|null $controls
  34. * @property string|null $coords
  35. * @property string|null $crossorigin
  36. * @property string|null $data
  37. * @property string|null $datetime
  38. * @property string|null $decoding
  39. * @property bool|null $default
  40. * @property bool|null $defer
  41. * @property string|null $dir
  42. * @property string|null $dirname
  43. * @property bool|null $disabled
  44. * @property bool|null $download
  45. * @property string|null $draggable
  46. * @property string|null $dropzone
  47. * @property string|null $enctype
  48. * @property string|null $for
  49. * @property string|null $form
  50. * @property string|null $formaction
  51. * @property string|null $formenctype
  52. * @property string|null $formmethod
  53. * @property bool|null $formnovalidate
  54. * @property string|null $formtarget
  55. * @property string|null $headers
  56. * @property int|null $height
  57. * @property bool|null $hidden
  58. * @property float|null $high
  59. * @property string|null $href
  60. * @property string|null $hreflang
  61. * @property string|null $id
  62. * @property string|null $integrity
  63. * @property string|null $inputmode
  64. * @property bool|null $ismap
  65. * @property string|null $itemprop
  66. * @property string|null $kind
  67. * @property string|null $label
  68. * @property string|null $lang
  69. * @property string|null $list
  70. * @property bool|null $loop
  71. * @property float|null $low
  72. * @property float|null $max
  73. * @property int|null $maxlength
  74. * @property int|null $minlength
  75. * @property string|null $media
  76. * @property string|null $method
  77. * @property float|null $min
  78. * @property bool|null $multiple
  79. * @property bool|null $muted
  80. * @property string|null $name
  81. * @property bool|null $novalidate
  82. * @property bool|null $open
  83. * @property float|null $optimum
  84. * @property string|null $pattern
  85. * @property string|null $ping
  86. * @property string|null $placeholder
  87. * @property string|null $poster
  88. * @property string|null $preload
  89. * @property string|null $radiogroup
  90. * @property bool|null $readonly
  91. * @property string|null $rel
  92. * @property bool|null $required
  93. * @property bool|null $reversed
  94. * @property int|null $rows
  95. * @property int|null $rowspan
  96. * @property string|null $sandbox
  97. * @property string|null $scope
  98. * @property bool|null $selected
  99. * @property string|null $shape
  100. * @property int|null $size
  101. * @property string|null $sizes
  102. * @property string|null $slot
  103. * @property int|null $span
  104. * @property string|null $spellcheck
  105. * @property string|null $src
  106. * @property string|null $srcdoc
  107. * @property string|null $srclang
  108. * @property string|null $srcset
  109. * @property int|null $start
  110. * @property float|null $step
  111. * @property string|null $style
  112. * @property int|null $tabindex
  113. * @property string|null $target
  114. * @property string|null $title
  115. * @property string|null $translate
  116. * @property string|null $type
  117. * @property string|null $usemap
  118. * @property string|null $value
  119. * @property int|null $width
  120. * @property string|null $wrap
  121. *
  122. * @method self accept(?string $val)
  123. * @method self accesskey(?string $val, bool $state = null)
  124. * @method self action(?string $val)
  125. * @method self align(?string $val)
  126. * @method self allow(?string $val, bool $state = null)
  127. * @method self alt(?string $val)
  128. * @method self async(?bool $val)
  129. * @method self autocapitalize(?string $val)
  130. * @method self autocomplete(?string $val)
  131. * @method self autofocus(?bool $val)
  132. * @method self autoplay(?bool $val)
  133. * @method self charset(?string $val)
  134. * @method self checked(?bool $val)
  135. * @method self cite(?string $val)
  136. * @method self class(?string $val, bool $state = null)
  137. * @method self cols(?int $val)
  138. * @method self colspan(?int $val)
  139. * @method self content(?string $val)
  140. * @method self contenteditable(?bool $val)
  141. * @method self controls(?bool $val)
  142. * @method self coords(?string $val)
  143. * @method self crossorigin(?string $val)
  144. * @method self datetime(?string $val)
  145. * @method self decoding(?string $val)
  146. * @method self default(?bool $val)
  147. * @method self defer(?bool $val)
  148. * @method self dir(?string $val)
  149. * @method self dirname(?string $val)
  150. * @method self disabled(?bool $val)
  151. * @method self download(?bool $val)
  152. * @method self draggable(?string $val)
  153. * @method self dropzone(?string $val)
  154. * @method self enctype(?string $val)
  155. * @method self for(?string $val)
  156. * @method self form(?string $val)
  157. * @method self formaction(?string $val)
  158. * @method self formenctype(?string $val)
  159. * @method self formmethod(?string $val)
  160. * @method self formnovalidate(?bool $val)
  161. * @method self formtarget(?string $val)
  162. * @method self headers(?string $val, bool $state = null)
  163. * @method self height(?int $val)
  164. * @method self hidden(?bool $val)
  165. * @method self high(?float $val)
  166. * @method self hreflang(?string $val)
  167. * @method self id(?string $val)
  168. * @method self integrity(?string $val)
  169. * @method self inputmode(?string $val)
  170. * @method self ismap(?bool $val)
  171. * @method self itemprop(?string $val)
  172. * @method self kind(?string $val)
  173. * @method self label(?string $val)
  174. * @method self lang(?string $val)
  175. * @method self list(?string $val)
  176. * @method self loop(?bool $val)
  177. * @method self low(?float $val)
  178. * @method self max(?float $val)
  179. * @method self maxlength(?int $val)
  180. * @method self minlength(?int $val)
  181. * @method self media(?string $val)
  182. * @method self method(?string $val)
  183. * @method self min(?float $val)
  184. * @method self multiple(?bool $val)
  185. * @method self muted(?bool $val)
  186. * @method self name(?string $val)
  187. * @method self novalidate(?bool $val)
  188. * @method self open(?bool $val)
  189. * @method self optimum(?float $val)
  190. * @method self pattern(?string $val)
  191. * @method self ping(?string $val, bool $state = null)
  192. * @method self placeholder(?string $val)
  193. * @method self poster(?string $val)
  194. * @method self preload(?string $val)
  195. * @method self radiogroup(?string $val)
  196. * @method self readonly(?bool $val)
  197. * @method self rel(?string $val)
  198. * @method self required(?bool $val)
  199. * @method self reversed(?bool $val)
  200. * @method self rows(?int $val)
  201. * @method self rowspan(?int $val)
  202. * @method self sandbox(?string $val, bool $state = null)
  203. * @method self scope(?string $val)
  204. * @method self selected(?bool $val)
  205. * @method self shape(?string $val)
  206. * @method self size(?int $val)
  207. * @method self sizes(?string $val)
  208. * @method self slot(?string $val)
  209. * @method self span(?int $val)
  210. * @method self spellcheck(?string $val)
  211. * @method self src(?string $val)
  212. * @method self srcdoc(?string $val)
  213. * @method self srclang(?string $val)
  214. * @method self srcset(?string $val)
  215. * @method self start(?int $val)
  216. * @method self step(?float $val)
  217. * @method self style(?string $property, string $val = null)
  218. * @method self tabindex(?int $val)
  219. * @method self target(?string $val)
  220. * @method self title(?string $val)
  221. * @method self translate(?string $val)
  222. * @method self type(?string $val)
  223. * @method self usemap(?string $val)
  224. * @method self value(?string $val)
  225. * @method self width(?int $val)
  226. * @method self wrap(?string $val)
  227. */
  228. class Html implements \ArrayAccess, \Countable, \IteratorAggregate, HtmlStringable
  229. {
  230. use Nette\SmartObject;
  231. /** @var array<string, mixed> element's attributes */
  232. public $attrs = [];
  233. /** @var bool use XHTML syntax? */
  234. public static $xhtml = false;
  235. /** @var array<string, int> void elements */
  236. public static $emptyElements = [
  237. 'img' => 1, 'hr' => 1, 'br' => 1, 'input' => 1, 'meta' => 1, 'area' => 1, 'embed' => 1, 'keygen' => 1,
  238. 'source' => 1, 'base' => 1, 'col' => 1, 'link' => 1, 'param' => 1, 'basefont' => 1, 'frame' => 1,
  239. 'isindex' => 1, 'wbr' => 1, 'command' => 1, 'track' => 1,
  240. ];
  241. /** @var array<int, HtmlStringable|string> nodes */
  242. protected $children = [];
  243. /** @var string element's name */
  244. private $name;
  245. /** @var bool is element empty? */
  246. private $isEmpty;
  247. /**
  248. * Constructs new HTML element.
  249. * @param array|string $attrs element's attributes or plain text content
  250. * @return static
  251. */
  252. public static function el(?string $name = null, $attrs = null)
  253. {
  254. $el = new static;
  255. $parts = explode(' ', (string) $name, 2);
  256. $el->setName($parts[0]);
  257. if (is_array($attrs)) {
  258. $el->attrs = $attrs;
  259. } elseif ($attrs !== null) {
  260. $el->setText($attrs);
  261. }
  262. if (isset($parts[1])) {
  263. foreach (Strings::matchAll($parts[1] . ' ', '#([a-z0-9:-]+)(?:=(["\'])?(.*?)(?(2)\2|\s))?#i') as $m) {
  264. $el->attrs[$m[1]] = $m[3] ?? true;
  265. }
  266. }
  267. return $el;
  268. }
  269. /**
  270. * Returns an object representing HTML text.
  271. */
  272. public static function fromHtml(string $html): self
  273. {
  274. return (new static)->setHtml($html);
  275. }
  276. /**
  277. * Returns an object representing plain text.
  278. */
  279. public static function fromText(string $text): self
  280. {
  281. return (new static)->setText($text);
  282. }
  283. /**
  284. * Converts to HTML.
  285. */
  286. final public function toHtml(): string
  287. {
  288. return $this->render();
  289. }
  290. /**
  291. * Converts to plain text.
  292. */
  293. final public function toText(): string
  294. {
  295. return $this->getText();
  296. }
  297. /**
  298. * Converts given HTML code to plain text.
  299. */
  300. public static function htmlToText(string $html): string
  301. {
  302. return html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8');
  303. }
  304. /**
  305. * Changes element's name.
  306. * @return static
  307. */
  308. final public function setName(string $name, ?bool $isEmpty = null)
  309. {
  310. $this->name = $name;
  311. $this->isEmpty = $isEmpty ?? isset(static::$emptyElements[$name]);
  312. return $this;
  313. }
  314. /**
  315. * Returns element's name.
  316. */
  317. final public function getName(): string
  318. {
  319. return $this->name;
  320. }
  321. /**
  322. * Is element empty?
  323. */
  324. final public function isEmpty(): bool
  325. {
  326. return $this->isEmpty;
  327. }
  328. /**
  329. * Sets multiple attributes.
  330. * @return static
  331. */
  332. public function addAttributes(array $attrs)
  333. {
  334. $this->attrs = array_merge($this->attrs, $attrs);
  335. return $this;
  336. }
  337. /**
  338. * Appends value to element's attribute.
  339. * @param mixed $value
  340. * @param mixed $option
  341. * @return static
  342. */
  343. public function appendAttribute(string $name, $value, $option = true)
  344. {
  345. if (is_array($value)) {
  346. $prev = isset($this->attrs[$name]) ? (array) $this->attrs[$name] : [];
  347. $this->attrs[$name] = $value + $prev;
  348. } elseif ((string) $value === '') {
  349. $tmp = &$this->attrs[$name]; // appending empty value? -> ignore, but ensure it exists
  350. } elseif (!isset($this->attrs[$name]) || is_array($this->attrs[$name])) { // needs array
  351. $this->attrs[$name][$value] = $option;
  352. } else {
  353. $this->attrs[$name] = [$this->attrs[$name] => true, $value => $option];
  354. }
  355. return $this;
  356. }
  357. /**
  358. * Sets element's attribute.
  359. * @param mixed $value
  360. * @return static
  361. */
  362. public function setAttribute(string $name, $value)
  363. {
  364. $this->attrs[$name] = $value;
  365. return $this;
  366. }
  367. /**
  368. * Returns element's attribute.
  369. * @return mixed
  370. */
  371. public function getAttribute(string $name)
  372. {
  373. return $this->attrs[$name] ?? null;
  374. }
  375. /**
  376. * Unsets element's attribute.
  377. * @return static
  378. */
  379. public function removeAttribute(string $name)
  380. {
  381. unset($this->attrs[$name]);
  382. return $this;
  383. }
  384. /**
  385. * Unsets element's attributes.
  386. * @return static
  387. */
  388. public function removeAttributes(array $attributes)
  389. {
  390. foreach ($attributes as $name) {
  391. unset($this->attrs[$name]);
  392. }
  393. return $this;
  394. }
  395. /**
  396. * Overloaded setter for element's attribute.
  397. * @param mixed $value
  398. */
  399. final public function __set(string $name, $value): void
  400. {
  401. $this->attrs[$name] = $value;
  402. }
  403. /**
  404. * Overloaded getter for element's attribute.
  405. * @return mixed
  406. */
  407. final public function &__get(string $name)
  408. {
  409. return $this->attrs[$name];
  410. }
  411. /**
  412. * Overloaded tester for element's attribute.
  413. */
  414. final public function __isset(string $name): bool
  415. {
  416. return isset($this->attrs[$name]);
  417. }
  418. /**
  419. * Overloaded unsetter for element's attribute.
  420. */
  421. final public function __unset(string $name): void
  422. {
  423. unset($this->attrs[$name]);
  424. }
  425. /**
  426. * Overloaded setter for element's attribute.
  427. * @return mixed
  428. */
  429. final public function __call(string $m, array $args)
  430. {
  431. $p = substr($m, 0, 3);
  432. if ($p === 'get' || $p === 'set' || $p === 'add') {
  433. $m = substr($m, 3);
  434. $m[0] = $m[0] | "\x20";
  435. if ($p === 'get') {
  436. return $this->attrs[$m] ?? null;
  437. } elseif ($p === 'add') {
  438. $args[] = true;
  439. }
  440. }
  441. if (count($args) === 0) { // invalid
  442. } elseif (count($args) === 1) { // set
  443. $this->attrs[$m] = $args[0];
  444. } else { // add
  445. $this->appendAttribute($m, $args[0], $args[1]);
  446. }
  447. return $this;
  448. }
  449. /**
  450. * Special setter for element's attribute.
  451. * @return static
  452. */
  453. final public function href(string $path, ?array $query = null)
  454. {
  455. if ($query) {
  456. $query = http_build_query($query, '', '&');
  457. if ($query !== '') {
  458. $path .= '?' . $query;
  459. }
  460. }
  461. $this->attrs['href'] = $path;
  462. return $this;
  463. }
  464. /**
  465. * Setter for data-* attributes. Booleans are converted to 'true' resp. 'false'.
  466. * @param mixed $value
  467. * @return static
  468. */
  469. public function data(string $name, $value = null)
  470. {
  471. if (func_num_args() === 1) {
  472. $this->attrs['data'] = $name;
  473. } else {
  474. $this->attrs["data-$name"] = is_bool($value)
  475. ? json_encode($value)
  476. : $value;
  477. }
  478. return $this;
  479. }
  480. /**
  481. * Sets element's HTML content.
  482. * @param HtmlStringable|string $html
  483. * @return static
  484. */
  485. final public function setHtml($html)
  486. {
  487. $this->children = [(string) $html];
  488. return $this;
  489. }
  490. /**
  491. * Returns element's HTML content.
  492. */
  493. final public function getHtml(): string
  494. {
  495. return implode('', $this->children);
  496. }
  497. /**
  498. * Sets element's textual content.
  499. * @param HtmlStringable|string|int|float $text
  500. * @return static
  501. */
  502. final public function setText($text)
  503. {
  504. if (!$text instanceof HtmlStringable) {
  505. $text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
  506. }
  507. $this->children = [(string) $text];
  508. return $this;
  509. }
  510. /**
  511. * Returns element's textual content.
  512. */
  513. final public function getText(): string
  514. {
  515. return self::htmlToText($this->getHtml());
  516. }
  517. /**
  518. * Adds new element's child.
  519. * @param HtmlStringable|string $child Html node or raw HTML string
  520. * @return static
  521. */
  522. final public function addHtml($child)
  523. {
  524. return $this->insert(null, $child);
  525. }
  526. /**
  527. * Appends plain-text string to element content.
  528. * @param HtmlStringable|string|int|float $text
  529. * @return static
  530. */
  531. public function addText($text)
  532. {
  533. if (!$text instanceof HtmlStringable) {
  534. $text = htmlspecialchars((string) $text, ENT_NOQUOTES, 'UTF-8');
  535. }
  536. return $this->insert(null, $text);
  537. }
  538. /**
  539. * Creates and adds a new Html child.
  540. * @param array|string $attrs element's attributes or raw HTML string
  541. * @return static created element
  542. */
  543. final public function create(string $name, $attrs = null)
  544. {
  545. $this->insert(null, $child = static::el($name, $attrs));
  546. return $child;
  547. }
  548. /**
  549. * Inserts child node.
  550. * @param HtmlStringable|string $child Html node or raw HTML string
  551. * @return static
  552. */
  553. public function insert(?int $index, $child, bool $replace = false)
  554. {
  555. $child = $child instanceof self ? $child : (string) $child;
  556. if ($index === null) { // append
  557. $this->children[] = $child;
  558. } else { // insert or replace
  559. array_splice($this->children, $index, $replace ? 1 : 0, [$child]);
  560. }
  561. return $this;
  562. }
  563. /**
  564. * Inserts (replaces) child node (\ArrayAccess implementation).
  565. * @param int|null $index position or null for appending
  566. * @param Html|string $child Html node or raw HTML string
  567. */
  568. final public function offsetSet($index, $child): void
  569. {
  570. $this->insert($index, $child, true);
  571. }
  572. /**
  573. * Returns child node (\ArrayAccess implementation).
  574. * @param int $index
  575. * @return HtmlStringable|string
  576. */
  577. #[\ReturnTypeWillChange]
  578. final public function offsetGet($index)
  579. {
  580. return $this->children[$index];
  581. }
  582. /**
  583. * Exists child node? (\ArrayAccess implementation).
  584. * @param int $index
  585. */
  586. final public function offsetExists($index): bool
  587. {
  588. return isset($this->children[$index]);
  589. }
  590. /**
  591. * Removes child node (\ArrayAccess implementation).
  592. * @param int $index
  593. */
  594. public function offsetUnset($index): void
  595. {
  596. if (isset($this->children[$index])) {
  597. array_splice($this->children, $index, 1);
  598. }
  599. }
  600. /**
  601. * Returns children count.
  602. */
  603. final public function count(): int
  604. {
  605. return count($this->children);
  606. }
  607. /**
  608. * Removes all children.
  609. */
  610. public function removeChildren(): void
  611. {
  612. $this->children = [];
  613. }
  614. /**
  615. * Iterates over elements.
  616. * @return \ArrayIterator<int, HtmlStringable|string>
  617. */
  618. final public function getIterator(): \ArrayIterator
  619. {
  620. return new \ArrayIterator($this->children);
  621. }
  622. /**
  623. * Returns all children.
  624. */
  625. final public function getChildren(): array
  626. {
  627. return $this->children;
  628. }
  629. /**
  630. * Renders element's start tag, content and end tag.
  631. */
  632. final public function render(?int $indent = null): string
  633. {
  634. $s = $this->startTag();
  635. if (!$this->isEmpty) {
  636. // add content
  637. if ($indent !== null) {
  638. $indent++;
  639. }
  640. foreach ($this->children as $child) {
  641. if ($child instanceof self) {
  642. $s .= $child->render($indent);
  643. } else {
  644. $s .= $child;
  645. }
  646. }
  647. // add end tag
  648. $s .= $this->endTag();
  649. }
  650. if ($indent !== null) {
  651. return "\n" . str_repeat("\t", $indent - 1) . $s . "\n" . str_repeat("\t", max(0, $indent - 2));
  652. }
  653. return $s;
  654. }
  655. final public function __toString(): string
  656. {
  657. try {
  658. return $this->render();
  659. } catch (\Throwable $e) {
  660. if (PHP_VERSION_ID >= 70400) {
  661. throw $e;
  662. }
  663. trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
  664. return '';
  665. }
  666. }
  667. /**
  668. * Returns element's start tag.
  669. */
  670. final public function startTag(): string
  671. {
  672. return $this->name
  673. ? '<' . $this->name . $this->attributes() . (static::$xhtml && $this->isEmpty ? ' />' : '>')
  674. : '';
  675. }
  676. /**
  677. * Returns element's end tag.
  678. */
  679. final public function endTag(): string
  680. {
  681. return $this->name && !$this->isEmpty ? '</' . $this->name . '>' : '';
  682. }
  683. /**
  684. * Returns element's attributes.
  685. * @internal
  686. */
  687. final public function attributes(): string
  688. {
  689. if (!is_array($this->attrs)) {
  690. return '';
  691. }
  692. $s = '';
  693. $attrs = $this->attrs;
  694. foreach ($attrs as $key => $value) {
  695. if ($value === null || $value === false) {
  696. continue;
  697. } elseif ($value === true) {
  698. if (static::$xhtml) {
  699. $s .= ' ' . $key . '="' . $key . '"';
  700. } else {
  701. $s .= ' ' . $key;
  702. }
  703. continue;
  704. } elseif (is_array($value)) {
  705. if (strncmp($key, 'data-', 5) === 0) {
  706. $value = Json::encode($value);
  707. } else {
  708. $tmp = null;
  709. foreach ($value as $k => $v) {
  710. if ($v != null) { // intentionally ==, skip nulls & empty string
  711. // composite 'style' vs. 'others'
  712. $tmp[] = $v === true
  713. ? $k
  714. : (is_string($k) ? $k . ':' . $v : $v);
  715. }
  716. }
  717. if ($tmp === null) {
  718. continue;
  719. }
  720. $value = implode($key === 'style' || !strncmp($key, 'on', 2) ? ';' : ' ', $tmp);
  721. }
  722. } elseif (is_float($value)) {
  723. $value = rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
  724. } else {
  725. $value = (string) $value;
  726. }
  727. $q = strpos($value, '"') === false ? '"' : "'";
  728. $s .= ' ' . $key . '=' . $q
  729. . str_replace(
  730. ['&', $q, '<'],
  731. ['&amp;', $q === '"' ? '&quot;' : '&#39;', self::$xhtml ? '&lt;' : '<'],
  732. $value
  733. )
  734. . (strpos($value, '`') !== false && strpbrk($value, ' <>"\'') === false ? ' ' : '')
  735. . $q;
  736. }
  737. $s = str_replace('@', '&#64;', $s);
  738. return $s;
  739. }
  740. /**
  741. * Clones all children too.
  742. */
  743. public function __clone()
  744. {
  745. foreach ($this->children as $key => $value) {
  746. if (is_object($value)) {
  747. $this->children[$key] = clone $value;
  748. }
  749. }
  750. }
  751. }