mediawiki.jqueryMsg: Strip leading colon in wikilinks
[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', 14, function ( assert ) {
347 var testCases,
348 expectedDisambiguationsText,
349 expectedMultipleBars,
350 expectedSpecialCharacters;
351
352 // The below three are all identical to or based on real messages. For disambiguations-text,
353 // the bold was removed because it is not yet implemented.
354
355 assert.htmlEqual(
356 formatParse( 'jquerymsg-test-statistics-users' ),
357 expectedListUsers,
358 'Piped wikilink'
359 );
360
361 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 ' +
362 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
363
364 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]].' );
365 assert.htmlEqual(
366 formatParse( 'disambiguations-text' ),
367 expectedDisambiguationsText,
368 'Wikilink without pipe'
369 );
370
371 assert.htmlEqual(
372 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
373 expectedEntrypoints,
374 'External link'
375 );
376
377 // Pipe trick is not supported currently, but should not parse as text either.
378 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
379 mw.messages.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
380 mw.messages.set( 'empty-link', '[[]]' );
381 this.suppressWarnings();
382 assert.equal(
383 formatParse( 'pipe-trick' ),
384 '[[Tampa, Florida|]]',
385 'Pipe trick should not be parsed.'
386 );
387 assert.equal(
388 formatParse( 'reverse-pipe-trick' ),
389 '[[|Tampa, Florida]]',
390 'Reverse pipe trick should not be parsed.'
391 );
392 assert.equal(
393 formatParse( 'empty-link' ),
394 '[[]]',
395 'Empty link should not be parsed.'
396 );
397 this.restoreWarnings();
398
399 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
400 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
401 assert.htmlEqual(
402 formatParse( 'multiple-bars' ),
403 expectedMultipleBars,
404 'Bar in anchor'
405 );
406
407 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>';
408
409 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
410 assert.htmlEqual(
411 formatParse( 'special-characters' ),
412 expectedSpecialCharacters,
413 'Special characters'
414 );
415
416 mw.messages.set( 'leading-colon', '[[:File:Foo.jpg]]' );
417 assert.htmlEqual(
418 formatParse( 'leading-colon' ),
419 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
420 'Leading colon in links is stripped'
421 );
422
423 assert.htmlEqual(
424 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
425 expectedListUsersSitename,
426 'Piped wikilink with parser function in the text'
427 );
428
429 testCases = [
430 [
431 'extlink-html-full',
432 'asd [http://example.org <strong>Example</strong>] asd',
433 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
434 ],
435 [
436 'extlink-html-partial',
437 'asd [http://example.org foo <strong>Example</strong> bar] asd',
438 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
439 ],
440 [
441 'wikilink-html-full',
442 'asd [[Example|<strong>Example</strong>]] asd',
443 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
444 ],
445 [
446 'wikilink-html-partial',
447 'asd [[Example|foo <strong>Example</strong> bar]] asd',
448 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
449 ]
450 ];
451
452 $.each( testCases, function () {
453 var
454 key = this[ 0 ],
455 input = this[ 1 ],
456 output = this[ 2 ];
457 mw.messages.set( key, input );
458 assert.htmlEqual(
459 formatParse( key ),
460 output,
461 'HTML in links: ' + key
462 );
463 } );
464 } );
465
466 QUnit.test( 'Replacements in links', 14, function ( assert ) {
467 var testCases = [
468 [
469 'extlink-param-href-full',
470 'asd [$1 Example] asd',
471 'asd <a href="http://example.com">Example</a> asd'
472 ],
473 [
474 'extlink-param-href-partial',
475 'asd [$1/example Example] asd',
476 'asd <a href="http://example.com/example">Example</a> asd'
477 ],
478 [
479 'extlink-param-text-full',
480 'asd [http://example.org $2] asd',
481 'asd <a href="http://example.org">Text</a> asd'
482 ],
483 [
484 'extlink-param-text-partial',
485 'asd [http://example.org Example $2] asd',
486 'asd <a href="http://example.org">Example Text</a> asd'
487 ],
488 [
489 'extlink-param-both-full',
490 'asd [$1 $2] asd',
491 'asd <a href="http://example.com">Text</a> asd'
492 ],
493 [
494 'extlink-param-both-partial',
495 'asd [$1/example Example $2] asd',
496 'asd <a href="http://example.com/example">Example Text</a> asd'
497 ],
498 [
499 'wikilink-param-href-full',
500 'asd [[$1|Example]] asd',
501 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
502 ],
503 [
504 'wikilink-param-href-partial',
505 'asd [[$1/Test|Example]] asd',
506 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
507 ],
508 [
509 'wikilink-param-text-full',
510 'asd [[Example|$2]] asd',
511 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
512 ],
513 [
514 'wikilink-param-text-partial',
515 'asd [[Example|Example $2]] asd',
516 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
517 ],
518 [
519 'wikilink-param-both-full',
520 'asd [[$1|$2]] asd',
521 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
522 ],
523 [
524 'wikilink-param-both-partial',
525 'asd [[$1/Test|Example $2]] asd',
526 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
527 ],
528 [
529 'wikilink-param-unpiped-full',
530 'asd [[$1]] asd',
531 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
532 ],
533 [
534 'wikilink-param-unpiped-partial',
535 'asd [[$1/Test]] asd',
536 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
537 ]
538 ];
539
540 $.each( testCases, function () {
541 var
542 key = this[ 0 ],
543 input = this[ 1 ],
544 output = this[ 2 ],
545 paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
546 paramText = 'Text';
547 mw.messages.set( key, input );
548 assert.htmlEqual(
549 formatParse( key, paramHref, paramText ),
550 output,
551 'Replacements in links: ' + key
552 );
553 } );
554 } );
555
556 // Tests that {{-transformation vs. general parsing are done as requested
557 QUnit.test( 'Curly brace transformation', 16, function ( assert ) {
558 var oldUserLang = mw.config.get( 'wgUserLanguage' );
559
560 assertBothModes( assert, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
561
562 assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
563
564 assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' );
565
566 mw.config.set( 'wgUserLanguage', 'en' );
567 assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
568
569 // Test non-{{ wikitext, where behavior differs
570
571 // Wikilink
572 assert.equal(
573 formatText( 'jquerymsg-test-statistics-users' ),
574 mw.messages.get( 'jquerymsg-test-statistics-users' ),
575 'Internal link message unchanged when format is \'text\''
576 );
577 assert.htmlEqual(
578 formatParse( 'jquerymsg-test-statistics-users' ),
579 expectedListUsers,
580 'Internal link message parsed when format is \'parse\''
581 );
582
583 // External link
584 assert.equal(
585 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
586 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
587 'External link message unchanged when format is \'text\''
588 );
589 assert.htmlEqual(
590 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
591 expectedEntrypoints,
592 'External link message processed when format is \'parse\''
593 );
594
595 // External link with parameter
596 assert.equal(
597 formatText( 'external-link-replace', 'http://example.com' ),
598 'Foo [http://example.com bar]',
599 'External link message only substitutes parameter when format is \'text\''
600 );
601 assert.htmlEqual(
602 formatParse( 'external-link-replace', 'http://example.com' ),
603 'Foo <a href="http://example.com">bar</a>',
604 'External link message processed when format is \'parse\''
605 );
606 assert.htmlEqual(
607 formatParse( 'external-link-replace', $( '<i>' ) ),
608 'Foo <i>bar</i>',
609 'External link message processed as jQuery object when format is \'parse\''
610 );
611 assert.htmlEqual(
612 formatParse( 'external-link-replace', function () {} ),
613 'Foo <a href="#">bar</a>',
614 'External link message processed as function when format is \'parse\''
615 );
616
617 mw.config.set( 'wgUserLanguage', oldUserLang );
618 } );
619
620 QUnit.test( 'Int', 4, function ( assert ) {
621 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.',
622 expectedNewarticletext,
623 helpPageTitle = 'Help:Foobar';
624
625 mw.messages.set( 'foobar', helpPageTitle );
626
627 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 ' +
628 '<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.';
629
630 mw.messages.set( 'newarticletext', newarticletextSource );
631
632 assert.htmlEqual(
633 formatParse( 'newarticletext' ),
634 expectedNewarticletext,
635 'Link with nested message'
636 );
637
638 assert.equal(
639 formatParse( 'see-portal-url' ),
640 'Project:Community portal is an important community page.',
641 'Nested message'
642 );
643
644 mw.messages.set( 'newarticletext-lowercase',
645 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
646
647 assert.htmlEqual(
648 formatParse( 'newarticletext-lowercase' ),
649 expectedNewarticletext,
650 'Link with nested message, lowercase include'
651 );
652
653 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
654
655 assert.equal(
656 formatParse( 'uses-missing-int' ),
657 '[doesnt-exist]',
658 'int: where nested message does not exist'
659 );
660 } );
661
662 // Tests that getMessageFunction is used for non-plain messages with curly braces or
663 // square brackets, but not otherwise.
664 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
665 var oldGMF, outerCalled, innerCalled;
666
667 mw.messages.set( {
668 'curly-brace': '{{int:message}}',
669 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
670 'double-square-bracket': '[[Some page]]',
671 regular: 'Other message'
672 } );
673
674 oldGMF = mw.jqueryMsg.getMessageFunction;
675
676 mw.jqueryMsg.getMessageFunction = function () {
677 outerCalled = true;
678 return function () {
679 innerCalled = true;
680 };
681 };
682
683 function verifyGetMessageFunction( key, format, shouldCall ) {
684 var message;
685 outerCalled = false;
686 innerCalled = false;
687 message = mw.message( key );
688 message[ format ]();
689 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
690 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
691 }
692
693 verifyGetMessageFunction( 'curly-brace', 'parse', true );
694 verifyGetMessageFunction( 'curly-brace', 'plain', false );
695
696 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
697 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
698
699 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
700 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
701
702 verifyGetMessageFunction( 'regular', 'parse', false );
703 verifyGetMessageFunction( 'regular', 'plain', false );
704
705 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
706 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
707 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
708
709 mw.jqueryMsg.getMessageFunction = oldGMF;
710 } );
711
712 formatnumTests = [
713 {
714 lang: 'en',
715 number: 987654321.654321,
716 result: '987,654,321.654',
717 description: 'formatnum test for English, decimal separator'
718 },
719 {
720 lang: 'ar',
721 number: 987654321.654321,
722 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
723 description: 'formatnum test for Arabic, with decimal separator'
724 },
725 {
726 lang: 'ar',
727 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
728 result: 987654321,
729 integer: true,
730 description: 'formatnum test for Arabic, with decimal separator, reverse'
731 },
732 {
733 lang: 'ar',
734 number: -12.89,
735 result: '-١٢٫٨٩',
736 description: 'formatnum test for Arabic, negative number'
737 },
738 {
739 lang: 'ar',
740 number: '-١٢٫٨٩',
741 result: -12,
742 integer: true,
743 description: 'formatnum test for Arabic, negative number, reverse'
744 },
745 {
746 lang: 'nl',
747 number: 987654321.654321,
748 result: '987.654.321,654',
749 description: 'formatnum test for Nederlands, decimal separator'
750 },
751 {
752 lang: 'nl',
753 number: -12.89,
754 result: '-12,89',
755 description: 'formatnum test for Nederlands, negative number'
756 },
757 {
758 lang: 'nl',
759 number: '.89',
760 result: '0,89',
761 description: 'formatnum test for Nederlands'
762 },
763 {
764 lang: 'nl',
765 number: 'invalidnumber',
766 result: 'invalidnumber',
767 description: 'formatnum test for Nederlands, invalid number'
768 },
769 {
770 lang: 'ml',
771 number: '1000000000',
772 result: '1,00,00,00,000',
773 description: 'formatnum test for Malayalam'
774 },
775 {
776 lang: 'ml',
777 number: '-1000000000',
778 result: '-1,00,00,00,000',
779 description: 'formatnum test for Malayalam, negative number'
780 },
781 /*
782 * This will fail because of wrong pattern for ml in MW(different from CLDR)
783 {
784 lang: 'ml',
785 number: '1000000000.000',
786 result: '1,00,00,00,000.000',
787 description: 'formatnum test for Malayalam with decimal place'
788 },
789 */
790 {
791 lang: 'hi',
792 number: '123456789.123456789',
793 result: '१२,३४,५६,७८९',
794 description: 'formatnum test for Hindi'
795 },
796 {
797 lang: 'hi',
798 number: '१२,३४,५६,७८९',
799 result: '१२,३४,५६,७८९',
800 description: 'formatnum test for Hindi, Devanagari digits passed'
801 },
802 {
803 lang: 'hi',
804 number: '१२३४५६,७८९',
805 result: '123456',
806 integer: true,
807 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
808 }
809 ];
810
811 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
812 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
813 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
814 var queue = $.map( formatnumTests, function ( test ) {
815 return function ( next, abort ) {
816 getMwLanguage( test.lang )
817 .then( function ( langClass ) {
818 mw.config.set( 'wgUserLanguage', test.lang );
819 var parser = new mw.jqueryMsg.parser( { language: langClass } );
820 assert.equal(
821 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
822 [ test.number ] ).html(),
823 test.result,
824 test.description
825 );
826 }, function () {
827 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
828 } )
829 .then( next, abort );
830 };
831 } );
832 QUnit.stop();
833 process( queue, QUnit.start );
834 } );
835
836 // HTML in wikitext
837 QUnit.test( 'HTML', 32, function ( assert ) {
838 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
839
840 assertBothModes( assert, [ 'jquerymsg-italics-msg' ], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
841
842 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
843 assertBothModes( assert, [ 'jquerymsg-bold-msg' ], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
844
845 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
846 assertBothModes( assert, [ 'jquerymsg-bold-italics-msg' ], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
847
848 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
849 assertBothModes( assert, [ 'jquerymsg-italics-bold-msg' ], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
850
851 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
852
853 assert.htmlEqual(
854 formatParse( 'jquerymsg-italics-with-link' ),
855 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
856 'Italics with link inside in parse mode'
857 );
858
859 assert.equal(
860 formatText( 'jquerymsg-italics-with-link' ),
861 mw.messages.get( 'jquerymsg-italics-with-link' ),
862 'Italics with link unchanged in text mode'
863 );
864
865 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
866 assert.htmlEqual(
867 formatParse( 'jquerymsg-italics-id-class' ),
868 mw.messages.get( 'jquerymsg-italics-id-class' ),
869 'ID and class are allowed'
870 );
871
872 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
873 assert.htmlEqual(
874 formatParse( 'jquerymsg-italics-onclick' ),
875 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
876 'element with onclick is escaped because it is not allowed'
877 );
878
879 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
880 assert.htmlEqual(
881 formatParse( 'jquerymsg-script-msg' ),
882 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
883 'Tag outside whitelist escaped in parse mode'
884 );
885
886 assert.equal(
887 formatText( 'jquerymsg-script-msg' ),
888 mw.messages.get( 'jquerymsg-script-msg' ),
889 'Tag outside whitelist unchanged in text mode'
890 );
891
892 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
893 assert.htmlEqual(
894 formatParse( 'jquerymsg-script-link-msg' ),
895 '&lt;script&gt;<a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a>&lt;/script&gt;',
896 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
897 );
898
899 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
900 assert.htmlEqual(
901 formatParse( 'jquerymsg-mismatched-html' ),
902 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
903 'Mismatched HTML start and end tag treated as text'
904 );
905
906 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
907 assert.htmlEqual(
908 formatParse( 'jquerymsg-script-and-external-link' ),
909 '&lt;script&gt;alert( "jquerymsg-script-and-external-link test" );&lt;/script&gt; <a href="http://example.com"><i>Foo</i> bar</a>',
910 'HTML tags in external links not interfering with escaping of other tags'
911 );
912
913 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
914 assert.htmlEqual(
915 formatParse( 'jquerymsg-link-script' ),
916 '<a href="http://example.com">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</a>',
917 'Non-whitelisted HTML tag in external link anchor treated as text'
918 );
919
920 // Intentionally not using htmlEqual for the quote tests
921 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
922 assert.equal(
923 formatParse( 'jquerymsg-double-quotes-preserved' ),
924 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
925 'Attributes with double quotes are preserved as such'
926 );
927
928 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
929 assert.equal(
930 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
931 '<i id="single">Single</i>',
932 'Attributes with single quotes are normalized to double'
933 );
934
935 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:&quot;Arial&quot;">Styled</i>' );
936 assert.htmlEqual(
937 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
938 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
939 'Escaped attributes are parsed correctly'
940 );
941
942 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:&#039;Arial&#039;\'>Styled</i>' );
943 assert.htmlEqual(
944 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
945 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
946 'Escaped attributes are parsed correctly'
947 );
948
949 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
950 assert.htmlEqual(
951 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
952 '<i><a href="http://example.com">Example</a></i>',
953 'Contents of valid tag are treated as wikitext, so external link is parsed'
954 );
955
956 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
957 assert.htmlEqual(
958 formatParse( 'jquerymsg-wikitext-contents-script' ),
959 '<i>&lt;script&gt;Script inside&lt;/script&gt;</i>',
960 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
961 );
962
963 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
964 assert.htmlEqual(
965 formatParse( 'jquerymsg-unclosed-tag' ),
966 'Foo&lt;tag&gt;bar',
967 'Nonsupported unclosed tags are escaped'
968 );
969
970 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
971 assert.htmlEqual(
972 formatParse( 'jquerymsg-self-closing-tag' ),
973 'Foo&lt;tag/&gt;bar',
974 'Self-closing tags don\'t cause a parse error'
975 );
976
977 mw.messages.set( 'jquerymsg-entities1', 'A&B' );
978 mw.messages.set( 'jquerymsg-entities2', 'A&gt;B' );
979 mw.messages.set( 'jquerymsg-entities3', 'A&rarr;B' );
980 assert.htmlEqual(
981 formatParse( 'jquerymsg-entities1' ),
982 'A&amp;B',
983 'Lone "&" is escaped in text'
984 );
985 assert.htmlEqual(
986 formatParse( 'jquerymsg-entities2' ),
987 'A&amp;gt;B',
988 '"&gt;" entity is double-escaped in text' // (WHY?)
989 );
990 assert.htmlEqual(
991 formatParse( 'jquerymsg-entities3' ),
992 'A&amp;rarr;B',
993 '"&rarr;" entity is double-escaped in text'
994 );
995
996 mw.messages.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
997 mw.messages.set( 'jquerymsg-entities-attr2', '<i title="A&gt;B"></i>' );
998 mw.messages.set( 'jquerymsg-entities-attr3', '<i title="A&rarr;B"></i>' );
999 assert.htmlEqual(
1000 formatParse( 'jquerymsg-entities-attr1' ),
1001 '<i title="A&amp;B"></i>',
1002 'Lone "&" is escaped in attribute'
1003 );
1004 assert.htmlEqual(
1005 formatParse( 'jquerymsg-entities-attr2' ),
1006 '<i title="A&gt;B"></i>',
1007 '"&gt;" entity is not double-escaped in attribute' // (WHY?)
1008 );
1009 assert.htmlEqual(
1010 formatParse( 'jquerymsg-entities-attr3' ),
1011 '<i title="A&amp;rarr;B"></i>',
1012 '"&rarr;" entity is double-escaped in attribute'
1013 );
1014 } );
1015
1016 QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
1017 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1018
1019 this.suppressWarnings();
1020 var logSpy = this.sandbox.spy( mw.log, 'warn' );
1021
1022 assert.equal(
1023 formatParse( 'invalid-wikitext' ),
1024 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
1025 'Invalid wikitext: \'parse\' format'
1026 );
1027
1028 assert.equal(
1029 formatText( 'invalid-wikitext' ),
1030 '<b>{{FAIL}}</b>',
1031 'Invalid wikitext: \'text\' format'
1032 );
1033
1034 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
1035 } );
1036
1037 }( mediaWiki, jQuery ) );