From 3bf7584c51d6fbe8ac3328fce2ecf80774c31689 Mon Sep 17 00:00:00 2001 From: Jon Robson Date: Sat, 9 Mar 2024 10:27:28 -0800 Subject: [PATCH] Refactor logic for component rendering Result is exactly same, but mediawiki ui markup is centralized to a single function. Bug: T353371 Change-Id: If021e39beb48246985edf40dc94e2442e9fc49ee --- includes/InputBox.php | 103 ++++++++++++++++++--------- tests/parser/inputBoxParserTests.txt | 46 ++++++------ 2 files changed, 91 insertions(+), 58 deletions(-) diff --git a/includes/InputBox.php b/includes/InputBox.php index 1fd8905e..c2a31696 100644 --- a/includes/InputBox.php +++ b/includes/InputBox.php @@ -209,7 +209,7 @@ class InputBox { $htmlOut .= $this->buildTextBox( [ // enable SearchSuggest with mw-searchInput class - 'class' => $this->getLinebreakClasses() . 'mw-searchInput searchboxInput mw-ui-input mw-ui-input-inline', + 'class' => $this->getLinebreakClasses() . 'mw-searchInput searchboxInput', 'name' => 'search', 'type' => $this->mHidden ? 'hidden' : 'text', 'value' => $this->mDefaultText, @@ -291,18 +291,9 @@ class InputBox { ); } else { // Checkbox - $htmlOut .= '
'; - $htmlOut .= Xml::element( 'input', - [ - 'type' => 'checkbox', - 'name' => 'ns' . $i, - 'value' => 1, - 'id' => 'mw-inputbox-ns' . $i . $idRandStr - ] + $checked + $htmlOut .= $this->buildCheckboxInput( + 'ns' . $i, 'mw-inputbox-ns' . $i . $idRandStr, "1", $checked ); - // Label - $htmlOut .= Xml::label( $name, 'mw-inputbox-ns' . $i . $idRandStr ); - $htmlOut .= '
'; } } @@ -310,11 +301,10 @@ class InputBox { $htmlOut .= $this->mBR; } elseif ( $type === 'search' ) { // Go button - $htmlOut .= Xml::element( 'input', + $htmlOut .= $this->buildSubmitInput( [ 'type' => 'submit', 'name' => 'go', - 'class' => 'mw-ui-button', 'value' => $this->mButtonLabel ] ); @@ -322,11 +312,10 @@ class InputBox { } // Search button - $htmlOut .= Xml::element( 'input', + $htmlOut .= $this->buildSubmitInput( [ 'type' => 'submit', 'name' => 'fulltext', - 'class' => 'mw-ui-button', 'value' => $this->mSearchButtonLabel ] ); @@ -388,29 +377,27 @@ class InputBox { 'type' => $this->mHidden ? 'hidden' : 'text', 'name' => 'search', // enable SearchSuggest with mw-searchInput class - 'class' => 'mw-searchInput mw-ui-input mw-ui-input-inline', + 'class' => 'mw-searchInput', 'size' => $this->mWidth, 'id' => 'bodySearchInput' . $id, 'dir' => $this->mDir, 'placeholder' => $this->mPlaceholderText ] ); - $htmlOut .= "\u{00A0}" . Xml::element( 'input', + $htmlOut .= "\u{00A0}" . $this->buildSubmitInput( [ 'type' => 'submit', 'name' => 'go', 'value' => $this->mButtonLabel, - 'class' => 'mw-ui-button', ] ); // Better testing needed here! if ( $this->mFullTextButton !== '' ) { - $htmlOut .= Xml::element( 'input', + $htmlOut .= $this->buildSubmitInput( [ 'type' => 'submit', 'name' => 'fulltext', - 'class' => 'mw-ui-button', 'value' => $this->mSearchButtonLabel ] ); @@ -491,7 +478,7 @@ class InputBox { 'type' => $this->mHidden ? 'hidden' : 'text', 'name' => 'title', 'class' => $this->getLinebreakClasses() . - 'mw-ui-input mw-ui-input-inline mw-inputbox-createbox', + 'mw-inputbox-createbox', 'value' => $this->mDefaultText, 'placeholder' => $this->mPlaceholderText, // For visible input fields, use required so that the form will not @@ -502,13 +489,13 @@ class InputBox { ] ); $htmlOut .= $this->mBR; - $htmlOut .= Xml::openElement( 'input', + $htmlOut .= $this->buildSubmitInput( [ 'type' => 'submit', 'name' => 'create', - 'class' => 'mw-ui-button mw-ui-progressive', 'value' => $this->mButtonLabel - ] + ], + true ); $htmlOut .= Xml::closeElement( 'form' ); $htmlOut .= Xml::closeElement( 'div' ); @@ -550,7 +537,7 @@ class InputBox { $htmlOut .= $this->buildTextBox( [ 'type' => $this->mHidden ? 'hidden' : 'text', 'name' => 'wpNewTitle', - 'class' => $this->getLinebreakClasses() . 'mw-moveboxInput mw-ui-input mw-ui-input-inline', + 'class' => $this->getLinebreakClasses() . 'mw-moveboxInput', 'value' => $this->mDefaultText, 'placeholder' => $this->mPlaceholderText, 'size' => $this->mWidth, @@ -558,12 +545,12 @@ class InputBox { ] ); $htmlOut .= $this->mBR; - $htmlOut .= Xml::openElement( 'input', + $htmlOut .= $this->buildSubmitInput( [ 'type' => 'submit', - 'class' => 'mw-ui-button mw-ui-progressive', 'value' => $this->mButtonLabel - ] + ], + true ); $htmlOut .= Xml::closeElement( 'form' ); $htmlOut .= Xml::closeElement( 'div' ); @@ -614,7 +601,7 @@ class InputBox { $htmlOut .= $this->buildTextBox( [ 'type' => $this->mHidden ? 'hidden' : 'text', 'name' => 'preloadtitle', - 'class' => $this->getLinebreakClasses() . 'commentboxInput mw-ui-input mw-ui-input-inline', + 'class' => $this->getLinebreakClasses() . 'commentboxInput', 'value' => $this->mDefaultText, 'placeholder' => $this->mPlaceholderText, 'size' => $this->mWidth, @@ -627,13 +614,13 @@ class InputBox { } $htmlOut .= Html::hidden( 'title', $this->mPage ); $htmlOut .= $this->mBR; - $htmlOut .= Xml::openElement( 'input', + $htmlOut .= $this->buildSubmitInput( [ 'type' => 'submit', 'name' => 'create', - 'class' => 'mw-ui-button mw-ui-progressive', 'value' => $this->mButtonLabel - ] + ], + true ); $htmlOut .= Xml::closeElement( 'form' ); $htmlOut .= Xml::closeElement( 'div' ); @@ -756,7 +743,8 @@ REGEX; } /** - * Factory method to help build the textbox widget + * Factory method to help build the textbox widget. + * * @param array $defaultAttr * @return string */ @@ -765,7 +753,52 @@ REGEX; $defaultAttr[ 'aria-label' ] = $this->mTextBoxAriaLabel; } - return Html::openElement( 'input', $defaultAttr ); + $class = $defaultAttr[ 'class' ] ?? ''; + $class .= ' mw-ui-input mw-ui-input-inline'; + $defaultAttr[ 'class' ] = $class; + return Html::element( 'input', $defaultAttr ); + } + + /** + * Factory method to help build checkbox input. + * + * @param string $name name of input + * @param string $id id of input + * @param string $value value of input + * @param array $defaultAttr (optional) + * @return string + */ + private function buildCheckboxInput( $name, $id, $value, $defaultAttr = [] ) { + $htmlOut = '
'; + $htmlOut .= Xml::element( 'input', + [ + 'type' => 'checkbox', + 'name' => $name, + 'value' => $value, + 'id' => $id, + ] + $defaultAttr + ); + // Label + $htmlOut .= Xml::label( $name, $id ); + $htmlOut .= '
'; + return $htmlOut; + } + + /** + * Factory method to help build submit button. + * + * @param array $defaultAttr + * @param bool $isProgressive (optional) + * @return string + */ + private function buildSubmitInput( $defaultAttr, $isProgressive = false ) { + $defaultAttr[ 'class' ] ??= ''; + $defaultAttr[ 'class' ] .= ' mw-ui-button'; + if ( $isProgressive ) { + $defaultAttr[ 'class' ] .= ' mw-ui-progressive'; + } + $defaultAttr[ 'class' ] = trim( $defaultAttr[ 'class' ] ); + return Xml::element( 'input', $defaultAttr ); } private function bgColorStyle() { diff --git a/tests/parser/inputBoxParserTests.txt b/tests/parser/inputBoxParserTests.txt index 68548296..2b794a8d 100644 --- a/tests/parser/inputBoxParserTests.txt +++ b/tests/parser/inputBoxParserTests.txt @@ -11,7 +11,7 @@ InputBox type=search type=search !! html -
+
!! end !! test @@ -21,7 +21,7 @@ InputBox type=create type=create !! html -

+

!! end !! test @@ -32,7 +32,7 @@ type=create minor=1 !! html -

+

!! end !! test @@ -43,7 +43,7 @@ type=create minor=0 !! html -

+

!! end !! test @@ -55,7 +55,7 @@ preloadparams[]=param1 preloadparams[]=param2 !! html -

+

!! end !! test @@ -67,7 +67,7 @@ preloadparams[]= preloadparams[]= !! html -

+

!! end !! test @@ -78,7 +78,7 @@ type=create editintro=MediaWiki:Test !! html -

+

!! end !! test @@ -89,7 +89,7 @@ type=create editintro= !! html -

+

!! end !! test @@ -100,7 +100,7 @@ type=create summary=Summary test !! html -

+

!! end !! test @@ -111,7 +111,7 @@ type=create summary= !! html -

+

!! end !! test @@ -122,7 +122,7 @@ type=create nosummary=true !! html -

+

!! end !! test @@ -133,7 +133,7 @@ type=create nosummary= !! html -

+

!! end !! test @@ -144,7 +144,7 @@ type=create prefix=Test/ !! html -

+

!! end !! test @@ -155,7 +155,7 @@ type=create prefix= !! html -

+

!! end !! test @@ -166,7 +166,7 @@ type=create preload=test !! html -

+

!! end !! test @@ -177,7 +177,7 @@ type=create preload= !! html -

+

!! end !! test @@ -187,7 +187,7 @@ InputBox type=comment type=comment !! html -

+

!! end !! test @@ -197,7 +197,7 @@ InputBox type=commenttitle type=commenttitle !! html -

+

!! end !! test @@ -207,7 +207,7 @@ InputBox type=fulltext type=fulltext !! html -
+
!! end !! test @@ -217,7 +217,7 @@ InputBox type=move type=move !! html -

+

!! end !! test @@ -228,7 +228,7 @@ type=search tour=test !! html -
+
!! end !! test @@ -242,7 +242,7 @@ default=-{sr-Latn: Some latin; sr-Cyrl: Not latin }- placeholder=-{sr-Latn: Latn; sr-Cyrl: Cyrl}- !! html -

+

!! end !! test @@ -253,5 +253,5 @@ type=search arialabel=Search this wiki !! html -
+
!! end