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