MediaWiki master
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
4
7use OOUI\Widget;
8
21 protected $mPlaceholder = '';
22
34 public function __construct( $params ) {
35 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
36 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
37 }
38
39 parent::__construct( $params );
40
41 if ( isset( $params['placeholder-message'] ) ) {
42 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
43 } elseif ( isset( $params['placeholder'] ) ) {
44 $this->mPlaceholder = $params['placeholder'];
45 }
46 }
47
52 public function getSize() {
53 return $this->mParams['size'] ?? 45;
54 }
55
59 public function getSpellCheck() {
60 $val = $this->mParams['spellcheck'] ?? null;
61 if ( is_bool( $val ) ) {
62 // "spellcheck" attribute literally requires "true" or "false" to work.
63 return $val ? 'true' : 'false';
64 }
65 return null;
66 }
67
71 public function isPersistent() {
72 if ( isset( $this->mParams['persistent'] ) ) {
73 return $this->mParams['persistent'];
74 }
75 // don't put passwords into the HTML body, they could get cached or otherwise leaked
76 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
77 }
78
83 public function getInputHTML( $value ) {
84 if ( !$this->isPersistent() ) {
85 $value = '';
86 }
87
88 $attribs = [
89 'id' => $this->mID,
90 'name' => $this->mName,
91 'size' => $this->getSize(),
92 'value' => $value,
93 'dir' => $this->mDir,
94 'spellcheck' => $this->getSpellCheck(),
95 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
96
97 if ( $this->mClass !== '' ) {
98 $attribs['class'] = $this->mClass;
99 }
100 if ( $this->mPlaceholder !== '' ) {
101 $attribs['placeholder'] = $this->mPlaceholder;
102 }
103
104 # @todo Enforce pattern, step, required, readonly on the server side as
105 # well
106 $allowedParams = [
107 'type',
108 'min',
109 'max',
110 'step',
111 'title',
112 'maxlength',
113 'minlength',
114 'tabindex',
115 'disabled',
116 'required',
117 'autofocus',
118 'readonly',
119 'autocomplete',
120 'inputmode',
121 // Only used in HTML mode:
122 'pattern',
123 'list',
124 ];
125
126 $attribs += $this->getAttributes( $allowedParams );
127
128 # Extract 'type'
129 $type = $this->getType( $attribs );
130
131 $inputHtml = Html::input( $this->mName, $value, $type, $attribs );
132 return $inputHtml;
133 }
134
139 protected function getType( &$attribs ) {
140 $type = $attribs['type'] ?? 'text';
141 unset( $attribs['type'] );
142
143 # Implement tiny differences between some field variants
144 # here, rather than creating a new class for each one which
145 # is essentially just a clone of this one.
146 if ( isset( $this->mParams['type'] ) ) {
147 switch ( $this->mParams['type'] ) {
148 case 'int':
149 $type = 'number';
150 $attribs['step'] = 1;
151 break;
152 case 'float':
153 $type = 'number';
154 $attribs['step'] = 'any';
155 break;
156 # Pass through
157 case 'email':
158 case 'password':
159 case 'url':
160 $type = $this->mParams['type'];
161 break;
162 case 'textwithbutton':
163 $type = $this->mParams['inputtype'] ?? 'text';
164 break;
165 }
166 }
167
168 return $type;
169 }
170
175 public function getInputOOUI( $value ) {
176 if ( !$this->isPersistent() ) {
177 $value = '';
178 }
179
180 $attribs = $this->getTooltipAndAccessKeyOOUI();
181
182 if ( $this->mClass !== '' ) {
183 $attribs['classes'] = [ $this->mClass ];
184 }
185 if ( $this->mPlaceholder !== '' ) {
186 $attribs['placeholder'] = $this->mPlaceholder;
187 }
188
189 # @todo Enforce pattern, step, required, readonly on the server side as
190 # well
191 $allowedParams = [
192 'type',
193 'min',
194 'max',
195 'step',
196 'title',
197 'maxlength',
198 'minlength',
199 'tabindex',
200 'disabled',
201 'required',
202 'autofocus',
203 'readonly',
204 'autocomplete',
205 'inputmode',
206 // Only used in OOUI mode:
207 'autosize',
208 'flags',
209 'indicator',
210 ];
211
212 $attribs += \OOUI\Element::configFromHtmlAttributes(
213 $this->getAttributes( $allowedParams )
214 );
215
216 $type = $this->getType( $attribs );
217 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
218 $attribs['step'] = null;
219 }
220
221 return $this->getInputWidget( [
222 'id' => $this->mID,
223 'name' => $this->mName,
224 'value' => $value,
225 'type' => $type,
226 'dir' => $this->mDir,
227 ] + $attribs );
228 }
229
231 public function getInputCodex( $value, $hasErrors ) {
232 if ( !$this->isPersistent() ) {
233 $value = '';
234 }
235
236 $attribs = [
237 'id' => $this->mID,
238 'name' => $this->mName,
239 'size' => $this->getSize(),
240 'value' => $value,
241 'dir' => $this->mDir,
242 'spellcheck' => $this->getSpellCheck(),
243 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
244
245 if ( $this->mPlaceholder !== '' ) {
246 $attribs['placeholder'] = $this->mPlaceholder;
247 }
248 $attribs['class'] = $this->mClass ? [ $this->mClass ] : [];
249
250 $allowedParams = [
251 'type',
252 'min',
253 'max',
254 'step',
255 'title',
256 'maxlength',
257 'minlength',
258 'tabindex',
259 'disabled',
260 'required',
261 'autofocus',
262 'readonly',
263 'autocomplete',
264 'inputmode',
265 'pattern',
266 'list',
267 ];
268
269 $attribs += $this->getAttributes( $allowedParams );
270
271 // Extract 'type'.
272 $type = $this->getType( $attribs );
273
274 return static::buildCodexComponent( $value, $hasErrors, $type, $this->mName, $attribs );
275 }
276
287 public static function buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs ) {
288 // Set up classes for the outer <div>.
289 $wrapperClass = [ 'cdx-text-input' ];
290 if ( $hasErrors ) {
291 $wrapperClass[] = 'cdx-text-input--status-error';
292 }
293
294 $inputAttribs['class'][] = 'cdx-text-input__input';
295 $inputHtml = Html::input( $name, $value, $type, $inputAttribs );
296
297 return Html::rawElement( 'div', [ 'class' => $wrapperClass ], $inputHtml );
298 }
299
307 protected function getInputWidget( $params ) {
308 return new \OOUI\TextInputWidget( $params );
309 }
310
317 protected function getDataAttribs() {
318 return [];
319 }
320}
321
323class_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.This is called by CodexHTMLForm.If not overridden,...
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:43