2 var formatText, formatParse, formatnumTests, specialCharactersPageName, expectedListUsers,
3 expectedListUsersSitename, expectedLinkPagenamee, expectedEntrypoints,
5 hasOwn = Object.hasOwnProperty;
7 // When the expected result is the same in both modes
8 function assertBothModes( assert, parserArguments, expectedResult, assertMessage ) {
9 assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' );
10 assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' );
13 QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
15 this.originalMwLanguage = mw.language;
16 this.parserDefaults = mw.jqueryMsg.getParserDefaults();
17 mw.jqueryMsg.setParserDefaults( {
20 PAGENAMEE: mw.util.wikiUrlencode( '2 + 2' ),
25 specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
27 expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
28 expectedListUsersSitename = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
30 expectedLinkPagenamee = '<a href="https://example.org/wiki/Foo?bar=baz#val/2_%2B_2">Test</a>';
32 expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
34 formatText = mw.jqueryMsg.getMessageFunction( {
38 formatParse = mw.jqueryMsg.getMessageFunction( {
42 teardown: function () {
43 mw.language = this.originalMwLanguage;
44 mw.jqueryMsg.setParserDefaults( this.parserDefaults );
47 wgArticlePath: '/wiki/$1',
48 // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
56 // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
57 wgFormattedNamespaces: {
60 11: 'Dyskusja szablonu'
63 // Messages that are reused in multiple tests
65 // The values for gender are not significant,
66 // what matters is which of the values is choosen by the parser
67 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
68 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
70 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
71 // See https://phabricator.wikimedia.org/T71993
72 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
73 // Assume the grammar form grammar_case_foo is not valid in any language
74 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
76 'formatnum-msg': '{{formatnum:$1}}',
78 'portal-url': 'Project:Community portal',
79 'see-portal-url': '{{Int:portal-url}} is an important community page.',
81 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
82 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
83 'jquerymsg-test-link-pagenamee': '[https://example.org/wiki/Foo?bar=baz#val/{{PAGENAMEE}} Test]',
85 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
87 'external-link-replace': 'Foo [$1 bar]',
88 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b}} things.',
89 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.',
90 'plural-empty-explicit-form': 'There is me{{PLURAL:$1|0=| and other people}}.'
95 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
96 * to transport the module back to the test. It musn't be overwritten concurrentely.
98 * This function caches the mw.language data to avoid having to request the same module
99 * multiple times. There is more than one test case for any given language.
101 function getMwLanguage( langCode ) {
102 if ( !hasOwn.call( mwLanguageCache, langCode ) ) {
103 mwLanguageCache[ langCode ] = $.ajax( {
104 url: mw.util.wikiScript( 'load' ),
106 skin: mw.config.get( 'skin' ),
108 debug: mw.config.get( 'debug' ),
110 'mediawiki.language.data',
117 } ).then( function () {
121 return mwLanguageCache[ langCode ];
125 * @param {Function[]} tasks List of functions that perform tasks
126 * that may be asynchronous. Invoke the callback parameter when done.
128 function process( tasks ) {
129 /*jshint latedef:false */
131 tasks.splice( 0, tasks.length );
136 // This happens if after the process is completed, one of our callbacks is
137 // invoked. This can happen if a test timed out but the process was still
138 // running. In that case, ignore it. Don't invoke complete() a second time.
141 var task = tasks.shift();
145 // Remove tasks list to indicate the process is final.
152 QUnit.test( 'Replace', 15, function ( assert ) {
153 mw.messages.set( 'simple', 'Foo $1 baz $2' );
155 assert.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
156 assert.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
157 assert.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
159 mw.messages.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
162 formatParse( 'plain-input', 'bar' ),
163 '<foo foo="foo">xbary&lt;</foo>z',
164 'Input is not considered html'
167 mw.messages.set( 'plain-replace', 'Foo $1' );
170 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
171 'Foo <bar bar="bar">&gt;</bar>',
172 'Replacement is not considered html'
175 mw.messages.set( 'object-replace', 'Foo $1' );
178 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
179 'Foo <div class="bar">></div>',
180 'jQuery objects are preserved as raw html'
184 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
185 'Foo <div class="bar">></div>',
186 'HTMLElement objects are preserved as raw html'
190 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
191 'Foo <div class="bar">></div>',
192 'HTMLElement[] arrays are preserved as raw html'
196 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
197 'Foo <a href="http://example.org/?x=y&z">bar</a>',
198 'Href is not double-escaped in wikilink function'
201 formatParse( 'external-link-plural', 1, 'http://example.org' ),
202 'Foo is <a href="http://example.org">one</a> things.',
203 'Link is expanded inside plural and is not escaped html'
206 formatParse( 'external-link-plural', 2, 'http://example.org' ),
207 'Foo <a href=\"http://example.org\">two</a> things.',
208 'Link is expanded inside an explicit plural form and is not escaped html'
211 formatParse( 'external-link-plural', 3 ),
213 'A simple explicit plural form co-existing with complex explicit plural forms'
216 formatParse( 'external-link-plural', 4, 'http://example.org' ),
218 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
221 formatParse( 'external-link-plural', 6, 'http://example.org' ),
222 'Foo are <a href="http://example.org">some</a> things.',
223 'Plural fallback to the "other" plural form'
226 formatParse( 'plural-only-explicit-forms', 2 ),
227 'It is a double room.',
228 'Plural with explicit forms alone.'
232 QUnit.test( 'Plural', 9, function ( assert ) {
233 assert.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
234 assert.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
235 assert.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
236 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
237 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in Wiki', 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
238 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
239 assert.equal( formatParse( 'plural-empty-explicit-form', 0 ), 'There is me.' );
240 assert.equal( formatParse( 'plural-empty-explicit-form', 1 ), 'There is me and other people.' );
241 assert.equal( formatParse( 'plural-empty-explicit-form', 2 ), 'There is me and other people.' );
244 QUnit.test( 'Gender', 15, function ( assert ) {
245 var originalGender = mw.user.options.get( 'gender' );
247 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
248 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
249 mw.user.options.set( 'gender', 'male' );
251 formatParse( 'gender-msg', 'Bob', 'male' ),
253 'Masculine from string "male"'
256 formatParse( 'gender-msg', 'Bob', mw.user ),
258 'Masculine from mw.user object'
261 formatParse( 'gender-msg-currentuser' ),
263 'Masculine for current user'
266 mw.user.options.set( 'gender', 'female' );
268 formatParse( 'gender-msg', 'Alice', 'female' ),
270 'Feminine from string "female"' );
272 formatParse( 'gender-msg', 'Alice', mw.user ),
274 'Feminine from mw.user object'
277 formatParse( 'gender-msg-currentuser' ),
279 'Feminine for current user'
282 mw.user.options.set( 'gender', 'unknown' );
284 formatParse( 'gender-msg', 'Foo', mw.user ),
286 'Neutral from mw.user object' );
288 formatParse( 'gender-msg', 'User' ),
290 'Neutral when no parameter given' );
292 formatParse( 'gender-msg', 'User', 'unknown' ),
294 'Neutral from string "unknown"'
297 formatParse( 'gender-msg-currentuser' ),
299 'Neutral for current user'
302 mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
305 formatParse( 'gender-msg-one-form', 'male', 10 ),
307 'Gender neutral and plural form'
310 formatParse( 'gender-msg-one-form', 'female', 1 ),
312 'Gender neutral and singular form'
315 mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
317 formatParse( 'gender-msg-lowercase', 'male' ),
322 formatParse( 'gender-msg-lowercase', 'female' ),
327 mw.messages.set( 'gender-msg-wrong', '{{gender}} test' );
329 formatParse( 'gender-msg-wrong', 'female' ),
331 'Invalid syntax should result in {{gender}} simply being stripped away'
334 mw.user.options.set( 'gender', originalGender );
337 QUnit.test( 'Case changing', 8, function ( assert ) {
338 mw.messages.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' );
339 assert.equal( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' );
341 mw.messages.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' );
342 assert.equal( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' );
344 mw.messages.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
345 mw.messages.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
346 assert.equal( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' );
347 assert.equal( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' );
349 mw.messages.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
350 mw.messages.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
351 assert.equal( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' );
352 assert.equal( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' );
354 mw.messages.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
355 assert.equal( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' );
356 mw.messages.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
357 assert.equal( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' );
360 QUnit.test( 'Grammar', 2, function ( assert ) {
361 assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' );
363 mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
364 assert.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
367 QUnit.test( 'Match PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
368 mw.messages.set( mw.libs.phpParserData.messages );
369 var tasks = $.map( mw.libs.phpParserData.tests, function ( test ) {
370 var done = assert.async();
371 return function ( next, abort ) {
372 getMwLanguage( test.lang )
373 .then( function ( langClass ) {
374 mw.config.set( 'wgUserLanguage', test.lang );
375 var parser = new mw.jqueryMsg.parser( { language: langClass } );
377 parser.parse( test.key, test.args ).html(),
382 assert.ok( false, 'Language "' + test.lang + '" failed to load.' );
385 .then( next, abort );
392 QUnit.test( 'Links', 15, function ( assert ) {
394 expectedDisambiguationsText,
395 expectedMultipleBars,
396 expectedSpecialCharacters;
398 // The below three are all identical to or based on real messages. For disambiguations-text,
399 // the bold was removed because it is not yet implemented.
402 formatParse( 'jquerymsg-test-statistics-users' ),
407 expectedDisambiguationsText = 'The following pages contain at least one link to a disambiguation page.\nThey may have to link to a more appropriate page instead.\nA page is treated as a disambiguation page if it uses a template that is linked from ' +
408 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
410 mw.messages.set( 'disambiguations-text', 'The following pages contain at least one link to a disambiguation page.\nThey may have to link to a more appropriate page instead.\nA page is treated as a disambiguation page if it uses a template that is linked from [[MediaWiki:Disambiguationspage]].' );
412 formatParse( 'disambiguations-text' ),
413 expectedDisambiguationsText,
414 'Wikilink without pipe'
418 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
423 // Pipe trick is not supported currently, but should not parse as text either.
424 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
425 mw.messages.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
426 mw.messages.set( 'empty-link', '[[]]' );
427 this.suppressWarnings();
429 formatParse( 'pipe-trick' ),
430 '[[Tampa, Florida|]]',
431 'Pipe trick should not be parsed.'
434 formatParse( 'reverse-pipe-trick' ),
435 '[[|Tampa, Florida]]',
436 'Reverse pipe trick should not be parsed.'
439 formatParse( 'empty-link' ),
441 'Empty link should not be parsed.'
443 this.restoreWarnings();
445 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
446 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
448 formatParse( 'multiple-bars' ),
449 expectedMultipleBars,
453 expectedSpecialCharacters = '<a title=""Who" wants to be a millionaire & live on 'Exotic Island'?" href="/wiki/%22Who%22_wants_to_be_a_millionaire_%26_live_on_%27Exotic_Island%27%3F">"Who" wants to be a millionaire & live on 'Exotic Island'?</a>';
455 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
457 formatParse( 'special-characters' ),
458 expectedSpecialCharacters,
462 mw.messages.set( 'leading-colon', '[[:File:Foo.jpg]]' );
464 formatParse( 'leading-colon' ),
465 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
466 'Leading colon in links is stripped'
470 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
471 expectedListUsersSitename,
472 'Piped wikilink with parser function in the text'
476 formatParse( 'jquerymsg-test-link-pagenamee' ),
477 expectedLinkPagenamee,
478 'External link with parser function in the URL'
484 'asd [http://example.org <strong>Example</strong>] asd',
485 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
488 'extlink-html-partial',
489 'asd [http://example.org foo <strong>Example</strong> bar] asd',
490 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
493 'wikilink-html-full',
494 'asd [[Example|<strong>Example</strong>]] asd',
495 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
498 'wikilink-html-partial',
499 'asd [[Example|foo <strong>Example</strong> bar]] asd',
500 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
504 $.each( testCases, function () {
509 mw.messages.set( key, input );
513 'HTML in links: ' + key
518 QUnit.test( 'Replacements in links', 14, function ( assert ) {
521 'extlink-param-href-full',
522 'asd [$1 Example] asd',
523 'asd <a href="http://example.com">Example</a> asd'
526 'extlink-param-href-partial',
527 'asd [$1/example Example] asd',
528 'asd <a href="http://example.com/example">Example</a> asd'
531 'extlink-param-text-full',
532 'asd [http://example.org $2] asd',
533 'asd <a href="http://example.org">Text</a> asd'
536 'extlink-param-text-partial',
537 'asd [http://example.org Example $2] asd',
538 'asd <a href="http://example.org">Example Text</a> asd'
541 'extlink-param-both-full',
543 'asd <a href="http://example.com">Text</a> asd'
546 'extlink-param-both-partial',
547 'asd [$1/example Example $2] asd',
548 'asd <a href="http://example.com/example">Example Text</a> asd'
551 'wikilink-param-href-full',
552 'asd [[$1|Example]] asd',
553 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
556 'wikilink-param-href-partial',
557 'asd [[$1/Test|Example]] asd',
558 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
561 'wikilink-param-text-full',
562 'asd [[Example|$2]] asd',
563 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
566 'wikilink-param-text-partial',
567 'asd [[Example|Example $2]] asd',
568 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
571 'wikilink-param-both-full',
573 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
576 'wikilink-param-both-partial',
577 'asd [[$1/Test|Example $2]] asd',
578 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
581 'wikilink-param-unpiped-full',
583 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
586 'wikilink-param-unpiped-partial',
587 'asd [[$1/Test]] asd',
588 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
592 $.each( testCases, function () {
597 paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
599 mw.messages.set( key, input );
601 formatParse( key, paramHref, paramText ),
603 'Replacements in links: ' + key
608 // Tests that {{-transformation vs. general parsing are done as requested
609 QUnit.test( 'Curly brace transformation', 16, function ( assert ) {
610 var oldUserLang = mw.config.get( 'wgUserLanguage' );
612 assertBothModes( assert, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
614 assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
616 assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
618 mw.config.set( 'wgUserLanguage', 'en' );
619 assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
621 // Test non-{{ wikitext, where behavior differs
625 formatText( 'jquerymsg-test-statistics-users' ),
626 mw.messages.get( 'jquerymsg-test-statistics-users' ),
627 'Internal link message unchanged when format is \'text\''
630 formatParse( 'jquerymsg-test-statistics-users' ),
632 'Internal link message parsed when format is \'parse\''
637 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
638 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
639 'External link message unchanged when format is \'text\''
642 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
644 'External link message processed when format is \'parse\''
647 // External link with parameter
649 formatText( 'external-link-replace', 'http://example.com' ),
650 'Foo [http://example.com bar]',
651 'External link message only substitutes parameter when format is \'text\''
654 formatParse( 'external-link-replace', 'http://example.com' ),
655 'Foo <a href="http://example.com">bar</a>',
656 'External link message processed when format is \'parse\''
659 formatParse( 'external-link-replace', $( '<i>' ) ),
661 'External link message processed as jQuery object when format is \'parse\''
664 formatParse( 'external-link-replace', function () {} ),
665 'Foo <a role="button" tabindex="0">bar</a>',
666 'External link message processed as function when format is \'parse\''
669 mw.config.set( 'wgUserLanguage', oldUserLang );
672 QUnit.test( 'Int', 4, function ( assert ) {
673 var newarticletextSource = 'You have followed a link to a page that does not exist yet. To create the page, start typing in the box below (see the [[{{Int:Foobar}}|foobar]] for more info). If you are here by mistake, click your browser\'s back button.',
674 expectedNewarticletext,
675 helpPageTitle = 'Help:Foobar';
677 mw.messages.set( 'foobar', helpPageTitle );
679 expectedNewarticletext = 'You have followed a link to a page that does not exist yet. To create the page, start typing in the box below (see the ' +
680 '<a title="Help:Foobar" href="/wiki/Help:Foobar">foobar</a> for more info). If you are here by mistake, click your browser\'s back button.';
682 mw.messages.set( 'newarticletext', newarticletextSource );
685 formatParse( 'newarticletext' ),
686 expectedNewarticletext,
687 'Link with nested message'
691 formatParse( 'see-portal-url' ),
692 'Project:Community portal is an important community page.',
696 mw.messages.set( 'newarticletext-lowercase',
697 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
700 formatParse( 'newarticletext-lowercase' ),
701 expectedNewarticletext,
702 'Link with nested message, lowercase include'
705 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
708 formatParse( 'uses-missing-int' ),
710 'int: where nested message does not exist'
714 QUnit.test( 'Ns', 4, function ( assert ) {
715 mw.messages.set( 'ns-template-talk', '{{ns:Template talk}}' );
717 formatParse( 'ns-template-talk' ),
719 'ns: returns localised namespace when used with a canonical namespace name'
722 mw.messages.set( 'ns-10', '{{ns:10}}' );
724 formatParse( 'ns-10' ),
726 'ns: returns localised namespace when used with a namespace number'
729 mw.messages.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
731 formatParse( 'ns-unknown' ),
733 'ns: returns empty string for unknown namespace name'
736 mw.messages.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
738 formatParse( 'ns-in-a-link' ),
739 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
740 'ns: works when used inside a wikilink'
744 // Tests that getMessageFunction is used for non-plain messages with curly braces or
745 // square brackets, but not otherwise.
746 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
747 var oldGMF, outerCalled, innerCalled;
750 'curly-brace': '{{int:message}}',
751 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
752 'double-square-bracket': '[[Some page]]',
753 regular: 'Other message'
756 oldGMF = mw.jqueryMsg.getMessageFunction;
758 mw.jqueryMsg.getMessageFunction = function () {
765 function verifyGetMessageFunction( key, format, shouldCall ) {
769 message = mw.message( key );
771 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
772 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
773 delete mw.messages[ format ];
776 verifyGetMessageFunction( 'curly-brace', 'parse', true );
777 verifyGetMessageFunction( 'curly-brace', 'plain', false );
779 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
780 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
782 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
783 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
785 verifyGetMessageFunction( 'regular', 'parse', false );
786 verifyGetMessageFunction( 'regular', 'plain', false );
788 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
789 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
790 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
792 mw.jqueryMsg.getMessageFunction = oldGMF;
798 number: 987654321.654321,
799 result: '987,654,321.654',
800 description: 'formatnum test for English, decimal separator'
804 number: 987654321.654321,
805 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
806 description: 'formatnum test for Arabic, with decimal separator'
810 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
813 description: 'formatnum test for Arabic, with decimal separator, reverse'
819 description: 'formatnum test for Arabic, negative number'
826 description: 'formatnum test for Arabic, negative number, reverse'
830 number: 987654321.654321,
831 result: '987.654.321,654',
832 description: 'formatnum test for Nederlands, decimal separator'
838 description: 'formatnum test for Nederlands, negative number'
844 description: 'formatnum test for Nederlands'
848 number: 'invalidnumber',
849 result: 'invalidnumber',
850 description: 'formatnum test for Nederlands, invalid number'
854 number: '1000000000',
855 result: '1,00,00,00,000',
856 description: 'formatnum test for Malayalam'
860 number: '-1000000000',
861 result: '-1,00,00,00,000',
862 description: 'formatnum test for Malayalam, negative number'
865 * This will fail because of wrong pattern for ml in MW(different from CLDR)
868 number: '1000000000.000',
869 result: '1,00,00,00,000.000',
870 description: 'formatnum test for Malayalam with decimal place'
875 number: '123456789.123456789',
876 result: '१२,३४,५६,७८९',
877 description: 'formatnum test for Hindi'
881 number: '१२,३४,५६,७८९',
882 result: '१२,३४,५६,७८९',
883 description: 'formatnum test for Hindi, Devanagari digits passed'
890 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
894 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
895 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
896 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
897 var queue = $.map( formatnumTests, function ( test ) {
898 var done = assert.async();
899 return function ( next, abort ) {
900 getMwLanguage( test.lang )
901 .then( function ( langClass ) {
902 mw.config.set( 'wgUserLanguage', test.lang );
903 var parser = new mw.jqueryMsg.parser( { language: langClass } );
905 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
906 [ test.number ] ).html(),
911 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
914 .then( next, abort );
921 QUnit.test( 'HTML', 33, function ( assert ) {
922 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
924 assertBothModes( assert, [ 'jquerymsg-italics-msg' ], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
926 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
927 assertBothModes( assert, [ 'jquerymsg-bold-msg' ], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
929 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
930 assertBothModes( assert, [ 'jquerymsg-bold-italics-msg' ], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
932 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
933 assertBothModes( assert, [ 'jquerymsg-italics-bold-msg' ], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
935 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
938 formatParse( 'jquerymsg-italics-with-link' ),
939 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
940 'Italics with link inside in parse mode'
944 formatText( 'jquerymsg-italics-with-link' ),
945 mw.messages.get( 'jquerymsg-italics-with-link' ),
946 'Italics with link unchanged in text mode'
949 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
951 formatParse( 'jquerymsg-italics-id-class' ),
952 mw.messages.get( 'jquerymsg-italics-id-class' ),
953 'ID and class are allowed'
956 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
958 formatParse( 'jquerymsg-italics-onclick' ),
959 '<i onclick="alert(\'foo\')">Foo</i>',
960 'element with onclick is escaped because it is not allowed'
963 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
965 formatParse( 'jquerymsg-script-msg' ),
966 '<script >alert( "Who put this tag here?" );</script>',
967 'Tag outside whitelist escaped in parse mode'
971 formatText( 'jquerymsg-script-msg' ),
972 mw.messages.get( 'jquerymsg-script-msg' ),
973 'Tag outside whitelist unchanged in text mode'
976 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
978 formatParse( 'jquerymsg-script-link-msg' ),
979 '<script><a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a></script>',
980 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
983 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
985 formatParse( 'jquerymsg-mismatched-html' ),
986 '<i class="important">test</b>',
987 'Mismatched HTML start and end tag treated as text'
990 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
992 formatParse( 'jquerymsg-script-and-external-link' ),
993 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><i>Foo</i> bar</a>',
994 'HTML tags in external links not interfering with escaping of other tags'
997 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
999 formatParse( 'jquerymsg-link-script' ),
1000 '<a href="http://example.com"><script>alert( "jquerymsg-link-script test" );</script></a>',
1001 'Non-whitelisted HTML tag in external link anchor treated as text'
1004 // Intentionally not using htmlEqual for the quote tests
1005 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
1007 formatParse( 'jquerymsg-double-quotes-preserved' ),
1008 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
1009 'Attributes with double quotes are preserved as such'
1012 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
1014 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
1015 '<i id="single">Single</i>',
1016 'Attributes with single quotes are normalized to double'
1019 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
1021 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
1022 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
1023 'Escaped attributes are parsed correctly'
1026 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
1028 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
1029 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
1030 'Escaped attributes are parsed correctly'
1033 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
1035 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
1036 '<i><a href="http://example.com">Example</a></i>',
1037 'Contents of valid tag are treated as wikitext, so external link is parsed'
1040 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1042 formatParse( 'jquerymsg-wikitext-contents-script' ),
1043 '<i><script>Script inside</script></i>',
1044 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1047 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1049 formatParse( 'jquerymsg-unclosed-tag' ),
1050 'Foo<tag>bar',
1051 'Nonsupported unclosed tags are escaped'
1054 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1056 formatParse( 'jquerymsg-self-closing-tag' ),
1057 'Foo<tag/>bar',
1058 'Self-closing tags don\'t cause a parse error'
1061 mw.messages.set( 'jquerymsg-asciialphabetliteral-regression', '<b >>>="dir">asd</b>' );
1063 formatParse( 'jquerymsg-asciialphabetliteral-regression' ),
1064 '<b>>>="dir">asd</b>',
1065 'Regression test for bad "asciiAlphabetLiteral" definition'
1068 mw.messages.set( 'jquerymsg-entities1', 'A&B' );
1069 mw.messages.set( 'jquerymsg-entities2', 'A>B' );
1070 mw.messages.set( 'jquerymsg-entities3', 'A→B' );
1072 formatParse( 'jquerymsg-entities1' ),
1074 'Lone "&" is escaped in text'
1077 formatParse( 'jquerymsg-entities2' ),
1079 '">" entity is double-escaped in text' // (WHY?)
1082 formatParse( 'jquerymsg-entities3' ),
1084 '"→" entity is double-escaped in text'
1087 mw.messages.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1088 mw.messages.set( 'jquerymsg-entities-attr2', '<i title="A>B"></i>' );
1089 mw.messages.set( 'jquerymsg-entities-attr3', '<i title="A→B"></i>' );
1091 formatParse( 'jquerymsg-entities-attr1' ),
1092 '<i title="A&B"></i>',
1093 'Lone "&" is escaped in attribute'
1096 formatParse( 'jquerymsg-entities-attr2' ),
1097 '<i title="A>B"></i>',
1098 '">" entity is not double-escaped in attribute' // (WHY?)
1101 formatParse( 'jquerymsg-entities-attr3' ),
1102 '<i title="A&rarr;B"></i>',
1103 '"→" entity is double-escaped in attribute'
1107 QUnit.test( 'Nowiki', 3, function ( assert ) {
1108 mw.messages.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
1110 formatParse( 'jquerymsg-nowiki-link' ),
1112 'Link inside nowiki is not parsed'
1115 mw.messages.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
1117 formatParse( 'jquerymsg-nowiki-htmltag' ),
1118 'Foo <b>bar</b> baz.',
1119 'HTML inside nowiki is not parsed and escaped'
1122 mw.messages.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
1124 formatParse( 'jquerymsg-nowiki-template' ),
1126 'Template inside nowiki is not parsed and does not cause a parse error'
1130 QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
1131 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1133 this.suppressWarnings();
1134 var logSpy = this.sandbox.spy( mw.log, 'warn' );
1137 formatParse( 'invalid-wikitext' ),
1138 '<b>{{FAIL}}</b>',
1139 'Invalid wikitext: \'parse\' format'
1143 formatText( 'invalid-wikitext' ),
1145 'Invalid wikitext: \'text\' format'
1148 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
1151 QUnit.test( 'Integration', 5, function ( assert ) {
1152 var expected, logSpy, msg;
1154 expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1155 mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
1157 this.suppressWarnings();
1158 logSpy = this.sandbox.spy( mw.log, 'warn' );
1160 window.gM( 'integration-test' ),
1162 'Global function gM() works correctly'
1164 assert.equal( logSpy.callCount, 1, 'mw.log.warn called' );
1165 this.restoreWarnings();
1168 mw.message( 'integration-test' ).parse(),
1170 'mw.message().parse() works correctly'
1174 $( '<span>' ).msg( 'integration-test' ).html(),
1176 'jQuery plugin $.fn.msg() works correctly'
1179 mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
1181 'integration-test-extlink',
1182 $( '<a>' ).attr( 'href', 'http://example.com/' )
1184 msg.parse(); // Not a no-op
1187 '<a href="http://example.com/">Link</a>',
1188 'Calling .parse() multiple times does not duplicate link contents'
1192 }( mediaWiki, jQuery ) );