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
;
17 specialCharactersPageName
= '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
19 expectedListUsers
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
20 expectedListUsersSitename
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
21 mw
.config
.get( 'wgSiteName' ) + '</a>';
23 expectedEntrypoints
= '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
25 formatText
= mw
.jqueryMsg
.getMessageFunction( {
29 formatParse
= mw
.jqueryMsg
.getMessageFunction( {
33 teardown: function () {
34 mw
.language
= this.originalMwLanguage
;
37 wgArticlePath
: '/wiki/$1'
39 // Messages that are reused in multiple tests
41 // The values for gender are not significant,
42 // what matters is which of the values is choosen by the parser
43 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
44 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
46 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
47 // See https://phabricator.wikimedia.org/T71993
48 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
49 // Assume the grammar form grammar_case_foo is not valid in any language
50 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
52 'formatnum-msg': '{{formatnum:$1}}',
54 'portal-url': 'Project:Community portal',
55 'see-portal-url': '{{Int:portal-url}} is an important community page.',
57 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
58 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
60 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
62 'external-link-replace': 'Foo [$1 bar]',
63 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
64 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
69 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
70 * to transport the module back to the test. It musn't be overwritten concurrentely.
72 * This function caches the mw.language data to avoid having to request the same module
73 * multiple times. There is more than one test case for any given language.
75 function getMwLanguage( langCode
) {
76 if ( !hasOwn
.call( mwLanguageCache
, langCode
) ) {
77 mwLanguageCache
[ langCode
] = $.ajax( {
78 url
: mw
.util
.wikiScript( 'load' ),
80 skin
: mw
.config
.get( 'skin' ),
82 debug
: mw
.config
.get( 'debug' ),
84 'mediawiki.language.data',
91 } ).then( function () {
95 return mwLanguageCache
[ langCode
];
99 * @param {Function[]} tasks List of functions that perform tasks
100 * that may be asynchronous. Invoke the callback parameter when done.
101 * @param {Function} complete Called when all tasks are done, or when the sequence is aborted.
103 function process( tasks
, complete
) {
104 /*jshint latedef:false */
106 tasks
.splice( 0, tasks
.length
);
111 // This happens if after the process is completed, one of our callbacks is
112 // invoked. This can happen if a test timed out but the process was still
113 // running. In that case, ignore it. Don't invoke complete() a second time.
116 var task
= tasks
.shift();
120 // Remove tasks list to indicate the process is final.
128 QUnit
.test( 'Replace', 16, function ( assert
) {
129 mw
.messages
.set( 'simple', 'Foo $1 baz $2' );
131 assert
.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
132 assert
.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
133 assert
.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
135 mw
.messages
.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
138 formatParse( 'plain-input', 'bar' ),
139 '<foo foo="foo">xbary&lt;</foo>z',
140 'Input is not considered html'
143 mw
.messages
.set( 'plain-replace', 'Foo $1' );
146 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
147 'Foo <bar bar="bar">&gt;</bar>',
148 'Replacement is not considered html'
151 mw
.messages
.set( 'object-replace', 'Foo $1' );
154 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
155 'Foo <div class="bar">></div>',
156 'jQuery objects are preserved as raw html'
160 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
161 'Foo <div class="bar">></div>',
162 'HTMLElement objects are preserved as raw html'
166 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
167 'Foo <div class="bar">></div>',
168 'HTMLElement[] arrays are preserved as raw html'
172 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
173 'Foo <a href="http://example.org/?x=y&z">bar</a>',
174 'Href is not double-escaped in wikilink function'
177 formatParse( 'external-link-plural', 1, 'http://example.org' ),
178 'Foo is <a href="http://example.org">one</a> things.',
179 'Link is expanded inside plural and is not escaped html'
182 formatParse( 'external-link-plural', 2, 'http://example.org' ),
183 'Foo <a href=\"http://example.org\">two</a> things.',
184 'Link is expanded inside an explicit plural form and is not escaped html'
187 formatParse( 'external-link-plural', 3 ),
189 'A simple explicit plural form co-existing with complex explicit plural forms'
192 formatParse( 'external-link-plural', 4, 'http://example.org' ),
194 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
197 formatParse( 'external-link-plural', 5, 'http://example.org' ),
198 'Foo are <a href="http://example.org">some</a> things.',
199 'Invalid explicit plural form. Plural fallback to the "other" plural form'
202 formatParse( 'external-link-plural', 6, 'http://example.org' ),
203 'Foo are <a href="http://example.org">some</a> things.',
204 'Plural fallback to the "other" plural form'
207 formatParse( 'plural-only-explicit-forms', 2 ),
208 'It is a double room.',
209 'Plural with explicit forms alone.'
213 QUnit
.test( 'Plural', 6, function ( assert
) {
214 assert
.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
215 assert
.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
216 assert
.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
217 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
218 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in ' + mw
.config
.get( 'wgSiteName' ), 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
219 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
222 QUnit
.test( 'Gender', 15, function ( assert
) {
223 var originalGender
= mw
.user
.options
.get( 'gender' );
225 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
226 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
227 mw
.user
.options
.set( 'gender', 'male' );
229 formatParse( 'gender-msg', 'Bob', 'male' ),
231 'Masculine from string "male"'
234 formatParse( 'gender-msg', 'Bob', mw
.user
),
236 'Masculine from mw.user object'
239 formatParse( 'gender-msg-currentuser' ),
241 'Masculine for current user'
244 mw
.user
.options
.set( 'gender', 'female' );
246 formatParse( 'gender-msg', 'Alice', 'female' ),
248 'Feminine from string "female"' );
250 formatParse( 'gender-msg', 'Alice', mw
.user
),
252 'Feminine from mw.user object'
255 formatParse( 'gender-msg-currentuser' ),
257 'Feminine for current user'
260 mw
.user
.options
.set( 'gender', 'unknown' );
262 formatParse( 'gender-msg', 'Foo', mw
.user
),
264 'Neutral from mw.user object' );
266 formatParse( 'gender-msg', 'User' ),
268 'Neutral when no parameter given' );
270 formatParse( 'gender-msg', 'User', 'unknown' ),
272 'Neutral from string "unknown"'
275 formatParse( 'gender-msg-currentuser' ),
277 'Neutral for current user'
280 mw
.messages
.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
283 formatParse( 'gender-msg-one-form', 'male', 10 ),
285 'Gender neutral and plural form'
288 formatParse( 'gender-msg-one-form', 'female', 1 ),
290 'Gender neutral and singular form'
293 mw
.messages
.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
295 formatParse( 'gender-msg-lowercase', 'male' ),
300 formatParse( 'gender-msg-lowercase', 'female' ),
305 mw
.messages
.set( 'gender-msg-wrong', '{{gender}} test' );
307 formatParse( 'gender-msg-wrong', 'female' ),
309 'Invalid syntax should result in {{gender}} simply being stripped away'
312 mw
.user
.options
.set( 'gender', originalGender
);
315 QUnit
.test( 'Grammar', 2, function ( assert
) {
316 assert
.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'Grammar Test with sitename' );
318 mw
.messages
.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
319 assert
.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
322 QUnit
.test( 'Match PHP parser', mw
.libs
.phpParserData
.tests
.length
, function ( assert
) {
323 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
324 var tasks
= $.map( mw
.libs
.phpParserData
.tests
, function ( test
) {
325 return function ( next
, abort
) {
326 getMwLanguage( test
.lang
)
327 .then( function ( langClass
) {
328 mw
.config
.set( 'wgUserLanguage', test
.lang
);
329 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
331 parser
.parse( test
.key
, test
.args
).html(),
336 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load.' );
338 .then( next
, abort
);
343 process( tasks
, QUnit
.start
);
346 QUnit
.test( 'Links', 11, function ( assert
) {
348 expectedDisambiguationsText
,
349 expectedMultipleBars
,
350 expectedSpecialCharacters
;
352 // The below three are all identical to or based on real messages. For disambiguations-text,
353 // the bold was removed because it is not yet implemented.
356 formatParse( 'jquerymsg-test-statistics-users' ),
361 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 ' +
362 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
364 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]].' );
366 formatParse( 'disambiguations-text' ),
367 expectedDisambiguationsText
,
368 'Wikilink without pipe'
372 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
377 // Pipe trick is not supported currently, but should not parse as text either.
378 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
379 this.suppressWarnings();
381 formatParse( 'pipe-trick' ),
382 '[[Tampa, Florida|]]',
383 'Pipe trick should not be parsed.'
385 this.restoreWarnings();
387 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
388 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
390 formatParse( 'multiple-bars' ),
391 expectedMultipleBars
,
395 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>';
397 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
399 formatParse( 'special-characters' ),
400 expectedSpecialCharacters
,
405 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
406 expectedListUsersSitename
,
407 'Piped wikilink with parser function in the text'
413 'asd [http://example.org <strong>Example</strong>] asd',
414 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
417 'extlink-html-partial',
418 'asd [http://example.org foo <strong>Example</strong> bar] asd',
419 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
422 'wikilink-html-full',
423 'asd [[Example|<strong>Example</strong>]] asd',
424 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
427 'wikilink-html-partial',
428 'asd [[Example|foo <strong>Example</strong> bar]] asd',
429 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
433 $.each( testCases
, function () {
438 mw
.messages
.set( key
, input
);
442 'HTML in links: ' + key
447 QUnit
.test( 'Replacements in links', 14, function ( assert
) {
450 'extlink-param-href-full',
451 'asd [$1 Example] asd',
452 'asd <a href="http://example.com">Example</a> asd'
455 'extlink-param-href-partial',
456 'asd [$1/example Example] asd',
457 'asd <a href="http://example.com/example">Example</a> asd'
460 'extlink-param-text-full',
461 'asd [http://example.org $2] asd',
462 'asd <a href="http://example.org">Text</a> asd'
465 'extlink-param-text-partial',
466 'asd [http://example.org Example $2] asd',
467 'asd <a href="http://example.org">Example Text</a> asd'
470 'extlink-param-both-full',
472 'asd <a href="http://example.com">Text</a> asd'
475 'extlink-param-both-partial',
476 'asd [$1/example Example $2] asd',
477 'asd <a href="http://example.com/example">Example Text</a> asd'
480 'wikilink-param-href-full',
481 'asd [[$1|Example]] asd',
482 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
485 'wikilink-param-href-partial',
486 'asd [[$1/Test|Example]] asd',
487 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
490 'wikilink-param-text-full',
491 'asd [[Example|$2]] asd',
492 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
495 'wikilink-param-text-partial',
496 'asd [[Example|Example $2]] asd',
497 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
500 'wikilink-param-both-full',
502 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
505 'wikilink-param-both-partial',
506 'asd [[$1/Test|Example $2]] asd',
507 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
510 'wikilink-param-unpiped-full',
512 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
515 'wikilink-param-unpiped-partial',
516 'asd [[$1/Test]] asd',
517 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
521 $.each( testCases
, function () {
526 paramHref
= key
.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
528 mw
.messages
.set( key
, input
);
530 formatParse( key
, paramHref
, paramText
),
532 'Replacements in links: ' + key
537 // Tests that {{-transformation vs. general parsing are done as requested
538 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
539 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
541 assertBothModes( assert
, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
543 assertBothModes( assert
, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
545 assertBothModes( assert
, [ 'grammar-msg' ], 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'grammar is resolved' );
547 mw
.config
.set( 'wgUserLanguage', 'en' );
548 assertBothModes( assert
, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
550 // Test non-{{ wikitext, where behavior differs
554 formatText( 'jquerymsg-test-statistics-users' ),
555 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
556 'Internal link message unchanged when format is \'text\''
559 formatParse( 'jquerymsg-test-statistics-users' ),
561 'Internal link message parsed when format is \'parse\''
566 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
567 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
568 'External link message unchanged when format is \'text\''
571 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
573 'External link message processed when format is \'parse\''
576 // External link with parameter
578 formatText( 'external-link-replace', 'http://example.com' ),
579 'Foo [http://example.com bar]',
580 'External link message only substitutes parameter when format is \'text\''
583 formatParse( 'external-link-replace', 'http://example.com' ),
584 'Foo <a href="http://example.com">bar</a>',
585 'External link message processed when format is \'parse\''
588 formatParse( 'external-link-replace', $( '<i>' ) ),
590 'External link message processed as jQuery object when format is \'parse\''
593 formatParse( 'external-link-replace', function () {} ),
594 'Foo <a href="#">bar</a>',
595 'External link message processed as function when format is \'parse\''
598 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
601 QUnit
.test( 'Int', 4, function ( assert
) {
602 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.',
603 expectedNewarticletext
,
604 helpPageTitle
= 'Help:Foobar';
606 mw
.messages
.set( 'foobar', helpPageTitle
);
608 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 ' +
609 '<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.';
611 mw
.messages
.set( 'newarticletext', newarticletextSource
);
614 formatParse( 'newarticletext' ),
615 expectedNewarticletext
,
616 'Link with nested message'
620 formatParse( 'see-portal-url' ),
621 'Project:Community portal is an important community page.',
625 mw
.messages
.set( 'newarticletext-lowercase',
626 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
629 formatParse( 'newarticletext-lowercase' ),
630 expectedNewarticletext
,
631 'Link with nested message, lowercase include'
634 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
637 formatParse( 'uses-missing-int' ),
639 'int: where nested message does not exist'
643 // Tests that getMessageFunction is used for non-plain messages with curly braces or
644 // square brackets, but not otherwise.
645 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
646 var oldGMF
, outerCalled
, innerCalled
;
649 'curly-brace': '{{int:message}}',
650 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
651 'double-square-bracket': '[[Some page]]',
652 regular
: 'Other message'
655 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
657 mw
.jqueryMsg
.getMessageFunction = function () {
664 function verifyGetMessageFunction( key
, format
, shouldCall
) {
668 message
= mw
.message( key
);
670 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
671 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
674 verifyGetMessageFunction( 'curly-brace', 'parse', true );
675 verifyGetMessageFunction( 'curly-brace', 'plain', false );
677 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
678 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
680 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
681 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
683 verifyGetMessageFunction( 'regular', 'parse', false );
684 verifyGetMessageFunction( 'regular', 'plain', false );
686 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
687 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
688 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
690 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
696 number
: 987654321.654321,
697 result
: '987,654,321.654',
698 description
: 'formatnum test for English, decimal separator'
702 number
: 987654321.654321,
703 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
704 description
: 'formatnum test for Arabic, with decimal separator'
708 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
711 description
: 'formatnum test for Arabic, with decimal separator, reverse'
717 description
: 'formatnum test for Arabic, negative number'
724 description
: 'formatnum test for Arabic, negative number, reverse'
728 number
: 987654321.654321,
729 result
: '987.654.321,654',
730 description
: 'formatnum test for Nederlands, decimal separator'
736 description
: 'formatnum test for Nederlands, negative number'
742 description
: 'formatnum test for Nederlands'
746 number
: 'invalidnumber',
747 result
: 'invalidnumber',
748 description
: 'formatnum test for Nederlands, invalid number'
752 number
: '1000000000',
753 result
: '1,00,00,00,000',
754 description
: 'formatnum test for Malayalam'
758 number
: '-1000000000',
759 result
: '-1,00,00,00,000',
760 description
: 'formatnum test for Malayalam, negative number'
763 * This will fail because of wrong pattern for ml in MW(different from CLDR)
766 number: '1000000000.000',
767 result: '1,00,00,00,000.000',
768 description: 'formatnum test for Malayalam with decimal place'
773 number
: '123456789.123456789',
774 result
: '१२,३४,५६,७८९',
775 description
: 'formatnum test for Hindi'
779 number
: '१२,३४,५६,७८९',
780 result
: '१२,३४,५६,७८९',
781 description
: 'formatnum test for Hindi, Devanagari digits passed'
785 number
: '१२३४५६,७८९',
788 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
792 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
793 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
794 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
795 var queue
= $.map( formatnumTests
, function ( test
) {
796 return function ( next
, abort
) {
797 getMwLanguage( test
.lang
)
798 .then( function ( langClass
) {
799 mw
.config
.set( 'wgUserLanguage', test
.lang
);
800 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
802 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
803 [ test
.number
] ).html(),
808 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
810 .then( next
, abort
);
814 process( queue
, QUnit
.start
);
818 QUnit
.test( 'HTML', 32, function ( assert
) {
819 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
821 assertBothModes( assert
, [ 'jquerymsg-italics-msg' ], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
823 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
824 assertBothModes( assert
, [ 'jquerymsg-bold-msg' ], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
826 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
827 assertBothModes( assert
, [ 'jquerymsg-bold-italics-msg' ], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
829 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
830 assertBothModes( assert
, [ 'jquerymsg-italics-bold-msg' ], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
832 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
835 formatParse( 'jquerymsg-italics-with-link' ),
836 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
837 'Italics with link inside in parse mode'
841 formatText( 'jquerymsg-italics-with-link' ),
842 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
843 'Italics with link unchanged in text mode'
846 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
848 formatParse( 'jquerymsg-italics-id-class' ),
849 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
850 'ID and class are allowed'
853 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
855 formatParse( 'jquerymsg-italics-onclick' ),
856 '<i onclick="alert(\'foo\')">Foo</i>',
857 'element with onclick is escaped because it is not allowed'
860 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
862 formatParse( 'jquerymsg-script-msg' ),
863 '<script >alert( "Who put this tag here?" );</script>',
864 'Tag outside whitelist escaped in parse mode'
868 formatText( 'jquerymsg-script-msg' ),
869 mw
.messages
.get( 'jquerymsg-script-msg' ),
870 'Tag outside whitelist unchanged in text mode'
873 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
875 formatParse( 'jquerymsg-script-link-msg' ),
876 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
877 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
880 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
882 formatParse( 'jquerymsg-mismatched-html' ),
883 '<i class="important">test</b>',
884 'Mismatched HTML start and end tag treated as text'
887 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
889 formatParse( 'jquerymsg-script-and-external-link' ),
890 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><i>Foo</i> bar</a>',
891 'HTML tags in external links not interfering with escaping of other tags'
894 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
896 formatParse( 'jquerymsg-link-script' ),
897 '<a href="http://example.com"><script>alert( "jquerymsg-link-script test" );</script></a>',
898 'Non-whitelisted HTML tag in external link anchor treated as text'
901 // Intentionally not using htmlEqual for the quote tests
902 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
904 formatParse( 'jquerymsg-double-quotes-preserved' ),
905 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
906 'Attributes with double quotes are preserved as such'
909 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
911 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
912 '<i id="single">Single</i>',
913 'Attributes with single quotes are normalized to double'
916 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
918 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
919 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
920 'Escaped attributes are parsed correctly'
923 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
925 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
926 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
927 'Escaped attributes are parsed correctly'
930 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
932 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
933 '<i><a href="http://example.com">Example</a></i>',
934 'Contents of valid tag are treated as wikitext, so external link is parsed'
937 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
939 formatParse( 'jquerymsg-wikitext-contents-script' ),
940 '<i><script>Script inside</script></i>',
941 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
944 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
946 formatParse( 'jquerymsg-unclosed-tag' ),
948 'Nonsupported unclosed tags are escaped'
951 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
953 formatParse( 'jquerymsg-self-closing-tag' ),
954 'Foo<tag/>bar',
955 'Self-closing tags don\'t cause a parse error'
958 mw
.messages
.set( 'jquerymsg-entities1', 'A&B' );
959 mw
.messages
.set( 'jquerymsg-entities2', 'A>B' );
960 mw
.messages
.set( 'jquerymsg-entities3', 'A→B' );
962 formatParse( 'jquerymsg-entities1' ),
964 'Lone "&" is escaped in text'
967 formatParse( 'jquerymsg-entities2' ),
969 '">" entity is double-escaped in text' // (WHY?)
972 formatParse( 'jquerymsg-entities3' ),
974 '"→" entity is double-escaped in text'
977 mw
.messages
.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
978 mw
.messages
.set( 'jquerymsg-entities-attr2', '<i title="A>B"></i>' );
979 mw
.messages
.set( 'jquerymsg-entities-attr3', '<i title="A→B"></i>' );
981 formatParse( 'jquerymsg-entities-attr1' ),
982 '<i title="A&B"></i>',
983 'Lone "&" is escaped in attribute'
986 formatParse( 'jquerymsg-entities-attr2' ),
987 '<i title="A>B"></i>',
988 '">" entity is not double-escaped in attribute' // (WHY?)
991 formatParse( 'jquerymsg-entities-attr3' ),
992 '<i title="A&rarr;B"></i>',
993 '"→" entity is double-escaped in attribute'
997 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
998 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1000 this.suppressWarnings();
1001 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1004 formatParse( 'invalid-wikitext' ),
1005 '<b>{{FAIL}}</b>',
1006 'Invalid wikitext: \'parse\' format'
1010 formatText( 'invalid-wikitext' ),
1012 'Invalid wikitext: \'text\' format'
1015 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
1018 }( mediaWiki
, jQuery
) );