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