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