2 var formatText
, formatParse
, formatnumTests
, specialCharactersPageName
, expectedListUsers
,
3 expectedListUsersSitename
, 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( {
23 specialCharactersPageName
= '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
25 expectedListUsers
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
26 expectedListUsersSitename
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
29 expectedEntrypoints
= '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
31 formatText
= mw
.jqueryMsg
.getMessageFunction( {
35 formatParse
= mw
.jqueryMsg
.getMessageFunction( {
39 teardown: function () {
40 mw
.language
= this.originalMwLanguage
;
41 mw
.jqueryMsg
.setParserDefaults( this.parserDefaults
);
44 wgArticlePath
: '/wiki/$1',
45 // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
53 // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
54 wgFormattedNamespaces
: {
57 11: 'Dyskusja szablonu'
60 // Messages that are reused in multiple tests
62 // The values for gender are not significant,
63 // what matters is which of the values is choosen by the parser
64 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
65 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
67 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
68 // See https://phabricator.wikimedia.org/T71993
69 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
70 // Assume the grammar form grammar_case_foo is not valid in any language
71 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
73 'formatnum-msg': '{{formatnum:$1}}',
75 'portal-url': 'Project:Community portal',
76 'see-portal-url': '{{Int:portal-url}} is an important community page.',
78 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
79 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
81 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
83 'external-link-replace': 'Foo [$1 bar]',
84 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b}} things.',
85 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.',
86 'plural-empty-explicit-form': 'There is me{{PLURAL:$1|0=| and other people}}.'
91 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
92 * to transport the module back to the test. It musn't be overwritten concurrentely.
94 * This function caches the mw.language data to avoid having to request the same module
95 * multiple times. There is more than one test case for any given language.
97 function getMwLanguage( langCode
) {
98 if ( !hasOwn
.call( mwLanguageCache
, langCode
) ) {
99 mwLanguageCache
[ langCode
] = $.ajax( {
100 url
: mw
.util
.wikiScript( 'load' ),
102 skin
: mw
.config
.get( 'skin' ),
104 debug
: mw
.config
.get( 'debug' ),
106 'mediawiki.language.data',
113 } ).then( function () {
117 return mwLanguageCache
[ langCode
];
121 * @param {Function[]} tasks List of functions that perform tasks
122 * that may be asynchronous. Invoke the callback parameter when done.
124 function process( tasks
) {
125 /*jshint latedef:false */
127 tasks
.splice( 0, tasks
.length
);
132 // This happens if after the process is completed, one of our callbacks is
133 // invoked. This can happen if a test timed out but the process was still
134 // running. In that case, ignore it. Don't invoke complete() a second time.
137 var task
= tasks
.shift();
141 // Remove tasks list to indicate the process is final.
148 QUnit
.test( 'Replace', 15, function ( assert
) {
149 mw
.messages
.set( 'simple', 'Foo $1 baz $2' );
151 assert
.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
152 assert
.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
153 assert
.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
155 mw
.messages
.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
158 formatParse( 'plain-input', 'bar' ),
159 '<foo foo="foo">xbary&lt;</foo>z',
160 'Input is not considered html'
163 mw
.messages
.set( 'plain-replace', 'Foo $1' );
166 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
167 'Foo <bar bar="bar">&gt;</bar>',
168 'Replacement is not considered html'
171 mw
.messages
.set( 'object-replace', 'Foo $1' );
174 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
175 'Foo <div class="bar">></div>',
176 'jQuery objects are preserved as raw html'
180 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
181 'Foo <div class="bar">></div>',
182 'HTMLElement objects are preserved as raw html'
186 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
187 'Foo <div class="bar">></div>',
188 'HTMLElement[] arrays are preserved as raw html'
192 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
193 'Foo <a href="http://example.org/?x=y&z">bar</a>',
194 'Href is not double-escaped in wikilink function'
197 formatParse( 'external-link-plural', 1, 'http://example.org' ),
198 'Foo is <a href="http://example.org">one</a> things.',
199 'Link is expanded inside plural and is not escaped html'
202 formatParse( 'external-link-plural', 2, 'http://example.org' ),
203 'Foo <a href=\"http://example.org\">two</a> things.',
204 'Link is expanded inside an explicit plural form and is not escaped html'
207 formatParse( 'external-link-plural', 3 ),
209 'A simple explicit plural form co-existing with complex explicit plural forms'
212 formatParse( 'external-link-plural', 4, 'http://example.org' ),
214 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
217 formatParse( 'external-link-plural', 6, 'http://example.org' ),
218 'Foo are <a href="http://example.org">some</a> things.',
219 'Plural fallback to the "other" plural form'
222 formatParse( 'plural-only-explicit-forms', 2 ),
223 'It is a double room.',
224 'Plural with explicit forms alone.'
228 QUnit
.test( 'Plural', 9, function ( assert
) {
229 assert
.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
230 assert
.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
231 assert
.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
232 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
233 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in Wiki', 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
234 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
235 assert
.equal( formatParse( 'plural-empty-explicit-form', 0 ), 'There is me.' );
236 assert
.equal( formatParse( 'plural-empty-explicit-form', 1 ), 'There is me and other people.' );
237 assert
.equal( formatParse( 'plural-empty-explicit-form', 2 ), 'There is me and other people.' );
240 QUnit
.test( 'Gender', 15, function ( assert
) {
241 var originalGender
= mw
.user
.options
.get( 'gender' );
243 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
244 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
245 mw
.user
.options
.set( 'gender', 'male' );
247 formatParse( 'gender-msg', 'Bob', 'male' ),
249 'Masculine from string "male"'
252 formatParse( 'gender-msg', 'Bob', mw
.user
),
254 'Masculine from mw.user object'
257 formatParse( 'gender-msg-currentuser' ),
259 'Masculine for current user'
262 mw
.user
.options
.set( 'gender', 'female' );
264 formatParse( 'gender-msg', 'Alice', 'female' ),
266 'Feminine from string "female"' );
268 formatParse( 'gender-msg', 'Alice', mw
.user
),
270 'Feminine from mw.user object'
273 formatParse( 'gender-msg-currentuser' ),
275 'Feminine for current user'
278 mw
.user
.options
.set( 'gender', 'unknown' );
280 formatParse( 'gender-msg', 'Foo', mw
.user
),
282 'Neutral from mw.user object' );
284 formatParse( 'gender-msg', 'User' ),
286 'Neutral when no parameter given' );
288 formatParse( 'gender-msg', 'User', 'unknown' ),
290 'Neutral from string "unknown"'
293 formatParse( 'gender-msg-currentuser' ),
295 'Neutral for current user'
298 mw
.messages
.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
301 formatParse( 'gender-msg-one-form', 'male', 10 ),
303 'Gender neutral and plural form'
306 formatParse( 'gender-msg-one-form', 'female', 1 ),
308 'Gender neutral and singular form'
311 mw
.messages
.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
313 formatParse( 'gender-msg-lowercase', 'male' ),
318 formatParse( 'gender-msg-lowercase', 'female' ),
323 mw
.messages
.set( 'gender-msg-wrong', '{{gender}} test' );
325 formatParse( 'gender-msg-wrong', 'female' ),
327 'Invalid syntax should result in {{gender}} simply being stripped away'
330 mw
.user
.options
.set( 'gender', originalGender
);
333 QUnit
.test( 'Case changing', 8, function ( assert
) {
334 mw
.messages
.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' );
335 assert
.equal( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' );
337 mw
.messages
.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' );
338 assert
.equal( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' );
340 mw
.messages
.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
341 mw
.messages
.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
342 assert
.equal( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' );
343 assert
.equal( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' );
345 mw
.messages
.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
346 mw
.messages
.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
347 assert
.equal( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' );
348 assert
.equal( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' );
350 mw
.messages
.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
351 assert
.equal( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' );
352 mw
.messages
.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
353 assert
.equal( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' );
356 QUnit
.test( 'Grammar', 2, function ( assert
) {
357 assert
.equal( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' );
359 mw
.messages
.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
360 assert
.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
363 QUnit
.test( 'Match PHP parser', mw
.libs
.phpParserData
.tests
.length
, function ( assert
) {
364 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
365 var tasks
= $.map( mw
.libs
.phpParserData
.tests
, function ( test
) {
366 return function ( next
, abort
) {
367 var done
= assert
.async();
368 getMwLanguage( test
.lang
)
369 .then( function ( langClass
) {
370 mw
.config
.set( 'wgUserLanguage', test
.lang
);
371 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
373 parser
.parse( test
.key
, test
.args
).html(),
378 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load.' );
381 .then( next
, abort
);
388 QUnit
.test( 'Links', 14, function ( assert
) {
390 expectedDisambiguationsText
,
391 expectedMultipleBars
,
392 expectedSpecialCharacters
;
394 // The below three are all identical to or based on real messages. For disambiguations-text,
395 // the bold was removed because it is not yet implemented.
398 formatParse( 'jquerymsg-test-statistics-users' ),
403 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 ' +
404 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
406 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]].' );
408 formatParse( 'disambiguations-text' ),
409 expectedDisambiguationsText
,
410 'Wikilink without pipe'
414 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
419 // Pipe trick is not supported currently, but should not parse as text either.
420 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
421 mw
.messages
.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
422 mw
.messages
.set( 'empty-link', '[[]]' );
423 this.suppressWarnings();
425 formatParse( 'pipe-trick' ),
426 '[[Tampa, Florida|]]',
427 'Pipe trick should not be parsed.'
430 formatParse( 'reverse-pipe-trick' ),
431 '[[|Tampa, Florida]]',
432 'Reverse pipe trick should not be parsed.'
435 formatParse( 'empty-link' ),
437 'Empty link should not be parsed.'
439 this.restoreWarnings();
441 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
442 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
444 formatParse( 'multiple-bars' ),
445 expectedMultipleBars
,
449 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>';
451 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
453 formatParse( 'special-characters' ),
454 expectedSpecialCharacters
,
458 mw
.messages
.set( 'leading-colon', '[[:File:Foo.jpg]]' );
460 formatParse( 'leading-colon' ),
461 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
462 'Leading colon in links is stripped'
466 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
467 expectedListUsersSitename
,
468 'Piped wikilink with parser function in the text'
474 'asd [http://example.org <strong>Example</strong>] asd',
475 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
478 'extlink-html-partial',
479 'asd [http://example.org foo <strong>Example</strong> bar] asd',
480 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
483 'wikilink-html-full',
484 'asd [[Example|<strong>Example</strong>]] asd',
485 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
488 'wikilink-html-partial',
489 'asd [[Example|foo <strong>Example</strong> bar]] asd',
490 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
494 $.each( testCases
, function () {
499 mw
.messages
.set( key
, input
);
503 'HTML in links: ' + key
508 QUnit
.test( 'Replacements in links', 14, function ( assert
) {
511 'extlink-param-href-full',
512 'asd [$1 Example] asd',
513 'asd <a href="http://example.com">Example</a> asd'
516 'extlink-param-href-partial',
517 'asd [$1/example Example] asd',
518 'asd <a href="http://example.com/example">Example</a> asd'
521 'extlink-param-text-full',
522 'asd [http://example.org $2] asd',
523 'asd <a href="http://example.org">Text</a> asd'
526 'extlink-param-text-partial',
527 'asd [http://example.org Example $2] asd',
528 'asd <a href="http://example.org">Example Text</a> asd'
531 'extlink-param-both-full',
533 'asd <a href="http://example.com">Text</a> asd'
536 'extlink-param-both-partial',
537 'asd [$1/example Example $2] asd',
538 'asd <a href="http://example.com/example">Example Text</a> asd'
541 'wikilink-param-href-full',
542 'asd [[$1|Example]] asd',
543 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
546 'wikilink-param-href-partial',
547 'asd [[$1/Test|Example]] asd',
548 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
551 'wikilink-param-text-full',
552 'asd [[Example|$2]] asd',
553 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
556 'wikilink-param-text-partial',
557 'asd [[Example|Example $2]] asd',
558 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
561 'wikilink-param-both-full',
563 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
566 'wikilink-param-both-partial',
567 'asd [[$1/Test|Example $2]] asd',
568 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
571 'wikilink-param-unpiped-full',
573 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
576 'wikilink-param-unpiped-partial',
577 'asd [[$1/Test]] asd',
578 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
582 $.each( testCases
, function () {
587 paramHref
= key
.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
589 mw
.messages
.set( key
, input
);
591 formatParse( key
, paramHref
, paramText
),
593 'Replacements in links: ' + key
598 // Tests that {{-transformation vs. general parsing are done as requested
599 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
600 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
602 assertBothModes( assert
, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
604 assertBothModes( assert
, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
606 assertBothModes( assert
, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
608 mw
.config
.set( 'wgUserLanguage', 'en' );
609 assertBothModes( assert
, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
611 // Test non-{{ wikitext, where behavior differs
615 formatText( 'jquerymsg-test-statistics-users' ),
616 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
617 'Internal link message unchanged when format is \'text\''
620 formatParse( 'jquerymsg-test-statistics-users' ),
622 'Internal link message parsed when format is \'parse\''
627 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
628 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
629 'External link message unchanged when format is \'text\''
632 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
634 'External link message processed when format is \'parse\''
637 // External link with parameter
639 formatText( 'external-link-replace', 'http://example.com' ),
640 'Foo [http://example.com bar]',
641 'External link message only substitutes parameter when format is \'text\''
644 formatParse( 'external-link-replace', 'http://example.com' ),
645 'Foo <a href="http://example.com">bar</a>',
646 'External link message processed when format is \'parse\''
649 formatParse( 'external-link-replace', $( '<i>' ) ),
651 'External link message processed as jQuery object when format is \'parse\''
654 formatParse( 'external-link-replace', function () {} ),
655 'Foo <a href="#">bar</a>',
656 'External link message processed as function when format is \'parse\''
659 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
662 QUnit
.test( 'Int', 4, function ( assert
) {
663 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.',
664 expectedNewarticletext
,
665 helpPageTitle
= 'Help:Foobar';
667 mw
.messages
.set( 'foobar', helpPageTitle
);
669 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 ' +
670 '<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.';
672 mw
.messages
.set( 'newarticletext', newarticletextSource
);
675 formatParse( 'newarticletext' ),
676 expectedNewarticletext
,
677 'Link with nested message'
681 formatParse( 'see-portal-url' ),
682 'Project:Community portal is an important community page.',
686 mw
.messages
.set( 'newarticletext-lowercase',
687 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
690 formatParse( 'newarticletext-lowercase' ),
691 expectedNewarticletext
,
692 'Link with nested message, lowercase include'
695 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
698 formatParse( 'uses-missing-int' ),
700 'int: where nested message does not exist'
704 QUnit
.test( 'Ns', 4, function ( assert
) {
705 mw
.messages
.set( 'ns-template-talk', '{{ns:Template talk}}' );
707 formatParse( 'ns-template-talk' ),
709 'ns: returns localised namespace when used with a canonical namespace name'
712 mw
.messages
.set( 'ns-10', '{{ns:10}}' );
714 formatParse( 'ns-10' ),
716 'ns: returns localised namespace when used with a namespace number'
719 mw
.messages
.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
721 formatParse( 'ns-unknown' ),
723 'ns: returns empty string for unknown namespace name'
726 mw
.messages
.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
728 formatParse( 'ns-in-a-link' ),
729 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
730 'ns: works when used inside a wikilink'
734 // Tests that getMessageFunction is used for non-plain messages with curly braces or
735 // square brackets, but not otherwise.
736 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
737 var oldGMF
, outerCalled
, innerCalled
;
740 'curly-brace': '{{int:message}}',
741 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
742 'double-square-bracket': '[[Some page]]',
743 regular
: 'Other message'
746 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
748 mw
.jqueryMsg
.getMessageFunction = function () {
755 function verifyGetMessageFunction( key
, format
, shouldCall
) {
759 message
= mw
.message( key
);
761 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
762 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
763 delete mw
.messages
[ format
];
766 verifyGetMessageFunction( 'curly-brace', 'parse', true );
767 verifyGetMessageFunction( 'curly-brace', 'plain', false );
769 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
770 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
772 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
773 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
775 verifyGetMessageFunction( 'regular', 'parse', false );
776 verifyGetMessageFunction( 'regular', 'plain', false );
778 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
779 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
780 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
782 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
788 number
: 987654321.654321,
789 result
: '987,654,321.654',
790 description
: 'formatnum test for English, decimal separator'
794 number
: 987654321.654321,
795 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
796 description
: 'formatnum test for Arabic, with decimal separator'
800 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
803 description
: 'formatnum test for Arabic, with decimal separator, reverse'
809 description
: 'formatnum test for Arabic, negative number'
816 description
: 'formatnum test for Arabic, negative number, reverse'
820 number
: 987654321.654321,
821 result
: '987.654.321,654',
822 description
: 'formatnum test for Nederlands, decimal separator'
828 description
: 'formatnum test for Nederlands, negative number'
834 description
: 'formatnum test for Nederlands'
838 number
: 'invalidnumber',
839 result
: 'invalidnumber',
840 description
: 'formatnum test for Nederlands, invalid number'
844 number
: '1000000000',
845 result
: '1,00,00,00,000',
846 description
: 'formatnum test for Malayalam'
850 number
: '-1000000000',
851 result
: '-1,00,00,00,000',
852 description
: 'formatnum test for Malayalam, negative number'
855 * This will fail because of wrong pattern for ml in MW(different from CLDR)
858 number: '1000000000.000',
859 result: '1,00,00,00,000.000',
860 description: 'formatnum test for Malayalam with decimal place'
865 number
: '123456789.123456789',
866 result
: '१२,३४,५६,७८९',
867 description
: 'formatnum test for Hindi'
871 number
: '१२,३४,५६,७८९',
872 result
: '१२,३४,५६,७८९',
873 description
: 'formatnum test for Hindi, Devanagari digits passed'
877 number
: '१२३४५६,७८९',
880 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
884 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
885 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
886 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
887 var queue
= $.map( formatnumTests
, function ( test
) {
888 return function ( next
, abort
) {
889 var done
= assert
.async();
890 getMwLanguage( test
.lang
)
891 .then( function ( langClass
) {
892 mw
.config
.set( 'wgUserLanguage', test
.lang
);
893 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
895 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
896 [ test
.number
] ).html(),
901 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
904 .then( next
, abort
);
911 QUnit
.test( 'HTML', 32, function ( assert
) {
912 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
914 assertBothModes( assert
, [ 'jquerymsg-italics-msg' ], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
916 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
917 assertBothModes( assert
, [ 'jquerymsg-bold-msg' ], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
919 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
920 assertBothModes( assert
, [ 'jquerymsg-bold-italics-msg' ], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
922 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
923 assertBothModes( assert
, [ 'jquerymsg-italics-bold-msg' ], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
925 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
928 formatParse( 'jquerymsg-italics-with-link' ),
929 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
930 'Italics with link inside in parse mode'
934 formatText( 'jquerymsg-italics-with-link' ),
935 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
936 'Italics with link unchanged in text mode'
939 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
941 formatParse( 'jquerymsg-italics-id-class' ),
942 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
943 'ID and class are allowed'
946 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
948 formatParse( 'jquerymsg-italics-onclick' ),
949 '<i onclick="alert(\'foo\')">Foo</i>',
950 'element with onclick is escaped because it is not allowed'
953 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
955 formatParse( 'jquerymsg-script-msg' ),
956 '<script >alert( "Who put this tag here?" );</script>',
957 'Tag outside whitelist escaped in parse mode'
961 formatText( 'jquerymsg-script-msg' ),
962 mw
.messages
.get( 'jquerymsg-script-msg' ),
963 'Tag outside whitelist unchanged in text mode'
966 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
968 formatParse( 'jquerymsg-script-link-msg' ),
969 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
970 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
973 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
975 formatParse( 'jquerymsg-mismatched-html' ),
976 '<i class="important">test</b>',
977 'Mismatched HTML start and end tag treated as text'
980 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
982 formatParse( 'jquerymsg-script-and-external-link' ),
983 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><i>Foo</i> bar</a>',
984 'HTML tags in external links not interfering with escaping of other tags'
987 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
989 formatParse( 'jquerymsg-link-script' ),
990 '<a href="http://example.com"><script>alert( "jquerymsg-link-script test" );</script></a>',
991 'Non-whitelisted HTML tag in external link anchor treated as text'
994 // Intentionally not using htmlEqual for the quote tests
995 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
997 formatParse( 'jquerymsg-double-quotes-preserved' ),
998 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
999 'Attributes with double quotes are preserved as such'
1002 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
1004 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
1005 '<i id="single">Single</i>',
1006 'Attributes with single quotes are normalized to double'
1009 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
1011 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
1012 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
1013 'Escaped attributes are parsed correctly'
1016 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
1018 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
1019 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
1020 'Escaped attributes are parsed correctly'
1023 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
1025 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
1026 '<i><a href="http://example.com">Example</a></i>',
1027 'Contents of valid tag are treated as wikitext, so external link is parsed'
1030 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1032 formatParse( 'jquerymsg-wikitext-contents-script' ),
1033 '<i><script>Script inside</script></i>',
1034 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1037 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1039 formatParse( 'jquerymsg-unclosed-tag' ),
1040 'Foo<tag>bar',
1041 'Nonsupported unclosed tags are escaped'
1044 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1046 formatParse( 'jquerymsg-self-closing-tag' ),
1047 'Foo<tag/>bar',
1048 'Self-closing tags don\'t cause a parse error'
1051 mw
.messages
.set( 'jquerymsg-entities1', 'A&B' );
1052 mw
.messages
.set( 'jquerymsg-entities2', 'A>B' );
1053 mw
.messages
.set( 'jquerymsg-entities3', 'A→B' );
1055 formatParse( 'jquerymsg-entities1' ),
1057 'Lone "&" is escaped in text'
1060 formatParse( 'jquerymsg-entities2' ),
1062 '">" entity is double-escaped in text' // (WHY?)
1065 formatParse( 'jquerymsg-entities3' ),
1067 '"→" entity is double-escaped in text'
1070 mw
.messages
.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1071 mw
.messages
.set( 'jquerymsg-entities-attr2', '<i title="A>B"></i>' );
1072 mw
.messages
.set( 'jquerymsg-entities-attr3', '<i title="A→B"></i>' );
1074 formatParse( 'jquerymsg-entities-attr1' ),
1075 '<i title="A&B"></i>',
1076 'Lone "&" is escaped in attribute'
1079 formatParse( 'jquerymsg-entities-attr2' ),
1080 '<i title="A>B"></i>',
1081 '">" entity is not double-escaped in attribute' // (WHY?)
1084 formatParse( 'jquerymsg-entities-attr3' ),
1085 '<i title="A&rarr;B"></i>',
1086 '"→" entity is double-escaped in attribute'
1090 QUnit
.test( 'Nowiki', 3, function ( assert
) {
1091 mw
.messages
.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
1093 formatParse( 'jquerymsg-nowiki-link' ),
1095 'Link inside nowiki is not parsed'
1098 mw
.messages
.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
1100 formatParse( 'jquerymsg-nowiki-htmltag' ),
1101 'Foo <b>bar</b> baz.',
1102 'HTML inside nowiki is not parsed and escaped'
1105 mw
.messages
.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
1107 formatParse( 'jquerymsg-nowiki-template' ),
1109 'Template inside nowiki is not parsed and does not cause a parse error'
1113 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
1114 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1116 this.suppressWarnings();
1117 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1120 formatParse( 'invalid-wikitext' ),
1121 '<b>{{FAIL}}</b>',
1122 'Invalid wikitext: \'parse\' format'
1126 formatText( 'invalid-wikitext' ),
1128 'Invalid wikitext: \'text\' format'
1131 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
1134 QUnit
.test( 'Integration', 5, function ( assert
) {
1135 var expected
, logSpy
, msg
;
1137 expected
= '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1138 mw
.messages
.set( 'integration-test', '<b>[[Bold]]!</b>' );
1140 this.suppressWarnings();
1141 logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1143 window
.gM( 'integration-test' ),
1145 'Global function gM() works correctly'
1147 assert
.equal( logSpy
.callCount
, 1, 'mw.log.warn called' );
1148 this.restoreWarnings();
1151 mw
.message( 'integration-test' ).parse(),
1153 'mw.message().parse() works correctly'
1157 $( '<span>' ).msg( 'integration-test' ).html(),
1159 'jQuery plugin $.fn.msg() works correctly'
1162 mw
.messages
.set( 'integration-test-extlink', '[$1 Link]' );
1164 'integration-test-extlink',
1165 $( '<a>' ).attr( 'href', 'http://example.com/' )
1167 msg
.parse(); // Not a no-op
1170 '<a href="http://example.com/">Link</a>',
1171 'Calling .parse() multiple times does not duplicate link contents'
1175 }( mediaWiki
, jQuery
) );