mediawiki.jqueryMsg: Don't duplicate link contents if parse() is called multiple...
[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 // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
39 wgNamespaceIds: {
40 template: 10,
41 template_talk: 11,
42 // Localised
43 szablon: 10,
44 dyskusja_szablonu: 11
45 },
46 // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
47 wgFormattedNamespaces: {
48 // Localised
49 10: 'Szablon',
50 11: 'Dyskusja szablonu'
51 }
52 },
53 // Messages that are reused in multiple tests
54 messages: {
55 // The values for gender are not significant,
56 // what matters is which of the values is choosen by the parser
57 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
58 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
59
60 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
61 // See https://phabricator.wikimedia.org/T71993
62 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
63 // Assume the grammar form grammar_case_foo is not valid in any language
64 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
65
66 'formatnum-msg': '{{formatnum:$1}}',
67
68 'portal-url': 'Project:Community portal',
69 'see-portal-url': '{{Int:portal-url}} is an important community page.',
70
71 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
72 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
73
74 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
75
76 'external-link-replace': 'Foo [$1 bar]',
77 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
78 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
79 }
80 } ) );
81
82 /**
83 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
84 * to transport the module back to the test. It musn't be overwritten concurrentely.
85 *
86 * This function caches the mw.language data to avoid having to request the same module
87 * multiple times. There is more than one test case for any given language.
88 */
89 function getMwLanguage( langCode ) {
90 if ( !hasOwn.call( mwLanguageCache, langCode ) ) {
91 mwLanguageCache[ langCode ] = $.ajax( {
92 url: mw.util.wikiScript( 'load' ),
93 data: {
94 skin: mw.config.get( 'skin' ),
95 lang: langCode,
96 debug: mw.config.get( 'debug' ),
97 modules: [
98 'mediawiki.language.data',
99 'mediawiki.language'
100 ].join( '|' ),
101 only: 'scripts'
102 },
103 dataType: 'script',
104 cache: true
105 } ).then( function () {
106 return mw.language;
107 } );
108 }
109 return mwLanguageCache[ langCode ];
110 }
111
112 /**
113 * @param {Function[]} tasks List of functions that perform tasks
114 * that may be asynchronous. Invoke the callback parameter when done.
115 * @param {Function} complete Called when all tasks are done, or when the sequence is aborted.
116 */
117 function process( tasks, complete ) {
118 /*jshint latedef:false */
119 function abort() {
120 tasks.splice( 0, tasks.length );
121 next();
122 }
123 function next() {
124 if ( !tasks ) {
125 // This happens if after the process is completed, one of our callbacks is
126 // invoked. This can happen if a test timed out but the process was still
127 // running. In that case, ignore it. Don't invoke complete() a second time.
128 return;
129 }
130 var task = tasks.shift();
131 if ( task ) {
132 task( next, abort );
133 } else {
134 // Remove tasks list to indicate the process is final.
135 tasks = null;
136 complete();
137 }
138 }
139 next();
140 }
141
142 QUnit.test( 'Replace', 16, function ( assert ) {
143 mw.messages.set( 'simple', 'Foo $1 baz $2' );
144
145 assert.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
146 assert.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
147 assert.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
148
149 mw.messages.set( 'plain-input', '<foo foo="foo">x$1y&lt;</foo>z' );
150
151 assert.equal(
152 formatParse( 'plain-input', 'bar' ),
153 '&lt;foo foo="foo"&gt;xbary&amp;lt;&lt;/foo&gt;z',
154 'Input is not considered html'
155 );
156
157 mw.messages.set( 'plain-replace', 'Foo $1' );
158
159 assert.equal(
160 formatParse( 'plain-replace', '<bar bar="bar">&gt;</bar>' ),
161 'Foo &lt;bar bar="bar"&gt;&amp;gt;&lt;/bar&gt;',
162 'Replacement is not considered html'
163 );
164
165 mw.messages.set( 'object-replace', 'Foo $1' );
166
167 assert.equal(
168 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ) ),
169 'Foo <div class="bar">&gt;</div>',
170 'jQuery objects are preserved as raw html'
171 );
172
173 assert.equal(
174 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
175 'Foo <div class="bar">&gt;</div>',
176 'HTMLElement objects are preserved as raw html'
177 );
178
179 assert.equal(
180 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).toArray() ),
181 'Foo <div class="bar">&gt;</div>',
182 'HTMLElement[] arrays are preserved as raw html'
183 );
184
185 assert.equal(
186 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
187 'Foo <a href="http://example.org/?x=y&amp;z">bar</a>',
188 'Href is not double-escaped in wikilink function'
189 );
190 assert.equal(
191 formatParse( 'external-link-plural', 1, 'http://example.org' ),
192 'Foo is <a href="http://example.org">one</a> things.',
193 'Link is expanded inside plural and is not escaped html'
194 );
195 assert.equal(
196 formatParse( 'external-link-plural', 2, 'http://example.org' ),
197 'Foo <a href=\"http://example.org\">two</a> things.',
198 'Link is expanded inside an explicit plural form and is not escaped html'
199 );
200 assert.equal(
201 formatParse( 'external-link-plural', 3 ),
202 'Foo three things.',
203 'A simple explicit plural form co-existing with complex explicit plural forms'
204 );
205 assert.equal(
206 formatParse( 'external-link-plural', 4, 'http://example.org' ),
207 'Foo a=b things.',
208 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
209 );
210 assert.equal(
211 formatParse( 'external-link-plural', 5, 'http://example.org' ),
212 'Foo are <a href="http://example.org">some</a> things.',
213 'Invalid explicit plural form. Plural fallback to the "other" plural form'
214 );
215 assert.equal(
216 formatParse( 'external-link-plural', 6, 'http://example.org' ),
217 'Foo are <a href="http://example.org">some</a> things.',
218 'Plural fallback to the "other" plural form'
219 );
220 assert.equal(
221 formatParse( 'plural-only-explicit-forms', 2 ),
222 'It is a double room.',
223 'Plural with explicit forms alone.'
224 );
225 } );
226
227 QUnit.test( 'Plural', 6, function ( assert ) {
228 assert.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
229 assert.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
230 assert.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
231 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
232 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}}' );
233 assert.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
234 } );
235
236 QUnit.test( 'Gender', 15, function ( assert ) {
237 var originalGender = mw.user.options.get( 'gender' );
238
239 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
240 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
241 mw.user.options.set( 'gender', 'male' );
242 assert.equal(
243 formatParse( 'gender-msg', 'Bob', 'male' ),
244 'Bob: blue',
245 'Masculine from string "male"'
246 );
247 assert.equal(
248 formatParse( 'gender-msg', 'Bob', mw.user ),
249 'Bob: blue',
250 'Masculine from mw.user object'
251 );
252 assert.equal(
253 formatParse( 'gender-msg-currentuser' ),
254 'blue',
255 'Masculine for current user'
256 );
257
258 mw.user.options.set( 'gender', 'female' );
259 assert.equal(
260 formatParse( 'gender-msg', 'Alice', 'female' ),
261 'Alice: pink',
262 'Feminine from string "female"' );
263 assert.equal(
264 formatParse( 'gender-msg', 'Alice', mw.user ),
265 'Alice: pink',
266 'Feminine from mw.user object'
267 );
268 assert.equal(
269 formatParse( 'gender-msg-currentuser' ),
270 'pink',
271 'Feminine for current user'
272 );
273
274 mw.user.options.set( 'gender', 'unknown' );
275 assert.equal(
276 formatParse( 'gender-msg', 'Foo', mw.user ),
277 'Foo: green',
278 'Neutral from mw.user object' );
279 assert.equal(
280 formatParse( 'gender-msg', 'User' ),
281 'User: green',
282 'Neutral when no parameter given' );
283 assert.equal(
284 formatParse( 'gender-msg', 'User', 'unknown' ),
285 'User: green',
286 'Neutral from string "unknown"'
287 );
288 assert.equal(
289 formatParse( 'gender-msg-currentuser' ),
290 'green',
291 'Neutral for current user'
292 );
293
294 mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
295
296 assert.equal(
297 formatParse( 'gender-msg-one-form', 'male', 10 ),
298 'User: 10 edits',
299 'Gender neutral and plural form'
300 );
301 assert.equal(
302 formatParse( 'gender-msg-one-form', 'female', 1 ),
303 'User: 1 edit',
304 'Gender neutral and singular form'
305 );
306
307 mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
308 assert.equal(
309 formatParse( 'gender-msg-lowercase', 'male' ),
310 'he is awesome',
311 'Gender masculine'
312 );
313 assert.equal(
314 formatParse( 'gender-msg-lowercase', 'female' ),
315 'she is awesome',
316 'Gender feminine'
317 );
318
319 mw.messages.set( 'gender-msg-wrong', '{{gender}} test' );
320 assert.equal(
321 formatParse( 'gender-msg-wrong', 'female' ),
322 ' test',
323 'Invalid syntax should result in {{gender}} simply being stripped away'
324 );
325
326 mw.user.options.set( 'gender', originalGender );
327 } );
328
329 QUnit.test( 'Grammar', 2, function ( assert ) {
330 assert.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'Grammar Test with sitename' );
331
332 mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
333 assert.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
334 } );
335
336 QUnit.test( 'Match PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
337 mw.messages.set( mw.libs.phpParserData.messages );
338 var tasks = $.map( mw.libs.phpParserData.tests, function ( test ) {
339 return function ( next, abort ) {
340 getMwLanguage( test.lang )
341 .then( function ( langClass ) {
342 mw.config.set( 'wgUserLanguage', test.lang );
343 var parser = new mw.jqueryMsg.parser( { language: langClass } );
344 assert.equal(
345 parser.parse( test.key, test.args ).html(),
346 test.result,
347 test.name
348 );
349 }, function () {
350 assert.ok( false, 'Language "' + test.lang + '" failed to load.' );
351 } )
352 .then( next, abort );
353 };
354 } );
355
356 QUnit.stop();
357 process( tasks, QUnit.start );
358 } );
359
360 QUnit.test( 'Links', 14, function ( assert ) {
361 var testCases,
362 expectedDisambiguationsText,
363 expectedMultipleBars,
364 expectedSpecialCharacters;
365
366 // The below three are all identical to or based on real messages. For disambiguations-text,
367 // the bold was removed because it is not yet implemented.
368
369 assert.htmlEqual(
370 formatParse( 'jquerymsg-test-statistics-users' ),
371 expectedListUsers,
372 'Piped wikilink'
373 );
374
375 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 ' +
376 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
377
378 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]].' );
379 assert.htmlEqual(
380 formatParse( 'disambiguations-text' ),
381 expectedDisambiguationsText,
382 'Wikilink without pipe'
383 );
384
385 assert.htmlEqual(
386 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
387 expectedEntrypoints,
388 'External link'
389 );
390
391 // Pipe trick is not supported currently, but should not parse as text either.
392 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
393 mw.messages.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
394 mw.messages.set( 'empty-link', '[[]]' );
395 this.suppressWarnings();
396 assert.equal(
397 formatParse( 'pipe-trick' ),
398 '[[Tampa, Florida|]]',
399 'Pipe trick should not be parsed.'
400 );
401 assert.equal(
402 formatParse( 'reverse-pipe-trick' ),
403 '[[|Tampa, Florida]]',
404 'Reverse pipe trick should not be parsed.'
405 );
406 assert.equal(
407 formatParse( 'empty-link' ),
408 '[[]]',
409 'Empty link should not be parsed.'
410 );
411 this.restoreWarnings();
412
413 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
414 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
415 assert.htmlEqual(
416 formatParse( 'multiple-bars' ),
417 expectedMultipleBars,
418 'Bar in anchor'
419 );
420
421 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>';
422
423 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
424 assert.htmlEqual(
425 formatParse( 'special-characters' ),
426 expectedSpecialCharacters,
427 'Special characters'
428 );
429
430 mw.messages.set( 'leading-colon', '[[:File:Foo.jpg]]' );
431 assert.htmlEqual(
432 formatParse( 'leading-colon' ),
433 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
434 'Leading colon in links is stripped'
435 );
436
437 assert.htmlEqual(
438 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
439 expectedListUsersSitename,
440 'Piped wikilink with parser function in the text'
441 );
442
443 testCases = [
444 [
445 'extlink-html-full',
446 'asd [http://example.org <strong>Example</strong>] asd',
447 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
448 ],
449 [
450 'extlink-html-partial',
451 'asd [http://example.org foo <strong>Example</strong> bar] asd',
452 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
453 ],
454 [
455 'wikilink-html-full',
456 'asd [[Example|<strong>Example</strong>]] asd',
457 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
458 ],
459 [
460 'wikilink-html-partial',
461 'asd [[Example|foo <strong>Example</strong> bar]] asd',
462 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
463 ]
464 ];
465
466 $.each( testCases, function () {
467 var
468 key = this[ 0 ],
469 input = this[ 1 ],
470 output = this[ 2 ];
471 mw.messages.set( key, input );
472 assert.htmlEqual(
473 formatParse( key ),
474 output,
475 'HTML in links: ' + key
476 );
477 } );
478 } );
479
480 QUnit.test( 'Replacements in links', 14, function ( assert ) {
481 var testCases = [
482 [
483 'extlink-param-href-full',
484 'asd [$1 Example] asd',
485 'asd <a href="http://example.com">Example</a> asd'
486 ],
487 [
488 'extlink-param-href-partial',
489 'asd [$1/example Example] asd',
490 'asd <a href="http://example.com/example">Example</a> asd'
491 ],
492 [
493 'extlink-param-text-full',
494 'asd [http://example.org $2] asd',
495 'asd <a href="http://example.org">Text</a> asd'
496 ],
497 [
498 'extlink-param-text-partial',
499 'asd [http://example.org Example $2] asd',
500 'asd <a href="http://example.org">Example Text</a> asd'
501 ],
502 [
503 'extlink-param-both-full',
504 'asd [$1 $2] asd',
505 'asd <a href="http://example.com">Text</a> asd'
506 ],
507 [
508 'extlink-param-both-partial',
509 'asd [$1/example Example $2] asd',
510 'asd <a href="http://example.com/example">Example Text</a> asd'
511 ],
512 [
513 'wikilink-param-href-full',
514 'asd [[$1|Example]] asd',
515 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
516 ],
517 [
518 'wikilink-param-href-partial',
519 'asd [[$1/Test|Example]] asd',
520 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
521 ],
522 [
523 'wikilink-param-text-full',
524 'asd [[Example|$2]] asd',
525 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
526 ],
527 [
528 'wikilink-param-text-partial',
529 'asd [[Example|Example $2]] asd',
530 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
531 ],
532 [
533 'wikilink-param-both-full',
534 'asd [[$1|$2]] asd',
535 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
536 ],
537 [
538 'wikilink-param-both-partial',
539 'asd [[$1/Test|Example $2]] asd',
540 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
541 ],
542 [
543 'wikilink-param-unpiped-full',
544 'asd [[$1]] asd',
545 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
546 ],
547 [
548 'wikilink-param-unpiped-partial',
549 'asd [[$1/Test]] asd',
550 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
551 ]
552 ];
553
554 $.each( testCases, function () {
555 var
556 key = this[ 0 ],
557 input = this[ 1 ],
558 output = this[ 2 ],
559 paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
560 paramText = 'Text';
561 mw.messages.set( key, input );
562 assert.htmlEqual(
563 formatParse( key, paramHref, paramText ),
564 output,
565 'Replacements in links: ' + key
566 );
567 } );
568 } );
569
570 // Tests that {{-transformation vs. general parsing are done as requested
571 QUnit.test( 'Curly brace transformation', 16, function ( assert ) {
572 var oldUserLang = mw.config.get( 'wgUserLanguage' );
573
574 assertBothModes( assert, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
575
576 assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
577
578 assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' );
579
580 mw.config.set( 'wgUserLanguage', 'en' );
581 assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
582
583 // Test non-{{ wikitext, where behavior differs
584
585 // Wikilink
586 assert.equal(
587 formatText( 'jquerymsg-test-statistics-users' ),
588 mw.messages.get( 'jquerymsg-test-statistics-users' ),
589 'Internal link message unchanged when format is \'text\''
590 );
591 assert.htmlEqual(
592 formatParse( 'jquerymsg-test-statistics-users' ),
593 expectedListUsers,
594 'Internal link message parsed when format is \'parse\''
595 );
596
597 // External link
598 assert.equal(
599 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
600 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
601 'External link message unchanged when format is \'text\''
602 );
603 assert.htmlEqual(
604 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
605 expectedEntrypoints,
606 'External link message processed when format is \'parse\''
607 );
608
609 // External link with parameter
610 assert.equal(
611 formatText( 'external-link-replace', 'http://example.com' ),
612 'Foo [http://example.com bar]',
613 'External link message only substitutes parameter when format is \'text\''
614 );
615 assert.htmlEqual(
616 formatParse( 'external-link-replace', 'http://example.com' ),
617 'Foo <a href="http://example.com">bar</a>',
618 'External link message processed when format is \'parse\''
619 );
620 assert.htmlEqual(
621 formatParse( 'external-link-replace', $( '<i>' ) ),
622 'Foo <i>bar</i>',
623 'External link message processed as jQuery object when format is \'parse\''
624 );
625 assert.htmlEqual(
626 formatParse( 'external-link-replace', function () {} ),
627 'Foo <a href="#">bar</a>',
628 'External link message processed as function when format is \'parse\''
629 );
630
631 mw.config.set( 'wgUserLanguage', oldUserLang );
632 } );
633
634 QUnit.test( 'Int', 4, function ( assert ) {
635 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.',
636 expectedNewarticletext,
637 helpPageTitle = 'Help:Foobar';
638
639 mw.messages.set( 'foobar', helpPageTitle );
640
641 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 ' +
642 '<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.';
643
644 mw.messages.set( 'newarticletext', newarticletextSource );
645
646 assert.htmlEqual(
647 formatParse( 'newarticletext' ),
648 expectedNewarticletext,
649 'Link with nested message'
650 );
651
652 assert.equal(
653 formatParse( 'see-portal-url' ),
654 'Project:Community portal is an important community page.',
655 'Nested message'
656 );
657
658 mw.messages.set( 'newarticletext-lowercase',
659 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
660
661 assert.htmlEqual(
662 formatParse( 'newarticletext-lowercase' ),
663 expectedNewarticletext,
664 'Link with nested message, lowercase include'
665 );
666
667 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
668
669 assert.equal(
670 formatParse( 'uses-missing-int' ),
671 '[doesnt-exist]',
672 'int: where nested message does not exist'
673 );
674 } );
675
676 QUnit.test( 'Ns', 4, function ( assert ) {
677 mw.messages.set( 'ns-template-talk', '{{ns:Template talk}}' );
678 assert.equal(
679 formatParse( 'ns-template-talk' ),
680 'Dyskusja szablonu',
681 'ns: returns localised namespace when used with a canonical namespace name'
682 );
683
684 mw.messages.set( 'ns-10', '{{ns:10}}' );
685 assert.equal(
686 formatParse( 'ns-10' ),
687 'Szablon',
688 'ns: returns localised namespace when used with a namespace number'
689 );
690
691 mw.messages.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
692 assert.equal(
693 formatParse( 'ns-unknown' ),
694 '',
695 'ns: returns empty string for unknown namespace name'
696 );
697
698 mw.messages.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
699 assert.equal(
700 formatParse( 'ns-in-a-link' ),
701 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
702 'ns: works when used inside a wikilink'
703 );
704 } );
705
706 // Tests that getMessageFunction is used for non-plain messages with curly braces or
707 // square brackets, but not otherwise.
708 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
709 var oldGMF, outerCalled, innerCalled;
710
711 mw.messages.set( {
712 'curly-brace': '{{int:message}}',
713 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
714 'double-square-bracket': '[[Some page]]',
715 regular: 'Other message'
716 } );
717
718 oldGMF = mw.jqueryMsg.getMessageFunction;
719
720 mw.jqueryMsg.getMessageFunction = function () {
721 outerCalled = true;
722 return function () {
723 innerCalled = true;
724 };
725 };
726
727 function verifyGetMessageFunction( key, format, shouldCall ) {
728 var message;
729 outerCalled = false;
730 innerCalled = false;
731 message = mw.message( key );
732 message[ format ]();
733 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
734 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
735 }
736
737 verifyGetMessageFunction( 'curly-brace', 'parse', true );
738 verifyGetMessageFunction( 'curly-brace', 'plain', false );
739
740 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
741 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
742
743 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
744 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
745
746 verifyGetMessageFunction( 'regular', 'parse', false );
747 verifyGetMessageFunction( 'regular', 'plain', false );
748
749 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
750 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
751 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
752
753 mw.jqueryMsg.getMessageFunction = oldGMF;
754 } );
755
756 formatnumTests = [
757 {
758 lang: 'en',
759 number: 987654321.654321,
760 result: '987,654,321.654',
761 description: 'formatnum test for English, decimal separator'
762 },
763 {
764 lang: 'ar',
765 number: 987654321.654321,
766 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
767 description: 'formatnum test for Arabic, with decimal separator'
768 },
769 {
770 lang: 'ar',
771 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
772 result: 987654321,
773 integer: true,
774 description: 'formatnum test for Arabic, with decimal separator, reverse'
775 },
776 {
777 lang: 'ar',
778 number: -12.89,
779 result: '-١٢٫٨٩',
780 description: 'formatnum test for Arabic, negative number'
781 },
782 {
783 lang: 'ar',
784 number: '-١٢٫٨٩',
785 result: -12,
786 integer: true,
787 description: 'formatnum test for Arabic, negative number, reverse'
788 },
789 {
790 lang: 'nl',
791 number: 987654321.654321,
792 result: '987.654.321,654',
793 description: 'formatnum test for Nederlands, decimal separator'
794 },
795 {
796 lang: 'nl',
797 number: -12.89,
798 result: '-12,89',
799 description: 'formatnum test for Nederlands, negative number'
800 },
801 {
802 lang: 'nl',
803 number: '.89',
804 result: '0,89',
805 description: 'formatnum test for Nederlands'
806 },
807 {
808 lang: 'nl',
809 number: 'invalidnumber',
810 result: 'invalidnumber',
811 description: 'formatnum test for Nederlands, invalid number'
812 },
813 {
814 lang: 'ml',
815 number: '1000000000',
816 result: '1,00,00,00,000',
817 description: 'formatnum test for Malayalam'
818 },
819 {
820 lang: 'ml',
821 number: '-1000000000',
822 result: '-1,00,00,00,000',
823 description: 'formatnum test for Malayalam, negative number'
824 },
825 /*
826 * This will fail because of wrong pattern for ml in MW(different from CLDR)
827 {
828 lang: 'ml',
829 number: '1000000000.000',
830 result: '1,00,00,00,000.000',
831 description: 'formatnum test for Malayalam with decimal place'
832 },
833 */
834 {
835 lang: 'hi',
836 number: '123456789.123456789',
837 result: '१२,३४,५६,७८९',
838 description: 'formatnum test for Hindi'
839 },
840 {
841 lang: 'hi',
842 number: '१२,३४,५६,७८९',
843 result: '१२,३४,५६,७८९',
844 description: 'formatnum test for Hindi, Devanagari digits passed'
845 },
846 {
847 lang: 'hi',
848 number: '१२३४५६,७८९',
849 result: '123456',
850 integer: true,
851 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
852 }
853 ];
854
855 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
856 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
857 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
858 var queue = $.map( formatnumTests, function ( test ) {
859 return function ( next, abort ) {
860 getMwLanguage( test.lang )
861 .then( function ( langClass ) {
862 mw.config.set( 'wgUserLanguage', test.lang );
863 var parser = new mw.jqueryMsg.parser( { language: langClass } );
864 assert.equal(
865 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
866 [ test.number ] ).html(),
867 test.result,
868 test.description
869 );
870 }, function () {
871 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
872 } )
873 .then( next, abort );
874 };
875 } );
876 QUnit.stop();
877 process( queue, QUnit.start );
878 } );
879
880 // HTML in wikitext
881 QUnit.test( 'HTML', 32, function ( assert ) {
882 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
883
884 assertBothModes( assert, [ 'jquerymsg-italics-msg' ], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
885
886 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
887 assertBothModes( assert, [ 'jquerymsg-bold-msg' ], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
888
889 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
890 assertBothModes( assert, [ 'jquerymsg-bold-italics-msg' ], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
891
892 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
893 assertBothModes( assert, [ 'jquerymsg-italics-bold-msg' ], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
894
895 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
896
897 assert.htmlEqual(
898 formatParse( 'jquerymsg-italics-with-link' ),
899 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
900 'Italics with link inside in parse mode'
901 );
902
903 assert.equal(
904 formatText( 'jquerymsg-italics-with-link' ),
905 mw.messages.get( 'jquerymsg-italics-with-link' ),
906 'Italics with link unchanged in text mode'
907 );
908
909 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
910 assert.htmlEqual(
911 formatParse( 'jquerymsg-italics-id-class' ),
912 mw.messages.get( 'jquerymsg-italics-id-class' ),
913 'ID and class are allowed'
914 );
915
916 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
917 assert.htmlEqual(
918 formatParse( 'jquerymsg-italics-onclick' ),
919 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
920 'element with onclick is escaped because it is not allowed'
921 );
922
923 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
924 assert.htmlEqual(
925 formatParse( 'jquerymsg-script-msg' ),
926 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
927 'Tag outside whitelist escaped in parse mode'
928 );
929
930 assert.equal(
931 formatText( 'jquerymsg-script-msg' ),
932 mw.messages.get( 'jquerymsg-script-msg' ),
933 'Tag outside whitelist unchanged in text mode'
934 );
935
936 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
937 assert.htmlEqual(
938 formatParse( 'jquerymsg-script-link-msg' ),
939 '&lt;script&gt;<a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a>&lt;/script&gt;',
940 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
941 );
942
943 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
944 assert.htmlEqual(
945 formatParse( 'jquerymsg-mismatched-html' ),
946 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
947 'Mismatched HTML start and end tag treated as text'
948 );
949
950 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
951 assert.htmlEqual(
952 formatParse( 'jquerymsg-script-and-external-link' ),
953 '&lt;script&gt;alert( "jquerymsg-script-and-external-link test" );&lt;/script&gt; <a href="http://example.com"><i>Foo</i> bar</a>',
954 'HTML tags in external links not interfering with escaping of other tags'
955 );
956
957 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
958 assert.htmlEqual(
959 formatParse( 'jquerymsg-link-script' ),
960 '<a href="http://example.com">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</a>',
961 'Non-whitelisted HTML tag in external link anchor treated as text'
962 );
963
964 // Intentionally not using htmlEqual for the quote tests
965 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
966 assert.equal(
967 formatParse( 'jquerymsg-double-quotes-preserved' ),
968 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
969 'Attributes with double quotes are preserved as such'
970 );
971
972 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
973 assert.equal(
974 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
975 '<i id="single">Single</i>',
976 'Attributes with single quotes are normalized to double'
977 );
978
979 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:&quot;Arial&quot;">Styled</i>' );
980 assert.htmlEqual(
981 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
982 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
983 'Escaped attributes are parsed correctly'
984 );
985
986 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:&#039;Arial&#039;\'>Styled</i>' );
987 assert.htmlEqual(
988 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
989 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
990 'Escaped attributes are parsed correctly'
991 );
992
993 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
994 assert.htmlEqual(
995 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
996 '<i><a href="http://example.com">Example</a></i>',
997 'Contents of valid tag are treated as wikitext, so external link is parsed'
998 );
999
1000 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1001 assert.htmlEqual(
1002 formatParse( 'jquerymsg-wikitext-contents-script' ),
1003 '<i>&lt;script&gt;Script inside&lt;/script&gt;</i>',
1004 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1005 );
1006
1007 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1008 assert.htmlEqual(
1009 formatParse( 'jquerymsg-unclosed-tag' ),
1010 'Foo&lt;tag&gt;bar',
1011 'Nonsupported unclosed tags are escaped'
1012 );
1013
1014 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1015 assert.htmlEqual(
1016 formatParse( 'jquerymsg-self-closing-tag' ),
1017 'Foo&lt;tag/&gt;bar',
1018 'Self-closing tags don\'t cause a parse error'
1019 );
1020
1021 mw.messages.set( 'jquerymsg-entities1', 'A&B' );
1022 mw.messages.set( 'jquerymsg-entities2', 'A&gt;B' );
1023 mw.messages.set( 'jquerymsg-entities3', 'A&rarr;B' );
1024 assert.htmlEqual(
1025 formatParse( 'jquerymsg-entities1' ),
1026 'A&amp;B',
1027 'Lone "&" is escaped in text'
1028 );
1029 assert.htmlEqual(
1030 formatParse( 'jquerymsg-entities2' ),
1031 'A&amp;gt;B',
1032 '"&gt;" entity is double-escaped in text' // (WHY?)
1033 );
1034 assert.htmlEqual(
1035 formatParse( 'jquerymsg-entities3' ),
1036 'A&amp;rarr;B',
1037 '"&rarr;" entity is double-escaped in text'
1038 );
1039
1040 mw.messages.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1041 mw.messages.set( 'jquerymsg-entities-attr2', '<i title="A&gt;B"></i>' );
1042 mw.messages.set( 'jquerymsg-entities-attr3', '<i title="A&rarr;B"></i>' );
1043 assert.htmlEqual(
1044 formatParse( 'jquerymsg-entities-attr1' ),
1045 '<i title="A&amp;B"></i>',
1046 'Lone "&" is escaped in attribute'
1047 );
1048 assert.htmlEqual(
1049 formatParse( 'jquerymsg-entities-attr2' ),
1050 '<i title="A&gt;B"></i>',
1051 '"&gt;" entity is not double-escaped in attribute' // (WHY?)
1052 );
1053 assert.htmlEqual(
1054 formatParse( 'jquerymsg-entities-attr3' ),
1055 '<i title="A&amp;rarr;B"></i>',
1056 '"&rarr;" entity is double-escaped in attribute'
1057 );
1058 } );
1059
1060 QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
1061 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1062
1063 this.suppressWarnings();
1064 var logSpy = this.sandbox.spy( mw.log, 'warn' );
1065
1066 assert.equal(
1067 formatParse( 'invalid-wikitext' ),
1068 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
1069 'Invalid wikitext: \'parse\' format'
1070 );
1071
1072 assert.equal(
1073 formatText( 'invalid-wikitext' ),
1074 '<b>{{FAIL}}</b>',
1075 'Invalid wikitext: \'text\' format'
1076 );
1077
1078 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
1079 } );
1080
1081 QUnit.test( 'Integration', 5, function ( assert ) {
1082 var expected, logSpy, msg;
1083
1084 expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1085 mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
1086
1087 this.suppressWarnings();
1088 logSpy = this.sandbox.spy( mw.log, 'warn' );
1089 assert.equal(
1090 window.gM( 'integration-test' ),
1091 expected,
1092 'Global function gM() works correctly'
1093 );
1094 assert.equal( logSpy.callCount, 1, 'mw.log.warn called' );
1095 this.restoreWarnings();
1096
1097 assert.equal(
1098 mw.message( 'integration-test' ).parse(),
1099 expected,
1100 'mw.message().parse() works correctly'
1101 );
1102
1103 assert.equal(
1104 $( '<span>' ).msg( 'integration-test' ).html(),
1105 expected,
1106 'jQuery plugin $.fn.msg() works correctly'
1107 );
1108
1109 mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
1110 msg = mw.message(
1111 'integration-test-extlink',
1112 $( '<a>' ).attr( 'href', 'http://example.com/' )
1113 );
1114 msg.parse(); // Not a no-op
1115 assert.equal(
1116 msg.parse(),
1117 '<a href="http://example.com/">Link</a>',
1118 'Calling .parse() multiple times does not duplicate link contents'
1119 );
1120 } );
1121
1122 }( mediaWiki, jQuery ) );