MediaWiki REL1_40
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
4use OOUI\Widget;
5
17 protected $mPlaceholder = '';
18
20 protected $autocomplete;
21
33 public function __construct( $params ) {
34 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
35 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
36 }
37
38 parent::__construct( $params );
39
40 if ( isset( $params['placeholder-message'] ) ) {
41 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
42 } elseif ( isset( $params['placeholder'] ) ) {
43 $this->mPlaceholder = $params['placeholder'];
44 }
45 }
46
51 public function getSize() {
52 return $this->mParams['size'] ?? 45;
53 }
54
55 public function getSpellCheck() {
56 $val = $this->mParams['spellcheck'] ?? null;
57 if ( is_bool( $val ) ) {
58 // "spellcheck" attribute literally requires "true" or "false" to work.
59 return $val ? 'true' : 'false';
60 }
61 return null;
62 }
63
64 public function isPersistent() {
65 if ( isset( $this->mParams['persistent'] ) ) {
66 return $this->mParams['persistent'];
67 }
68 // don't put passwords into the HTML body, they could get cached or otherwise leaked
69 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
70 }
71
76 public function getInputHTML( $value ) {
77 if ( !$this->isPersistent() ) {
78 $value = '';
79 }
80
81 $attribs = [
82 'id' => $this->mID,
83 'name' => $this->mName,
84 'size' => $this->getSize(),
85 'value' => $value,
86 'dir' => $this->mDir,
87 'spellcheck' => $this->getSpellCheck(),
88 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
89
90 if ( $this->mClass !== '' ) {
91 $attribs['class'] = $this->mClass;
92 }
93 if ( $this->mPlaceholder !== '' ) {
94 $attribs['placeholder'] = $this->mPlaceholder;
95 }
96
97 # @todo Enforce pattern, step, required, readonly on the server side as
98 # well
99 $allowedParams = [
100 'type',
101 'min',
102 'max',
103 'step',
104 'title',
105 'maxlength',
106 'minlength',
107 'tabindex',
108 'disabled',
109 'required',
110 'autofocus',
111 'readonly',
112 'autocomplete',
113 // Only used in HTML mode:
114 'pattern',
115 'list',
116 ];
117
118 $attribs += $this->getAttributes( $allowedParams );
119
120 # Extract 'type'
121 $type = $this->getType( $attribs );
122 return Html::input( $this->mName, $value, $type, $attribs );
123 }
124
125 protected function getType( &$attribs ) {
126 $type = $attribs['type'] ?? 'text';
127 unset( $attribs['type'] );
128
129 # Implement tiny differences between some field variants
130 # here, rather than creating a new class for each one which
131 # is essentially just a clone of this one.
132 if ( isset( $this->mParams['type'] ) ) {
133 switch ( $this->mParams['type'] ) {
134 case 'int':
135 $type = 'number';
136 $attribs['step'] = 1;
137 break;
138 case 'float':
139 $type = 'number';
140 $attribs['step'] = 'any';
141 break;
142 # Pass through
143 case 'email':
144 case 'password':
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 'minlength',
185 'tabindex',
186 'disabled',
187 'required',
188 'autofocus',
189 'readonly',
190 'autocomplete',
191 // Only used in OOUI mode:
192 'autosize',
193 'flags',
194 'indicator',
195 ];
196
197 $attribs += OOUI\Element::configFromHtmlAttributes(
198 $this->getAttributes( $allowedParams )
199 );
200
201 $type = $this->getType( $attribs );
202 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
203 $attribs['step'] = null;
204 }
205
206 return $this->getInputWidget( [
207 'id' => $this->mID,
208 'name' => $this->mName,
209 'value' => $value,
210 'type' => $type,
211 'dir' => $this->mDir,
212 ] + $attribs );
213 }
214
222 protected function getInputWidget( $params ) {
223 return new OOUI\TextInputWidget( $params );
224 }
225
232 protected function getDataAttribs() {
233 return [];
234 }
235}
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.
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)
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getType(&$attribs)
__construct( $params)
getDataAttribs()
Returns an array of data-* attributes to add to the field.
bool $autocomplete
HTML autocomplete attribute.
This class is a collection of static functions that serve two purposes:
Definition Html.php:55