r94237 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94236‎ | r94237 | r94238 >
Date:12:36, 11 August 2011
Author:krinkle
Status:resolved (Comments)
Tags:
Comment:
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 (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js (modified) (history)
  • /trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js
@@ -166,6 +166,11 @@
167167 .byteLimit( 6, function( val ) {
168168 _titleConfig();
169169
 170+ // Invalid title
 171+ if ( val == '' ) {
 172+ return '';
 173+ }
 174+
170175 // Return without namespace prefix
171176 return new mw.Title( '' + val ).getMain();
172177 } ),
@@ -185,6 +190,11 @@
186191 .byteLimit( function( val ) {
187192 _titleConfig();
188193
 194+ // Invalid title
 195+ if ( val === '' ) {
 196+ return '';
 197+ }
 198+
189199 // Return without namespace prefix
190200 return new mw.Title( '' + val ).getMain();
191201 } ),
Index: trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js
@@ -115,6 +115,17 @@
116116 equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
117117 });
118118
 119+test( 'Throw error on invalid title', function() {
 120+ expect(1);
 121+ _titleConfig();
 122+
 123+ raises(function() {
 124+
 125+ var title = new mw.Title( '' );
 126+
 127+ }, new RegExp( $.escapeRE( 'Could not parse title' ) ), 'Throw error "Could not parse title" on empty string' );
 128+});
 129+
119130 test( 'Case-sensivity', function() {
120131 expect(3);
121132 _titleConfig();
Index: trunk/phase3/resources/mediawiki/mediawiki.Title.js
@@ -120,14 +120,20 @@
121121 * @return {mw.Title}
122122 */
123123 setAll = function( title, s ) {
 124+ // In normal browsers the match-array contains null/undefined if there's no match,
 125+ // IE returns an empty string.
124126 var matches = s.match( /^(?:([^:]+):)?(.*?)(?:\.(\w{1,5}))?$/ ),
125127 ns_match = getNsIdByName( matches[1] );
126 - if ( matches.length && ns_match ) {
127 - if ( matches[1] != null ) { title._ns = ns_match; }
128 - if ( matches[2] != null ) { title._name = fixName( matches[2] ); }
129 - if ( matches[3] != null ) { title._ext = fixExt( matches[3] ); }
 128+
 129+ // Namespace must be valid, and title must be a non-empty string.
 130+ if ( ns_match && typeof matches[2] === 'string' && matches[2] !== '' ) {
 131+ title._ns = ns_match;
 132+ title._name = fixName( matches[2] );
 133+ if ( typeof matches[3] === 'string' && matches[3] !== '' ) {
 134+ title._ext = fixExt( matches[3] );
 135+ }
130136 } else {
131 - // Consistency with MediaWiki: Unknown namespace > fallback to main namespace.
 137+ // Consistency with MediaWiki PHP: Unknown namespace -> fallback to main namespace.
132138 title._ns = 0;
133139 setNameAndExtension( title, s );
134140 }
@@ -142,10 +148,16 @@
143149 * @return {mw.Title}
144150 */
145151 setNameAndExtension = function( title, raw ) {
 152+ // In normal browsers the match-array contains null/undefined if there's no match,
 153+ // IE returns an empty string.
146154 var matches = raw.match( /^(?:)?(.*?)(?:\.(\w{1,5}))?$/ );
147 - if ( matches.length ) {
148 - if ( matches[1] != null ) { title._name = fixName( matches[1] ); }
149 - if ( matches[2] != null ) { title._ext = fixExt( matches[2] ); }
 155+
 156+ // Title must be a non-empty string.
 157+ if ( typeof matches[1] === 'string' && matches[1] !== '' ) {
 158+ title._name = fixName( matches[1] );
 159+ if ( typeof matches[2] === 'string' && matches[2] !== '' ) {
 160+ title._ext = fixExt( matches[2] );
 161+ }
150162 } else {
151163 throw new Error( 'mw.Title: Could not parse title "' + raw + '"' );
152164 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r94238Oh, right....krinkle12:42, 11 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r94066Adding support for a callback to jquery.byteLimit...krinkle17:02, 8 August 2011

Comments

#Comment by Catrope (talk | contribs)   12:43, 11 August 2011

The new test that checks new mw.Title( ) throws an error succeeds in IE8 and IE9 (the latter seems to have never had the empty-string-instead-of-null bug to begin with) but fails in IE7: http://toolserver.org/~krinkle/testswarm/job/289

#Comment by Catrope (talk | contribs)   12:44, 11 August 2011

...which you fixed a minute ago. My bad.

Status & tagging log