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