Mwalker has uploaded a new change for review. https://gerrit.wikimedia.org/r/62630
Change subject: HTMLForm entity labels are now optional and escaped ...................................................................... HTMLForm entity labels are now optional and escaped Things like checkboxes have no label, yet a label div gets generated anyway. This is annoying when maybe I don't want that empty div hanging around (i.e., it looks like it's part of other option groups when I have left margins on all .mw-input). This patch will now also escape 'label' fields by default. For the old functionality you must now explicitly use the 'label-raw' field. Change-Id: I8f8340911b7495a91c93e7f2eb7c041b2a7f2179 --- M includes/HTMLForm.php M includes/Preferences.php 2 files changed, 53 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/30/62630/1 diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index c08206d..0cda0dc 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -1093,7 +1093,6 @@ $this->mAction = $action; return $this; } - } /** @@ -1110,6 +1109,12 @@ protected $mID; protected $mClass = ''; protected $mDefault; + + /** + * @var bool If true will generate an empty div element with no label + * @since 1.22 + */ + protected $mShowEmptyLabels = true; /** * @var HTMLForm @@ -1203,6 +1208,8 @@ /** * Initialise the object * @param array $params Associative Array. See HTMLForm doc for syntax. + * + * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead * @throws MWException */ function __construct( $params ) { @@ -1221,7 +1228,14 @@ $this->mLabel = wfMessage( $msg, $msgInfo )->parse(); } elseif ( isset( $params['label'] ) ) { - $this->mLabel = $params['label']; + if ( $params['label'] === ' ' ) { + // Apparently some things set   directly and in an odd format + $this->mLabel = ' '; + } else { + $this->mLabel = htmlspecialchars( $params['label'] ); + } + } elseif ( isset( $params['label-raw'] ) ) { + $this->mLabel = $params['label-raw']; } $this->mName = "wp{$params['fieldname']}"; @@ -1265,6 +1279,10 @@ if ( isset( $params['flatlist'] ) ) { $this->mClass .= ' mw-htmlform-flatlist'; + } + + if ( isset( $params['hidelabel'] ) ) { + $this->mShowEmptyLabels = false; } } @@ -1326,9 +1344,14 @@ $cellAttributes = array(); $label = $this->getLabelHtml( $cellAttributes ); + $outerDivClass = array( + 'mw-input', + 'mw-htmlform-nolabel' => ( $label === '' ) + ); + $field = Html::rawElement( 'div', - array( 'class' => 'mw-input' ) + $cellAttributes, + array( 'class' => $outerDivClass ) + $cellAttributes, $inputHtml . "\n$errors" ); $html = Html::rawElement( 'div', @@ -1457,7 +1480,7 @@ } function getLabel() { - return $this->mLabel; + return is_null( $this->mLabel ) ? '' : $this->mLabel; } function getLabelHtml( $cellAttributes = array() ) { @@ -1469,20 +1492,32 @@ $for['for'] = $this->mID; } - $displayFormat = $this->mParent->getDisplayFormat(); - $labelElement = Html::rawElement( 'label', $for, $this->getLabel() ); - - if ( $displayFormat == 'table' ) { - return Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes, - Html::rawElement( 'label', $for, $this->getLabel() ) - ); - } elseif ( $displayFormat == 'div' ) { - return Html::rawElement( 'div', array( 'class' => 'mw-label' ) + $cellAttributes, - Html::rawElement( 'label', $for, $this->getLabel() ) - ); - } else { - return $labelElement; + $labelValue = trim( $this->getLabel() ); + $hasLabel = false; + if ( $labelValue !== ' ' && $labelValue !== '' ) { + $hasLabel = true; } + + $displayFormat = $this->mParent->getDisplayFormat(); + $html = ''; + + if ( $displayFormat === 'table' ) { + $html = Html::rawElement( 'td', array( 'class' => 'mw-label' ) + $cellAttributes, + Html::rawElement( 'label', $for, $labelValue ) + ); + } elseif ( $hasLabel || $this->mShowEmptyLabels ) { + if ( $displayFormat === 'div' ) { + $html = Html::rawElement( + 'div', + array( 'class' => 'mw-label' ) + $cellAttributes, + Html::rawElement( 'label', $for, $labelValue ) + ); + } else { + $html = Html::rawElement( 'label', $for, $labelValue ); + } + } + + return $html; } function getDefault() { diff --git a/includes/Preferences.php b/includes/Preferences.php index 64b48dd..0d765fa 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -674,7 +674,7 @@ 'section' => 'rendering/advancedrendering', 'options' => $stubThresholdOptions, 'size' => 20, - 'label' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay? + 'label-raw' => $context->msg( 'stub-threshold' )->text(), // Raw HTML message. Yay? ); if ( $wgAllowUserCssPrefs ) { -- To view, visit https://gerrit.wikimedia.org/r/62630 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I8f8340911b7495a91c93e7f2eb7c041b2a7f2179 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: wmf/1.22wmf3 Gerrit-Owner: Mwalker <mwal...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits