← All changes
|
vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php
+19
-36
3.0.11
→
3.2.90
View file →
| @@ -14,11 +14,8 @@ | ||
| 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 | /** |
| @@ -176,9 +171,9 @@ | ||
| 176 | 171 | if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) { |
| 177 | 172 | $this->xpath = new \DOMXPath($ele->ownerDocument); |
| 178 | 173 | } |
| 179 | 174 | foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) { |
| 180 | - if (!\in_array($nsNode->nodeValue, $this->implicitNamespaces)) { | |
| 175 | + if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) { | |
| 181 | 176 | $this->wr(' ')->wr($nsNode->nodeName)->wr('="')->wr($nsNode->nodeValue)->wr('"'); |
| 182 | 177 | } |
| 183 | 178 | } |
| 184 | 179 | } |
| @@ -196,14 +191,12 @@ | ||
| 196 | 191 | $this->attrs($ele); |
| 197 | 192 | $this->namespaceAttrs($ele); |
| 198 | 193 | if ($this->outputMode == static::IM_IN_HTML) { |
| 199 | 194 | $this->wr('>'); |
| 195 | + } else if ($ele->hasChildNodes()) { | |
| 196 | + $this->wr('>'); | |
| 200 | 197 | } else { |
| 201 | - if ($ele->hasChildNodes()) { | |
| 202 | - $this->wr('>'); | |
| 203 | - } else { | |
| 204 | - $this->wr(' />'); | |
| 205 | - } | |
| 198 | + $this->wr(' />'); | |
| 206 | 199 | } |
| 207 | 200 | } |
| 208 | 201 | protected function attrs($ele) |
| 209 | 202 | { |
| @@ -245,18 +238,18 @@ | ||
| 245 | 238 | } |
| 246 | 239 | if (isset($rule['attNamespace']) && $rule['attNamespace'] !== $attr->namespaceURI) { |
| 247 | 240 | continue; |
| 248 | 241 | } |
| 249 | - if (isset($rule['nodeName']) && !\is_array($rule['nodeName']) && $rule['nodeName'] !== $ele->localName) { | |
| 242 | + if (isset($rule['nodeName']) && !is_array($rule['nodeName']) && $rule['nodeName'] !== $ele->localName) { | |
| 250 | 243 | continue; |
| 251 | 244 | } |
| 252 | - if (isset($rule['nodeName']) && \is_array($rule['nodeName']) && !\in_array($ele->localName, $rule['nodeName'], \true)) { | |
| 245 | + if (isset($rule['nodeName']) && is_array($rule['nodeName']) && !in_array($ele->localName, $rule['nodeName'], \true)) { | |
| 253 | 246 | continue; |
| 254 | 247 | } |
| 255 | - if (isset($rule['attrName']) && !\is_array($rule['attrName']) && $rule['attrName'] !== $attr->localName) { | |
| 248 | + if (isset($rule['attrName']) && !is_array($rule['attrName']) && $rule['attrName'] !== $attr->localName) { | |
| 256 | 249 | continue; |
| 257 | 250 | } |
| 258 | - if (isset($rule['attrName']) && \is_array($rule['attrName']) && !\in_array($attr->localName, $rule['attrName'], \true)) { | |
| 251 | + if (isset($rule['attrName']) && is_array($rule['attrName']) && !in_array($attr->localName, $rule['attrName'], \true)) { | |
| 259 | 252 | continue; |
| 260 | 253 | } |
| 261 | 254 | if (isset($rule['xpath'])) { |
| 262 | 255 | $xp = $this->getXPath($attr); |
| @@ -302,9 +295,9 @@ | ||
| 302 | 295 | * @return $this |
| 303 | 296 | */ |
| 304 | 297 | protected function wr($text) |
| 305 | 298 | { |
| 306 | - \fwrite($this->out, $text); | |
| 299 | + fwrite($this->out, (string) $text); | |
| 307 | 300 | return $this; |
| 308 | 301 | } |
| 309 | 302 | /** |
| 310 | 303 | * Write a new line character. |
| @@ -312,9 +305,9 @@ | ||
| 312 | 305 | * @return $this |
| 313 | 306 | */ |
| 314 | 307 | protected function nl() |
| 315 | 308 | { |
| 316 | - \fwrite($this->out, \PHP_EOL); | |
| 309 | + fwrite($this->out, \PHP_EOL); | |
| 317 | 310 | return $this; |
| 318 | 311 | } |
| 319 | 312 | /** |
| 320 | 313 | * Encode text. |
| @@ -333,14 +326,10 @@ | ||
| 333 | 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. |
| 334 | 327 | * This includes such characters as +.# and many other common ones. By default |
| 335 | 328 | * encoding here will just escape &'<>". |
| 336 | 329 | * |
| 337 | - * Note, PHP 5.4+ has better html5 encoding. | |
| 338 | - * | |
| 339 | - * @todo Use the Entities class in php 5.3 to have html5 entities. | |
| 340 | - * | |
| 341 | 330 | * @param string $text Text to encode. |
| 342 | - * @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. | |
| 343 | 332 | * |
| 344 | 333 | * @return string The encoded text. |
| 345 | 334 | */ |
| 346 | 335 | protected function enc($text, $attribute = \false) |
| @@ -348,15 +337,9 @@ | ||
| 348 | 337 | // Escape the text rather than convert to named character references. |
| 349 | 338 | if (!$this->encode) { |
| 350 | 339 | return $this->escape($text, $attribute); |
| 351 | 340 | } |
| 352 | - // If we are in PHP 5.4+ we can use the native html5 entity functionality to | |
| 353 | - // convert the named character references. | |
| 354 | - if ($this->hasHTML5) { | |
| 355 | - return \htmlentities($text, \ENT_HTML5 | \ENT_SUBSTITUTE | \ENT_QUOTES, 'UTF-8', \false); | |
| 356 | - } else { | |
| 357 | - return \strtr($text, HTML5Entities::$map); | |
| 358 | - } | |
| 341 | + return htmlentities($text, \ENT_HTML5 | \ENT_SUBSTITUTE | \ENT_QUOTES, 'UTF-8', \false); | |
| 359 | 342 | } |
| 360 | 343 | /** |
| 361 | 344 | * Escape test. |
| 362 | 345 | * |
| @@ -383,7 +366,7 @@ | ||
| 383 | 366 | $replace = array('"' => '"', '&' => '&', " " => ' '); |
| 384 | 367 | } else { |
| 385 | 368 | $replace = array('<' => '<', '>' => '>', '&' => '&', " " => ' '); |
| 386 | 369 | } |
| 387 | - return \strtr($text, $replace); | |
| 370 | + return strtr($text, $replace); | |
| 388 | 371 | } |
| 389 | 372 | } |