diff --git a/src/Html/FormElementTrait.php b/src/Html/FormElementTrait.php index 3b94d4b..ef29248 100644 --- a/src/Html/FormElementTrait.php +++ b/src/Html/FormElementTrait.php @@ -165,14 +165,14 @@ final public function setValue(string $value = null) : FormElementInterface return $this; } - final protected function renderFormElementAttributes() : string + final protected function renderFormElementAttributes(bool $renderValueAttribute = true) : string { return Util::renderHtmlAttribute('autofocus', $this->autofocus) . Util::renderHtmlAttribute('disabled', $this->disabled) . Util::renderHtmlAttribute('form', $this->FormElementTraitForm) . Util::renderHtmlAttribute('name', $this->name) - . Util::renderHtmlAttribute('value', $this->value) + . ($renderValueAttribute ? Util::renderHtmlAttribute('value', $this->value) : '') ; } } diff --git a/src/Html/Textarea.php b/src/Html/Textarea.php index 5cc0118..b5c3727 100644 --- a/src/Html/Textarea.php +++ b/src/Html/Textarea.php @@ -162,7 +162,7 @@ final public function setWrap(string $wrap = null) : self protected function renderAttributes() : string { return parent::renderAttributes() - . $this->renderFormElementAttributes() + . $this->renderFormElementAttributes(false) . $this->renderAutocompleteAttribute() . $this->renderMinlengthMaxlengthAttributes() . $this->renderPlaceholderAttribute() @@ -173,4 +173,12 @@ protected function renderAttributes() : string . Util::renderHtmlAttribute('wrap', $this->wrap) ; } + + public function render(): string + { + return '<' . self::TAG . $this->renderAttributes() . '>' + . ($this->hasValue() ? Util::escapeHtml($this->getValue()) : null) + . '' . self::TAG . '>' . PHP_EOL + ; + } } diff --git a/src/Util.php b/src/Util.php index f99da21..b762c21 100644 --- a/src/Util.php +++ b/src/Util.php @@ -83,12 +83,12 @@ public static final function getRelativePath($sourceFile, $targetFile, $canonica public static function escapeHtml($value) { - return \htmlentities($value, \ENT_COMPAT|\ENT_HTML5, 'UTF-8'); + return \htmlentities($value, \ENT_COMPAT|\ENT_HTML401, 'UTF-8'); } public static function escapeHtmlAttributeValue($value) { - return \htmlspecialchars($value, \ENT_QUOTES||\ENT_HTML5, 'UTF-8'); + return \htmlspecialchars($value, \ENT_QUOTES|\ENT_HTML401, 'UTF-8'); } public static function renderHtmlAttribute(string $name, $value) : string diff --git a/tests/Html/TextareaTest.php b/tests/Html/TextareaTest.php index 3cfa1f0..e9079cf 100644 --- a/tests/Html/TextareaTest.php +++ b/tests/Html/TextareaTest.php @@ -31,7 +31,6 @@ protected function factory() : HtmlElement "disabled" => true, "form" => null, "name" => ["foo", "bar"], - "value" => ["foo", "bar"], "autocomplete" => [Form::AUTOCOMPLETE_ON, Form::AUTOCOMPLETE_OFF], "cols" => [20, 10], @@ -45,4 +44,30 @@ protected function factory() : HtmlElement "rows" => [5, 10], "wrap" => [Textarea::WRAP_SOFT, Textarea::WRAP_HARD], ]; + + /** + * Special handling for text area value (that is written as element content, not value attribute) + * + * @test + */ + public function testValue() + { + $values = [ + "simple text", + "text with\n\nnewlines\n", + "text with
htmlwhich must be escaped\nand a newline\nand a element", + ]; + + $element = $this->factory(); + + foreach ($values as $value) { + $this->assertSame($element, $element->setValue($value)); + $this->assertTrue($element->hasValue()); + $this->assertSame($value, $element->getValue()); + + $parsed = \DOMDocument::loadHtml($element->render()); + $dom = $parsed->getElementsByTagName($element::TAG)[0]; + $this->assertEquals($value, $dom->nodeValue); + } + } }