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