mediawiki.Title fix for IE.
authorKrinkle <krinkle@users.mediawiki.org>
Thu, 11 Aug 2011 12:36:00 +0000 (12:36 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Thu, 11 Aug 2011 12:36:00 +0000 (12:36 +0000)
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
tests/qunit/suites/resources/jquery/jquery.byteLimit.js
tests/qunit/suites/resources/mediawiki/mediawiki.Title.js

index 1412eb8..34f85ab 100644 (file)
@@ -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 + '"' );
                }
index e450e84..1d8ca9b 100644 (file)
@@ -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();
                } ),
index 82a1ebc..58cb7e2 100644 (file)
@@ -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();