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