MediaWiki REL1_32
HTMLTextField.php
Go to the documentation of this file.
1<?php
2
12 protected $mPlaceholder = '';
13
15 protected $autocomplete;
16
26 public function __construct( $params ) {
27 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
28 $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
29 }
30
31 parent::__construct( $params );
32
33 if ( isset( $params['placeholder-message'] ) ) {
34 $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
35 } elseif ( isset( $params['placeholder'] ) ) {
36 $this->mPlaceholder = $params['placeholder'];
37 }
38 }
39
40 public function getSize() {
41 return $this->mParams['size'] ?? 45;
42 }
43
44 public function getSpellCheck() {
45 $val = $this->mParams['spellcheck'] ?? null;
46 if ( is_bool( $val ) ) {
47 // "spellcheck" attribute literally requires "true" or "false" to work.
48 return $val === true ? 'true' : 'false';
49 }
50 return null;
51 }
52
53 public function isPersistent() {
54 if ( isset( $this->mParams['persistent'] ) ) {
55 return $this->mParams['persistent'];
56 }
57 // don't put passwords into the HTML body, they could get cached or otherwise leaked
58 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
59 }
60
61 public function getInputHTML( $value ) {
62 if ( !$this->isPersistent() ) {
63 $value = '';
64 }
65
66 $attribs = [
67 'id' => $this->mID,
68 'name' => $this->mName,
69 'size' => $this->getSize(),
70 'value' => $value,
71 'dir' => $this->mDir,
72 'spellcheck' => $this->getSpellCheck(),
73 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
74
75 if ( $this->mClass !== '' ) {
76 $attribs['class'] = $this->mClass;
77 }
78 if ( $this->mPlaceholder !== '' ) {
79 $attribs['placeholder'] = $this->mPlaceholder;
80 }
81
82 # @todo Enforce pattern, step, required, readonly on the server side as
83 # well
84 $allowedParams = [
85 'type',
86 'min',
87 'max',
88 'step',
89 'title',
90 'maxlength',
91 'tabindex',
92 'disabled',
93 'required',
94 'autofocus',
95 'readonly',
96 'autocomplete',
97 // Only used in HTML mode:
98 'pattern',
99 'list',
100 'multiple',
101 ];
102
103 $attribs += $this->getAttributes( $allowedParams );
104
105 # Extract 'type'
106 $type = $this->getType( $attribs );
107 return Html::input( $this->mName, $value, $type, $attribs );
108 }
109
110 protected function getType( &$attribs ) {
111 $type = $attribs['type'] ?? 'text';
112 unset( $attribs['type'] );
113
114 # Implement tiny differences between some field variants
115 # here, rather than creating a new class for each one which
116 # is essentially just a clone of this one.
117 if ( isset( $this->mParams['type'] ) ) {
118 switch ( $this->mParams['type'] ) {
119 case 'int':
120 $type = 'number';
121 $attribs['step'] = 1;
122 break;
123 case 'float':
124 $type = 'number';
125 $attribs['step'] = 'any';
126 break;
127 # Pass through
128 case 'email':
129 case 'password':
130 case 'file':
131 case 'url':
132 $type = $this->mParams['type'];
133 break;
134 }
135 }
136
137 return $type;
138 }
139
140 public function getInputOOUI( $value ) {
141 if ( !$this->isPersistent() ) {
142 $value = '';
143 }
144
146
147 if ( $this->mClass !== '' ) {
148 $attribs['classes'] = [ $this->mClass ];
149 }
150 if ( $this->mPlaceholder !== '' ) {
151 $attribs['placeholder'] = $this->mPlaceholder;
152 }
153
154 # @todo Enforce pattern, step, required, readonly on the server side as
155 # well
156 $allowedParams = [
157 'type',
158 'min',
159 'max',
160 'step',
161 'title',
162 'maxlength',
163 'tabindex',
164 'disabled',
165 'required',
166 'autofocus',
167 'readonly',
168 'autocomplete',
169 // Only used in OOUI mode:
170 'autosize',
171 'flags',
172 'indicator',
173 ];
174
175 $attribs += OOUI\Element::configFromHtmlAttributes(
176 $this->getAttributes( $allowedParams )
177 );
178
179 // FIXME T150983 downgrade autocomplete
180 if ( isset( $attribs['autocomplete'] ) ) {
181 if ( $attribs['autocomplete'] === 'on' ) {
182 $attribs['autocomplete'] = true;
183 } elseif ( $attribs['autocomplete'] === 'off' ) {
184 $attribs['autocomplete'] = false;
185 } else {
186 unset( $attribs['autocomplete'] );
187 }
188 }
189
190 $type = $this->getType( $attribs );
191 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
192 $attribs['step'] = null;
193 }
194
195 return $this->getInputWidget( [
196 'id' => $this->mID,
197 'name' => $this->mName,
198 'value' => $value,
199 'type' => $type,
200 'dir' => $this->mDir,
201 ] + $attribs );
202 }
203
204 protected function getInputWidget( $params ) {
205 return new OOUI\TextInputWidget( $params );
206 }
207
213 protected function getDataAttribs() {
214 return [];
215 }
216}
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.
getType(&$attribs)
__construct( $params)
getDataAttribs()
Returns an array of data-* attributes to add to the field.
bool $autocomplete
HTML autocomplete attribute.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2063
$params