From 1541859e0ee21228f67055fbef40e44e08c54e9f Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Wed, 5 Dec 2018 17:13:51 +0100 Subject: [PATCH] PHP < 7 compat for Html renderables --- src/Html/Renderable.php | 3 +- src/Html/Renderables/Element.php | 38 +++++++++++++---------- src/Html/Renderables/Text.php | 8 +++-- src/Html/Sanitisation/CharacterFilter.php | 12 +++++-- src/Html/Sanitisation/Escaper.php | 23 ++++++++++---- 5 files changed, 57 insertions(+), 27 deletions(-) diff --git a/src/Html/Renderable.php b/src/Html/Renderable.php index d51c554..478718b 100644 --- a/src/Html/Renderable.php +++ b/src/Html/Renderable.php @@ -4,5 +4,6 @@ namespace Erusev\Parsedown\Html; interface Renderable { - public function getHtml(): string; + /** @return string */ + public function getHtml(); } diff --git a/src/Html/Renderables/Element.php b/src/Html/Renderables/Element.php index a3fa681..b847bf6 100644 --- a/src/Html/Renderables/Element.php +++ b/src/Html/Renderables/Element.php @@ -22,11 +22,8 @@ final class Element implements Renderable * @param array $attributes * @param Renderable[]|null $Contents */ - public function __construct( - string $name, - array $attributes, - ?array $Contents - ) { + public function __construct($name, $attributes, $Contents) + { $this->name = $name; $this->attributes = $attributes; $this->Contents = $Contents; @@ -35,10 +32,10 @@ final class Element implements Renderable /** * @param string $name * @param array $attributes - * @param Renderable ...$Contents - * + * @param Renderable[] $Contents + * @return self */ - public static function new(string $name, array $attributes, Renderable ...$Contents): self + public static function create($name, array $attributes, array $Contents) { return new self($name, $attributes, $Contents); } @@ -46,13 +43,15 @@ final class Element implements Renderable /** * @param string $name * @param array $attributes + * @return self */ - public static function selfClosing(string $name, array $attributes): self + public static function selfClosing($name, array $attributes) { return new self($name, $attributes, null); } - public function name(): string + /** @return string */ + public function name() { return $this->name; } @@ -60,7 +59,7 @@ final class Element implements Renderable /** * @return array */ - public function attributes(): array + public function attributes() { return $this->attributes; } @@ -68,33 +67,40 @@ final class Element implements Renderable /** * @return Renderable[]|null */ - public function contents(): ?array + public function contents() { return $this->Contents; } - public function settingName(string $name): self + /** + * @param string $name + * @return self + */ + public function settingName($name) { return new self($name, $this->attributes, $this->Contents); } /** * @param array $attributes + * @return self */ - public function settingAttributes(array $attributes): self + public function settingAttributes(array $attributes) { return new self($this->name, $attributes, $this->Contents); } /** * @param Renderable[]|null $Contents + * @return self */ - public function settingContents(?array $Contents): self + public function settingContents($Contents) { return new self($this->name, $this->attributes, $Contents); } - public function getHtml(): string + /** @return string */ + public function getHtml() { $html = ''; diff --git a/src/Html/Renderables/Text.php b/src/Html/Renderables/Text.php index da0b6bc..ddccbae 100644 --- a/src/Html/Renderables/Text.php +++ b/src/Html/Renderables/Text.php @@ -10,12 +10,16 @@ final class Text implements Renderable /** @var string */ private $text; - public function __construct(string $text = '') + /** + * @param string $text + */ + public function __construct($text = '') { $this->text = $text; } - public function getHtml(): string + /** @return string */ + public function getHtml() { return Escaper::htmlElementValue($this->text); } diff --git a/src/Html/Sanitisation/CharacterFilter.php b/src/Html/Sanitisation/CharacterFilter.php index 56294b9..f7baeb0 100644 --- a/src/Html/Sanitisation/CharacterFilter.php +++ b/src/Html/Sanitisation/CharacterFilter.php @@ -4,7 +4,11 @@ namespace Erusev\Parsedown\Html\Sanitisation; final class CharacterFilter { - public static function htmlAttributeName(string $text) : string + /** + * @param string $text + * @return string + */ + public static function htmlAttributeName($text) { /** * https://www.w3.org/TR/html/syntax.html#name @@ -23,7 +27,11 @@ final class CharacterFilter ); } - public static function htmlElementName(string $text) : string + /** + * @param string $text + * @return string + */ + public static function htmlElementName($text) { /** * https://www.w3.org/TR/html/syntax.html#tag-name diff --git a/src/Html/Sanitisation/Escaper.php b/src/Html/Sanitisation/Escaper.php index 1c33f8a..0ee611f 100644 --- a/src/Html/Sanitisation/Escaper.php +++ b/src/Html/Sanitisation/Escaper.php @@ -4,20 +4,31 @@ namespace Erusev\Parsedown\Html\Sanitisation; final class Escaper { - public static function htmlAttributeValue(string $text) : string + /** + * @param string $text + * @return string + */ + public static function htmlAttributeValue($text) { return self::escape($text); } - public static function htmlElementValue(string $text) : string + /** + * @param string $text + * @return string + */ + public static function htmlElementValue($text) { return self::escape($text, true); } - private static function escape( - string $text, - bool $allowQuotes = false - ) : string { + /** + * @param string $text + * @param bool $allowQuotes + * @return string + */ + private static function escape($text, $allowQuotes = false) + { return \htmlentities( $text, $allowQuotes ? \ENT_NOQUOTES : \ENT_QUOTES,