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', 21, 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' );
185 title
= new mw
.Title( '.foo' );
186 assert
.equal( title
.getPrefixedText(), '.foo' );
187 assert
.equal( title
.getName(), '' );
188 assert
.equal( title
.getNameText(), '' );
189 assert
.equal( title
.getExtension(), 'foo' );
190 assert
.equal( title
.getDotExtension(), '.foo' );
191 assert
.equal( title
.getMain(), '.foo' );
192 assert
.equal( title
.getMainText(), '.foo' );
193 assert
.equal( title
.getPrefixedDb(), '.foo' );
194 assert
.equal( title
.getPrefixedText(), '.foo' );
197 QUnit
.test( 'Transformation', 11, function ( assert
) {
200 title
= new mw
.Title( 'File:quux pif.jpg' );
201 assert
.equal( title
.getNameText(), 'Quux pif', 'First character of title' );
203 title
= new mw
.Title( 'File:Glarg_foo_glang.jpg' );
204 assert
.equal( title
.getNameText(), 'Glarg foo glang', 'Underscores' );
206 title
= new mw
.Title( 'User:ABC.DEF' );
207 assert
.equal( title
.toText(), 'User:ABC.DEF', 'Round trip text' );
208 assert
.equal( title
.getNamespaceId(), 2, 'Parse canonical namespace prefix' );
210 title
= new mw
.Title( 'Image:quux pix.jpg' );
211 assert
.equal( title
.getNamespacePrefix(), 'File:', 'Transform alias to canonical namespace' );
213 title
= new mw
.Title( 'uSEr:hAshAr' );
214 assert
.equal( title
.toText(), 'User:HAshAr' );
215 assert
.equal( title
.getNamespaceId(), 2, 'Case-insensitive namespace prefix' );
217 // Don't ask why, it's the way the backend works. One space is kept of each set.
218 title
= new mw
.Title( 'Foo __ \t __ bar' );
219 assert
.equal( title
.getMain(), 'Foo_bar', 'Merge multiple types of whitespace/underscores into a single underscore' );
221 // Regression test: Previously it would only detect an extension if there is no space after it
222 title
= new mw
.Title( 'Example.js ' );
223 assert
.equal( title
.getExtension(), 'js', 'Space after an extension is stripped' );
225 title
= new mw
.Title( 'Example#foo' );
226 assert
.equal( title
.getFragment(), 'foo', 'Fragment' );
228 title
= new mw
.Title( 'Example#_foo_bar baz_' );
229 assert
.equal( title
.getFragment(), ' foo bar baz', 'Fragment' );
232 QUnit
.test( 'Namespace detection and conversion', 10, function ( assert
) {
235 title
= new mw
.Title( 'File:User:Example' );
236 assert
.equal( title
.getNamespaceId(), 6, 'Titles can contain namespace prefixes, which are otherwise ignored' );
238 title
= new mw
.Title( 'Example', 6 );
239 assert
.equal( title
.getNamespaceId(), 6, 'Default namespace passed is used' );
241 title
= new mw
.Title( 'User:Example', 6 );
242 assert
.equal( title
.getNamespaceId(), 2, 'Included namespace prefix overrides the given default' );
244 title
= new mw
.Title( ':Example', 6 );
245 assert
.equal( title
.getNamespaceId(), 0, 'Colon forces main namespace' );
247 title
= new mw
.Title( 'something.PDF', 6 );
248 assert
.equal( title
.toString(), 'File:Something.PDF' );
250 title
= new mw
.Title( 'NeilK', 3 );
251 assert
.equal( title
.toString(), 'User_talk:NeilK' );
252 assert
.equal( title
.toText(), 'User talk:NeilK' );
254 title
= new mw
.Title( 'Frobisher', 100 );
255 assert
.equal( title
.toString(), 'Penguins:Frobisher' );
257 title
= new mw
.Title( 'antarctic_waterfowl:flightless_yet_cute.jpg' );
258 assert
.equal( title
.toString(), 'Penguins:Flightless_yet_cute.jpg' );
260 title
= new mw
.Title( 'Penguins:flightless_yet_cute.jpg' );
261 assert
.equal( title
.toString(), 'Penguins:Flightless_yet_cute.jpg' );
264 QUnit
.test( 'Throw error on invalid title', 1, function ( assert
) {
265 assert
.throws( function () {
266 return new mw
.Title( '' );
267 }, 'Throw error on empty string' );
270 QUnit
.test( 'Case-sensivity', 3, function ( assert
) {
274 mw
.config
.set( 'wgCaseSensitiveNamespaces', [] );
276 title
= new mw
.Title( 'article' );
277 assert
.equal( title
.toString(), 'Article', 'Default config: No sensitive namespaces by default. First-letter becomes uppercase' );
279 // $wgCapitalLinks = false;
280 mw
.config
.set( 'wgCaseSensitiveNamespaces', [ 0, -2, 1, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15 ] );
282 title
= new mw
.Title( 'article' );
283 assert
.equal( title
.toString(), 'article', '$wgCapitalLinks=false: Article namespace is sensitive, first-letter case stays lowercase' );
285 title
= new mw
.Title( 'john', 2 );
286 assert
.equal( title
.toString(), 'User:John', '$wgCapitalLinks=false: User namespace is insensitive, first-letter becomes uppercase' );
289 QUnit
.test( 'toString / toText', 2, function ( assert
) {
290 var title
= new mw
.Title( 'Some random page' );
292 assert
.equal( title
.toString(), title
.getPrefixedDb() );
293 assert
.equal( title
.toText(), title
.getPrefixedText() );
296 QUnit
.test( 'getExtension', 7, function ( assert
) {
297 function extTest( pagename
, ext
, description
) {
298 var title
= new mw
.Title( pagename
);
299 assert
.equal( title
.getExtension(), ext
, description
|| pagename
);
302 extTest( 'MediaWiki:Vector.js', 'js' );
303 extTest( 'User:Example/common.css', 'css' );
304 extTest( 'File:Example.longextension', 'longextension', 'Extension parsing not limited (bug 36151)' );
305 extTest( 'Example/information.json', 'json', 'Extension parsing not restricted from any namespace' );
306 extTest( 'Foo.', null, 'Trailing dot is not an extension' );
307 extTest( 'Foo..', null, 'Trailing dots are not an extension' );
308 extTest( 'Foo.a.', null, 'Page name with dots and ending in a dot does not have an extension' );
310 // @broken: Throws an exception
311 // extTest( '.NET', null, 'Leading dot is (or is not?) an extension' );
314 QUnit
.test( 'exists', 3, function ( assert
) {
317 // Empty registry, checks default to null
319 title
= new mw
.Title( 'Some random page', 4 );
320 assert
.strictEqual( title
.exists(), null, 'Return null with empty existance registry' );
322 // Basic registry, checks default to boolean
323 mw
.Title
.exist
.set( [ 'Does_exist', 'User_talk:NeilK', 'Wikipedia:Sandbox_rules' ], true );
324 mw
.Title
.exist
.set( [ 'Does_not_exist', 'User:John', 'Foobar' ], false );
326 title
= new mw
.Title( 'Project:Sandbox rules' );
327 assert
.assertTrue( title
.exists(), 'Return true for page titles marked as existing' );
328 title
= new mw
.Title( 'Foobar' );
329 assert
.assertFalse( title
.exists(), 'Return false for page titles marked as nonexistent' );
333 QUnit
.test( 'getUrl', 3, function ( assert
) {
337 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
339 title
= new mw
.Title( 'Foobar' );
340 assert
.equal( title
.getUrl(), '/wiki/Foobar', 'Basic functionality, getUrl uses mw.util.getUrl' );
341 assert
.equal( title
.getUrl( { action
: 'edit' } ), '/wiki/Foobar?action=edit', 'Basic functionality, \'params\' parameter' );
343 title
= new mw
.Title( 'John Doe', 3 );
344 assert
.equal( title
.getUrl(), '/wiki/User_talk:John_Doe', 'Escaping in title and namespace for urls' );
347 QUnit
.test( 'newFromImg', 40, function ( assert
) {
348 var title
, i
, thisCase
, prefix
,
351 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',
352 typeOfUrl
: 'Hashed thumb with shortened path',
353 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)',
354 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'
357 url
: '/wiki/images/thumb/9/91/Anticlockwise_heliotrope%27s.jpg/99px-Anticlockwise_heliotrope%27s.jpg',
358 typeOfUrl
: 'Normal hashed directory thumbnail',
359 nameText
: 'Anticlockwise heliotrope\'s',
360 prefixedText
: 'File:Anticlockwise heliotrope\'s.jpg'
364 url
: '/wiki/images/thumb/8/80/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
365 typeOfUrl
: 'Normal hashed directory thumbnail with complex thumbnail parameters',
366 nameText
: 'Wikipedia-logo-v2',
367 prefixedText
: 'File:Wikipedia-logo-v2.svg'
371 url
: '//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
372 typeOfUrl
: 'Commons thumbnail',
373 nameText
: 'Wikipedia-logo-v2',
374 prefixedText
: 'File:Wikipedia-logo-v2.svg'
378 url
: '/wiki/images/9/91/Anticlockwise_heliotrope%27s.jpg',
379 typeOfUrl
: 'Full image',
380 nameText
: 'Anticlockwise heliotrope\'s',
381 prefixedText
: 'File:Anticlockwise heliotrope\'s.jpg'
385 url
: 'http://localhost/thumb.php?f=Stuffless_Figaro%27s.jpg&width=180',
386 typeOfUrl
: 'thumb.php-based thumbnail',
387 nameText
: 'Stuffless Figaro\'s',
388 prefixedText
: 'File:Stuffless Figaro\'s.jpg'
392 url
: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/150px-Wikipedia-logo-v2.svg.png',
393 typeOfUrl
: 'Commons unhashed thumbnail',
394 nameText
: 'Wikipedia-logo-v2',
395 prefixedText
: 'File:Wikipedia-logo-v2.svg'
399 url
: '/wikipedia/commons/thumb/Wikipedia-logo-v2.svg/langde-150px-Wikipedia-logo-v2.svg.png',
400 typeOfUrl
: 'Commons unhashed thumbnail with complex thumbnail parameters',
401 nameText
: 'Wikipedia-logo-v2',
402 prefixedText
: 'File:Wikipedia-logo-v2.svg'
406 url
: '/wiki/images/Anticlockwise_heliotrope%27s.jpg',
407 typeOfUrl
: 'Unhashed local file',
408 nameText
: 'Anticlockwise heliotrope\'s',
409 prefixedText
: 'File:Anticlockwise heliotrope\'s.jpg'
414 typeOfUrl
: 'Empty string'
419 typeOfUrl
: 'String with only alphabet characters'
423 url
: 'foobar.foobar',
424 typeOfUrl
: 'Not a file path'
428 url
: '/a/a0/blah blah blah',
429 typeOfUrl
: 'Space characters'
433 for ( i
= 0; i
< cases
.length
; i
++ ) {
434 thisCase
= cases
[ i
];
435 title
= mw
.Title
.newFromImg( { src
: thisCase
.url
} );
437 if ( thisCase
.nameText
!== undefined ) {
438 prefix
= '[' + thisCase
.typeOfUrl
+ ' URL' + '] ';
440 assert
.notStrictEqual( title
, null, prefix
+ 'Parses successfully' );
441 assert
.equal( title
.getNameText(), thisCase
.nameText
, prefix
+ 'Filename matches original' );
442 assert
.equal( title
.getPrefixedText(), thisCase
.prefixedText
, prefix
+ 'File page title matches original' );
443 assert
.equal( title
.getNamespaceId(), 6, prefix
+ 'Namespace ID matches File namespace' );
445 assert
.strictEqual( title
, null, thisCase
.typeOfUrl
+ ', should not produce an mw.Title object' );
450 QUnit
.test( 'getRelativeText', 5, function ( assert
) {
451 var i
, thisCase
, title
,
456 expectedResult
: ':Asd'
461 expectedResult
: 'Dfg'
464 text
: 'Template:Ghj',
466 expectedResult
: 'Template:Ghj'
476 expectedResult
: 'User:Hi'
480 for ( i
= 0; i
< cases
.length
; i
++ ) {
481 thisCase
= cases
[ i
];
483 title
= mw
.Title
.newFromText( thisCase
.text
);
484 assert
.equal( title
.getRelativeText( thisCase
.relativeTo
), thisCase
.expectedResult
);
488 QUnit
.test( 'newFromUserInput', 8, function ( assert
) {
489 var title
, i
, thisCase
, prefix
,
492 title
: 'DCS0001557854455.JPG',
497 expected
: 'DCS0001557854455.JPG',
498 description
: 'Title in normal namespace without anything invalid but with "file extension"'
501 title
: 'MediaWiki:Msg-awesome',
502 defaultNamespace
: undefined,
503 expected
: 'MediaWiki:Msg-awesome',
504 description
: 'Full title (page in MediaWiki namespace) supplied as string'
507 title
: 'The/Mw/Sound.flac',
508 defaultNamespace
: -2,
509 expected
: 'Media:The-Mw-Sound.flac',
510 description
: 'Page in Media-namespace without explicit options'
513 title
: 'File:The/Mw/Sound.kml',
518 expected
: 'File:The/Mw/Sound.kml',
519 description
: 'Page in File-namespace without explicit options'
523 for ( i
= 0; i
< cases
.length
; i
++ ) {
524 thisCase
= cases
[ i
];
525 title
= mw
.Title
.newFromUserInput( thisCase
.title
, thisCase
.defaultNamespace
, thisCase
.options
);
527 if ( thisCase
.expected
!== undefined ) {
528 prefix
= '[' + thisCase
.description
+ '] ';
530 assert
.notStrictEqual( title
, null, prefix
+ 'Parses successfully' );
531 assert
.equal( title
.toText(), thisCase
.expected
, prefix
+ 'Title as expected' );
533 assert
.strictEqual( title
, null, thisCase
.description
+ ', should not produce an mw.Title object' );
538 QUnit
.test( 'newFromFileName', 62, function ( assert
) {
539 var title
, i
, thisCase
, prefix
,
542 fileName
: 'DCS0001557854455.JPG',
543 typeOfName
: 'Standard camera output',
544 nameText
: 'DCS0001557854455',
545 prefixedText
: 'File:DCS0001557854455.JPG',
546 extensionDesired
: 'jpg'
549 fileName
: 'File:Sample.png',
550 typeOfName
: 'Carrying namespace',
551 nameText
: 'File-Sample',
552 prefixedText
: 'File:File-Sample.png'
555 fileName
: 'Treppe 2222 Test upload.jpg',
556 typeOfName
: 'File name with spaces in it and lower case file extension',
557 nameText
: 'Treppe 2222 Test upload',
558 prefixedText
: 'File:Treppe 2222 Test upload.jpg',
559 extensionDesired
: 'JPG'
562 fileName
: 'I contain a \ttab.jpg',
563 typeOfName
: 'Name containing a tab character',
564 nameText
: 'I contain a tab',
565 prefixedText
: 'File:I contain a tab.jpg'
568 fileName
: 'I_contain multiple__ ___ _underscores.jpg',
569 typeOfName
: 'Name containing multiple underscores',
570 nameText
: 'I contain multiple underscores',
571 prefixedText
: 'File:I contain multiple underscores.jpg'
574 fileName
: 'I like ~~~~~~~~es.jpg',
575 typeOfName
: 'Name containing more than three consecutive tilde characters',
576 nameText
: 'I like ~~es',
577 prefixedText
: 'File:I like ~~es.jpg'
580 fileName
: 'BI\u200EDI.jpg',
581 typeOfName
: 'Name containing BIDI overrides',
583 prefixedText
: 'File:BIDI.jpg'
586 fileName
: '100%ab progress.jpg',
587 typeOfName
: 'File name with URL encoding',
588 nameText
: '100% ab progress',
589 prefixedText
: 'File:100% ab progress.jpg'
592 fileName
: '<([>]):/#.jpg',
593 typeOfName
: 'File name with characters not permitted in titles that are replaced',
594 nameText
: '((()))---',
595 prefixedText
: 'File:((()))---.jpg'
598 fileName
: 'spaces\u0009\u2000\u200A\u200Bx.djvu',
599 typeOfName
: 'File name with different kind of spaces',
600 nameText
: 'Spaces \u200Bx',
601 prefixedText
: 'File:Spaces \u200Bx.djvu'
604 fileName
: 'dot.dot.dot.dot.dotdot',
605 typeOfName
: 'File name with a lot of dots',
606 nameText
: 'Dot.dot.dot.dot',
607 prefixedText
: 'File:Dot.dot.dot.dot.dotdot'
610 fileName
: 'dot. dot ._dot',
611 typeOfName
: 'File name with multiple dots and spaces',
612 nameText
: 'Dot. dot',
613 prefixedText
: 'File:Dot. dot. dot'
616 fileName
: 'dot. dot ._dot',
617 typeOfName
: 'File name with different file extension desired',
618 nameText
: 'Dot. dot . dot',
619 prefixedText
: 'File:Dot. dot . dot.png',
620 extensionDesired
: 'png'
623 fileName
: 'fileWOExt',
624 typeOfName
: 'File W/O extension with extension desired',
625 nameText
: 'FileWOExt',
626 prefixedText
: 'File:FileWOExt.png',
627 extensionDesired
: 'png'
630 fileName
: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂.png',
631 typeOfName
: 'File name longer than 240 bytes',
632 nameText
: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵',
633 prefixedText
: 'File:𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢𠻗𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵𢫕𢭃𢯊𢱑𢱕𢳂𠻹𠻺𠼭𠼮𠽌𠾴𠾼𠿪𡁜𡁯𡁵𡁶𡁻𡃁𡃉𡇙𢃇𢞵.png'
637 typeOfName
: 'Empty string'
641 typeOfName
: 'String with only alphabet characters'
645 for ( i
= 0; i
< cases
.length
; i
++ ) {
646 thisCase
= cases
[ i
];
647 title
= mw
.Title
.newFromFileName( thisCase
.fileName
, thisCase
.extensionDesired
);
649 if ( thisCase
.nameText
!== undefined ) {
650 prefix
= '[' + thisCase
.typeOfName
+ '] ';
652 assert
.notStrictEqual( title
, null, prefix
+ 'Parses successfully' );
653 assert
.equal( title
.getNameText(), thisCase
.nameText
, prefix
+ 'Filename matches original' );
654 assert
.equal( title
.getPrefixedText(), thisCase
.prefixedText
, prefix
+ 'File page title matches original' );
655 assert
.equal( title
.getNamespaceId(), 6, prefix
+ 'Namespace ID matches File namespace' );
657 assert
.strictEqual( title
, null, thisCase
.typeOfName
+ ', should not produce an mw.Title object' );
662 }( mediaWiki
, jQuery
) );