diff options
author | Asmir Mustafic <[email protected]> | 2018-11-25 16:45:06 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2018-11-25 16:45:06 +0100 |
commit | 54d066b1664a5fb749511c764e8d208f6bc80c3b (patch) | |
tree | 5089aec726a28a1247e21f7febd96e746cf4c6f1 /src/HTML5/Serializer | |
parent | c76378a560eeefa900429989fb6e85859f3a6b5e (diff) | |
parent | 91e7977d14f1ea2e78b5d46f85851c30bf3c6695 (diff) |
Merge pull request #160 from tgalopin/coding-style
Improve Travis configuration and add PHP-CS-Fixer
Diffstat (limited to 'src/HTML5/Serializer')
-rw-r--r-- | src/HTML5/Serializer/HTML5Entities.php | 6 | ||||
-rw-r--r-- | src/HTML5/Serializer/OutputRules.php | 114 | ||||
-rw-r--r-- | src/HTML5/Serializer/RulesInterface.php | 26 | ||||
-rw-r--r-- | src/HTML5/Serializer/Traverser.php | 35 |
4 files changed, 81 insertions, 100 deletions
diff --git a/src/HTML5/Serializer/HTML5Entities.php b/src/HTML5/Serializer/HTML5Entities.php index 4f90f84..e9421a1 100644 --- a/src/HTML5/Serializer/HTML5Entities.php +++ b/src/HTML5/Serializer/HTML5Entities.php @@ -3,11 +3,12 @@ * @file * This contains HTML5 entities to use with serializing. * - * The list here is mildly different from the list at \Masterminds\HTML5\Entities because + * The list here is mildly different from the list at Entities because * that list was generated from the w3c. It contains some entities that are * not entirely proper such as &am; which maps to &. This list is meant to be * a fallback for PHP versions prior to PHP 5.4 when dealing with encoding. */ + namespace Masterminds\HTML5\Serializer; /** @@ -17,7 +18,6 @@ namespace Masterminds\HTML5\Serializer; */ class HTML5Entities { - public static $map = array( ' ' => '	', "\n" => '
', @@ -1528,6 +1528,6 @@ class HTML5Entities '𝕨' => '𝕨', '𝕩' => '𝕩', '𝕪' => '𝕪', - '𝕫' => '𝕫' + '𝕫' => '𝕫', ); } 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" => ' ', ); } diff --git a/src/HTML5/Serializer/RulesInterface.php b/src/HTML5/Serializer/RulesInterface.php index 6ef5e5e..69a6ecd 100644 --- a/src/HTML5/Serializer/RulesInterface.php +++ b/src/HTML5/Serializer/RulesInterface.php @@ -3,28 +3,24 @@ * @file * The interface definition for Rules to generate output. */ + namespace Masterminds\HTML5\Serializer; /** - * To create a new rule set for writing output the RulesInterface needs to be - * implemented. - * The resulting class can be specified in the options with the - * key of rules. + * To create a new rule set for writing output the RulesInterface needs to be implemented. + * The resulting class can be specified in the options with the key of rules. * - * For an example implementation see \Masterminds\HTML5\Serializer\OutputRules. + * For an example implementation see Serializer\OutputRules. */ interface RulesInterface { - /** * The class constructor. * * Note, before the rules can be used a traverser must be registered. * - * @param mixed $output - * The output stream to write output to. - * @param array $options - * An array of options. + * @param mixed $output The output stream to write output to. + * @param array $options An array of options. */ public function __construct($output, $options = array()); @@ -33,11 +29,11 @@ interface RulesInterface * * Note, only one traverser can be used by the rules. * - * @param \Masterminds\HTML5\Serializer\Traverser $traverser - * The traverser used in the rules. - * @return \Masterminds\HTML5\Serializer\RulesInterface $this for the current object. + * @param Traverser $traverser The traverser used in the rules. + * + * @return RulesInterface $this for the current object. */ - public function setTraverser(\Masterminds\HTML5\Serializer\Traverser $traverser); + public function setTraverser(Traverser $traverser); /** * Write a document element (\DOMDocument). @@ -92,7 +88,7 @@ interface RulesInterface /** * Write a processor instruction. * - * To learn about processor instructions see \Masterminds\HTML5\InstructionProcessor + * To learn about processor instructions see InstructionProcessor * * Instead of returning the result write it to the output stream ($output) * that was passed into the constructor. diff --git a/src/HTML5/Serializer/Traverser.php b/src/HTML5/Serializer/Traverser.php index 399570d..1e8d792 100644 --- a/src/HTML5/Serializer/Traverser.php +++ b/src/HTML5/Serializer/Traverser.php @@ -1,4 +1,5 @@ <?php + namespace Masterminds\HTML5\Serializer; /** @@ -12,14 +13,13 @@ namespace Masterminds\HTML5\Serializer; */ class Traverser { - /** * Namespaces that should be treated as "local" to HTML5. */ protected static $local_ns = array( 'http://www.w3.org/1999/xhtml' => 'html', 'http://www.w3.org/1998/Math/MathML' => 'math', - 'http://www.w3.org/2000/svg' => 'svg' + 'http://www.w3.org/2000/svg' => 'svg', ); protected $dom; @@ -35,16 +35,13 @@ class Traverser /** * Create a traverser. * - * @param DOMNode|DOMNodeList $dom - * The document or node to traverse. - * @param resource $out - * A stream that allows writing. The traverser will output into this - * stream. - * @param array $options - * An array or options for the traverser as key/value pairs. These include: - * - encode_entities: A bool to specify if full encding should happen for all named - * charachter references. Defaults to false which escapes &'<>". - * - output_rules: The path to the class handling the output rules. + * @param \DOMNode|\DOMNodeList $dom The document or node to traverse. + * @param resource $out A stream that allows writing. The traverser will output into this + * stream. + * @param array $options An array of options for the traverser as key/value pairs. These include: + * - encode_entities: A bool to specify if full encding should happen for all named + * charachter references. Defaults to false which escapes &'<>". + * - output_rules: The path to the class handling the output rules. */ public function __construct($dom, $out, RulesInterface $rules, $options = array()) { @@ -59,8 +56,7 @@ class Traverser /** * Tell the traverser to walk the DOM. * - * @return resource $out - * Returns the output stream. + * @return resource $out Returns the output stream. */ public function walk() { @@ -87,8 +83,7 @@ class Traverser /** * Process a node in the DOM. * - * @param mixed $node - * A node implementing \DOMNode. + * @param mixed $node A node implementing \DOMNode. */ public function node($node) { @@ -119,8 +114,7 @@ class Traverser /** * Walk through all the nodes on a node list. * - * @param \DOMNodeList $nl - * A list of child elements to walk through. + * @param \DOMNodeList $nl A list of child elements to walk through. */ public function children($nl) { @@ -132,10 +126,9 @@ class Traverser /** * Is an element local? * - * @param mixed $ele - * An element that implement \DOMNode. + * @param mixed $ele An element that implement \DOMNode. * - * @return bool True if local and false otherwise. + * @return bool true if local and false otherwise. */ public function isLocalElement($ele) { |