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', 14, 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 mw
.messages
.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
380 mw
.messages
.set( 'empty-link', '[[]]' );
381 this.suppressWarnings();
383 formatParse( 'pipe-trick' ),
384 '[[Tampa, Florida|]]',
385 'Pipe trick should not be parsed.'
388 formatParse( 'reverse-pipe-trick' ),
389 '[[|Tampa, Florida]]',
390 'Reverse pipe trick should not be parsed.'
393 formatParse( 'empty-link' ),
395 'Empty link should not be parsed.'
397 this.restoreWarnings();
399 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
400 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
402 formatParse( 'multiple-bars' ),
403 expectedMultipleBars
,
407 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>';
409 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
411 formatParse( 'special-characters' ),
412 expectedSpecialCharacters
,
416 mw
.messages
.set( 'leading-colon', '[[:File:Foo.jpg]]' );
418 formatParse( 'leading-colon' ),
419 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
420 'Leading colon in links is stripped'
424 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
425 expectedListUsersSitename
,
426 'Piped wikilink with parser function in the text'
432 'asd [http://example.org <strong>Example</strong>] asd',
433 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
436 'extlink-html-partial',
437 'asd [http://example.org foo <strong>Example</strong> bar] asd',
438 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
441 'wikilink-html-full',
442 'asd [[Example|<strong>Example</strong>]] asd',
443 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
446 'wikilink-html-partial',
447 'asd [[Example|foo <strong>Example</strong> bar]] asd',
448 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
452 $.each( testCases
, function () {
457 mw
.messages
.set( key
, input
);
461 'HTML in links: ' + key
466 QUnit
.test( 'Replacements in links', 14, function ( assert
) {
469 'extlink-param-href-full',
470 'asd [$1 Example] asd',
471 'asd <a href="http://example.com">Example</a> asd'
474 'extlink-param-href-partial',
475 'asd [$1/example Example] asd',
476 'asd <a href="http://example.com/example">Example</a> asd'
479 'extlink-param-text-full',
480 'asd [http://example.org $2] asd',
481 'asd <a href="http://example.org">Text</a> asd'
484 'extlink-param-text-partial',
485 'asd [http://example.org Example $2] asd',
486 'asd <a href="http://example.org">Example Text</a> asd'
489 'extlink-param-both-full',
491 'asd <a href="http://example.com">Text</a> asd'
494 'extlink-param-both-partial',
495 'asd [$1/example Example $2] asd',
496 'asd <a href="http://example.com/example">Example Text</a> asd'
499 'wikilink-param-href-full',
500 'asd [[$1|Example]] asd',
501 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
504 'wikilink-param-href-partial',
505 'asd [[$1/Test|Example]] asd',
506 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
509 'wikilink-param-text-full',
510 'asd [[Example|$2]] asd',
511 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
514 'wikilink-param-text-partial',
515 'asd [[Example|Example $2]] asd',
516 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
519 'wikilink-param-both-full',
521 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
524 'wikilink-param-both-partial',
525 'asd [[$1/Test|Example $2]] asd',
526 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
529 'wikilink-param-unpiped-full',
531 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
534 'wikilink-param-unpiped-partial',
535 'asd [[$1/Test]] asd',
536 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
540 $.each( testCases
, function () {
545 paramHref
= key
.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
547 mw
.messages
.set( key
, input
);
549 formatParse( key
, paramHref
, paramText
),
551 'Replacements in links: ' + key
556 // Tests that {{-transformation vs. general parsing are done as requested
557 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
558 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
560 assertBothModes( assert
, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
562 assertBothModes( assert
, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
564 assertBothModes( assert
, [ 'grammar-msg' ], 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'grammar is resolved' );
566 mw
.config
.set( 'wgUserLanguage', 'en' );
567 assertBothModes( assert
, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
569 // Test non-{{ wikitext, where behavior differs
573 formatText( 'jquerymsg-test-statistics-users' ),
574 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
575 'Internal link message unchanged when format is \'text\''
578 formatParse( 'jquerymsg-test-statistics-users' ),
580 'Internal link message parsed when format is \'parse\''
585 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
586 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
587 'External link message unchanged when format is \'text\''
590 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
592 'External link message processed when format is \'parse\''
595 // External link with parameter
597 formatText( 'external-link-replace', 'http://example.com' ),
598 'Foo [http://example.com bar]',
599 'External link message only substitutes parameter when format is \'text\''
602 formatParse( 'external-link-replace', 'http://example.com' ),
603 'Foo <a href="http://example.com">bar</a>',
604 'External link message processed when format is \'parse\''
607 formatParse( 'external-link-replace', $( '<i>' ) ),
609 'External link message processed as jQuery object when format is \'parse\''
612 formatParse( 'external-link-replace', function () {} ),
613 'Foo <a href="#">bar</a>',
614 'External link message processed as function when format is \'parse\''
617 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
620 QUnit
.test( 'Int', 4, function ( assert
) {
621 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.',
622 expectedNewarticletext
,
623 helpPageTitle
= 'Help:Foobar';
625 mw
.messages
.set( 'foobar', helpPageTitle
);
627 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 ' +
628 '<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.';
630 mw
.messages
.set( 'newarticletext', newarticletextSource
);
633 formatParse( 'newarticletext' ),
634 expectedNewarticletext
,
635 'Link with nested message'
639 formatParse( 'see-portal-url' ),
640 'Project:Community portal is an important community page.',
644 mw
.messages
.set( 'newarticletext-lowercase',
645 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
648 formatParse( 'newarticletext-lowercase' ),
649 expectedNewarticletext
,
650 'Link with nested message, lowercase include'
653 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
656 formatParse( 'uses-missing-int' ),
658 'int: where nested message does not exist'
662 // Tests that getMessageFunction is used for non-plain messages with curly braces or
663 // square brackets, but not otherwise.
664 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
665 var oldGMF
, outerCalled
, innerCalled
;
668 'curly-brace': '{{int:message}}',
669 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
670 'double-square-bracket': '[[Some page]]',
671 regular
: 'Other message'
674 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
676 mw
.jqueryMsg
.getMessageFunction = function () {
683 function verifyGetMessageFunction( key
, format
, shouldCall
) {
687 message
= mw
.message( key
);
689 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
690 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
693 verifyGetMessageFunction( 'curly-brace', 'parse', true );
694 verifyGetMessageFunction( 'curly-brace', 'plain', false );
696 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
697 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
699 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
700 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
702 verifyGetMessageFunction( 'regular', 'parse', false );
703 verifyGetMessageFunction( 'regular', 'plain', false );
705 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
706 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
707 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
709 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
715 number
: 987654321.654321,
716 result
: '987,654,321.654',
717 description
: 'formatnum test for English, decimal separator'
721 number
: 987654321.654321,
722 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
723 description
: 'formatnum test for Arabic, with decimal separator'
727 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
730 description
: 'formatnum test for Arabic, with decimal separator, reverse'
736 description
: 'formatnum test for Arabic, negative number'
743 description
: 'formatnum test for Arabic, negative number, reverse'
747 number
: 987654321.654321,
748 result
: '987.654.321,654',
749 description
: 'formatnum test for Nederlands, decimal separator'
755 description
: 'formatnum test for Nederlands, negative number'
761 description
: 'formatnum test for Nederlands'
765 number
: 'invalidnumber',
766 result
: 'invalidnumber',
767 description
: 'formatnum test for Nederlands, invalid number'
771 number
: '1000000000',
772 result
: '1,00,00,00,000',
773 description
: 'formatnum test for Malayalam'
777 number
: '-1000000000',
778 result
: '-1,00,00,00,000',
779 description
: 'formatnum test for Malayalam, negative number'
782 * This will fail because of wrong pattern for ml in MW(different from CLDR)
785 number: '1000000000.000',
786 result: '1,00,00,00,000.000',
787 description: 'formatnum test for Malayalam with decimal place'
792 number
: '123456789.123456789',
793 result
: '१२,३४,५६,७८९',
794 description
: 'formatnum test for Hindi'
798 number
: '१२,३४,५६,७८९',
799 result
: '१२,३४,५६,७८९',
800 description
: 'formatnum test for Hindi, Devanagari digits passed'
804 number
: '१२३४५६,७८९',
807 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
811 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
812 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
813 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
814 var queue
= $.map( formatnumTests
, function ( test
) {
815 return function ( next
, abort
) {
816 getMwLanguage( test
.lang
)
817 .then( function ( langClass
) {
818 mw
.config
.set( 'wgUserLanguage', test
.lang
);
819 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
821 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
822 [ test
.number
] ).html(),
827 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
829 .then( next
, abort
);
833 process( queue
, QUnit
.start
);
837 QUnit
.test( 'HTML', 32, function ( assert
) {
838 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
840 assertBothModes( assert
, [ 'jquerymsg-italics-msg' ], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
842 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
843 assertBothModes( assert
, [ 'jquerymsg-bold-msg' ], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
845 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
846 assertBothModes( assert
, [ 'jquerymsg-bold-italics-msg' ], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
848 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
849 assertBothModes( assert
, [ 'jquerymsg-italics-bold-msg' ], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
851 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
854 formatParse( 'jquerymsg-italics-with-link' ),
855 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
856 'Italics with link inside in parse mode'
860 formatText( 'jquerymsg-italics-with-link' ),
861 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
862 'Italics with link unchanged in text mode'
865 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
867 formatParse( 'jquerymsg-italics-id-class' ),
868 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
869 'ID and class are allowed'
872 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
874 formatParse( 'jquerymsg-italics-onclick' ),
875 '<i onclick="alert(\'foo\')">Foo</i>',
876 'element with onclick is escaped because it is not allowed'
879 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
881 formatParse( 'jquerymsg-script-msg' ),
882 '<script >alert( "Who put this tag here?" );</script>',
883 'Tag outside whitelist escaped in parse mode'
887 formatText( 'jquerymsg-script-msg' ),
888 mw
.messages
.get( 'jquerymsg-script-msg' ),
889 'Tag outside whitelist unchanged in text mode'
892 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
894 formatParse( 'jquerymsg-script-link-msg' ),
895 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
896 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
899 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
901 formatParse( 'jquerymsg-mismatched-html' ),
902 '<i class="important">test</b>',
903 'Mismatched HTML start and end tag treated as text'
906 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
908 formatParse( 'jquerymsg-script-and-external-link' ),
909 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><i>Foo</i> bar</a>',
910 'HTML tags in external links not interfering with escaping of other tags'
913 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
915 formatParse( 'jquerymsg-link-script' ),
916 '<a href="http://example.com"><script>alert( "jquerymsg-link-script test" );</script></a>',
917 'Non-whitelisted HTML tag in external link anchor treated as text'
920 // Intentionally not using htmlEqual for the quote tests
921 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
923 formatParse( 'jquerymsg-double-quotes-preserved' ),
924 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
925 'Attributes with double quotes are preserved as such'
928 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
930 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
931 '<i id="single">Single</i>',
932 'Attributes with single quotes are normalized to double'
935 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
937 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
938 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
939 'Escaped attributes are parsed correctly'
942 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
944 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
945 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
946 'Escaped attributes are parsed correctly'
949 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
951 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
952 '<i><a href="http://example.com">Example</a></i>',
953 'Contents of valid tag are treated as wikitext, so external link is parsed'
956 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
958 formatParse( 'jquerymsg-wikitext-contents-script' ),
959 '<i><script>Script inside</script></i>',
960 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
963 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
965 formatParse( 'jquerymsg-unclosed-tag' ),
967 'Nonsupported unclosed tags are escaped'
970 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
972 formatParse( 'jquerymsg-self-closing-tag' ),
973 'Foo<tag/>bar',
974 'Self-closing tags don\'t cause a parse error'
977 mw
.messages
.set( 'jquerymsg-entities1', 'A&B' );
978 mw
.messages
.set( 'jquerymsg-entities2', 'A>B' );
979 mw
.messages
.set( 'jquerymsg-entities3', 'A→B' );
981 formatParse( 'jquerymsg-entities1' ),
983 'Lone "&" is escaped in text'
986 formatParse( 'jquerymsg-entities2' ),
988 '">" entity is double-escaped in text' // (WHY?)
991 formatParse( 'jquerymsg-entities3' ),
993 '"→" entity is double-escaped in text'
996 mw
.messages
.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
997 mw
.messages
.set( 'jquerymsg-entities-attr2', '<i title="A>B"></i>' );
998 mw
.messages
.set( 'jquerymsg-entities-attr3', '<i title="A→B"></i>' );
1000 formatParse( 'jquerymsg-entities-attr1' ),
1001 '<i title="A&B"></i>',
1002 'Lone "&" is escaped in attribute'
1005 formatParse( 'jquerymsg-entities-attr2' ),
1006 '<i title="A>B"></i>',
1007 '">" entity is not double-escaped in attribute' // (WHY?)
1010 formatParse( 'jquerymsg-entities-attr3' ),
1011 '<i title="A&rarr;B"></i>',
1012 '"→" entity is double-escaped in attribute'
1016 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
1017 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1019 this.suppressWarnings();
1020 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1023 formatParse( 'invalid-wikitext' ),
1024 '<b>{{FAIL}}</b>',
1025 'Invalid wikitext: \'parse\' format'
1029 formatText( 'invalid-wikitext' ),
1031 'Invalid wikitext: \'text\' format'
1034 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
1037 }( mediaWiki
, jQuery
) );