Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.byteLimit.js |
— | — | @@ -166,6 +166,11 @@ |
167 | 167 | .byteLimit( 6, function( val ) { |
168 | 168 | _titleConfig(); |
169 | 169 | |
| 170 | + // Invalid title |
| 171 | + if ( val == '' ) { |
| 172 | + return ''; |
| 173 | + } |
| 174 | + |
170 | 175 | // Return without namespace prefix |
171 | 176 | return new mw.Title( '' + val ).getMain(); |
172 | 177 | } ), |
— | — | @@ -185,6 +190,11 @@ |
186 | 191 | .byteLimit( function( val ) { |
187 | 192 | _titleConfig(); |
188 | 193 | |
| 194 | + // Invalid title |
| 195 | + if ( val === '' ) { |
| 196 | + return ''; |
| 197 | + } |
| 198 | + |
189 | 199 | // Return without namespace prefix |
190 | 200 | return new mw.Title( '' + val ).getMain(); |
191 | 201 | } ), |
Index: trunk/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js |
— | — | @@ -115,6 +115,17 @@ |
116 | 116 | equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' ); |
117 | 117 | }); |
118 | 118 | |
| 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 | + |
119 | 130 | test( 'Case-sensivity', function() { |
120 | 131 | expect(3); |
121 | 132 | _titleConfig(); |
Index: trunk/phase3/resources/mediawiki/mediawiki.Title.js |
— | — | @@ -120,14 +120,20 @@ |
121 | 121 | * @return {mw.Title} |
122 | 122 | */ |
123 | 123 | 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. |
124 | 126 | var matches = s.match( /^(?:([^:]+):)?(.*?)(?:\.(\w{1,5}))?$/ ), |
125 | 127 | 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 | + } |
130 | 136 | } else { |
131 | | - // Consistency with MediaWiki: Unknown namespace > fallback to main namespace. |
| 137 | + // Consistency with MediaWiki PHP: Unknown namespace -> fallback to main namespace. |
132 | 138 | title._ns = 0; |
133 | 139 | setNameAndExtension( title, s ); |
134 | 140 | } |
— | — | @@ -142,10 +148,16 @@ |
143 | 149 | * @return {mw.Title} |
144 | 150 | */ |
145 | 151 | 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. |
146 | 154 | 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 | + } |
150 | 162 | } else { |
151 | 163 | throw new Error( 'mw.Title: Could not parse title "' + raw + '"' ); |
152 | 164 | } |