← All changes
|
vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php
+11
-26
3.0.0
→
3.2.89
View file →
| @@ -6,19 +6,16 @@ | ||
| 6 | 6 | * |
| 7 | 7 | * These output rules are likely to generate output similar to the document that |
| 8 | 8 | * was parsed. It is not intended to output exactly the document that was parsed. |
| 9 | 9 | */ |
| 10 | -namespace WindPressPackages\Masterminds\HTML5\Serializer; | |
| 10 | +namespace WindPressDeps\Masterminds\HTML5\Serializer; | |
| 11 | 11 | |
| 12 | -use WindPressPackages\Masterminds\HTML5\Elements; | |
| 12 | +use WindPressDeps\Masterminds\HTML5\Elements; | |
| 13 | 13 | /** |
| 14 | 14 | * Generate the output html5 based on element rules. |
| 15 | 15 | */ |
| 16 | 16 | class OutputRules implements RulesInterface |
| 17 | 17 | { |
| 18 | - /** | |
| 19 | - * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0. | |
| 20 | - */ | |
| 21 | 18 | const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml'; |
| 22 | 19 | const NAMESPACE_MATHML = 'http://www.w3.org/1998/Math/MathML'; |
| 23 | 20 | const NAMESPACE_SVG = 'http://www.w3.org/2000/svg'; |
| 24 | 21 | const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink'; |
| @@ -32,14 +29,8 @@ | ||
| 32 | 29 | protected $implicitNamespaces = array(self::NAMESPACE_HTML, self::NAMESPACE_SVG, self::NAMESPACE_MATHML, self::NAMESPACE_XML, self::NAMESPACE_XMLNS); |
| 33 | 30 | const IM_IN_HTML = 1; |
| 34 | 31 | const IM_IN_SVG = 2; |
| 35 | 32 | const IM_IN_MATHML = 3; |
| 36 | - /** | |
| 37 | - * Used as cache to detect if is available ENT_HTML5. | |
| 38 | - * | |
| 39 | - * @var bool | |
| 40 | - */ | |
| 41 | - private $hasHTML5 = \false; | |
| 42 | 33 | protected $traverser; |
| 43 | 34 | protected $encode = \false; |
| 44 | 35 | protected $out; |
| 45 | 36 | protected $outputMode; |
| @@ -64,9 +55,8 @@ | ||
| 64 | 55 | $this->encode = $options['encode_entities']; |
| 65 | 56 | } |
| 66 | 57 | $this->outputMode = static::IM_IN_HTML; |
| 67 | 58 | $this->out = $output; |
| 68 | - $this->hasHTML5 = defined('ENT_HTML5'); | |
| 69 | 59 | } |
| 70 | 60 | public function addRule(array $rule) |
| 71 | 61 | { |
| 72 | 62 | $this->nonBooleanAttributes[] = $rule; |
| @@ -95,8 +85,11 @@ | ||
| 95 | 85 | { |
| 96 | 86 | $this->wr(static::DOCTYPE); |
| 97 | 87 | $this->nl(); |
| 98 | 88 | } |
| 89 | + /** | |
| 90 | + * @param \DOMElement $ele | |
| 91 | + */ | |
| 99 | 92 | public function element($ele) |
| 100 | 93 | { |
| 101 | 94 | $name = $ele->tagName; |
| 102 | 95 | // Per spec: |
| @@ -113,8 +106,10 @@ | ||
| 113 | 106 | } elseif ('math' == $name) { |
| 114 | 107 | $this->outputMode = static::IM_IN_MATHML; |
| 115 | 108 | } |
| 116 | 109 | $this->openTag($ele); |
| 110 | + // The tag is already self-closed (`<svg />` or `<math />`) in `openTag` if there are no child nodes. | |
| 111 | + $handledAsVoidTag = $this->outputMode !== static::IM_IN_HTML && !$ele->hasChildNodes(); | |
| 117 | 112 | if (Elements::isA($name, Elements::TEXT_RAW)) { |
| 118 | 113 | foreach ($ele->childNodes as $child) { |
| 119 | 114 | if ($child instanceof \DOMCharacterData) { |
| 120 | 115 | $this->wr($child->data); |
| @@ -132,9 +127,9 @@ | ||
| 132 | 127 | $this->outputMode = static::IM_IN_HTML; |
| 133 | 128 | } |
| 134 | 129 | } |
| 135 | 130 | // If not unary, add a closing tag. |
| 136 | - if (!Elements::isA($name, Elements::VOID_TAG)) { | |
| 131 | + if (!$handledAsVoidTag && !Elements::isA($name, Elements::VOID_TAG)) { | |
| 137 | 132 | $this->closeTag($ele); |
| 138 | 133 | } |
| 139 | 134 | } |
| 140 | 135 | /** |
| @@ -300,9 +295,9 @@ | ||
| 300 | 295 | * @return $this |
| 301 | 296 | */ |
| 302 | 297 | protected function wr($text) |
| 303 | 298 | { |
| 304 | - fwrite($this->out, $text); | |
| 299 | + fwrite($this->out, (string) $text); | |
| 305 | 300 | return $this; |
| 306 | 301 | } |
| 307 | 302 | /** |
| 308 | 303 | * Write a new line character. |
| @@ -331,14 +326,10 @@ | ||
| 331 | 326 | * @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. |
| 332 | 327 | * This includes such characters as +.# and many other common ones. By default |
| 333 | 328 | * encoding here will just escape &'<>". |
| 334 | 329 | * |
| 335 | - * Note, PHP 5.4+ has better html5 encoding. | |
| 336 | - * | |
| 337 | - * @todo Use the Entities class in php 5.3 to have html5 entities. | |
| 338 | - * | |
| 339 | 330 | * @param string $text Text to encode. |
| 340 | - * @param bool $attribute True if we are encoding an attrubute, false otherwise. | |
| 331 | + * @param bool $attribute True if we are encoding an attribute, false otherwise. | |
| 341 | 332 | * |
| 342 | 333 | * @return string The encoded text. |
| 343 | 334 | */ |
| 344 | 335 | protected function enc($text, $attribute = \false) |
| @@ -346,15 +337,9 @@ | ||
| 346 | 337 | // Escape the text rather than convert to named character references. |
| 347 | 338 | if (!$this->encode) { |
| 348 | 339 | return $this->escape($text, $attribute); |
| 349 | 340 | } |
| 350 | - // If we are in PHP 5.4+ we can use the native html5 entity functionality to | |
| 351 | - // convert the named character references. | |
| 352 | - if ($this->hasHTML5) { | |
| 353 | - return htmlentities($text, \ENT_HTML5 | \ENT_SUBSTITUTE | \ENT_QUOTES, 'UTF-8', \false); | |
| 354 | - } else { | |
| 355 | - return strtr($text, HTML5Entities::$map); | |
| 356 | - } | |
| 341 | + return htmlentities($text, \ENT_HTML5 | \ENT_SUBSTITUTE | \ENT_QUOTES, 'UTF-8', \false); | |
| 357 | 342 | } |
| 358 | 343 | /** |
| 359 | 344 | * Escape test. |
| 360 | 345 | * |