-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59ab7c6
commit 5293848
Showing
3 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of Replum: the web widget framework. | ||
* | ||
* Copyright (c) Dennis Birkholz <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Replum\Html; | ||
|
||
use \Replum\Util; | ||
|
||
/** | ||
* @author Dennis Birkholz <[email protected]> | ||
* @link https://www.w3.org/TR/html5/forms.html#the-textarea-element | ||
*/ | ||
final class Textarea extends HtmlElement implements FormInputInterface | ||
{ | ||
use FormElementTrait; | ||
use AutocompleteAttributeTrait; | ||
use MinlengthMaxLengthAttributesTrait; | ||
use PlaceholderAttributeTrait; | ||
use ReadonlyAttributeTrait; | ||
use RequiredAttributeTrait; | ||
|
||
const TAG = 'textarea'; | ||
|
||
const WRAP_SOFT = 'soft'; | ||
const WRAP_HARD = 'hard'; | ||
|
||
/** | ||
* Maximum number of characters per line | ||
* | ||
* @var int | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols | ||
*/ | ||
private $cols; | ||
|
||
/** | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols | ||
*/ | ||
final public function getCols() : int | ||
{ | ||
return $this->cols; | ||
} | ||
|
||
/** | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols | ||
*/ | ||
final public function hasCols() : bool | ||
{ | ||
return ($this->cols !== null); | ||
} | ||
|
||
/** | ||
* @return static $this | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-cols | ||
*/ | ||
final public function setCols(int $cols = null) : self | ||
{ | ||
if ($this->cols !== $cols) { | ||
$this->cols = $cols; | ||
$this->setChanged(true); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Number of lines to show | ||
* | ||
* @var int | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows | ||
*/ | ||
private $rows; | ||
|
||
/** | ||
* Get the number of lines to show | ||
* | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows | ||
*/ | ||
final public function getRows() : int | ||
{ | ||
return $this->rows; | ||
} | ||
|
||
/** | ||
* Check whether the number of lines to show is set | ||
* | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows | ||
*/ | ||
final public function hasRows() : bool | ||
{ | ||
return ($this->rows !== null); | ||
} | ||
|
||
/** | ||
* Set the number of lines to show | ||
* | ||
* @return static $this | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-rows | ||
*/ | ||
final public function setRows(int $rows = null) : self | ||
{ | ||
if ($this->rows !== $rows) { | ||
$this->rows = $rows; | ||
$this->setChanged(true); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* How the value of the form control is to be wrapped for form submission | ||
* | ||
* @var WRAP_SOFT|WRAP_HARD | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap | ||
*/ | ||
private $wrap; | ||
|
||
/** | ||
* Get how the value of the form control is to be wrapped for form submission | ||
* | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap | ||
*/ | ||
final public function getWrap() : string | ||
{ | ||
return $this->wrap; | ||
} | ||
|
||
/** | ||
* Check whether how the value of the form control is to be wrapped for form submission is set | ||
* | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap | ||
*/ | ||
final public function hasWrap() : bool | ||
{ | ||
return ($this->wrap !== null); | ||
} | ||
|
||
/** | ||
* Set how the value of the form control is to be wrapped for form submission | ||
* | ||
* @return static $this | ||
* @link https://www.w3.org/TR/html5/forms.html#attr-textarea-wrap | ||
*/ | ||
final public function setWrap(string $wrap = null) : self | ||
{ | ||
if ($wrap !== Textarea::WRAP_SOFT && $wrap !== Textarea::WRAP_HARD && $wrap !== null) { | ||
throw new \InvalidArgumentException('Valid values for wrap are: ' . Textarea::class . '::WRAP_SOFT, ' . Textarea::class . '::WRAP_HARD and NULL!'); | ||
} | ||
|
||
if ($this->wrap !== $wrap) { | ||
$this->wrap = $wrap; | ||
$this->setChanged(true); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
protected function renderAttributes() : string | ||
{ | ||
return parent::renderAttributes() | ||
. $this->renderFormElementAttributes() | ||
. $this->renderAutocompleteAttribute() | ||
. $this->renderMinlengthMaxlengthAttributes() | ||
. $this->renderPlaceholderAttribute() | ||
. $this->renderReadonlyAttribute() | ||
. $this->renderRequiredAttribute() | ||
. Util::renderHtmlAttribute('cols', $this->cols) | ||
. Util::renderHtmlAttribute('rows', $this->rows) | ||
. Util::renderHtmlAttribute('wrap', $this->wrap) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* This file is part of Replum: the web widget framework. | ||
* | ||
* Copyright (c) Dennis Birkholz <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Replum\Html; | ||
|
||
use \PHPUnit\Framework\TestCase; | ||
use \Replum\Context; | ||
use \Replum\HtmlFactory; | ||
use Symfony\Component\Console\Tests\Input\InputTest; | ||
|
||
class TextareaTest extends HtmlTestBase | ||
{ | ||
/** | ||
* @return Textarea | ||
*/ | ||
protected function factory() : HtmlElement | ||
{ | ||
return HtmlFactory::textarea($this->page); | ||
} | ||
|
||
protected $attributes = [ | ||
// Common attributes | ||
"autofocus" => true, | ||
"disabled" => true, | ||
"form" => null, | ||
"name" => ["foo", "bar"], | ||
"value" => ["foo", "bar"], | ||
|
||
"autocomplete" => [Input::AUTOCOMPLETE_ON, Input::AUTOCOMPLETE_OFF], | ||
"cols" => [20, 10], | ||
"dirname" => null, | ||
"inputmode" => null, | ||
"maxlength" => [20, 10], | ||
"minlength" => [10, 20], | ||
"placeholder" => ["placeholder I am", "Stupid text"], | ||
"readonly" => true, | ||
"required" => true, | ||
"rows" => [5, 10], | ||
"wrap" => [Textarea::WRAP_SOFT, Textarea::WRAP_HARD], | ||
]; | ||
} |