From f3c8623ee7a8e231f46b9505b10d44156f38a601 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Thu, 11 Aug 2011 12:36:00 +0000 Subject: [PATCH] mediawiki.Title fix for IE. In normal browsers the matches-array contains null/undefined if there's no match, IE returns an empty string. Changing the checks to really validate that it's a non-empty string, which means that from now on mw.Title will also throw an error on "new mw.Title('');", which makes it consistent with the PHP backend (Title::newFromText('') return null instead of an object). Adding unit test to make sure this behavior is tracked from now on. The _name and _ext properties are either left to their default (null) or set to a valid value. So reverting the checks from r94066, and instead checking for empty string inside the byteLimit callback, that way mw.Title will not get the empty string in the first place. (Follows-up r94066 CR) --- resources/mediawiki/mediawiki.Title.js | 28 +++++++++++++------ .../resources/jquery/jquery.byteLimit.js | 10 +++++++ .../resources/mediawiki/mediawiki.Title.js | 11 ++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/resources/mediawiki/mediawiki.Title.js b/resources/mediawiki/mediawiki.Title.js index 1412eb8fd7..34f85abfa4 100644 --- a/resources/mediawiki/mediawiki.Title.js +++ b/resources/mediawiki/mediawiki.Title.js @@ -120,14 +120,20 @@ var Title = function( title, namespace ) { * @return {mw.Title} */ setAll = function( title, s ) { + // In normal browsers the match-array contains null/undefined if there's no match, + // IE returns an empty string. var matches = s.match( /^(?:([^:]+):)?(.*?)(?:\.(\w{1,5}))?$/ ), ns_match = getNsIdByName( matches[1] ); - if ( matches.length && ns_match ) { - if ( matches[1] != null ) { title._ns = ns_match; } - if ( matches[2] != null ) { title._name = fixName( matches[2] ); } - if ( matches[3] != null ) { title._ext = fixExt( matches[3] ); } + + // Namespace must be valid, and title must be a non-empty string. + if ( ns_match && typeof matches[2] === 'string' && matches[2] !== '' ) { + title._ns = ns_match; + title._name = fixName( matches[2] ); + if ( typeof matches[3] === 'string' && matches[3] !== '' ) { + title._ext = fixExt( matches[3] ); + } } else { - // Consistency with MediaWiki: Unknown namespace > fallback to main namespace. + // Consistency with MediaWiki PHP: Unknown namespace -> fallback to main namespace. title._ns = 0; setNameAndExtension( title, s ); } @@ -142,10 +148,16 @@ var Title = function( title, namespace ) { * @return {mw.Title} */ setNameAndExtension = function( title, raw ) { + // In normal browsers the match-array contains null/undefined if there's no match, + // IE returns an empty string. var matches = raw.match( /^(?:)?(.*?)(?:\.(\w{1,5}))?$/ ); - if ( matches.length ) { - if ( matches[1] != null ) { title._name = fixName( matches[1] ); } - if ( matches[2] != null ) { title._ext = fixExt( matches[2] ); } + + // Title must be a non-empty string. + if ( typeof matches[1] === 'string' && matches[1] !== '' ) { + title._name = fixName( matches[1] ); + if ( typeof matches[2] === 'string' && matches[2] !== '' ) { + title._ext = fixExt( matches[2] ); + } } else { throw new Error( 'mw.Title: Could not parse title "' + raw + '"' ); } diff --git a/tests/qunit/suites/resources/jquery/jquery.byteLimit.js b/tests/qunit/suites/resources/jquery/jquery.byteLimit.js index e450e844e3..1d8ca9ba26 100644 --- a/tests/qunit/suites/resources/jquery/jquery.byteLimit.js +++ b/tests/qunit/suites/resources/jquery/jquery.byteLimit.js @@ -166,6 +166,11 @@ byteLimitTest({ .byteLimit( 6, function( val ) { _titleConfig(); + // Invalid title + if ( val == '' ) { + return ''; + } + // Return without namespace prefix return new mw.Title( '' + val ).getMain(); } ), @@ -185,6 +190,11 @@ byteLimitTest({ .byteLimit( function( val ) { _titleConfig(); + // Invalid title + if ( val === '' ) { + return ''; + } + // Return without namespace prefix return new mw.Title( '' + val ).getMain(); } ), diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js index 82a1ebc398..58cb7e2022 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js @@ -115,6 +115,17 @@ test( 'Namespace detection and conversion', function() { equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' ); }); +test( 'Throw error on invalid title', function() { + expect(1); + _titleConfig(); + + raises(function() { + + var title = new mw.Title( '' ); + + }, new RegExp( $.escapeRE( 'Could not parse title' ) ), 'Throw error "Could not parse title" on empty string' ); +}); + test( 'Case-sensivity', function() { expect(3); _titleConfig(); -- 2.20.1