Skip to content

Commit

Permalink
#9: Value of Textarea requires special handling, fixed that
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisBirkholz committed Sep 1, 2017
1 parent 8a05b48 commit 42a8d1f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Html/FormElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) : '')
;
}
}
10 changes: 9 additions & 1 deletion src/Html/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
;
}
}
4 changes: 2 additions & 2 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 26 additions & 1 deletion tests/Html/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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 <pre>html</pre> which must be escaped\nand a newline\nand a <textarea>textarea</textarea> 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);
}
}
}

0 comments on commit 42a8d1f

Please sign in to comment.