MediaWiki master
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
4
8use OOUI\Widget;
9
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
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
68 public function isPersistent() {
69 if ( isset( $this->mParams['persistent'] ) ) {
70 return $this->mParams['persistent'];
71 }
72 // don't put passwords into the HTML body, they could get cached or otherwise leaked
73 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
74 }
75
80 public function getInputHTML( $value ) {
81 if ( !$this->isPersistent() ) {
82 $value = '';
83 }
84
85 $attribs = [
86 'id' => $this->mID,
87 'name' => $this->mName,
88 'size' => $this->getSize(),
89 'value' => $value,
90 'dir' => $this->mDir,
91 'spellcheck' => $this->getSpellCheck(),
92 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
93
94 if ( $this->mClass !== '' ) {
95 $attribs['class'] = $this->mClass;
96 }
97 if ( $this->mPlaceholder !== '' ) {
98 $attribs['placeholder'] = $this->mPlaceholder;
99 }
100
101 # @todo Enforce pattern, step, required, readonly on the server side as
102 # well
103 $allowedParams = [
104 'type',
105 'min',
106 'max',
107 'step',
108 'title',
109 'maxlength',
110 'minlength',
111 'tabindex',
112 'disabled',
113 'required',
114 'autofocus',
115 'readonly',
116 'autocomplete',
117 // Only used in HTML mode:
118 'pattern',
119 'list',
120 ];
121
122 $attribs += $this->getAttributes( $allowedParams );
123
124 # Extract 'type'
125 $type = $this->getType( $attribs );
126 $isCodexForm = $this->mParent && $this->mParent instanceof CodexHTMLForm;
127 if ( $isCodexForm ) {
128 $class = $attribs['class'] ?? [];
129 if ( is_string( $class ) ) {
130 $attribs['class'] .= ' cdx-text-input__input';
131 } else {
132 $class[] = 'cdx-text-input__input';
133 $attribs['class'] = $class;
134 }
135 }
136 $inputHtml = Html::input( $this->mName, $value, $type, $attribs );
137 return $isCodexForm
138 ? Html::rawElement( 'div', [ 'class' => 'cdx-text-input' ], $inputHtml )
139 : $inputHtml;
140 }
141
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 // Only used in OOUI mode:
209 'autosize',
210 'flags',
211 'indicator',
212 ];
213
214 $attribs += \OOUI\Element::configFromHtmlAttributes(
215 $this->getAttributes( $allowedParams )
216 );
217
218 $type = $this->getType( $attribs );
219 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
220 $attribs['step'] = null;
221 }
222
223 return $this->getInputWidget( [
224 'id' => $this->mID,
225 'name' => $this->mName,
226 'value' => $value,
227 'type' => $type,
228 'dir' => $this->mDir,
229 ] + $attribs );
230 }
231
239 protected function getInputWidget( $params ) {
240 return new \OOUI\TextInputWidget( $params );
241 }
242
249 protected function getDataAttribs() {
250 return [];
251 }
252}
253
255class_alias( HTMLTextField::class, 'HTMLTextField' );
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
bool $autocomplete
HTML autocomplete attribute.
getDataAttribs()
Returns an array of data-* attributes to add to the field.
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:56