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