Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 118
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
HTMLTextAreaField
0.00% covered (danger)
0.00%
0 / 117
0.00% covered (danger)
0.00%
0 / 7
650
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getCols
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRows
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSpellCheck
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 getInputHTML
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
30
 getInputOOUI
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
42
 getInputCodex
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace MediaWiki\HTMLForm\Field;
4
5use InvalidArgumentException;
6use MediaWiki\Html\Html;
7use MediaWiki\HTMLForm\HTMLFormField;
8use MediaWiki\MediaWikiServices;
9
10/*
11 * @stable to extend
12 */
13class HTMLTextAreaField extends HTMLFormField {
14    protected const DEFAULT_COLS = 80;
15    protected const DEFAULT_ROWS = 25;
16
17    /** @var string */
18    protected $mPlaceholder = '';
19    /** @var bool */
20    protected $mUseEditFont = false;
21
22    /**
23     * @stable to call
24     *
25     * @param array $params
26     *   - cols, rows: textarea size
27     *   - placeholder/placeholder-message: set HTML placeholder attribute
28     *   - spellcheck: set HTML spellcheck attribute
29     *   - useeditfont: add CSS classes to use the same font as the wikitext editor
30     */
31    public function __construct( $params ) {
32        parent::__construct( $params );
33
34        if ( isset( $params['placeholder-message'] ) ) {
35            $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
36        } elseif ( isset( $params['placeholder'] ) ) {
37            $this->mPlaceholder = $params['placeholder'];
38        }
39
40        if ( isset( $params['useeditfont'] ) ) {
41            $this->mUseEditFont = $params['useeditfont'];
42        }
43    }
44
45    public function getCols() {
46        return $this->mParams['cols'] ?? static::DEFAULT_COLS;
47    }
48
49    public function getRows() {
50        return $this->mParams['rows'] ?? static::DEFAULT_ROWS;
51    }
52
53    public function getSpellCheck() {
54        $val = $this->mParams['spellcheck'] ?? null;
55        if ( is_bool( $val ) ) {
56            // "spellcheck" attribute literally requires "true" or "false" to work.
57            return $val ? 'true' : 'false';
58        }
59        return null;
60    }
61
62    /**
63     * @inheritDoc
64     * @stable to override
65     */
66    public function getInputHTML( $value ) {
67        $classes = [];
68
69        $attribs = [
70                'id' => $this->mID,
71                'cols' => $this->getCols(),
72                'rows' => $this->getRows(),
73                'spellcheck' => $this->getSpellCheck(),
74            ] + $this->getTooltipAndAccessKey();
75
76        if ( $this->mClass !== '' ) {
77            $classes[] = $this->mClass;
78        }
79        if ( $this->mUseEditFont ) {
80            $userOptionsLookup = MediaWikiServices::getInstance()
81                ->getUserOptionsLookup();
82            // The following classes can be used here:
83            // * mw-editfont-monospace
84            // * mw-editfont-sans-serif
85            // * mw-editfont-serif
86            $classes[] =
87                'mw-editfont-' .
88                $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
89            $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
90        }
91        if ( $this->mPlaceholder !== '' ) {
92            $attribs['placeholder'] = $this->mPlaceholder;
93        }
94        if ( $classes ) {
95            $attribs['class'] = $classes;
96        }
97
98        $allowedParams = [
99            'maxlength',
100            'minlength',
101            'tabindex',
102            'disabled',
103            'readonly',
104            'required',
105            'autofocus'
106        ];
107
108        $attribs += $this->getAttributes( $allowedParams );
109        return Html::textarea( $this->mName, $value, $attribs );
110    }
111
112    /**
113     * @inheritDoc
114     * @stable to override
115     */
116    public function getInputOOUI( $value ) {
117        $classes = [];
118
119        if ( isset( $this->mParams['cols'] ) ) {
120            throw new InvalidArgumentException( "OOUIHTMLForm does not support the 'cols' parameter for textareas" );
121        }
122
123        $attribs = $this->getTooltipAndAccessKeyOOUI();
124
125        if ( $this->mClass !== '' ) {
126            $classes[] = $this->mClass;
127        }
128        if ( $this->mUseEditFont ) {
129            $userOptionsLookup = MediaWikiServices::getInstance()
130                ->getUserOptionsLookup();
131            // The following classes can be used here:
132            // * mw-editfont-monospace
133            // * mw-editfont-sans-serif
134            // * mw-editfont-serif
135            $classes[] =
136                'mw-editfont-' .
137                $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
138            $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
139        }
140        if ( $this->mPlaceholder !== '' ) {
141            $attribs['placeholder'] = $this->mPlaceholder;
142        }
143        if ( count( $classes ) ) {
144            $attribs['classes'] = $classes;
145        }
146
147        $allowedParams = [
148            'maxlength',
149            'minlength',
150            'tabindex',
151            'disabled',
152            'readonly',
153            'required',
154            'autofocus',
155        ];
156
157        $attribs += \OOUI\Element::configFromHtmlAttributes(
158            $this->getAttributes( $allowedParams )
159        );
160
161        return new \OOUI\MultilineTextInputWidget( [
162            'id' => $this->mID,
163            'name' => $this->mName,
164            'value' => $value,
165            'rows' => $this->getRows(),
166        ] + $attribs );
167    }
168
169    public function getInputCodex( $value, $hasErrors ) {
170        $textareaClasses = [ 'cdx-text-area__textarea' ];
171        if ( $this->mClass !== '' ) {
172            $textareaClasses[] = $this->mClass;
173        }
174        if ( $this->mUseEditFont ) {
175            $userOptionsLookup = MediaWikiServices::getInstance()
176                ->getUserOptionsLookup();
177            // The following classes can be used here:
178            // * mw-editfont-monospace
179            // * mw-editfont-sans-serif
180            // * mw-editfont-serif
181            $textareaClasses[] =
182                'mw-editfont-' .
183                $userOptionsLookup->getOption( $this->mParent->getUser(), 'editfont' );
184            $this->mParent->getOutput()->addModuleStyles( 'mediawiki.editfont.styles' );
185        }
186
187        $textareaAttribs = [
188            'id' => $this->mID,
189            'cols' => $this->getCols(),
190            'rows' => $this->getRows(),
191            'spellcheck' => $this->getSpellCheck(),
192            'class' => $textareaClasses
193        ] + $this->getTooltipAndAccessKey();
194
195        if ( $this->mPlaceholder !== '' ) {
196            $textareaAttribs['placeholder'] = $this->mPlaceholder;
197        }
198
199        $allowedParams = [
200            'maxlength',
201            'minlength',
202            'tabindex',
203            'disabled',
204            'readonly',
205            'required',
206            'autofocus'
207        ];
208        $textareaAttribs += $this->getAttributes( $allowedParams );
209
210        $textarea = Html::textarea( $this->mName, $value, $textareaAttribs );
211
212        $wrapperAttribs = [ 'class' => [ 'cdx-text-area' ] ];
213        if ( $hasErrors ) {
214            $wrapperAttribs['class'][] = 'cdx-text-area--status-error';
215        }
216        return Html::rawElement(
217            'div',
218            $wrapperAttribs,
219            $textarea
220        );
221    }
222}
223
224/** @deprecated class alias since 1.42 */
225class_alias( HTMLTextAreaField::class, 'HTMLTextAreaField' );