2 /* eslint-disable camelcase */
3 var formatText
, formatParse
, formatnumTests
, specialCharactersPageName
, expectedListUsers
,
4 expectedListUsersSitename
, expectedLinkPagenamee
, expectedEntrypoints
,
6 hasOwn
= Object
.hasOwnProperty
;
8 // When the expected result is the same in both modes
9 function assertBothModes( assert
, parserArguments
, expectedResult
, assertMessage
) {
10 assert
.equal( formatText
.apply( null, parserArguments
), expectedResult
, assertMessage
+ ' when format is \'text\'' );
11 assert
.equal( formatParse
.apply( null, parserArguments
), expectedResult
, assertMessage
+ ' when format is \'parse\'' );
14 QUnit
.module( 'mediawiki.jqueryMsg', QUnit
.newMwEnvironment( {
16 this.originalMwLanguage
= mw
.language
;
17 this.parserDefaults
= mw
.jqueryMsg
.getParserDefaults();
18 mw
.jqueryMsg
.setParserDefaults( {
21 PAGENAMEE
: mw
.util
.wikiUrlencode( '2 + 2' ),
26 specialCharactersPageName
= '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
28 expectedListUsers
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
29 expectedListUsersSitename
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
31 expectedLinkPagenamee
= '<a href="https://example.org/wiki/Foo?bar=baz#val/2_%2B_2">Test</a>';
33 expectedEntrypoints
= '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
35 formatText
= mw
.jqueryMsg
.getMessageFunction( {
39 formatParse
= mw
.jqueryMsg
.getMessageFunction( {
43 teardown: function () {
44 mw
.language
= this.originalMwLanguage
;
45 mw
.jqueryMsg
.setParserDefaults( this.parserDefaults
);
48 wgArticlePath
: '/wiki/$1',
56 wgFormattedNamespaces
: {
59 11: 'Dyskusja szablonu'
62 // Messages that are reused in multiple tests
64 // The values for gender are not significant,
65 // what matters is which of the values is choosen by the parser
66 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
67 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
69 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
70 // See https://phabricator.wikimedia.org/T71993
71 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
72 // Assume the grammar form grammar_case_foo is not valid in any language
73 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
75 'formatnum-msg': '{{formatnum:$1}}',
77 'portal-url': 'Project:Community portal',
78 'see-portal-url': '{{Int:portal-url}} is an important community page.',
80 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
81 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
82 'jquerymsg-test-link-pagenamee': '[https://example.org/wiki/Foo?bar=baz#val/{{PAGENAMEE}} Test]',
84 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
86 'external-link-replace': 'Foo [$1 bar]',
87 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b}} things.',
88 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.',
89 'plural-empty-explicit-form': 'There is me{{PLURAL:$1|0=| and other people}}.'
94 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
95 * to transport the module back to the test. It musn't be overwritten concurrentely.
97 * This function caches the mw.language data to avoid having to request the same module
98 * multiple times. There is more than one test case for any given language.
100 function getMwLanguage( langCode
) {
101 if ( !hasOwn
.call( mwLanguageCache
, langCode
) ) {
102 mwLanguageCache
[ langCode
] = $.ajax( {
103 url
: mw
.util
.wikiScript( 'load' ),
105 skin
: mw
.config
.get( 'skin' ),
107 debug
: mw
.config
.get( 'debug' ),
109 'mediawiki.language.data',
116 } ).then( function () {
120 return mwLanguageCache
[ langCode
];
124 * @param {Function[]} tasks List of functions that perform tasks
125 * that may be asynchronous. Invoke the callback parameter when done.
127 function process( tasks
) {
129 tasks
.splice( 0, tasks
.length
);
130 // eslint-disable-next-line no-use-before-define
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 task
= tasks
.shift();
145 // Remove tasks list to indicate the process is final.
152 QUnit
.test( 'Replace', 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', 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', 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', 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', 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', function ( assert
) {
369 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
370 tasks
= $.map( mw
.libs
.phpParserData
.tests
, function ( test
) {
371 var done
= assert
.async();
372 return function ( next
, abort
) {
373 getMwLanguage( test
.lang
)
374 .then( function ( langClass
) {
376 mw
.config
.set( 'wgUserLanguage', test
.lang
);
377 // eslint-disable-next-line new-cap
378 parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
380 parser
.parse( test
.key
, test
.args
).html(),
385 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load.' );
388 .then( next
, abort
);
395 QUnit
.test( 'Links', function ( assert
) {
397 expectedDisambiguationsText
,
398 expectedMultipleBars
,
399 expectedSpecialCharacters
;
401 // The below three are all identical to or based on real messages. For disambiguations-text,
402 // the bold was removed because it is not yet implemented.
405 formatParse( 'jquerymsg-test-statistics-users' ),
410 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 ' +
411 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
413 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]].' );
415 formatParse( 'disambiguations-text' ),
416 expectedDisambiguationsText
,
417 'Wikilink without pipe'
421 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
426 // Pipe trick is not supported currently, but should not parse as text either.
427 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
428 mw
.messages
.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
429 mw
.messages
.set( 'empty-link', '[[]]' );
430 this.suppressWarnings();
432 formatParse( 'pipe-trick' ),
433 '[[Tampa, Florida|]]',
434 'Pipe trick should not be parsed.'
437 formatParse( 'reverse-pipe-trick' ),
438 '[[|Tampa, Florida]]',
439 'Reverse pipe trick should not be parsed.'
442 formatParse( 'empty-link' ),
444 'Empty link should not be parsed.'
446 this.restoreWarnings();
448 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
449 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
451 formatParse( 'multiple-bars' ),
452 expectedMultipleBars
,
456 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>';
458 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
460 formatParse( 'special-characters' ),
461 expectedSpecialCharacters
,
465 mw
.messages
.set( 'leading-colon', '[[:File:Foo.jpg]]' );
467 formatParse( 'leading-colon' ),
468 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
469 'Leading colon in links is stripped'
473 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
474 expectedListUsersSitename
,
475 'Piped wikilink with parser function in the text'
479 formatParse( 'jquerymsg-test-link-pagenamee' ),
480 expectedLinkPagenamee
,
481 'External link with parser function in the URL'
487 'asd [http://example.org <strong>Example</strong>] asd',
488 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
491 'extlink-html-partial',
492 'asd [http://example.org foo <strong>Example</strong> bar] asd',
493 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
496 'wikilink-html-full',
497 'asd [[Example|<strong>Example</strong>]] asd',
498 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
501 'wikilink-html-partial',
502 'asd [[Example|foo <strong>Example</strong> bar]] asd',
503 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
507 $.each( testCases
, function () {
512 mw
.messages
.set( key
, input
);
516 'HTML in links: ' + key
521 QUnit
.test( 'Replacements in links', function ( assert
) {
524 'extlink-param-href-full',
525 'asd [$1 Example] asd',
526 'asd <a href="http://example.com">Example</a> asd'
529 'extlink-param-href-partial',
530 'asd [$1/example Example] asd',
531 'asd <a href="http://example.com/example">Example</a> asd'
534 'extlink-param-text-full',
535 'asd [http://example.org $2] asd',
536 'asd <a href="http://example.org">Text</a> asd'
539 'extlink-param-text-partial',
540 'asd [http://example.org Example $2] asd',
541 'asd <a href="http://example.org">Example Text</a> asd'
544 'extlink-param-both-full',
546 'asd <a href="http://example.com">Text</a> asd'
549 'extlink-param-both-partial',
550 'asd [$1/example Example $2] asd',
551 'asd <a href="http://example.com/example">Example Text</a> asd'
554 'wikilink-param-href-full',
555 'asd [[$1|Example]] asd',
556 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
559 'wikilink-param-href-partial',
560 'asd [[$1/Test|Example]] asd',
561 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
564 'wikilink-param-text-full',
565 'asd [[Example|$2]] asd',
566 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
569 'wikilink-param-text-partial',
570 'asd [[Example|Example $2]] asd',
571 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
574 'wikilink-param-both-full',
576 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
579 'wikilink-param-both-partial',
580 'asd [[$1/Test|Example $2]] asd',
581 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
584 'wikilink-param-unpiped-full',
586 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
589 'wikilink-param-unpiped-partial',
590 'asd [[$1/Test]] asd',
591 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
595 $.each( testCases
, function () {
600 paramHref
= key
.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
602 mw
.messages
.set( key
, input
);
604 formatParse( key
, paramHref
, paramText
),
606 'Replacements in links: ' + key
611 // Tests that {{-transformation vs. general parsing are done as requested
612 QUnit
.test( 'Curly brace transformation', function ( assert
) {
613 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
615 assertBothModes( assert
, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
617 assertBothModes( assert
, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
619 assertBothModes( assert
, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
621 mw
.config
.set( 'wgUserLanguage', 'en' );
622 assertBothModes( assert
, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
624 // Test non-{{ wikitext, where behavior differs
628 formatText( 'jquerymsg-test-statistics-users' ),
629 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
630 'Internal link message unchanged when format is \'text\''
633 formatParse( 'jquerymsg-test-statistics-users' ),
635 'Internal link message parsed when format is \'parse\''
640 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
641 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
642 'External link message unchanged when format is \'text\''
645 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
647 'External link message processed when format is \'parse\''
650 // External link with parameter
652 formatText( 'external-link-replace', 'http://example.com' ),
653 'Foo [http://example.com bar]',
654 'External link message only substitutes parameter when format is \'text\''
657 formatParse( 'external-link-replace', 'http://example.com' ),
658 'Foo <a href="http://example.com">bar</a>',
659 'External link message processed when format is \'parse\''
662 formatParse( 'external-link-replace', $( '<i>' ) ),
664 'External link message processed as jQuery object when format is \'parse\''
667 formatParse( 'external-link-replace', function () {} ),
668 'Foo <a role="button" tabindex="0">bar</a>',
669 'External link message processed as function when format is \'parse\''
672 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
675 QUnit
.test( 'Int', function ( assert
) {
676 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.',
677 expectedNewarticletext
,
678 helpPageTitle
= 'Help:Foobar';
680 mw
.messages
.set( 'foobar', helpPageTitle
);
682 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 ' +
683 '<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.';
685 mw
.messages
.set( 'newarticletext', newarticletextSource
);
688 formatParse( 'newarticletext' ),
689 expectedNewarticletext
,
690 'Link with nested message'
694 formatParse( 'see-portal-url' ),
695 'Project:Community portal is an important community page.',
699 mw
.messages
.set( 'newarticletext-lowercase',
700 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
703 formatParse( 'newarticletext-lowercase' ),
704 expectedNewarticletext
,
705 'Link with nested message, lowercase include'
708 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
711 formatParse( 'uses-missing-int' ),
713 'int: where nested message does not exist'
717 QUnit
.test( 'Ns', function ( assert
) {
718 mw
.messages
.set( 'ns-template-talk', '{{ns:Template talk}}' );
720 formatParse( 'ns-template-talk' ),
722 'ns: returns localised namespace when used with a canonical namespace name'
725 mw
.messages
.set( 'ns-10', '{{ns:10}}' );
727 formatParse( 'ns-10' ),
729 'ns: returns localised namespace when used with a namespace number'
732 mw
.messages
.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
734 formatParse( 'ns-unknown' ),
736 'ns: returns empty string for unknown namespace name'
739 mw
.messages
.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
741 formatParse( 'ns-in-a-link' ),
742 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
743 'ns: works when used inside a wikilink'
747 // Tests that getMessageFunction is used for non-plain messages with curly braces or
748 // square brackets, but not otherwise.
749 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', function ( assert
) {
750 var oldGMF
, outerCalled
, innerCalled
;
753 'curly-brace': '{{int:message}}',
754 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
755 'double-square-bracket': '[[Some page]]',
756 regular
: 'Other message'
759 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
761 mw
.jqueryMsg
.getMessageFunction = function () {
768 function verifyGetMessageFunction( key
, format
, shouldCall
) {
772 message
= mw
.message( key
);
774 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
775 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
776 delete mw
.messages
[ format
];
779 verifyGetMessageFunction( 'curly-brace', 'parse', true );
780 verifyGetMessageFunction( 'curly-brace', 'plain', false );
782 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
783 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
785 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
786 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
788 verifyGetMessageFunction( 'regular', 'parse', false );
789 verifyGetMessageFunction( 'regular', 'plain', false );
791 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
792 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
793 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
795 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
801 number
: 987654321.654321,
802 result
: '987,654,321.654',
803 description
: 'formatnum test for English, decimal separator'
807 number
: 987654321.654321,
808 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
809 description
: 'formatnum test for Arabic, with decimal separator'
813 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
816 description
: 'formatnum test for Arabic, with decimal separator, reverse'
822 description
: 'formatnum test for Arabic, negative number'
829 description
: 'formatnum test for Arabic, negative number, reverse'
833 number
: 987654321.654321,
834 result
: '987.654.321,654',
835 description
: 'formatnum test for Nederlands, decimal separator'
841 description
: 'formatnum test for Nederlands, negative number'
847 description
: 'formatnum test for Nederlands'
851 number
: 'invalidnumber',
852 result
: 'invalidnumber',
853 description
: 'formatnum test for Nederlands, invalid number'
857 number
: '1000000000',
858 result
: '1,00,00,00,000',
859 description
: 'formatnum test for Malayalam'
863 number
: '-1000000000',
864 result
: '-1,00,00,00,000',
865 description
: 'formatnum test for Malayalam, negative number'
868 * This will fail because of wrong pattern for ml in MW(different from CLDR)
871 number: '1000000000.000',
872 result: '1,00,00,00,000.000',
873 description: 'formatnum test for Malayalam with decimal place'
878 number
: '123456789.123456789',
879 result
: '१२,३४,५६,७८९',
880 description
: 'formatnum test for Hindi'
884 number
: '१२,३४,५६,७८९',
885 result
: '१२,३४,५६,७८९',
886 description
: 'formatnum test for Hindi, Devanagari digits passed'
893 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
897 QUnit
.test( 'formatnum', function ( assert
) {
899 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
900 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
901 queue
= $.map( formatnumTests
, function ( test
) {
902 var done
= assert
.async();
903 return function ( next
, abort
) {
904 getMwLanguage( test
.lang
)
905 .then( function ( langClass
) {
907 mw
.config
.set( 'wgUserLanguage', test
.lang
);
908 // eslint-disable-next-line new-cap
909 parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
911 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
912 [ test
.number
] ).html(),
917 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
920 .then( next
, abort
);
927 QUnit
.test( 'HTML', function ( assert
) {
928 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
930 assertBothModes( assert
, [ 'jquerymsg-italics-msg' ], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
932 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
933 assertBothModes( assert
, [ 'jquerymsg-bold-msg' ], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
935 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
936 assertBothModes( assert
, [ 'jquerymsg-bold-italics-msg' ], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
938 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
939 assertBothModes( assert
, [ 'jquerymsg-italics-bold-msg' ], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
941 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
944 formatParse( 'jquerymsg-italics-with-link' ),
945 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
946 'Italics with link inside in parse mode'
950 formatText( 'jquerymsg-italics-with-link' ),
951 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
952 'Italics with link unchanged in text mode'
955 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
957 formatParse( 'jquerymsg-italics-id-class' ),
958 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
959 'ID and class are allowed'
962 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
964 formatParse( 'jquerymsg-italics-onclick' ),
965 '<i onclick="alert(\'foo\')">Foo</i>',
966 'element with onclick is escaped because it is not allowed'
969 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
971 formatParse( 'jquerymsg-script-msg' ),
972 '<script >alert( "Who put this tag here?" );</script>',
973 'Tag outside whitelist escaped in parse mode'
977 formatText( 'jquerymsg-script-msg' ),
978 mw
.messages
.get( 'jquerymsg-script-msg' ),
979 'Tag outside whitelist unchanged in text mode'
982 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
984 formatParse( 'jquerymsg-script-link-msg' ),
985 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
986 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
989 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
991 formatParse( 'jquerymsg-mismatched-html' ),
992 '<i class="important">test</b>',
993 'Mismatched HTML start and end tag treated as text'
996 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
998 formatParse( 'jquerymsg-script-and-external-link' ),
999 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><i>Foo</i> bar</a>',
1000 'HTML tags in external links not interfering with escaping of other tags'
1003 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
1005 formatParse( 'jquerymsg-link-script' ),
1006 '<a href="http://example.com"><script>alert( "jquerymsg-link-script test" );</script></a>',
1007 'Non-whitelisted HTML tag in external link anchor treated as text'
1010 // Intentionally not using htmlEqual for the quote tests
1011 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
1013 formatParse( 'jquerymsg-double-quotes-preserved' ),
1014 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
1015 'Attributes with double quotes are preserved as such'
1018 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
1020 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
1021 '<i id="single">Single</i>',
1022 'Attributes with single quotes are normalized to double'
1025 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
1027 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
1028 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
1029 'Escaped attributes are parsed correctly'
1032 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
1034 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
1035 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
1036 'Escaped attributes are parsed correctly'
1039 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
1041 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
1042 '<i><a href="http://example.com">Example</a></i>',
1043 'Contents of valid tag are treated as wikitext, so external link is parsed'
1046 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1048 formatParse( 'jquerymsg-wikitext-contents-script' ),
1049 '<i><script>Script inside</script></i>',
1050 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1053 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1055 formatParse( 'jquerymsg-unclosed-tag' ),
1056 'Foo<tag>bar',
1057 'Nonsupported unclosed tags are escaped'
1060 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1062 formatParse( 'jquerymsg-self-closing-tag' ),
1063 'Foo<tag/>bar',
1064 'Self-closing tags don\'t cause a parse error'
1067 mw
.messages
.set( 'jquerymsg-asciialphabetliteral-regression', '<b >>>="dir">asd</b>' );
1069 formatParse( 'jquerymsg-asciialphabetliteral-regression' ),
1070 '<b>>>="dir">asd</b>',
1071 'Regression test for bad "asciiAlphabetLiteral" definition'
1074 mw
.messages
.set( 'jquerymsg-entities1', 'A&B' );
1075 mw
.messages
.set( 'jquerymsg-entities2', 'A>B' );
1076 mw
.messages
.set( 'jquerymsg-entities3', 'A→B' );
1078 formatParse( 'jquerymsg-entities1' ),
1080 'Lone "&" is escaped in text'
1083 formatParse( 'jquerymsg-entities2' ),
1085 '">" entity is double-escaped in text' // (WHY?)
1088 formatParse( 'jquerymsg-entities3' ),
1090 '"→" entity is double-escaped in text'
1093 mw
.messages
.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1094 mw
.messages
.set( 'jquerymsg-entities-attr2', '<i title="A>B"></i>' );
1095 mw
.messages
.set( 'jquerymsg-entities-attr3', '<i title="A→B"></i>' );
1097 formatParse( 'jquerymsg-entities-attr1' ),
1098 '<i title="A&B"></i>',
1099 'Lone "&" is escaped in attribute'
1102 formatParse( 'jquerymsg-entities-attr2' ),
1103 '<i title="A>B"></i>',
1104 '">" entity is not double-escaped in attribute' // (WHY?)
1107 formatParse( 'jquerymsg-entities-attr3' ),
1108 '<i title="A&rarr;B"></i>',
1109 '"→" entity is double-escaped in attribute'
1113 QUnit
.test( 'Nowiki', function ( assert
) {
1114 mw
.messages
.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
1116 formatParse( 'jquerymsg-nowiki-link' ),
1118 'Link inside nowiki is not parsed'
1121 mw
.messages
.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
1123 formatParse( 'jquerymsg-nowiki-htmltag' ),
1124 'Foo <b>bar</b> baz.',
1125 'HTML inside nowiki is not parsed and escaped'
1128 mw
.messages
.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
1130 formatParse( 'jquerymsg-nowiki-template' ),
1132 'Template inside nowiki is not parsed and does not cause a parse error'
1136 QUnit
.test( 'Behavior in case of invalid wikitext', function ( assert
) {
1138 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1140 this.suppressWarnings();
1141 logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1144 formatParse( 'invalid-wikitext' ),
1145 '<b>{{FAIL}}</b>',
1146 'Invalid wikitext: \'parse\' format'
1150 formatText( 'invalid-wikitext' ),
1152 'Invalid wikitext: \'text\' format'
1155 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
1158 QUnit
.test( 'Integration', function ( assert
) {
1159 var expected
, logSpy
, msg
;
1161 expected
= '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1162 mw
.messages
.set( 'integration-test', '<b>[[Bold]]!</b>' );
1164 this.suppressWarnings();
1165 logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1167 window
.gM( 'integration-test' ),
1169 'Global function gM() works correctly'
1171 assert
.equal( logSpy
.callCount
, 1, 'mw.log.warn called' );
1172 this.restoreWarnings();
1175 mw
.message( 'integration-test' ).parse(),
1177 'mw.message().parse() works correctly'
1181 $( '<span>' ).msg( 'integration-test' ).html(),
1183 'jQuery plugin $.fn.msg() works correctly'
1186 mw
.messages
.set( 'integration-test-extlink', '[$1 Link]' );
1188 'integration-test-extlink',
1189 $( '<a>' ).attr( 'href', 'http://example.com/' )
1191 msg
.parse(); // Not a no-op
1194 '<a href="http://example.com/">Link</a>',
1195 'Calling .parse() multiple times does not duplicate link contents'
1199 QUnit
.test( 'setParserDefaults', function ( assert
) {
1200 mw
.jqueryMsg
.setParserDefaults( {
1208 mw
.jqueryMsg
.getParserDefaults().magic
,
1213 'setParserDefaults is shallow by default'
1216 mw
.jqueryMsg
.setParserDefaults(
1226 mw
.jqueryMsg
.getParserDefaults().magic
,
1232 'setParserDefaults is deep if requested'
1235 }( mediaWiki
, jQuery
) );