3 var repeat = function ( input
, multiplier
) {
4 return new Array( multiplier
+ 1 ).join( input
);
7 // See also TitleTest.php#testSecureAndSplit
19 'File_talk:Example.svg',
24 // Length is 256 total, but only title part matters
25 'Category:' + repeat( 'x', 248 ),
33 // Bad characters forbidden regardless of wgLegalTitleChars
45 // XML/HTML character entity references
46 // Note: The ones with # are commented out as those are interpreted as fragment and
47 // as such end up being valid.
51 // Subject of NS_TALK does not roundtrip to NS_MAIN
52 'Talk:File:Example.svg',
53 // Directory navigation
67 // Extension separation is a js invention, for length
68 // purposes it is part of the title
69 repeat( 'x', 252 ) + '.json',
70 // Namespace prefix without actual title
77 QUnit
.module( 'mediawiki.Title', QUnit
.newMwEnvironment( {
78 // mw.Title relies on these three config vars
79 // Restore them after each test run
81 wgFormattedNamespaces
: {
100 // testing custom / localized namespace
126 // Testing custom namespaces and aliases
128 'antarctic_waterfowl': 100
130 wgCaseSensitiveNamespaces
: []
134 QUnit
.test( 'constructor', cases
.invalid
.length
, function ( assert
) {
136 for ( i
= 0; i
< cases
.valid
.length
; i
++ ) {
137 title
= new mw
.Title( cases
.valid
[i
] );
139 for ( i
= 0; i
< cases
.invalid
.length
; i
++ ) {
140 /*jshint loopfunc:true */
141 title
= cases
.invalid
[i
];
142 assert
.throws( function () {
143 return new mw
.Title( title
);
144 }, cases
.invalid
[i
] );
148 QUnit
.test( 'newFromText', cases
.valid
.length
+ cases
.invalid
.length
, function ( assert
) {
150 for ( i
= 0; i
< cases
.valid
.length
; i
++ ) {
152 $.type( mw
.Title
.newFromText( cases
.valid
[i
] ) ),
157 for ( i
= 0; i
< cases
.invalid
.length
; i
++ ) {
159 $.type( mw
.Title
.newFromText( cases
.invalid
[i
] ) ),
166 QUnit
.test( 'Basic parsing', 12, function ( assert
) {
168 title
= new mw
.Title( 'File:Foo_bar.JPG' );
170 assert
.equal( title
.getNamespaceId(), 6 );
171 assert
.equal( title
.getNamespacePrefix(), 'File:' );
172 assert
.equal( title
.getName(), 'Foo_bar' );
173 assert
.equal( title
.getNameText(), 'Foo bar' );
174 assert
.equal( title
.getExtension(), 'JPG' );
175 assert
.equal( title
.getDotExtension(), '.JPG' );
176 assert
.equal( title
.getMain(), 'Foo_bar.JPG' );
177 assert
.equal( title
.getMainText(), 'Foo bar.JPG' );
178 assert
.equal( title
.getPrefixedDb(), 'File:Foo_bar.JPG' );
179 assert
.equal( title
.getPrefixedText(), 'File:Foo bar.JPG' );
181 title
= new mw
.Title( 'Foo#bar' );
182 assert
.equal( title
.getPrefixedText(), 'Foo' );
183 assert
.equal( title
.getFragment(), 'bar' );
186 QUnit
.test( 'Transformation', 11, function ( assert
) {
189 title
= new mw
.Title( 'File:quux pif.jpg' );
190 assert
.equal( title
.getNameText(), 'Quux pif', 'First character of title' );
192 title
= new mw
.Title( 'File:Glarg_foo_glang.jpg' );
193 assert
.equal( title
.getNameText(), 'Glarg foo glang', 'Underscores' );
195 title
= new mw
.Title( 'User:ABC.DEF' );
196 assert
.equal( title
.toText(), 'User:ABC.DEF', 'Round trip text' );
197 assert
.equal( title
.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
199 title
= new mw
.Title( 'Image:quux pix.jpg' );
200 assert
.equal( title
.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
202 title
= new mw
.Title( 'uSEr:hAshAr' );
203 assert
.equal( title
.toText(), 'User:HAshAr' );
204 assert
.equal( title
.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
206 // Don't ask why, it's the way the backend works. One space is kept of each set.
207 title
= new mw
.Title( 'Foo __ \t __ bar' );
208 assert
.equal( title
.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
210 // Regression test: Previously it would only detect an extension if there is no space after it
211 title
= new mw
.Title( 'Example.js ' );
212 assert
.equal( title
.getExtension(), 'js', 'Space after an extension is stripped' );
214 title
= new mw
.Title( 'Example#foo' );
215 assert
.equal( title
.getFragment(), 'foo', 'Fragment' );
217 title
= new mw
.Title( 'Example#_foo_bar baz_' );
218 assert
.equal( title
.getFragment(), ' foo bar baz', 'Fragment' );
221 QUnit
.test( 'Namespace detection and conversion', 10, function ( assert
) {
224 title
= new mw
.Title( 'File:User:Example' );
225 assert
.equal( title
.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
227 title
= new mw
.Title( 'Example', 6 );
228 assert
.equal( title
.getNamespaceId(), 6, 'Default namespace passed is used' );
230 title
= new mw
.Title( 'User:Example', 6 );
231 assert
.equal( title
.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
233 title
= new mw
.Title( ':Example', 6 );
234 assert
.equal( title
.getNamespaceId(), 0, 'Colon forces main namespace' );
236 title
= new mw
.Title( 'something.PDF', 6 );
237 assert
.equal( title
.toString(), 'File:Something.PDF' );
239 title
= new mw
.Title( 'NeilK', 3 );
240 assert
.equal( title
.toString(), 'User_talk:NeilK' );
241 assert
.equal( title
.toText(), 'User talk:NeilK' );
243 title
= new mw
.Title( 'Frobisher', 100 );
244 assert
.equal( title
.toString(), 'Penguins:Frobisher' );
246 title
= new mw
.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
247 assert
.equal( title
.toString(), 'Penguins:Flightless_yet_cute.jpg' );
249 title
= new mw
.Title( 'Penguins:flightless_yet_cute.jpg' );
250 assert
.equal( title
.toString(), 'Penguins:Flightless_yet_cute.jpg' );
253 QUnit
.test( 'Throw error on invalid title', 1, function ( assert
) {
254 assert
.throws( function () {
255 return new mw
.Title( '' );
256 }, 'Throw error on empty string' );
259 QUnit
.test( 'Case-sensivity', 3, function ( assert
) {
263 mw
.config
.set( 'wgCaseSensitiveNamespaces', [] );
265 title
= new mw
.Title( 'article' );
266 assert
.equal( title
.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
268 // $wgCapitalLinks = false;
269 mw
.config
.set( 'wgCaseSensitiveNamespaces', [0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15] );
271 title
= new mw
.Title( 'article' );
272 assert
.equal( title
.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
274 title
= new mw
.Title( 'john', 2 );
275 assert
.equal( title
.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
278 QUnit
.test( 'toString / toText', 2, function ( assert
) {
279 var title
= new mw
.Title( 'Some random page' );
281 assert
.equal( title
.toString(), title
.getPrefixedDb() );
282 assert
.equal( title
.toText(), title
.getPrefixedText() );
285 QUnit
.test( 'getExtension', 7, function ( assert
) {
286 function extTest( pagename
, ext
, description
) {
287 var title
= new mw
.Title( pagename
);
288 assert
.equal( title
.getExtension(), ext
, description
|| pagename
);
291 extTest( 'MediaWiki:Vector.js', 'js' );
292 extTest( 'User:Example/common.css', 'css' );
293 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (bug 36151)' );
294 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
295 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
296 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
297 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
299 // @broken: Throws an exception
300 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
303 QUnit
.test( 'exists', 3, function ( assert
) {
306 // Empty registry, checks default to null
308 title
= new mw
.Title( 'Some random page', 4 );
309 assert
.strictEqual( title
.exists(), null, 'Return null with empty existance registry' );
311 // Basic registry, checks default to boolean
312 mw
.Title
.exist
.set( ['Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules'], true );
313 mw
.Title
.exist
.set( ['Does_not_exist', 'User:John', 'Foobar'], false );
315 title
= new mw
.Title( 'Project:Sandbox rules' );
316 assert
.assertTrue( title
.exists(), 'Return true for page titles marked as existing' );
317 title
= new mw
.Title( 'Foobar' );
318 assert
.assertFalse( title
.exists(), 'Return false for page titles marked as nonexistent' );
322 QUnit
.test( 'getUrl', 3, function ( assert
) {
326 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
328 title
= new mw
.Title( 'Foobar' );
329 assert
.equal( title
.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
330 assert
.equal( title
.getUrl({ action
: 'edit' }), '/wiki/Foobar?action=edit', 'Basic functionality, \'params\' parameter' );
332 title
= new mw
.Title( 'John Doe', 3 );
333 assert
.equal( title
.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
336 QUnit
.test( 'newFromImg', 40, function ( assert
) {
337 var title
, i
, thisCase
, prefix
,
340 url
: '//upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Princess_Alexandra_of_Denmark_%28later_Queen_Alexandra%2C_wife_of_Edward_VII%29_with_her_two_eldest_sons%2C_Prince_Albert_Victor_%28Eddy%29_and_George_Frederick_Ernest_Albert_%28later_George_V%29.jpg/939px-thumbnail.jpg',
341 typeOfUrl
: 'Hashed thumb with shortened path',
342 nameText
: 'Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V)',
343 prefixedText
: 'File:Princess Alexandra of Denmark (later Queen Alexandra, wife of Edward VII) with her two eldest sons, Prince Albert Victor (Eddy) and George Frederick Ernest Albert (later George V).jpg'
346 url
: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg',
347 typeOfUrl
: 'Normal hashed directory thumbnail',
348 nameText
: 'Anticlockwise heliotrope\'s',
349 prefixedText
: 'File:Anticlockwise heliotrope\'s.jpg'
353 url
: '/wiki/images/thumb/8/80/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
354 typeOfUrl
: 'Normal hashed directory thumbnail with complex thumbnail parameters',
355 nameText
: 'Wikipedia-logo-v2',
356 prefixedText
: 'File:Wikipedia-logo-v2.svg'
360 url
: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
361 typeOfUrl
: 'Commons thumbnail',
362 nameText
: 'Wikipedia-logo-v2',
363 prefixedText
: 'File:Wikipedia-logo-v2.svg'
367 url
: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg',
368 typeOfUrl
: 'Full image',
369 nameText
: 'Anticlockwise heliotrope\'s',
370 prefixedText
: 'File:Anticlockwise heliotrope\'s.jpg'
374 url
: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180',
375 typeOfUrl
: 'thumb.php-based thumbnail',
376 nameText
: 'Stuffless Figaro\'s',
377 prefixedText
: 'File:Stuffless Figaro\'s.jpg'
381 url
: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
382 typeOfUrl
: 'Commons unhashed thumbnail',
383 nameText
: 'Wikipedia-logo-v2',
384 prefixedText
: 'File:Wikipedia-logo-v2.svg'
388 url
: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
389 typeOfUrl
: 'Commons unhashed thumbnail with complex thumbnail parameters',
390 nameText
: 'Wikipedia-logo-v2',
391 prefixedText
: 'File:Wikipedia-logo-v2.svg'
395 url
: '/wiki/images/Anticlockwise_heliotrope%27s.jpg',
396 typeOfUrl
: 'Unhashed local file',
397 nameText
: 'Anticlockwise heliotrope\'s',
398 prefixedText
: 'File:Anticlockwise heliotrope\'s.jpg'
403 typeOfUrl
: 'Empty string'
408 typeOfUrl
: 'String with only alphabet characters'
412 url
: 'foobar.foobar',
413 typeOfUrl
: 'Not a file path'
417 url
: '/a/a0/blah blah blah',
418 typeOfUrl
: 'Space characters'
422 for ( i
= 0; i
< cases
.length
; i
++ ) {
424 title
= mw
.Title
.newFromImg( { src
: thisCase
.url
} );
426 if ( thisCase
.nameText
!== undefined ) {
427 prefix
= '[' + thisCase
.typeOfUrl
+ ' URL' + '] ';
429 assert
.notStrictEqual( title
, null, prefix
+ 'Parses successfully' );
430 assert
.equal( title
.getNameText(), thisCase
.nameText
, prefix
+ 'Filename matches original' );
431 assert
.equal( title
.getPrefixedText(), thisCase
.prefixedText
, prefix
+ 'File page title matches original' );
432 assert
.equal( title
.getNamespaceId(), 6, prefix
+ 'Namespace ID matches File namespace' );
434 assert
.strictEqual( title
, null, thisCase
.typeOfUrl
+ ', should not produce an mw.Title object' );
439 QUnit
.test( 'getRelativeText', 5, function ( assert
) {
444 expectedResult
: ':Asd'
449 expectedResult
: 'Dfg'
452 text
: 'Template:Ghj',
454 expectedResult
: 'Template:Ghj'
464 expectedResult
: 'User:Hi'
466 ], i
, thisCase
, title
;
468 for ( i
= 0; i
< cases
.length
; i
++ ) {
471 title
= mw
.Title
.newFromText( thisCase
.text
);
472 assert
.equal( title
.getRelativeText( thisCase
.relativeTo
), thisCase
.expectedResult
);
475 }( mediaWiki
, jQuery
) );