r57867 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57866‎ | r57867 | r57868 >
Date:19:29, 18 October 2009
Author:btongminh
Status:ok (Comments)
Tags:
Comment:
Update HTMLForm for upcoming Special:Upload rewrite.
* Add support for edittools
* Add support for multipart/form-data
* Set id for wpEditToken
* Add support for tooltip and accesskey
* Allow setting a name for the submit button
* Give sections an id
Modified paths:
  • /trunk/phase3/includes/HTMLForm.php (modified) (history)
  • /trunk/phase3/includes/Linker.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Linker.php
@@ -1938,9 +1938,9 @@
19391939 }
19401940
19411941 /**
1942 - * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
 1942+ * Returns the attributes for the tooltip and access key
19431943 */
1944 - public function tooltipAndAccesskey( $name ) {
 1944+ public function tooltipAndAccesskeyAttribs( $name ) {
19451945 global $wgEnableTooltipsAndAccesskeys;
19461946 if ( !$wgEnableTooltipsAndAccesskeys )
19471947 return '';
@@ -1957,8 +1957,15 @@
19581958 if ( $attribs['accesskey'] === false ) {
19591959 unset( $attribs['accesskey'] );
19601960 }
1961 - return Xml::expandAttributes( $attribs );
 1961+ return $attribs;
19621962 }
 1963+ /**
 1964+ * @deprecated Returns raw bits of HTML, use titleAttrib() and accesskey()
 1965+ */
 1966+ public function tooltipAndAccesskey( $name ) {
 1967+ return Xml::expandAttributes( $this->tooltipAndAccesskeyAttribs( $name ) );
 1968+ }
 1969+
19631970
19641971 /** @deprecated Returns raw bits of HTML, use titleAttrib() */
19651972 public function tooltip( $name, $options = null ) {
Index: trunk/phase3/includes/HTMLForm.php
@@ -63,6 +63,7 @@
6464 'selectorother' => 'HTMLSelectOrOtherField',
6565 'submit' => 'HTMLSubmitField',
6666 'hidden' => 'HTMLHiddenField',
 67+ 'edittools' => 'HTMLEditTools',
6768
6869 # HTMLTextField will output the correct type="" attribute automagically.
6970 # There are about four zillion other HTML 5 input types, like url, but
@@ -83,11 +84,15 @@
8485 protected $mPre = '';
8586 protected $mHeader = '';
8687 protected $mPost = '';
 88+ protected $mId;
8789
8890 protected $mSubmitID;
 91+ protected $mSubmitName;
8992 protected $mSubmitText;
 93+ protected $mSubmitTooltip;
9094 protected $mTitle;
9195
 96+ protected $mUseMultipart = false;
9297 protected $mHiddenFields = array();
9398 protected $mButtons = array();
9499
@@ -112,6 +117,9 @@
113118
114119 $info['name'] = $fieldname;
115120
 121+ if ( isset( $info['type'] ) && $info['type'] == 'file' )
 122+ $this->mUseMultipart = true;
 123+
116124 $field = self::loadInputFromParameters( $info );
117125 $field->mParent = $this;
118126
@@ -287,8 +295,8 @@
288296 $this->mHiddenFields[ $name ] = $value;
289297 }
290298
291 - public function addButton( $name, $value, $id=null ){
292 - $this->mButtons[] = compact( 'name', 'value', 'id' );
 299+ public function addButton( $name, $value, $id=null, $attribs=null ){
 300+ $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' );
293301 }
294302
295303 /**
@@ -327,19 +335,24 @@
328336 function wrapForm( $html ) {
329337
330338 # Include a <fieldset> wrapper for style, if requested.
331 - if( $this->mWrapperLegend !== false ){
 339+ if ( $this->mWrapperLegend !== false ){
332340 $html = Xml::fieldset( $this->mWrapperLegend, $html );
333341 }
334 -
335 - return Html::rawElement(
336 - 'form',
337 - array(
338 - 'action' => $this->getTitle()->getFullURL(),
339 - 'method' => 'post',
340 - 'class' => 'visualClear',
341 - ),
342 - $html
 342+ # Use multipart/form-data
 343+ $encType = $this->mUseMultipart
 344+ ? 'multipart/form-data'
 345+ : 'application/x-www-form-urlencoded';
 346+ # Attributes
 347+ $attribs = array(
 348+ 'action' => $this->getTitle()->getFullURL(),
 349+ 'method' => 'post',
 350+ 'class' => 'visualClear',
 351+ 'enctype' => $encType,
343352 );
 353+ if ( !empty( $this->mId ) )
 354+ $attribs['id'] = $this->mId;
 355+
 356+ return Html::rawElement( 'form', $attribs, $html );
344357 }
345358
346359 /**
@@ -350,7 +363,7 @@
351364 global $wgUser;
352365 $html = '';
353366
354 - $html .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
 367+ $html .= Html::hidden( 'wpEditToken', $wgUser->editToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
355368 $html .= Html::hidden( 'title', $this->getTitle() ) . "\n";
356369
357370 foreach( $this->mHiddenFields as $name => $value ){
@@ -371,6 +384,12 @@
372385
373386 if ( isset( $this->mSubmitID ) )
374387 $attribs['id'] = $this->mSubmitID;
 388+ if ( isset( $this->mSubmitName ) )
 389+ $attribs['name'] = $this->mSubmitName;
 390+ if ( isset( $this->mSubmitTooltip ) ) {
 391+ global $wgUser;
 392+ $attribs += $wgUser->getSkin()->tooltipAndAccessKeyAttribs( $this->mSubmitTooltip );
 393+ }
375394
376395 $attribs['class'] = 'mw-htmlform-submit';
377396
@@ -392,6 +411,8 @@
393412 'name' => $button['name'],
394413 'value' => $button['value']
395414 );
 415+ if ( $button['attribs'] )
 416+ $attrs += $button['attribs'];
396417 if( isset( $button['id'] ) )
397418 $attrs['id'] = $button['id'];
398419 $html .= Html::element( 'input', $attrs );
@@ -467,7 +488,16 @@
468489 ? $this->mSubmitText
469490 : wfMsg( 'htmlform-submit' );
470491 }
 492+
 493+ public function setSubmitName( $name ) {
 494+ $this->mSubmitName = $name;
 495+ }
 496+
 497+ public function setSubmitTooltip( $name ) {
 498+ $this->mSubmitTooltip = $name;
 499+ }
471500
 501+
472502 /**
473503 * Set the id for the submit button.
474504 * @param $t String. FIXME: Integrity is *not* validated
@@ -476,6 +506,9 @@
477507 $this->mSubmitID = $t;
478508 }
479509
 510+ public function setId( $id ) {
 511+ $this->mId = $id;
 512+ }
480513 /**
481514 * Prompt the whole form to be wrapped in a <fieldset>, with
482515 * this text as its <legend> element.
@@ -514,7 +547,7 @@
515548 * TODO: Document
516549 * @param $fields
517550 */
518 - function displaySection( $fields ) {
 551+ function displaySection( $fields, $sectionName = '' ) {
519552 $tableHtml = '';
520553 $subsectionHtml = '';
521554 $hasLeftColumn = false;
@@ -529,7 +562,7 @@
530563 if( $value->getLabel() != '&nbsp;' )
531564 $hasLeftColumn = true;
532565 } elseif ( is_array( $value ) ) {
533 - $section = $this->displaySection( $value );
 566+ $section = $this->displaySection( $value, $key );
534567 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
535568 $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
536569 }
@@ -538,9 +571,13 @@
539572 $classes = array();
540573 if( !$hasLeftColumn ) // Avoid strange spacing when no labels exist
541574 $classes[] = 'mw-htmlform-nolabel';
542 - $classes = implode( ' ', $classes );
 575+ $attribs = array(
 576+ 'classes' => implode( ' ', $classes ),
 577+ );
 578+ if ( $sectionName )
 579+ $attribs['id'] = Sanitizer::escapeId( "mw-htmlform-$sectionName" );
543580
544 - $tableHtml = Html::rawElement( 'table', array( 'class' => $classes ),
 581+ $tableHtml = Html::rawElement( 'table', $attribs,
545582 Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
546583
547584 return $subsectionHtml . "\n" . $tableHtml;
@@ -791,7 +828,20 @@
792829 return null;
793830 }
794831 }
 832+
 833+ /**
 834+ * Returns the attributes required for the tooltip and accesskey.
 835+ *
 836+ * @return array Attributes
 837+ */
 838+ public function getTooltipAndAccessKey() {
 839+ if ( empty( $this->mParams['tooltip'] ) )
 840+ return array();
795841
 842+ global $wgUser;
 843+ return $wgUser->getSkin()->tooltipAndAccessKeyAttribs();
 844+ }
 845+
796846 /**
797847 * flatten an array of options to a single array, for instance,
798848 * a set of <options> inside <optgroups>.
@@ -812,6 +862,7 @@
813863
814864 return $flatOpts;
815865 }
 866+
816867 }
817868
818869 class HTMLTextField extends HTMLFormField {
@@ -829,7 +880,7 @@
830881 'name' => $this->mName,
831882 'size' => $this->getSize(),
832883 'value' => $value,
833 - );
 884+ ) + $this->getTooltipAndAccessKey();
834885
835886 if ( isset( $this->mParams['maxlength'] ) ) {
836887 $attribs['maxlength'] = $this->mParams['maxlength'];
@@ -908,7 +959,7 @@
909960 'name' => $this->mName,
910961 'cols' => $this->getCols(),
911962 'rows' => $this->getRows(),
912 - );
 963+ ) + $this->getTooltipAndAccessKey();
913964
914965
915966 if ( !empty( $this->mParams['disabled'] ) ) {
@@ -996,7 +1047,8 @@
9971048 if ( !empty( $this->mParams['invert'] ) )
9981049 $value = !$value;
9991050
1000 - $attr = array( 'id' => $this->mID );
 1051+ $attr = $this->getTooltipAndAccessKey();
 1052+ $attr['id'] = $this->mID;
10011053 if( !empty( $this->mParams['disabled'] ) ) {
10021054 $attr['disabled'] = 'disabled';
10031055 }
@@ -1329,9 +1381,22 @@
13301382 $this->mParent->addButton(
13311383 $this->mParams['name'],
13321384 $this->mParams['default'],
1333 - isset($this->mParams['id']) ? $this->mParams['id'] : null
 1385+ isset($this->mParams['id']) ? $this->mParams['id'] : null,
 1386+ $this->getTooltipAndAccessKey()
13341387 );
13351388 }
13361389
13371390 public function getInputHTML( $value ){ return ''; }
 1391+}
 1392+
 1393+class HTMLEditTools extends HTMLFormField {
 1394+ public function getInputHTML( $value ) {
 1395+ return '';
 1396+ }
 1397+ public function getTableRow( $value ) {
 1398+ return "<tr><td></td><td class=\"mw-input\">"
 1399+ . '<div class="mw-editTools">'
 1400+ . wfMsgForContent( 'edittools' )
 1401+ . "</div></td></tr>\n";
 1402+ }
13381403 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r63764(bug 19627) Fix regression from r57867 where HTMLForm would output <element c...catrope08:11, 15 March 2010
r76541Fix fail from r57867: tooltip param was never used in HTMLFormbtongminh20:06, 11 November 2010

Comments

#Comment by Reedy (talk | contribs)   23:55, 31 October 2010

For the lazy, what would the direct replacement be for a call to tooltipAndAccesskey?

Status & tagging log