PHP < 7 compat for Html renderables

This commit is contained in:
Aidan Woods 2018-12-05 17:13:51 +01:00
parent 0f6c0fa84d
commit 1541859e0e
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
5 changed files with 57 additions and 27 deletions

View File

@ -4,5 +4,6 @@ namespace Erusev\Parsedown\Html;
interface Renderable interface Renderable
{ {
public function getHtml(): string; /** @return string */
public function getHtml();
} }

View File

@ -22,11 +22,8 @@ final class Element implements Renderable
* @param array<string, string> $attributes * @param array<string, string> $attributes
* @param Renderable[]|null $Contents * @param Renderable[]|null $Contents
*/ */
public function __construct( public function __construct($name, $attributes, $Contents)
string $name, {
array $attributes,
?array $Contents
) {
$this->name = $name; $this->name = $name;
$this->attributes = $attributes; $this->attributes = $attributes;
$this->Contents = $Contents; $this->Contents = $Contents;
@ -35,10 +32,10 @@ final class Element implements Renderable
/** /**
* @param string $name * @param string $name
* @param array<string, string> $attributes * @param array<string, string> $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); return new self($name, $attributes, $Contents);
} }
@ -46,13 +43,15 @@ final class Element implements Renderable
/** /**
* @param string $name * @param string $name
* @param array<string, string> $attributes * @param array<string, string> $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); return new self($name, $attributes, null);
} }
public function name(): string /** @return string */
public function name()
{ {
return $this->name; return $this->name;
} }
@ -60,7 +59,7 @@ final class Element implements Renderable
/** /**
* @return array<string, string> * @return array<string, string>
*/ */
public function attributes(): array public function attributes()
{ {
return $this->attributes; return $this->attributes;
} }
@ -68,33 +67,40 @@ final class Element implements Renderable
/** /**
* @return Renderable[]|null * @return Renderable[]|null
*/ */
public function contents(): ?array public function contents()
{ {
return $this->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); return new self($name, $this->attributes, $this->Contents);
} }
/** /**
* @param array<string, string> $attributes * @param array<string, string> $attributes
* @return self
*/ */
public function settingAttributes(array $attributes): self public function settingAttributes(array $attributes)
{ {
return new self($this->name, $attributes, $this->Contents); return new self($this->name, $attributes, $this->Contents);
} }
/** /**
* @param Renderable[]|null $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); return new self($this->name, $this->attributes, $Contents);
} }
public function getHtml(): string /** @return string */
public function getHtml()
{ {
$html = ''; $html = '';

View File

@ -10,12 +10,16 @@ final class Text implements Renderable
/** @var string */ /** @var string */
private $text; private $text;
public function __construct(string $text = '') /**
* @param string $text
*/
public function __construct($text = '')
{ {
$this->text = $text; $this->text = $text;
} }
public function getHtml(): string /** @return string */
public function getHtml()
{ {
return Escaper::htmlElementValue($this->text); return Escaper::htmlElementValue($this->text);
} }

View File

@ -4,7 +4,11 @@ namespace Erusev\Parsedown\Html\Sanitisation;
final class CharacterFilter 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 * 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 * https://www.w3.org/TR/html/syntax.html#tag-name

View File

@ -4,20 +4,31 @@ namespace Erusev\Parsedown\Html\Sanitisation;
final class Escaper final class Escaper
{ {
public static function htmlAttributeValue(string $text) : string /**
* @param string $text
* @return string
*/
public static function htmlAttributeValue($text)
{ {
return self::escape($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); return self::escape($text, true);
} }
private static function escape( /**
string $text, * @param string $text
bool $allowQuotes = false * @param bool $allowQuotes
) : string { * @return string
*/
private static function escape($text, $allowQuotes = false)
{
return \htmlentities( return \htmlentities(
$text, $text,
$allowQuotes ? \ENT_NOQUOTES : \ENT_QUOTES, $allowQuotes ? \ENT_NOQUOTES : \ENT_QUOTES,