xref: /dokuwiki/inc/Form/TextareaElement.php (revision 884caed926ca0aa0af6ce3f34ae3aa7317a3361a)
1<?php
2
3namespace dokuwiki\Form;
4
5/**
6 * Class TextareaElement
7 * @package dokuwiki\Form
8 */
9class TextareaElement extends InputElement
10{
11    /**
12     * @var string the actual text within the area
13     */
14    protected $text;
15
16    /**
17     * @param string $name The name of this form element
18     * @param string $label The label text for this element
19     */
20    public function __construct($name, $label)
21    {
22        parent::__construct('textarea', $name, $label);
23        $this->attr('dir', 'auto');
24    }
25
26    /**
27     * Get or set the element's value
28     *
29     * This is the preferred way of setting the element's value
30     *
31     * @param null|string $value
32     * @return string|$this
33     */
34    public function val($value = null)
35    {
36        if ($value !== null) {
37            $this->text = cleanText($value);
38            return $this;
39        }
40        return $this->text;
41    }
42
43    /**
44     * The HTML representation of this element
45     *
46     * @return string
47     */
48    protected function mainElementHTML()
49    {
50        if ($this->useInput) $this->prefillInput();
51        // The browser's HTML parser ignores a single newline immediately
52        // after a <textarea> start tag, so a value that itself begins with a
53        // newline would lose it on round-trip. We emit a guard newline that
54        // absorbs the one the browser drops, keeping the value intact.
55        // See the HTML Standard, "in body" insertion mode (the LF right after
56        // <textarea> is dropped as an authoring convenience):
57        // https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
58        return '<textarea ' . buildAttributes($this->attrs()) . '>' . "\n" .
59            formText($this->val()) . '</textarea>';
60    }
61}
62