MediaWiki REL1_35
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
3use OOUI\Widget;
4
16 protected $mPlaceholder = '';
17
19 protected $autocomplete;
20
32 public function __construct( $params ) {
33 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
34 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
35 }
36
37 parent::__construct( $params );
38
39 if ( isset( $params['placeholder-message'] ) ) {
40 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
41 } elseif ( isset( $params['placeholder'] ) ) {
42 $this->mPlaceholder = $params['placeholder'];
43 }
44 }
45
50 public function getSize() {
51 return $this->mParams['size'] ?? 45;
52 }
53
54 public function getSpellCheck() {
55 $val = $this->mParams['spellcheck'] ?? null;
56 if ( is_bool( $val ) ) {
57 // "spellcheck" attribute literally requires "true" or "false" to work.
58 return $val === true ? 'true' : 'false';
59 }
60 return null;
61 }
62
63 public function isPersistent() {
64 if ( isset( $this->mParams['persistent'] ) ) {
65 return $this->mParams['persistent'];
66 }
67 // don't put passwords into the HTML body, they could get cached or otherwise leaked
68 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
69 }
70
75 public function getInputHTML( $value ) {
76 if ( !$this->isPersistent() ) {
77 $value = '';
78 }
79
80 $attribs = [
81 'id' => $this->mID,
82 'name' => $this->mName,
83 'size' => $this->getSize(),
84 'value' => $value,
85 'dir' => $this->mDir,
86 'spellcheck' => $this->getSpellCheck(),
87 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
88
89 if ( $this->mClass !== '' ) {
90 $attribs['class'] = $this->mClass;
91 }
92 if ( $this->mPlaceholder !== '' ) {
93 $attribs['placeholder'] = $this->mPlaceholder;
94 }
95
96 # @todo Enforce pattern, step, required, readonly on the server side as
97 # well
98 $allowedParams = [
99 'type',
100 'min',
101 'max',
102 'step',
103 'title',
104 'maxlength',
105 'tabindex',
106 'disabled',
107 'required',
108 'autofocus',
109 'readonly',
110 'autocomplete',
111 // Only used in HTML mode:
112 'pattern',
113 'list',
114 'multiple',
115 ];
116
117 $attribs += $this->getAttributes( $allowedParams );
118
119 # Extract 'type'
120 $type = $this->getType( $attribs );
121 return Html::input( $this->mName, $value, $type, $attribs );
122 }
123
124 protected function getType( &$attribs ) {
125 $type = $attribs['type'] ?? 'text';
126 unset( $attribs['type'] );
127
128 # Implement tiny differences between some field variants
129 # here, rather than creating a new class for each one which
130 # is essentially just a clone of this one.
131 if ( isset( $this->mParams['type'] ) ) {
132 switch ( $this->mParams['type'] ) {
133 case 'int':
134 $type = 'number';
135 $attribs['step'] = 1;
136 break;
137 case 'float':
138 $type = 'number';
139 $attribs['step'] = 'any';
140 break;
141 # Pass through
142 case 'email':
143 case 'password':
144 case 'file':
145 case 'url':
146 $type = $this->mParams['type'];
147 break;
148 case 'textwithbutton':
149 $type = $this->mParams['inputtype'] ?? 'text';
150 break;
151 }
152 }
153
154 return $type;
155 }
156
161 public function getInputOOUI( $value ) {
162 if ( !$this->isPersistent() ) {
163 $value = '';
164 }
165
166 $attribs = $this->getTooltipAndAccessKeyOOUI();
167
168 if ( $this->mClass !== '' ) {
169 $attribs['classes'] = [ $this->mClass ];
170 }
171 if ( $this->mPlaceholder !== '' ) {
172 $attribs['placeholder'] = $this->mPlaceholder;
173 }
174
175 # @todo Enforce pattern, step, required, readonly on the server side as
176 # well
177 $allowedParams = [
178 'type',
179 'min',
180 'max',
181 'step',
182 'title',
183 'maxlength',
184 'tabindex',
185 'disabled',
186 'required',
187 'autofocus',
188 'readonly',
189 'autocomplete',
190 // Only used in OOUI mode:
191 'autosize',
192 'flags',
193 'indicator',
194 ];
195
196 $attribs += OOUI\Element::configFromHtmlAttributes(
197 $this->getAttributes( $allowedParams )
198 );
199
200 // FIXME T150983 downgrade autocomplete
201 if ( isset( $attribs['autocomplete'] ) ) {
202 if ( $attribs['autocomplete'] === 'on' ) {
203 $attribs['autocomplete'] = true;
204 } elseif ( $attribs['autocomplete'] === 'off' ) {
205 $attribs['autocomplete'] = false;
206 } else {
207 unset( $attribs['autocomplete'] );
208 }
209 }
210
211 $type = $this->getType( $attribs );
212 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
213 $attribs['step'] = null;
214 }
215
216 return $this->getInputWidget( [
217 'id' => $this->mID,
218 'name' => $this->mName,
219 'value' => $value,
220 'type' => $type,
221 'dir' => $this->mDir,
222 ] + $attribs );
223 }
224
232 protected function getInputWidget( $params ) {
233 return new OOUI\TextInputWidget( $params );
234 }
235
242 protected function getDataAttribs() {
243 return [];
244 }
245}
The parent class to generate form fields.
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 Stable to override.
getTooltipAndAccessKey()
Returns the attributes required for the tooltip and accesskey, for Html::element() etc.
<input> field.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputWidget( $params)
Stable to override.
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getSize()
Stable to override.
getType(&$attribs)
__construct( $params)
Stable to call.
getDataAttribs()
Returns an array of data-* attributes to add to the field.
bool $autocomplete
HTML autocomplete attribute.