Add flag to HTMLTextField to not persist submitted data
[lhc/web/wiklou.git] / includes / htmlform / HTMLTextField.php
1 <?php
2
3 class HTMLTextField extends HTMLFormField {
4 function getSize() {
5 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
6 }
7
8 function getSpellCheck() {
9 $val = isset( $this->mParams['spellcheck'] ) ? $this->mParams['spellcheck'] : null;
10 if ( is_bool( $val ) ) {
11 // "spellcheck" attribute literally requires "true" or "false" to work.
12 return $val === true ? 'true' : 'false';
13 }
14 return null;
15 }
16
17 public function isPersistent() {
18 if ( isset( $this->mParams['persistent'] ) ) {
19 return $this->mParams['persistent'];
20 }
21 // don't put passwords into the HTML body, they could get cached or otherwise leaked
22 return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
23 }
24
25 function getInputHTML( $value ) {
26 if ( !$this->isPersistent() ) {
27 $value = '';
28 }
29
30 $attribs = [
31 'id' => $this->mID,
32 'name' => $this->mName,
33 'size' => $this->getSize(),
34 'value' => $value,
35 'dir' => $this->mDir,
36 'spellcheck' => $this->getSpellCheck(),
37 ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
38
39 if ( $this->mClass !== '' ) {
40 $attribs['class'] = $this->mClass;
41 }
42
43 # @todo Enforce pattern, step, required, readonly on the server side as
44 # well
45 $allowedParams = [
46 'type',
47 'min',
48 'max',
49 'pattern',
50 'title',
51 'step',
52 'placeholder',
53 'list',
54 'maxlength',
55 'tabindex',
56 'disabled',
57 'required',
58 'autofocus',
59 'multiple',
60 'readonly'
61 ];
62
63 $attribs += $this->getAttributes( $allowedParams );
64
65 # Extract 'type'
66 $type = $this->getType( $attribs );
67 return Html::input( $this->mName, $value, $type, $attribs );
68 }
69
70 protected function getType( &$attribs ) {
71 $type = isset( $attribs['type'] ) ? $attribs['type'] : 'text';
72 unset( $attribs['type'] );
73
74 # Implement tiny differences between some field variants
75 # here, rather than creating a new class for each one which
76 # is essentially just a clone of this one.
77 if ( isset( $this->mParams['type'] ) ) {
78 switch ( $this->mParams['type'] ) {
79 case 'int':
80 $type = 'number';
81 break;
82 case 'float':
83 $type = 'number';
84 $attribs['step'] = 'any';
85 break;
86 # Pass through
87 case 'email':
88 case 'password':
89 case 'file':
90 case 'url':
91 $type = $this->mParams['type'];
92 break;
93 }
94 }
95
96 return $type;
97 }
98
99 function getInputOOUI( $value ) {
100 if ( !$this->isPersistent() ) {
101 $value = '';
102 }
103
104 $attribs = $this->getTooltipAndAccessKey();
105
106 if ( $this->mClass !== '' ) {
107 $attribs['classes'] = [ $this->mClass ];
108 }
109
110 # @todo Enforce pattern, step, required, readonly on the server side as
111 # well
112 $allowedParams = [
113 'autofocus',
114 'autosize',
115 'disabled',
116 'flags',
117 'indicator',
118 'maxlength',
119 'placeholder',
120 'readonly',
121 'required',
122 'tabindex',
123 'type',
124 ];
125
126 $attribs += OOUI\Element::configFromHtmlAttributes(
127 $this->getAttributes( $allowedParams )
128 );
129
130 $type = $this->getType( $attribs );
131
132 return $this->getInputWidget( [
133 'id' => $this->mID,
134 'name' => $this->mName,
135 'value' => $value,
136 'type' => $type,
137 ] + $attribs );
138 }
139
140 protected function getInputWidget( $params ) {
141 return new OOUI\TextInputWidget( $params );
142 }
143
144 /**
145 * Returns an array of data-* attributes to add to the field.
146 *
147 * @return array
148 */
149 protected function getDataAttribs() {
150 return [];
151 }
152 }