diff options
Diffstat (limited to 'src/HTML5/Serializer/OutputRules.php')
-rw-r--r-- | src/HTML5/Serializer/OutputRules.php | 114 |
1 files changed, 53 insertions, 61 deletions
diff --git a/src/HTML5/Serializer/OutputRules.php b/src/HTML5/Serializer/OutputRules.php index a22683c..41616d5 100644 --- a/src/HTML5/Serializer/OutputRules.php +++ b/src/HTML5/Serializer/OutputRules.php @@ -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 @@ use Masterminds\HTML5\Elements; /** * 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface // 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface // 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface } // 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface /** * 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface ->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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface * 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface // 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface $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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface * 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface /** * 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface * * @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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface } // 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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface * * @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 @@ class OutputRules implements \Masterminds\HTML5\Serializer\RulesInterface $replace = array( '"' => '"', '&' => '&', - "\xc2\xa0" => ' ' + "\xc2\xa0" => ' ', ); } else { $replace = array( '<' => '<', '>' => '>', '&' => '&', - "\xc2\xa0" => ' ' + "\xc2\xa0" => ' ', ); } |