http://www.mediawiki.org/wiki/Special:Code/MediaWiki/94237
Revision: 94237
Author: krinkle
Date: 2011-08-11 12:36:00 +0000 (Thu, 11 Aug 2011)
Log Message:
-----------
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)
Modified Paths:
--------------
trunk/phase3/resources/mediawiki/mediawiki.Title.js
trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js
trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js
Modified: trunk/phase3/resources/mediawiki/mediawiki.Title.js
===================================================================
--- trunk/phase3/resources/mediawiki/mediawiki.Title.js 2011-08-11 11:09:53 UTC
(rev 94236)
+++ trunk/phase3/resources/mediawiki/mediawiki.Title.js 2011-08-11 12:36:00 UTC
(rev 94237)
@@ -120,14 +120,20 @@
* @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 @@
* @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 + '"' );
}
Modified: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js
===================================================================
--- trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js
2011-08-11 11:09:53 UTC (rev 94236)
+++ trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js
2011-08-11 12:36:00 UTC (rev 94237)
@@ -166,6 +166,11 @@
.byteLimit( 6, function( val ) {
_titleConfig();
+ // Invalid title
+ if ( val == '' ) {
+ return '';
+ }
+
// Return without namespace prefix
return new mw.Title( '' + val ).getMain();
} ),
@@ -185,6 +190,11 @@
.byteLimit( function( val ) {
_titleConfig();
+ // Invalid title
+ if ( val === '' ) {
+ return '';
+ }
+
// Return without namespace prefix
return new mw.Title( '' + val ).getMain();
} ),
Modified: trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js
===================================================================
--- trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js
2011-08-11 11:09:53 UTC (rev 94236)
+++ trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js
2011-08-11 12:36:00 UTC (rev 94237)
@@ -115,6 +115,17 @@
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();
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs