Add flag to HTMLTextField to not persist submitted data
[lhc/web/wiklou.git] / includes / htmlform / HTMLTextField.php
index 157116d..671dfde 100644 (file)
@@ -14,15 +14,27 @@ class HTMLTextField extends HTMLFormField {
                return null;
        }
 
+       public function isPersistent() {
+               if ( isset( $this->mParams['persistent'] ) ) {
+                       return $this->mParams['persistent'];
+               }
+               // don't put passwords into the HTML body, they could get cached or otherwise leaked
+               return !( isset( $this->mParams['type'] ) && $this->mParams['type'] === 'password' );
+       }
+
        function getInputHTML( $value ) {
-               $attribs = array(
+               if ( !$this->isPersistent() ) {
+                       $value = '';
+               }
+
+               $attribs = [
                                'id' => $this->mID,
                                'name' => $this->mName,
                                'size' => $this->getSize(),
                                'value' => $value,
                                'dir' => $this->mDir,
                                'spellcheck' => $this->getSpellCheck(),
-                       ) + $this->getTooltipAndAccessKey();
+                       ] + $this->getTooltipAndAccessKey() + $this->getDataAttribs();
 
                if ( $this->mClass !== '' ) {
                        $attribs['class'] = $this->mClass;
@@ -30,7 +42,7 @@ class HTMLTextField extends HTMLFormField {
 
                # @todo Enforce pattern, step, required, readonly on the server side as
                # well
-               $allowedParams = array(
+               $allowedParams = [
                        'type',
                        'min',
                        'max',
@@ -46,7 +58,7 @@ class HTMLTextField extends HTMLFormField {
                        'autofocus',
                        'multiple',
                        'readonly'
-               );
+               ];
 
                $attribs += $this->getAttributes( $allowedParams );
 
@@ -85,15 +97,19 @@ class HTMLTextField extends HTMLFormField {
        }
 
        function getInputOOUI( $value ) {
+               if ( !$this->isPersistent() ) {
+                       $value = '';
+               }
+
                $attribs = $this->getTooltipAndAccessKey();
 
                if ( $this->mClass !== '' ) {
-                       $attribs['classes'] = array( $this->mClass );
+                       $attribs['classes'] = [ $this->mClass ];
                }
 
                # @todo Enforce pattern, step, required, readonly on the server side as
                # well
-               $allowedParams = array(
+               $allowedParams = [
                        'autofocus',
                        'autosize',
                        'disabled',
@@ -105,25 +121,32 @@ class HTMLTextField extends HTMLFormField {
                        'required',
                        'tabindex',
                        'type',
-               );
+               ];
 
-               $attribs += $this->getAttributes( $allowedParams, array(
-                       'maxlength' => 'maxLength',
-                       'readonly' => 'readOnly',
-                       'tabindex' => 'tabIndex',
-               ) );
+               $attribs += OOUI\Element::configFromHtmlAttributes(
+                       $this->getAttributes( $allowedParams )
+               );
 
                $type = $this->getType( $attribs );
 
-               return $this->getInputWidget( array(
+               return $this->getInputWidget( [
                        'id' => $this->mID,
                        'name' => $this->mName,
                        'value' => $value,
                        'type' => $type,
-               ) + $attribs );
+               ] + $attribs );
        }
 
        protected function getInputWidget( $params ) {
                return new OOUI\TextInputWidget( $params );
        }
+
+       /**
+        * Returns an array of data-* attributes to add to the field.
+        *
+        * @return array
+        */
+       protected function getDataAttribs() {
+               return [];
+       }
 }