← All changes
|
vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php
+10
-28
3.20.0
→
3.24.2
View file →
| @@ -15,11 +15,8 @@ | ||
| 15 | 15 | * Generate the output html5 based on element rules. |
| 16 | 16 | */ |
| 17 | 17 | class OutputRules implements RulesInterface |
| 18 | 18 | { |
| 19 | - /** | |
| 20 | - * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0. | |
| 21 | - */ | |
| 22 | 19 | const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml'; |
| 23 | 20 | |
| 24 | 21 | const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML'; |
| 25 | 22 | |
| @@ -49,15 +46,8 @@ | ||
| 49 | 46 | const IM_IN_SVG = 2; |
| 50 | 47 | |
| 51 | 48 | const IM_IN_MATHML = 3; |
| 52 | 49 | |
| 53 | - /** | |
| 54 | - * Used as cache to detect if is available ENT_HTML5. | |
| 55 | - * | |
| 56 | - * @var bool | |
| 57 | - */ | |
| 58 | - private $hasHTML5 = false; | |
| 59 | - | |
| 60 | 50 | protected $traverser; |
| 61 | 51 | |
| 62 | 52 | protected $encode = false; |
| 63 | 53 | |
| @@ -166,9 +156,8 @@ | ||
| 166 | 156 | } |
| 167 | 157 | |
| 168 | 158 | $this->outputMode = static::IM_IN_HTML; |
| 169 | 159 | $this->out = $output; |
| 170 | - $this->hasHTML5 = defined('ENT_HTML5'); | |
| 171 | 160 | } |
| 172 | 161 | |
| 173 | 162 | public function addRule(array $rule) |
| 174 | 163 | { |
| @@ -205,8 +194,11 @@ | ||
| 205 | 194 | $this->wr(static::DOCTYPE); |
| 206 | 195 | $this->nl(); |
| 207 | 196 | } |
| 208 | 197 | |
| 198 | + /** | |
| 199 | + * @param \DOMElement $ele | |
| 200 | + */ | |
| 209 | 201 | public function element($ele) |
| 210 | 202 | { |
| 211 | 203 | $name = $ele->tagName; |
| 212 | 204 | |
| @@ -226,8 +218,11 @@ | ||
| 226 | 218 | $this->outputMode = static::IM_IN_MATHML; |
| 227 | 219 | } |
| 228 | 220 | |
| 229 | 221 | $this->openTag($ele); |
| 222 | + // The tag is already self-closed (`<svg />` or `<math />`) in `openTag` if there are no child nodes. | |
| 223 | + $handledAsVoidTag = $this->outputMode !== static::IM_IN_HTML && !$ele->hasChildNodes(); | |
| 224 | + | |
| 230 | 225 | if (Elements::isA($name, Elements::TEXT_RAW)) { |
| 231 | 226 | foreach ($ele->childNodes as $child) { |
| 232 | 227 | if ($child instanceof \DOMCharacterData) { |
| 233 | 228 | $this->wr($child->data); |
| @@ -247,9 +242,9 @@ | ||
| 247 | 242 | } |
| 248 | 243 | } |
| 249 | 244 | |
| 250 | 245 | // If not unary, add a closing tag. |
| 251 | - if (!Elements::isA($name, Elements::VOID_TAG)) { | |
| 246 | + if (!$handledAsVoidTag && !Elements::isA($name, Elements::VOID_TAG)) { | |
| 252 | 247 | $this->closeTag($ele); |
| 253 | 248 | } |
| 254 | 249 | } |
| 255 | 250 | |
| @@ -447,9 +442,9 @@ | ||
| 447 | 442 | * @return $this |
| 448 | 443 | */ |
| 449 | 444 | protected function wr($text) |
| 450 | 445 | { |
| 451 | - fwrite($this->out, $text); | |
| 446 | + fwrite($this->out, (string) $text); | |
| 452 | 447 | |
| 453 | 448 | return $this; |
| 454 | 449 | } |
| 455 | 450 | |
| @@ -482,14 +477,10 @@ | ||
| 482 | 477 | * @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#named-character-references True encoding will turn all named character references into their entities. |
| 483 | 478 | * This includes such characters as +.# and many other common ones. By default |
| 484 | 479 | * encoding here will just escape &'<>". |
| 485 | 480 | * |
| 486 | - * Note, PHP 5.4+ has better html5 encoding. | |
| 487 | - * | |
| 488 | - * @todo Use the Entities class in php 5.3 to have html5 entities. | |
| 489 | - * | |
| 490 | 481 | * @param string $text Text to encode. |
| 491 | - * @param bool $attribute True if we are encoding an attrubute, false otherwise. | |
| 482 | + * @param bool $attribute True if we are encoding an attribute, false otherwise. | |
| 492 | 483 | * |
| 493 | 484 | * @return string The encoded text. |
| 494 | 485 | */ |
| 495 | 486 | protected function enc($text, $attribute = false) |
| @@ -498,18 +489,9 @@ | ||
| 498 | 489 | if (!$this->encode) { |
| 499 | 490 | return $this->escape($text, $attribute); |
| 500 | 491 | } |
| 501 | 492 | |
| 502 | - // If we are in PHP 5.4+ we can use the native html5 entity functionality to | |
| 503 | - // convert the named character references. | |
| 504 | - | |
| 505 | - if ($this->hasHTML5) { | |
| 506 | - return htmlentities($text, ENT_HTML5 | ENT_SUBSTITUTE | ENT_QUOTES, 'UTF-8', false); | |
| 507 | - } // If a version earlier than 5.4 html5 entities are not entirely handled. | |
| 508 | - // This manually handles them. | |
| 509 | - else { | |
| 510 | - return strtr($text, HTML5Entities::$map); | |
| 511 | - } | |
| 493 | + return htmlentities($text, ENT_HTML5 | ENT_SUBSTITUTE | ENT_QUOTES, 'UTF-8', false); | |
| 512 | 494 | } |
| 513 | 495 | |
| 514 | 496 | /** |
| 515 | 497 | * Escape test. |