mediawiki.jqueryMsg: Support arbitrary expressions in plural forms
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.jqueryMsg.test.js
1 ( function ( mw, $ ) {
2 var mwLanguageCache = {}, formatText, formatParse, formatnumTests, specialCharactersPageName,
3 expectedListUsers, expectedEntrypoints;
4
5 // When the expected result is the same in both modes
6 function assertBothModes( assert, parserArguments, expectedResult, assertMessage ) {
7 assert.equal( formatText.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' );
8 assert.equal( formatParse.apply( null, parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' );
9 }
10
11 QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
12 setup: function () {
13 this.originalMwLanguage = mw.language;
14
15 specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
16
17 expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
18
19 expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
20
21 formatText = mw.jqueryMsg.getMessageFunction( {
22 format: 'text'
23 } );
24
25 formatParse = mw.jqueryMsg.getMessageFunction( {
26 format: 'parse'
27 } );
28 },
29 teardown: function () {
30 mw.language = this.originalMwLanguage;
31 },
32 config: {
33 wgArticlePath: '/wiki/$1'
34 },
35 // Messages that are reused in multiple tests
36 messages: {
37 // The values for gender are not significant,
38 // what matters is which of the values is choosen by the parser
39 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
40 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
41
42 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
43 // See https://bugzilla.wikimedia.org/69993
44 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
45 // Assume the grammar form grammar_case_foo is not valid in any language
46 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
47
48 'formatnum-msg': '{{formatnum:$1}}',
49
50 'portal-url': 'Project:Community portal',
51 'see-portal-url': '{{Int:portal-url}} is an important community page.',
52
53 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
54
55 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
56
57 'external-link-replace': 'Foo [$1 bar]',
58 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
59 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
60 }
61 } ) );
62
63 function getMwLanguage( langCode, cb ) {
64 if ( mwLanguageCache[langCode] !== undefined ) {
65 mwLanguageCache[langCode].add( cb );
66 return;
67 }
68 mwLanguageCache[langCode] = $.Callbacks( 'once memory' );
69 mwLanguageCache[langCode].add( cb );
70 $.ajax( {
71 url: mw.util.wikiScript( 'load' ),
72 data: {
73 skin: mw.config.get( 'skin' ),
74 lang: langCode,
75 debug: mw.config.get( 'debug' ),
76 modules: [
77 'mediawiki.language.data',
78 'mediawiki.language'
79 ].join( '|' ),
80 only: 'scripts'
81 },
82 dataType: 'script'
83 } ).done(function () {
84 mwLanguageCache[langCode].fire( mw.language );
85 } ).fail( function () {
86 mwLanguageCache[langCode].fire( false );
87 } );
88 }
89
90 QUnit.test( 'Replace', 16, function ( assert ) {
91 mw.messages.set( 'simple', 'Foo $1 baz $2' );
92
93 assert.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
94 assert.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
95 assert.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
96
97 mw.messages.set( 'plain-input', '<foo foo="foo">x$1y&lt;</foo>z' );
98
99 assert.equal(
100 formatParse( 'plain-input', 'bar' ),
101 '&lt;foo foo="foo"&gt;xbary&amp;lt;&lt;/foo&gt;z',
102 'Input is not considered html'
103 );
104
105 mw.messages.set( 'plain-replace', 'Foo $1' );
106
107 assert.equal(
108 formatParse( 'plain-replace', '<bar bar="bar">&gt;</bar>' ),
109 'Foo &lt;bar bar="bar"&gt;&amp;gt;&lt;/bar&gt;',
110 'Replacement is not considered html'
111 );
112
113 mw.messages.set( 'object-replace', 'Foo $1' );
114
115 assert.equal(
116 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ) ),
117 'Foo <div class="bar">&gt;</div>',
118 'jQuery objects are preserved as raw html'
119 );
120
121 assert.equal(
122 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
123 'Foo <div class="bar">&gt;</div>',
124 'HTMLElement objects are preserved as raw html'
125 );
126
127 assert.equal(
128 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).toArray() ),
129 'Foo <div class="bar">&gt;</div>',
130 'HTMLElement[] arrays are preserved as raw html'
131 );
132
133 assert.equal(
134 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
135 'Foo <a href="http://example.org/?x=y&amp;z">bar</a>',
136 'Href is not double-escaped in wikilink function'
137 );
138 assert.equal(
139 formatParse( 'external-link-plural', 1, 'http://example.org' ),
140 'Foo is <a href="http://example.org">one</a> things.',
141 'Link is expanded inside plural and is not escaped html'
142 );
143 assert.equal(
144 formatParse( 'external-link-plural', 2, 'http://example.org' ),
145 'Foo <a href=\"http://example.org\">two</a> things.',
146 'Link is expanded inside an explicit plural form and is not escaped html'
147 );
148 assert.equal(
149 formatParse( 'external-link-plural', 3 ),
150 'Foo three things.',
151 'A simple explicit plural form co-existing with complex explicit plural forms'
152 );
153 assert.equal(
154 formatParse( 'external-link-plural', 4, 'http://example.org' ),
155 'Foo a=b things.',
156 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
157 );
158 assert.equal(
159 formatParse( 'external-link-plural', 5, 'http://example.org' ),
160 'Foo are <a href="http://example.org">some</a> things.',
161 'Invalid explicit plural form. Plural fallback to the "other" plural form'
162 );
163 assert.equal(
164 formatParse( 'external-link-plural', 6, 'http://example.org' ),
165 'Foo are <a href="http://example.org">some</a> things.',
166 'Plural fallback to the "other" plural form'
167 );
168 assert.equal(
169 formatParse( 'plural-only-explicit-forms', 2 ),
170 'It is a double room.',
171 'Plural with explicit forms alone.'
172 );
173 } );
174
175 QUnit.test( 'Plural', 6, function ( assert ) {
176 assert.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
177 assert.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
178 assert.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
179 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
180 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}}' );
181 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
182 } );
183
184 QUnit.test( 'Gender', 15, function ( assert ) {
185 var originalGender = mw.user.options.get( 'gender' );
186
187 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
188 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
189 mw.user.options.set( 'gender', 'male' );
190 assert.equal(
191 formatParse( 'gender-msg', 'Bob', 'male' ),
192 'Bob: blue',
193 'Masculine from string "male"'
194 );
195 assert.equal(
196 formatParse( 'gender-msg', 'Bob', mw.user ),
197 'Bob: blue',
198 'Masculine from mw.user object'
199 );
200 assert.equal(
201 formatParse( 'gender-msg-currentuser' ),
202 'blue',
203 'Masculine for current user'
204 );
205
206 mw.user.options.set( 'gender', 'female' );
207 assert.equal(
208 formatParse( 'gender-msg', 'Alice', 'female' ),
209 'Alice: pink',
210 'Feminine from string "female"' );
211 assert.equal(
212 formatParse( 'gender-msg', 'Alice', mw.user ),
213 'Alice: pink',
214 'Feminine from mw.user object'
215 );
216 assert.equal(
217 formatParse( 'gender-msg-currentuser' ),
218 'pink',
219 'Feminine for current user'
220 );
221
222 mw.user.options.set( 'gender', 'unknown' );
223 assert.equal(
224 formatParse( 'gender-msg', 'Foo', mw.user ),
225 'Foo: green',
226 'Neutral from mw.user object' );
227 assert.equal(
228 formatParse( 'gender-msg', 'User' ),
229 'User: green',
230 'Neutral when no parameter given' );
231 assert.equal(
232 formatParse( 'gender-msg', 'User', 'unknown' ),
233 'User: green',
234 'Neutral from string "unknown"'
235 );
236 assert.equal(
237 formatParse( 'gender-msg-currentuser' ),
238 'green',
239 'Neutral for current user'
240 );
241
242 mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
243
244 assert.equal(
245 formatParse( 'gender-msg-one-form', 'male', 10 ),
246 'User: 10 edits',
247 'Gender neutral and plural form'
248 );
249 assert.equal(
250 formatParse( 'gender-msg-one-form', 'female', 1 ),
251 'User: 1 edit',
252 'Gender neutral and singular form'
253 );
254
255 mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
256 assert.equal(
257 formatParse( 'gender-msg-lowercase', 'male' ),
258 'he is awesome',
259 'Gender masculine'
260 );
261 assert.equal(
262 formatParse( 'gender-msg-lowercase', 'female' ),
263 'she is awesome',
264 'Gender feminine'
265 );
266
267 mw.messages.set( 'gender-msg-wrong', '{{gender}} test' );
268 assert.equal(
269 formatParse( 'gender-msg-wrong', 'female' ),
270 ' test',
271 'Invalid syntax should result in {{gender}} simply being stripped away'
272 );
273
274 mw.user.options.set( 'gender', originalGender );
275 } );
276
277 QUnit.test( 'Grammar', 2, function ( assert ) {
278 assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'Grammar Test with sitename' );
279
280 mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
281 assert.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
282 } );
283
284 QUnit.test( 'Match PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
285 mw.messages.set( mw.libs.phpParserData.messages );
286 $.each( mw.libs.phpParserData.tests, function ( i, test ) {
287 QUnit.stop();
288 getMwLanguage( test.lang, function ( langClass ) {
289 QUnit.start();
290 if ( !langClass ) {
291 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
292 return;
293 }
294 mw.config.set( 'wgUserLanguage', test.lang );
295 var parser = new mw.jqueryMsg.parser( { language: langClass } );
296 assert.equal(
297 parser.parse( test.key, test.args ).html(),
298 test.result,
299 test.name
300 );
301 } );
302 } );
303 } );
304
305 QUnit.test( 'Links', 6, function ( assert ) {
306 var expectedDisambiguationsText,
307 expectedMultipleBars,
308 expectedSpecialCharacters;
309
310 // The below three are all identical to or based on real messages. For disambiguations-text,
311 // the bold was removed because it is not yet implemented.
312
313 assert.htmlEqual(
314 formatParse( 'jquerymsg-test-statistics-users' ),
315 expectedListUsers,
316 'Piped wikilink'
317 );
318
319 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 ' +
320 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
321
322 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]].' );
323 assert.htmlEqual(
324 formatParse( 'disambiguations-text' ),
325 expectedDisambiguationsText,
326 'Wikilink without pipe'
327 );
328
329 assert.htmlEqual(
330 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
331 expectedEntrypoints,
332 'External link'
333 );
334
335 // Pipe trick is not supported currently, but should not parse as text either.
336 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
337 this.suppressWarnings();
338 assert.equal(
339 formatParse( 'pipe-trick' ),
340 '[[Tampa, Florida|]]',
341 'Pipe trick should not be parsed.'
342 );
343 this.restoreWarnings();
344
345 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
346 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
347 assert.htmlEqual(
348 formatParse( 'multiple-bars' ),
349 expectedMultipleBars,
350 'Bar in anchor'
351 );
352
353 expectedSpecialCharacters = '<a title="&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?" href="/wiki/%22Who%22_wants_to_be_a_millionaire_%26_live_on_%27Exotic_Island%27%3F">&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?</a>';
354
355 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
356 assert.htmlEqual(
357 formatParse( 'special-characters' ),
358 expectedSpecialCharacters,
359 'Special characters'
360 );
361 } );
362
363 // Tests that {{-transformation vs. general parsing are done as requested
364 QUnit.test( 'Curly brace transformation', 16, function ( assert ) {
365 var oldUserLang = mw.config.get( 'wgUserLanguage' );
366
367 assertBothModes( assert, ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' );
368
369 assertBothModes( assert, ['plural-msg', 5], 'Found 5 items', 'plural is resolved' );
370
371 assertBothModes( assert, ['grammar-msg'], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' );
372
373 mw.config.set( 'wgUserLanguage', 'en' );
374 assertBothModes( assert, ['formatnum-msg', '987654321.654321'], '987,654,321.654', 'formatnum is resolved' );
375
376 // Test non-{{ wikitext, where behavior differs
377
378 // Wikilink
379 assert.equal(
380 formatText( 'jquerymsg-test-statistics-users' ),
381 mw.messages.get( 'jquerymsg-test-statistics-users' ),
382 'Internal link message unchanged when format is \'text\''
383 );
384 assert.htmlEqual(
385 formatParse( 'jquerymsg-test-statistics-users' ),
386 expectedListUsers,
387 'Internal link message parsed when format is \'parse\''
388 );
389
390 // External link
391 assert.equal(
392 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
393 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
394 'External link message unchanged when format is \'text\''
395 );
396 assert.htmlEqual(
397 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
398 expectedEntrypoints,
399 'External link message processed when format is \'parse\''
400 );
401
402 // External link with parameter
403 assert.equal(
404 formatText( 'external-link-replace', 'http://example.com' ),
405 'Foo [http://example.com bar]',
406 'External link message only substitutes parameter when format is \'text\''
407 );
408 assert.htmlEqual(
409 formatParse( 'external-link-replace', 'http://example.com' ),
410 'Foo <a href="http://example.com">bar</a>',
411 'External link message processed when format is \'parse\''
412 );
413 assert.htmlEqual(
414 formatParse( 'external-link-replace', $( '<i>' ) ),
415 'Foo <i>bar</i>',
416 'External link message processed as jQuery object when format is \'parse\''
417 );
418 assert.htmlEqual(
419 formatParse( 'external-link-replace', function () {} ),
420 'Foo <a href="#">bar</a>',
421 'External link message processed as function when format is \'parse\''
422 );
423
424 mw.config.set( 'wgUserLanguage', oldUserLang );
425 } );
426
427 QUnit.test( 'Int', 4, function ( assert ) {
428 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.',
429 expectedNewarticletext,
430 helpPageTitle = 'Help:Foobar';
431
432 mw.messages.set( 'foobar', helpPageTitle );
433
434 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 ' +
435 '<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.';
436
437 mw.messages.set( 'newarticletext', newarticletextSource );
438
439 assert.htmlEqual(
440 formatParse( 'newarticletext' ),
441 expectedNewarticletext,
442 'Link with nested message'
443 );
444
445 assert.equal(
446 formatParse( 'see-portal-url' ),
447 'Project:Community portal is an important community page.',
448 'Nested message'
449 );
450
451 mw.messages.set( 'newarticletext-lowercase',
452 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
453
454 assert.htmlEqual(
455 formatParse( 'newarticletext-lowercase' ),
456 expectedNewarticletext,
457 'Link with nested message, lowercase include'
458 );
459
460 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
461
462 assert.equal(
463 formatParse( 'uses-missing-int' ),
464 '[doesnt-exist]',
465 'int: where nested message does not exist'
466 );
467 } );
468
469 // Tests that getMessageFunction is used for non-plain messages with curly braces or
470 // square brackets, but not otherwise.
471 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
472 var oldGMF, outerCalled, innerCalled;
473
474 mw.messages.set( {
475 'curly-brace': '{{int:message}}',
476 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
477 'double-square-bracket': '[[Some page]]',
478 'regular': 'Other message'
479 } );
480
481 oldGMF = mw.jqueryMsg.getMessageFunction;
482
483 mw.jqueryMsg.getMessageFunction = function () {
484 outerCalled = true;
485 return function () {
486 innerCalled = true;
487 };
488 };
489
490 function verifyGetMessageFunction( key, format, shouldCall ) {
491 var message;
492 outerCalled = false;
493 innerCalled = false;
494 message = mw.message( key );
495 message[format]();
496 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
497 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
498 }
499
500 verifyGetMessageFunction( 'curly-brace', 'parse', true );
501 verifyGetMessageFunction( 'curly-brace', 'plain', false );
502
503 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
504 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
505
506 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
507 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
508
509 verifyGetMessageFunction( 'regular', 'parse', false );
510 verifyGetMessageFunction( 'regular', 'plain', false );
511
512 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
513 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
514 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
515
516 mw.jqueryMsg.getMessageFunction = oldGMF;
517 } );
518
519 formatnumTests = [
520 {
521 lang: 'en',
522 number: 987654321.654321,
523 result: '987,654,321.654',
524 description: 'formatnum test for English, decimal seperator'
525 },
526 {
527 lang: 'ar',
528 number: 987654321.654321,
529 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
530 description: 'formatnum test for Arabic, with decimal seperator'
531 },
532 {
533 lang: 'ar',
534 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
535 result: 987654321,
536 integer: true,
537 description: 'formatnum test for Arabic, with decimal seperator, reverse'
538 },
539 {
540 lang: 'ar',
541 number: -12.89,
542 result: '-١٢٫٨٩',
543 description: 'formatnum test for Arabic, negative number'
544 },
545 {
546 lang: 'ar',
547 number: '-١٢٫٨٩',
548 result: -12,
549 integer: true,
550 description: 'formatnum test for Arabic, negative number, reverse'
551 },
552 {
553 lang: 'nl',
554 number: 987654321.654321,
555 result: '987.654.321,654',
556 description: 'formatnum test for Nederlands, decimal seperator'
557 },
558 {
559 lang: 'nl',
560 number: -12.89,
561 result: '-12,89',
562 description: 'formatnum test for Nederlands, negative number'
563 },
564 {
565 lang: 'nl',
566 number: '.89',
567 result: '0,89',
568 description: 'formatnum test for Nederlands'
569 },
570 {
571 lang: 'nl',
572 number: 'invalidnumber',
573 result: 'invalidnumber',
574 description: 'formatnum test for Nederlands, invalid number'
575 },
576 {
577 lang: 'ml',
578 number: '1000000000',
579 result: '1,00,00,00,000',
580 description: 'formatnum test for Malayalam'
581 },
582 {
583 lang: 'ml',
584 number: '-1000000000',
585 result: '-1,00,00,00,000',
586 description: 'formatnum test for Malayalam, negative number'
587 },
588 /*
589 * This will fail because of wrong pattern for ml in MW(different from CLDR)
590 {
591 lang: 'ml',
592 number: '1000000000.000',
593 result: '1,00,00,00,000.000',
594 description: 'formatnum test for Malayalam with decimal place'
595 },
596 */
597 {
598 lang: 'hi',
599 number: '123456789.123456789',
600 result: '१२,३४,५६,७८९',
601 description: 'formatnum test for Hindi'
602 },
603 {
604 lang: 'hi',
605 number: '१२,३४,५६,७८९',
606 result: '१२,३४,५६,७८९',
607 description: 'formatnum test for Hindi, Devanagari digits passed'
608 },
609 {
610 lang: 'hi',
611 number: '१२३४५६,७८९',
612 result: '123456',
613 integer: true,
614 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
615 }
616 ];
617
618 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
619 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
620 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
621 $.each( formatnumTests, function ( i, test ) {
622 QUnit.stop();
623 getMwLanguage( test.lang, function ( langClass ) {
624 QUnit.start();
625 if ( !langClass ) {
626 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
627 return;
628 }
629 mw.messages.set(test.message );
630 mw.config.set( 'wgUserLanguage', test.lang );
631 var parser = new mw.jqueryMsg.parser( { language: langClass } );
632 assert.equal(
633 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
634 [ test.number ] ).html(),
635 test.result,
636 test.description
637 );
638 } );
639 } );
640 } );
641
642 // HTML in wikitext
643 QUnit.test( 'HTML', 26, function ( assert ) {
644 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
645
646 assertBothModes( assert, ['jquerymsg-italics-msg'], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
647
648 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
649 assertBothModes( assert, ['jquerymsg-bold-msg'], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
650
651 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
652 assertBothModes( assert, ['jquerymsg-bold-italics-msg'], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
653
654 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
655 assertBothModes( assert, ['jquerymsg-italics-bold-msg'], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
656
657 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
658
659 assert.htmlEqual(
660 formatParse( 'jquerymsg-italics-with-link' ),
661 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
662 'Italics with link inside in parse mode'
663 );
664
665 assert.equal(
666 formatText( 'jquerymsg-italics-with-link' ),
667 mw.messages.get( 'jquerymsg-italics-with-link' ),
668 'Italics with link unchanged in text mode'
669 );
670
671 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
672 assert.htmlEqual(
673 formatParse( 'jquerymsg-italics-id-class' ),
674 mw.messages.get( 'jquerymsg-italics-id-class' ),
675 'ID and class are allowed'
676 );
677
678 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
679 assert.htmlEqual(
680 formatParse( 'jquerymsg-italics-onclick' ),
681 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
682 'element with onclick is escaped because it is not allowed'
683 );
684
685 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
686 assert.htmlEqual(
687 formatParse( 'jquerymsg-script-msg' ),
688 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
689 'Tag outside whitelist escaped in parse mode'
690 );
691
692 assert.equal(
693 formatText( 'jquerymsg-script-msg' ),
694 mw.messages.get( 'jquerymsg-script-msg' ),
695 'Tag outside whitelist unchanged in text mode'
696 );
697
698 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
699 assert.htmlEqual(
700 formatParse( 'jquerymsg-script-link-msg' ),
701 '&lt;script&gt;<a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a>&lt;/script&gt;',
702 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
703 );
704
705 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
706 assert.htmlEqual(
707 formatParse( 'jquerymsg-mismatched-html' ),
708 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
709 'Mismatched HTML start and end tag treated as text'
710 );
711
712 // TODO (mattflaschen, 2013-03-18): It's not a security issue, but there's no real
713 // reason the htmlEmitter span needs to be here. It's an artifact of how emitting works.
714 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
715 assert.htmlEqual(
716 formatParse( 'jquerymsg-script-and-external-link' ),
717 '&lt;script&gt;alert( "jquerymsg-script-and-external-link test" );&lt;/script&gt; <a href="http://example.com"><span class="mediaWiki_htmlEmitter"><i>Foo</i> bar</span></a>',
718 'HTML tags in external links not interfering with escaping of other tags'
719 );
720
721 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
722 assert.htmlEqual(
723 formatParse( 'jquerymsg-link-script' ),
724 '<a href="http://example.com"><span class="mediaWiki_htmlEmitter">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</span></a>',
725 'Non-whitelisted HTML tag in external link anchor treated as text'
726 );
727
728 // Intentionally not using htmlEqual for the quote tests
729 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
730 assert.equal(
731 formatParse( 'jquerymsg-double-quotes-preserved' ),
732 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
733 'Attributes with double quotes are preserved as such'
734 );
735
736 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
737 assert.equal(
738 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
739 '<i id="single">Single</i>',
740 'Attributes with single quotes are normalized to double'
741 );
742
743 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:&quot;Arial&quot;">Styled</i>' );
744 assert.htmlEqual(
745 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
746 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
747 'Escaped attributes are parsed correctly'
748 );
749
750 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:&#039;Arial&#039;\'>Styled</i>' );
751 assert.htmlEqual(
752 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
753 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
754 'Escaped attributes are parsed correctly'
755 );
756
757 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
758 assert.htmlEqual(
759 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
760 '<i><a href="http://example.com">Example</a></i>',
761 'Contents of valid tag are treated as wikitext, so external link is parsed'
762 );
763
764 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
765 assert.htmlEqual(
766 formatParse( 'jquerymsg-wikitext-contents-script' ),
767 '<i><span class="mediaWiki_htmlEmitter">&lt;script&gt;Script inside&lt;/script&gt;</span></i>',
768 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
769 );
770
771 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
772 assert.htmlEqual(
773 formatParse( 'jquerymsg-unclosed-tag' ),
774 'Foo&lt;tag&gt;bar',
775 'Nonsupported unclosed tags are escaped'
776 );
777
778 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
779 assert.htmlEqual(
780 formatParse( 'jquerymsg-self-closing-tag' ),
781 'Foo&lt;tag/&gt;bar',
782 'Self-closing tags don\'t cause a parse error'
783 );
784 } );
785
786 QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
787 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
788
789 this.suppressWarnings();
790 var logSpy = this.sandbox.spy( mw.log, 'warn' );
791
792 assert.equal(
793 formatParse( 'invalid-wikitext' ),
794 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
795 'Invalid wikitext: \'parse\' format'
796 );
797
798 assert.equal(
799 formatText( 'invalid-wikitext' ),
800 '<b>{{FAIL}}</b>',
801 'Invalid wikitext: \'text\' format'
802 );
803
804 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
805 } );
806
807 }( mediaWiki, jQuery ) );