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
234 public function getInputCodex( $value, $hasErrors ) {
235 if ( !$this->isPersistent() ) {
236 $value = '';
237 }
238
239 $attribs = [
240 'id' => $this->mID,
241 'name' => $this->mName,
242 'size' => $this->getSize(),
243 'value' => $value,
244 'dir' => $this->mDir,
245 'spellcheck' => $this->getSpellCheck(),
246 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
247
248 if ( $this->mPlaceholder !== '' ) {
249 $attribs['placeholder'] = $this->mPlaceholder;
250 }
251 $attribs['class'] = $this->mClass ? [ $this->mClass ] : [];
252
253 $allowedParams = [
254 'type',
255 'min',
256 'max',
257 'step',
258 'title',
259 'maxlength',
260 'minlength',
261 'tabindex',
262 'disabled',
263 'required',
264 'autofocus',
265 'readonly',
266 'autocomplete',
267 'inputmode',
268 'pattern',
269 'list',
270 ];
271
272 $attribs += $this->getAttributes( $allowedParams );
273
274 // Extract 'type'.
275 $type = $this->getType( $attribs );
276
277 return static::buildCodexComponent( $value, $hasErrors, $type, $this->mName, $attribs );
278 }
279
290 public static function buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs ) {
291 // Set up classes for the outer <div>.
292 $wrapperClass = [ 'cdx-text-input' ];
293 if ( $hasErrors ) {
294 $wrapperClass[] = 'cdx-text-input--status-error';
295 }
296
297 $inputAttribs['class'][] = 'cdx-text-input__input';
298 $inputHtml = Html::input( $name, $value, $type, $inputAttribs );
299
300 return Html::rawElement( 'div', [ 'class' => $wrapperClass ], $inputHtml );
301 }
302
310 protected function getInputWidget( $params ) {
311 return new \OOUI\TextInputWidget( $params );
312 }
313
320 protected function getDataAttribs() {
321 return [];
322 }
323}
324
326class_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,...
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:43