r90089 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90088‎ | r90089 | r90090 >
Date:21:20, 14 June 2011
Author:krinkle
Status:ok
Tags:
Comment:
makeCollapsible fix for jQuery 1.6.1
* Pre 1.6, jQuery returned an empty string for attributes that were not set. Although in a way that was wrong, it was the way it was. From jQuery documentation: "As of jQuery 1.6, the .attr() method returns <code>undefined</code> for attributes that have not been set." [1]
** Fixing makeCollapsible by removing empty-string checks and casting to boolean instead
* Wrapped a long line

[1] http://api.jquery.com/attr/

(jQuery 1.6.1 upgrade was in r89866)
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.makeCollapsible.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.makeCollapsible.js
@@ -211,17 +211,24 @@
212212 };
213213
214214 // Use custom text or default ?
215 - if( !collapsetext || collapsetext === '' ){
 215+ if( !collapsetext ) {
216216 collapsetext = mw.msg( 'collapsible-collapse' );
217217 }
218 - if ( !expandtext || expandtext === '' ){
 218+ if ( !expandtext ) {
219219 expandtext = mw.msg( 'collapsible-expand' );
220220 }
221221
222222 // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
223 - var $toggleLink = $( '<a href="#"></a>' ).text( collapsetext ).wrap( '<span class="mw-collapsible-toggle"></span>' ).parent().prepend( '&nbsp;[' ).append( ']&nbsp;' ).bind( 'click.mw-collapse', function(e){
224 - toggleLinkDefault( this, e );
225 - } );
 223+ var $toggleLink =
 224+ $( '<a href="#"></a>' )
 225+ .text( collapsetext )
 226+ .wrap( '<span class="mw-collapsible-toggle"></span>' )
 227+ .parent()
 228+ .prepend( '&nbsp;[' )
 229+ .append( ']&nbsp;' )
 230+ .bind( 'click.mw-collapse', function(e) {
 231+ toggleLinkDefault( this, e );
 232+ } );
226233
227234 // Return if it has been enabled already.
228235 if ( $that.hasClass( 'mw-made-collapsible' ) ) {
@@ -233,7 +240,7 @@
234241 // Check if this element has a custom position for the toggle link
235242 // (ie. outside the container or deeper inside the tree)
236243 // Then: Locate the custom toggle link(s) and bind them
237 - if ( $that.attr( 'id' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
 244+ if ( ( $that.attr( 'id' ) || '' ).indexOf( 'mw-customcollapsible-' ) === 0 ) {
238245
239246 var thatId = $that.attr( 'id' ),
240247 $customTogglers = $( '.' + thatId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
@@ -268,7 +275,7 @@
269276 if ( !$toggle.length ) {
270277 $firstRowCells.eq(-1).prepend( $toggleLink );
271278 } else {
272 - $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
 279+ $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
273280 toggleLinkPremade( $toggle, e );
274281 } );
275282 }
@@ -280,15 +287,16 @@
281288
282289 // If theres no toggle link, add it
283290 if ( !$toggle.length ) {
284 - // Make sure the numeral order doesn't get messed up, reset to 1 unless value-attribute is already used
285 - // WebKit return '' if no value, Mozilla returns '-1' is no value.
286 - // Needs ==, will fail with ===
287 - if ( $firstItem.attr( 'value' ) == '' || $firstItem.attr( 'value' ) == '-1' ) {
 291+ // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
 292+ // to be "1". Except if the value-attribute is already used.
 293+ // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
 294+ var firstval = $firstItem.attr( 'value' );
 295+ if ( firstval === undefined || !firstval || firstval == '-1' ) {
288296 $firstItem.attr( 'value', '1' );
289297 }
290298 $that.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
291299 } else {
292 - $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
 300+ $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
293301 toggleLinkPremade( $toggle, e );
294302 } );
295303 }
@@ -307,7 +315,7 @@
308316 if ( !$toggle.length ) {
309317 $that.prepend( $toggleLink );
310318 } else {
311 - $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ){
 319+ $toggleLink = $toggle.unbind( 'click.mw-collapse' ).bind( 'click.mw-collapse', function( e ) {
312320 toggleLinkPremade( $toggle, e );
313321 } );
314322 }
@@ -315,7 +323,7 @@
316324 }
317325
318326 // Initial state (only for those that are not custom)
319 - if ( $that.hasClass( 'mw-collapsed' ) && $that.attr( 'id' ).indexOf( 'mw-customcollapsible-' ) !== 0 ) {
 327+ if ( $that.hasClass( 'mw-collapsed' ) && ( $that.attr( 'id' ) || '').indexOf( 'mw-customcollapsible-' ) !== 0 ) {
320328 $that.removeClass( 'mw-collapsed' );
321329 // The collapsible element could have multiple togglers
322330 // To toggle the initial state only click one of them (ie. the first one, eq(0) )

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r89866Upgrade jQuery from 1.4.4 to 1.6.1...krinkle00:25, 11 June 2011

Status & tagging log