diff vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
line wrap: on
line diff
--- a/vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php	Tue Jul 10 15:07:59 2018 +0100
+++ b/vendor/masterminds/html5/src/HTML5/Serializer/OutputRules.php	Thu Feb 28 13:21:36 2019 +0000
@@ -6,6 +6,7 @@
  * These output rules are likely to generate output similar to the document that
  * was parsed. It is not intended to output exactly the document that was parsed.
  */
+
 namespace Masterminds\HTML5\Serializer;
 
 use Masterminds\HTML5\Elements;
@@ -13,10 +14,10 @@
 /**
  * Generate the output html5 based on element rules.
  */
-class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface
+class OutputRules implements RulesInterface
 {
     /**
-     * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0
+     * Defined in http://www.w3.org/TR/html51/infrastructure.html#html-namespace-0.
      */
     const NAMESPACE_HTML = 'http://www.w3.org/1999/xhtml';
 
@@ -31,7 +32,7 @@
     const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/';
 
     /**
-     * Holds the HTML5 element names that causes a namespace switch
+     * Holds the HTML5 element names that causes a namespace switch.
      *
      * @var array
      */
@@ -50,8 +51,9 @@
     const IM_IN_MATHML = 3;
 
     /**
-     * Used as cache to detect if is available ENT_HTML5
-     * @var boolean
+     * Used as cache to detect if is available ENT_HTML5.
+     *
+     * @var bool
      */
     private $hasHTML5 = false;
 
@@ -169,12 +171,13 @@
         // If HHVM, see https://github.com/facebook/hhvm/issues/2727
         $this->hasHTML5 = defined('ENT_HTML5') && !defined('HHVM_VERSION');
     }
+
     public function addRule(array $rule)
     {
         $this->nonBooleanAttributes[] = $rule;
     }
 
-    public function setTraverser(\Masterminds\HTML5\Serializer\Traverser $traverser)
+    public function setTraverser(Traverser $traverser)
     {
         $this->traverser = $traverser;
 
@@ -211,10 +214,10 @@
 
         // If we are in SVG or MathML there is special handling.
         // Using if/elseif instead of switch because it's faster in PHP.
-        if ($name == 'svg') {
+        if ('svg' == $name) {
             $this->outputMode = static::IM_IN_SVG;
             $name = Elements::normalizeSvgElement($name);
-        } elseif ($name == 'math') {
+        } elseif ('math' == $name) {
             $this->outputMode = static::IM_IN_MATHML;
         }
 
@@ -234,13 +237,13 @@
             }
 
             // Close out the SVG or MathML special handling.
-            if ($name == 'svg' || $name == 'math') {
+            if ('svg' == $name || 'math' == $name) {
                 $this->outputMode = static::IM_IN_HTML;
             }
         }
 
         // If not unary, add a closing tag.
-        if (! Elements::isA($name, Elements::VOID_TAG)) {
+        if (!Elements::isA($name, Elements::VOID_TAG)) {
             $this->closeTag($ele);
         }
     }
@@ -248,13 +251,13 @@
     /**
      * Write a text node.
      *
-     * @param \DOMText $ele
-     *            The text node to write.
+     * @param \DOMText $ele The text node to write.
      */
     public function text($ele)
     {
         if (isset($ele->parentNode) && isset($ele->parentNode->tagName) && Elements::isA($ele->parentNode->localName, Elements::TEXT_RAW)) {
             $this->wr($ele->data);
+
             return;
         }
 
@@ -283,20 +286,19 @@
             ->wr($ele->data)
             ->wr('?>');
     }
+
     /**
-     * Write the namespace attributes
+     * Write the namespace attributes.
      *
-     *
-     * @param \DOMNode $ele
-     *            The element being written.
+     * @param \DOMNode $ele The element being written.
      */
     protected function namespaceAttrs($ele)
     {
-        if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument){
+        if (!$this->xpath || $this->xpath->document !== $ele->ownerDocument) {
             $this->xpath = new \DOMXPath($ele->ownerDocument);
         }
 
-        foreach( $this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele ) as $nsNode ) {
+        foreach ($this->xpath->query('namespace::*[not(.=../../namespace::*)]', $ele) as $nsNode) {
             if (!in_array($nsNode->nodeValue, $this->implicitNamespaces)) {
                 $this->wr(' ')->wr($nsNode->nodeName)->wr('="')->wr($nsNode->nodeValue)->wr('"');
             }
@@ -309,18 +311,15 @@
      * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the
      * qualified name (8.3).
      *
-     * @param \DOMNode $ele
-     *            The element being written.
+     * @param \DOMNode $ele The element being written.
      */
     protected function openTag($ele)
     {
         $this->wr('<')->wr($this->traverser->isLocalElement($ele) ? $ele->localName : $ele->tagName);
 
-
         $this->attrs($ele);
         $this->namespaceAttrs($ele);
 
-
         if ($this->outputMode == static::IM_IN_HTML) {
             $this->wr('>');
         }         // If we are not in html mode we are in SVG, MathML, or XML embedded content.
@@ -337,7 +336,7 @@
     protected function attrs($ele)
     {
         // FIXME: Needs support for xml, xmlns, xlink, and namespaced elements.
-        if (! $ele->hasAttributes()) {
+        if (!$ele->hasAttributes()) {
             return $this;
         }
 
@@ -345,7 +344,7 @@
         // value-less attributes.
         $map = $ele->attributes;
         $len = $map->length;
-        for ($i = 0; $i < $len; ++ $i) {
+        for ($i = 0; $i < $len; ++$i) {
             $node = $map->item($i);
             $val = $this->enc($node->value, true);
 
@@ -365,45 +364,42 @@
 
             $this->wr(' ')->wr($name);
 
-            if ((isset($val) && $val !== '') || $this->nonBooleanAttribute($node)) {
+            if ((isset($val) && '' !== $val) || $this->nonBooleanAttribute($node)) {
                 $this->wr('="')->wr($val)->wr('"');
             }
         }
     }
 
-
     protected function nonBooleanAttribute(\DOMAttr $attr)
     {
         $ele = $attr->ownerElement;
-        foreach($this->nonBooleanAttributes as $rule){
-
-            if(isset($rule['nodeNamespace']) && $rule['nodeNamespace']!==$ele->namespaceURI){
+        foreach ($this->nonBooleanAttributes as $rule) {
+            if (isset($rule['nodeNamespace']) && $rule['nodeNamespace'] !== $ele->namespaceURI) {
                 continue;
             }
-            if(isset($rule['attNamespace']) && $rule['attNamespace']!==$attr->namespaceURI){
+            if (isset($rule['attNamespace']) && $rule['attNamespace'] !== $attr->namespaceURI) {
                 continue;
             }
-            if(isset($rule['nodeName']) && !is_array($rule['nodeName']) && $rule['nodeName']!==$ele->localName){
+            if (isset($rule['nodeName']) && !is_array($rule['nodeName']) && $rule['nodeName'] !== $ele->localName) {
                 continue;
             }
-            if(isset($rule['nodeName']) && is_array($rule['nodeName']) && !in_array($ele->localName, $rule['nodeName'], true)){
+            if (isset($rule['nodeName']) && is_array($rule['nodeName']) && !in_array($ele->localName, $rule['nodeName'], true)) {
                 continue;
             }
-            if(isset($rule['attrName']) && !is_array($rule['attrName']) && $rule['attrName']!==$attr->localName){
+            if (isset($rule['attrName']) && !is_array($rule['attrName']) && $rule['attrName'] !== $attr->localName) {
                 continue;
             }
-            if(isset($rule['attrName']) && is_array($rule['attrName']) && !in_array($attr->localName, $rule['attrName'], true)){
+            if (isset($rule['attrName']) && is_array($rule['attrName']) && !in_array($attr->localName, $rule['attrName'], true)) {
                 continue;
             }
-            if(isset($rule['xpath'])){
-
+            if (isset($rule['xpath'])) {
                 $xp = $this->getXPath($attr);
-                if(isset($rule['prefixes'])){
-                    foreach($rule['prefixes'] as $nsPrefix => $ns){
+                if (isset($rule['prefixes'])) {
+                    foreach ($rule['prefixes'] as $nsPrefix => $ns) {
                         $xp->registerNamespace($nsPrefix, $ns);
                     }
                 }
-                if(!$xp->evaluate($rule['xpath'], $attr)){
+                if (!$xp->evaluate($rule['xpath'], $attr)) {
                     continue;
                 }
             }
@@ -414,10 +410,12 @@
         return false;
     }
 
-    private function getXPath(\DOMNode $node){
-        if(!$this->xpath){
+    private function getXPath(\DOMNode $node)
+    {
+        if (!$this->xpath) {
             $this->xpath = new \DOMXPath($node->ownerDocument);
         }
+
         return $this->xpath;
     }
 
@@ -427,8 +425,7 @@
      * Tags for HTML, MathML, and SVG are in the local name. Otherwise, use the
      * qualified name (8.3).
      *
-     * @param \DOMNode $ele
-     *            The element being written.
+     * @param \DOMNode $ele The element being written.
      */
     protected function closeTag($ele)
     {
@@ -440,25 +437,26 @@
     /**
      * Write to the output.
      *
-     * @param string $text
-     *            The string to put into the output.
+     * @param string $text The string to put into the output
      *
-     * @return \Masterminds\HTML5\Serializer\Traverser $this so it can be used in chaining.
+     * @return $this
      */
     protected function wr($text)
     {
         fwrite($this->out, $text);
+
         return $this;
     }
 
     /**
      * Write a new line character.
      *
-     * @return \Masterminds\HTML5\Serializer\Traverser $this so it can be used in chaining.
+     * @return $this
      */
     protected function nl()
     {
         fwrite($this->out, PHP_EOL);
+
         return $this;
     }
 
@@ -484,18 +482,15 @@
      *
      * @todo Use the Entities class in php 5.3 to have html5 entities.
      *
-     * @param string $text
-     *            text to encode.
-     * @param boolean $attribute
-     *            True if we are encoding an attrubute, false otherwise
+     * @param string $text      Text to encode.
+     * @param bool   $attribute True if we are encoding an attrubute, false otherwise.
      *
      * @return string The encoded text.
      */
     protected function enc($text, $attribute = false)
     {
-
         // Escape the text rather than convert to named character references.
-        if (! $this->encode) {
+        if (!$this->encode) {
             return $this->escape($text, $attribute);
         }
 
@@ -507,7 +502,7 @@
         }         // If a version earlier than 5.4 html5 entities are not entirely handled.
         // This manually handles them.
         else {
-            return strtr($text, \Masterminds\HTML5\Serializer\HTML5Entities::$map);
+            return strtr($text, HTML5Entities::$map);
         }
     }
 
@@ -525,14 +520,11 @@
      *
      * @see http://www.w3.org/TR/2013/CR-html5-20130806/syntax.html#escapingString
      *
-     * @param string $text
-     *            text to escape.
-     * @param boolean $attribute
-     *            True if we are escaping an attrubute, false otherwise
+     * @param string $text      Text to escape.
+     * @param bool   $attribute True if we are escaping an attrubute, false otherwise.
      */
     protected function escape($text, $attribute = false)
     {
-
         // Not using htmlspecialchars because, while it does escaping, it doesn't
         // match the requirements of section 8.5. For example, it doesn't handle
         // non-breaking spaces.
@@ -540,14 +532,14 @@
             $replace = array(
                 '"' => '&quot;',
                 '&' => '&amp;',
-                "\xc2\xa0" => '&nbsp;'
+                "\xc2\xa0" => '&nbsp;',
             );
         } else {
             $replace = array(
                 '<' => '&lt;',
                 '>' => '&gt;',
                 '&' => '&amp;',
-                "\xc2\xa0" => '&nbsp;'
+                "\xc2\xa0" => '&nbsp;',
             );
         }