MediaWiki master
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
4
7use OOUI\Widget;
8
21 protected $mPlaceholder = '';
22
24 protected $autocomplete;
25
37 public function __construct( $params ) {
38 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
39 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
40 }
41
42 parent::__construct( $params );
43
44 if ( isset( $params['placeholder-message'] ) ) {
45 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
46 } elseif ( isset( $params['placeholder'] ) ) {
47 $this->mPlaceholder = $params['placeholder'];
48 }
49 }
50
55 public function getSize() {
56 return $this->mParams['size'] ?? 45;
57 }
58
62 public function getSpellCheck() {
63 $val = $this->mParams['spellcheck'] ?? null;
64 if ( is_bool( $val ) ) {
65 // "spellcheck" attribute literally requires "true" or "false" to work.
66 return $val ? 'true' : 'false';
67 }
68 return null;
69 }
70
74 public function isPersistent() {
75 if ( isset( $this->mParams['persistent'] ) ) {
76 return $this->mParams['persistent'];
77 }
78 // don't put passwords into the HTML body, they could get cached or otherwise leaked
79 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
80 }
81
86 public function getInputHTML( $value ) {
87 if ( !$this->isPersistent() ) {
88 $value = '';
89 }
90
91 $attribs = [
92 'id' => $this->mID,
93 'name' => $this->mName,
94 'size' => $this->getSize(),
95 'value' => $value,
96 'dir' => $this->mDir,
97 'spellcheck' => $this->getSpellCheck(),
98 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
99
100 if ( $this->mClass !== '' ) {
101 $attribs['class'] = $this->mClass;
102 }
103 if ( $this->mPlaceholder !== '' ) {
104 $attribs['placeholder'] = $this->mPlaceholder;
105 }
106
107 # @todo Enforce pattern, step, required, readonly on the server side as
108 # well
109 $allowedParams = [
110 'type',
111 'min',
112 'max',
113 'step',
114 'title',
115 'maxlength',
116 'minlength',
117 'tabindex',
118 'disabled',
119 'required',
120 'autofocus',
121 'readonly',
122 'autocomplete',
123 'inputmode',
124 // Only used in HTML mode:
125 'pattern',
126 'list',
127 ];
128
129 $attribs += $this->getAttributes( $allowedParams );
130
131 # Extract 'type'
132 $type = $this->getType( $attribs );
133
134 $inputHtml = Html::input( $this->mName, $value, $type, $attribs );
135 return $inputHtml;
136 }
137
142 protected function getType( &$attribs ) {
143 $type = $attribs['type'] ?? 'text';
144 unset( $attribs['type'] );
145
146 # Implement tiny differences between some field variants
147 # here, rather than creating a new class for each one which
148 # is essentially just a clone of this one.
149 if ( isset( $this->mParams['type'] ) ) {
150 switch ( $this->mParams['type'] ) {
151 case 'int':
152 $type = 'number';
153 $attribs['step'] = 1;
154 break;
155 case 'float':
156 $type = 'number';
157 $attribs['step'] = 'any';
158 break;
159 # Pass through
160 case 'email':
161 case 'password':
162 case 'url':
163 $type = $this->mParams['type'];
164 break;
165 case 'textwithbutton':
166 $type = $this->mParams['inputtype'] ?? 'text';
167 break;
168 }
169 }
170
171 return $type;
172 }
173
178 public function getInputOOUI( $value ) {
179 if ( !$this->isPersistent() ) {
180 $value = '';
181 }
182
183 $attribs = $this->getTooltipAndAccessKeyOOUI();
184
185 if ( $this->mClass !== '' ) {
186 $attribs['classes'] = [ $this->mClass ];
187 }
188 if ( $this->mPlaceholder !== '' ) {
189 $attribs['placeholder'] = $this->mPlaceholder;
190 }
191
192 # @todo Enforce pattern, step, required, readonly on the server side as
193 # well
194 $allowedParams = [
195 'type',
196 'min',
197 'max',
198 'step',
199 'title',
200 'maxlength',
201 'minlength',
202 'tabindex',
203 'disabled',
204 'required',
205 'autofocus',
206 'readonly',
207 'autocomplete',
208 'inputmode',
209 // Only used in OOUI mode:
210 'autosize',
211 'flags',
212 'indicator',
213 ];
214
215 $attribs += \OOUI\Element::configFromHtmlAttributes(
216 $this->getAttributes( $allowedParams )
217 );
218
219 $type = $this->getType( $attribs );
220 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
221 $attribs['step'] = null;
222 }
223
224 return $this->getInputWidget( [
225 'id' => $this->mID,
226 'name' => $this->mName,
227 'value' => $value,
228 'type' => $type,
229 'dir' => $this->mDir,
230 ] + $attribs );
231 }
232
233 public function getInputCodex( $value, $hasErrors ) {
234 if ( !$this->isPersistent() ) {
235 $value = '';
236 }
237
238 $attribs = [
239 'id' => $this->mID,
240 'name' => $this->mName,
241 'size' => $this->getSize(),
242 'value' => $value,
243 'dir' => $this->mDir,
244 'spellcheck' => $this->getSpellCheck(),
245 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
246
247 if ( $this->mPlaceholder !== '' ) {
248 $attribs['placeholder'] = $this->mPlaceholder;
249 }
250 $attribs['class'] = $this->mClass ? [ $this->mClass ] : [];
251
252 $allowedParams = [
253 'type',
254 'min',
255 'max',
256 'step',
257 'title',
258 'maxlength',
259 'minlength',
260 'tabindex',
261 'disabled',
262 'required',
263 'autofocus',
264 'readonly',
265 'autocomplete',
266 'inputmode',
267 'pattern',
268 'list',
269 ];
270
271 $attribs += $this->getAttributes( $allowedParams );
272
273 // Extract 'type'.
274 $type = $this->getType( $attribs );
275
276 return static::buildCodexComponent( $value, $hasErrors, $type, $this->mName, $attribs );
277 }
278
289 public static function buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs ) {
290 // Set up classes for the outer <div>.
291 $wrapperClass = [ 'cdx-text-input' ];
292 if ( $hasErrors ) {
293 $wrapperClass[] = 'cdx-text-input--status-error';
294 }
295
296 $inputAttribs['class'][] = 'cdx-text-input__input';
297 $inputHtml = Html::input( $name, $value, $type, $inputAttribs );
298
299 return Html::rawElement( 'div', [ 'class' => $wrapperClass ], $inputHtml );
300 }
301
309 protected function getInputWidget( $params ) {
310 return new \OOUI\TextInputWidget( $params );
311 }
312
319 protected function getDataAttribs() {
320 return [];
321 }
322}
323
325class_alias( HTMLTextField::class, 'HTMLTextField' );
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.
bool $autocomplete
HTML autocomplete attribute.
getDataAttribs()
Returns an array of data-* attributes to add to the field.
static buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs)
Build the markup of the Codex component.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
The parent class to generate form fields.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
getTooltipAndAccessKeyOOUI()
Returns the attributes required for the tooltip and accesskey, for OOUI widgets' config.
getAttributes(array $list)
Returns the given attributes from the parameters.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57