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