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