Merge "Added unicode encoding support flags to FileBackend"
[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 /*
270 The below three are all identical to or based on real messages. For disambiguations-text,
271 the bold was removed because it is not yet implemented.
272 */
273
274 assert.htmlEqual(
275 formatParse( 'jquerymsg-test-statistics-users' ),
276 expectedListUsers,
277 'Piped wikilink'
278 );
279
280 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 ' +
281 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
282
283 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]].' );
284 assert.htmlEqual(
285 formatParse( 'disambiguations-text' ),
286 expectedDisambiguationsText,
287 'Wikilink without pipe'
288 );
289
290 assert.htmlEqual(
291 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
292 expectedEntrypoints,
293 'External link'
294 );
295
296 // Pipe trick is not supported currently, but should not parse as text either.
297 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
298 this.suppressWarnings();
299 assert.equal(
300 formatParse( 'pipe-trick' ),
301 '[[Tampa, Florida|]]',
302 'Pipe trick should not be parsed.'
303 );
304 this.restoreWarnings();
305
306 expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
307 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
308 assert.htmlEqual(
309 formatParse( 'multiple-bars' ),
310 expectedMultipleBars,
311 'Bar in anchor'
312 );
313
314 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>';
315
316 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
317 assert.htmlEqual(
318 formatParse( 'special-characters' ),
319 expectedSpecialCharacters,
320 'Special characters'
321 );
322 } );
323
324 // Tests that {{-transformation vs. general parsing are done as requested
325 QUnit.test( 'Curly brace transformation', 14, function ( assert ) {
326 var oldUserLang = mw.config.get( 'wgUserLanguage' );
327
328 assertBothModes( assert, ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' );
329
330 assertBothModes( assert, ['plural-msg', 5], 'Found 5 items', 'plural is resolved' );
331
332 assertBothModes( assert, ['grammar-msg'], 'Przeszukaj ' + mw.config.get( 'wgSiteName' ), 'grammar is resolved' );
333
334 mw.config.set( 'wgUserLanguage', 'en' );
335 assertBothModes( assert, ['formatnum-msg', '987654321.654321'], '987,654,321.654', 'formatnum is resolved' );
336
337 // Test non-{{ wikitext, where behavior differs
338
339 // Wikilink
340 assert.equal(
341 formatText( 'jquerymsg-test-statistics-users' ),
342 mw.messages.get( 'jquerymsg-test-statistics-users' ),
343 'Internal link message unchanged when format is \'text\''
344 );
345 assert.htmlEqual(
346 formatParse( 'jquerymsg-test-statistics-users' ),
347 expectedListUsers,
348 'Internal link message parsed when format is \'parse\''
349 );
350
351 // External link
352 assert.equal(
353 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
354 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
355 'External link message unchanged when format is \'text\''
356 );
357 assert.htmlEqual(
358 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
359 expectedEntrypoints,
360 'External link message processed when format is \'parse\''
361 );
362
363 // External link with parameter
364 assert.equal(
365 formatText( 'external-link-replace', 'http://example.com' ),
366 'Foo [http://example.com bar]',
367 'External link message only substitutes parameter when format is \'text\''
368 );
369 assert.htmlEqual(
370 formatParse( 'external-link-replace', 'http://example.com' ),
371 'Foo <a href="http://example.com">bar</a>',
372 'External link message processed when format is \'parse\''
373 );
374
375 mw.config.set( 'wgUserLanguage', oldUserLang );
376 } );
377
378 QUnit.test( 'Int', 4, function ( assert ) {
379 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.',
380 expectedNewarticletext,
381 helpPageTitle = 'Help:Foobar';
382
383 mw.messages.set( 'foobar', helpPageTitle );
384
385 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 ' +
386 '<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.';
387
388 mw.messages.set( 'newarticletext', newarticletextSource );
389
390 assert.htmlEqual(
391 formatParse( 'newarticletext' ),
392 expectedNewarticletext,
393 'Link with nested message'
394 );
395
396 assert.equal(
397 formatParse( 'see-portal-url' ),
398 'Project:Community portal is an important community page.',
399 'Nested message'
400 );
401
402 mw.messages.set( 'newarticletext-lowercase',
403 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
404
405 assert.htmlEqual(
406 formatParse( 'newarticletext-lowercase' ),
407 expectedNewarticletext,
408 'Link with nested message, lowercase include'
409 );
410
411 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
412
413 assert.equal(
414 formatParse( 'uses-missing-int' ),
415 '[doesnt-exist]',
416 'int: where nested message does not exist'
417 );
418 } );
419
420 // Tests that getMessageFunction is used for non-plain messages with curly braces or
421 // square brackets, but not otherwise.
422 QUnit.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert ) {
423 var oldGMF, outerCalled, innerCalled;
424
425 mw.messages.set( {
426 'curly-brace': '{{int:message}}',
427 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
428 'double-square-bracket': '[[Some page]]',
429 'regular': 'Other message'
430 } );
431
432 oldGMF = mw.jqueryMsg.getMessageFunction;
433
434 mw.jqueryMsg.getMessageFunction = function () {
435 outerCalled = true;
436 return function () {
437 innerCalled = true;
438 };
439 };
440
441 function verifyGetMessageFunction( key, format, shouldCall ) {
442 var message;
443 outerCalled = false;
444 innerCalled = false;
445 message = mw.message( key );
446 message[format]();
447 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
448 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
449 }
450
451 verifyGetMessageFunction( 'curly-brace', 'parse', true );
452 verifyGetMessageFunction( 'curly-brace', 'plain', false );
453
454 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
455 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
456
457 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
458 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
459
460 verifyGetMessageFunction( 'regular', 'parse', false );
461 verifyGetMessageFunction( 'regular', 'plain', false );
462
463 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
464 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
465 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
466
467 mw.jqueryMsg.getMessageFunction = oldGMF;
468 } );
469
470 formatnumTests = [
471 {
472 lang: 'en',
473 number: 987654321.654321,
474 result: '987,654,321.654',
475 description: 'formatnum test for English, decimal seperator'
476 },
477 {
478 lang: 'ar',
479 number: 987654321.654321,
480 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
481 description: 'formatnum test for Arabic, with decimal seperator'
482 },
483 {
484 lang: 'ar',
485 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
486 result: 987654321,
487 integer: true,
488 description: 'formatnum test for Arabic, with decimal seperator, reverse'
489 },
490 {
491 lang: 'ar',
492 number: -12.89,
493 result: '-١٢٫٨٩',
494 description: 'formatnum test for Arabic, negative number'
495 },
496 {
497 lang: 'ar',
498 number: '-١٢٫٨٩',
499 result: -12,
500 integer: true,
501 description: 'formatnum test for Arabic, negative number, reverse'
502 },
503 {
504 lang: 'nl',
505 number: 987654321.654321,
506 result: '987.654.321,654',
507 description: 'formatnum test for Nederlands, decimal seperator'
508 },
509 {
510 lang: 'nl',
511 number: -12.89,
512 result: '-12,89',
513 description: 'formatnum test for Nederlands, negative number'
514 },
515 {
516 lang: 'nl',
517 number: '.89',
518 result: '0,89',
519 description: 'formatnum test for Nederlands'
520 },
521 {
522 lang: 'nl',
523 number: 'invalidnumber',
524 result: 'invalidnumber',
525 description: 'formatnum test for Nederlands, invalid number'
526 },
527 {
528 lang: 'ml',
529 number: '1000000000',
530 result: '1,00,00,00,000',
531 description: 'formatnum test for Malayalam'
532 },
533 {
534 lang: 'ml',
535 number: '-1000000000',
536 result: '-1,00,00,00,000',
537 description: 'formatnum test for Malayalam, negative number'
538 },
539 /*
540 * This will fail because of wrong pattern for ml in MW(different from CLDR)
541 {
542 lang: 'ml',
543 number: '1000000000.000',
544 result: '1,00,00,00,000.000',
545 description: 'formatnum test for Malayalam with decimal place'
546 },
547 */
548 {
549 lang: 'hi',
550 number: '123456789.123456789',
551 result: '१२,३४,५६,७८९',
552 description: 'formatnum test for Hindi'
553 },
554 {
555 lang: 'hi',
556 number: '१२,३४,५६,७८९',
557 result: '१२,३४,५६,७८९',
558 description: 'formatnum test for Hindi, Devanagari digits passed'
559 },
560 {
561 lang: 'hi',
562 number: '१२३४५६,७८९',
563 result: '123456',
564 integer: true,
565 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
566 }
567 ];
568
569 QUnit.test( 'formatnum', formatnumTests.length, function ( assert ) {
570 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
571 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
572 $.each( formatnumTests, function ( i, test ) {
573 QUnit.stop();
574 getMwLanguage( test.lang, function ( langClass ) {
575 QUnit.start();
576 if ( !langClass ) {
577 assert.ok( false, 'Language "' + test.lang + '" failed to load' );
578 return;
579 }
580 mw.messages.set(test.message );
581 mw.config.set( 'wgUserLanguage', test.lang );
582 var parser = new mw.jqueryMsg.parser( { language: langClass } );
583 assert.equal(
584 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
585 [ test.number ] ).html(),
586 test.result,
587 test.description
588 );
589 } );
590 } );
591 } );
592
593 // HTML in wikitext
594 QUnit.test( 'HTML', 26, function ( assert ) {
595 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
596
597 assertBothModes( assert, ['jquerymsg-italics-msg'], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
598
599 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
600 assertBothModes( assert, ['jquerymsg-bold-msg'], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
601
602 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
603 assertBothModes( assert, ['jquerymsg-bold-italics-msg'], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
604
605 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
606 assertBothModes( assert, ['jquerymsg-italics-bold-msg'], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
607
608 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
609
610 assert.htmlEqual(
611 formatParse( 'jquerymsg-italics-with-link' ),
612 'An <i>italicized <a title="link" href="' + mw.html.escape( mw.util.getUrl( 'link' ) ) + '">wiki-link</i>',
613 'Italics with link inside in parse mode'
614 );
615
616 assert.equal(
617 formatText( 'jquerymsg-italics-with-link' ),
618 mw.messages.get( 'jquerymsg-italics-with-link' ),
619 'Italics with link unchanged in text mode'
620 );
621
622 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
623 assert.htmlEqual(
624 formatParse( 'jquerymsg-italics-id-class' ),
625 mw.messages.get( 'jquerymsg-italics-id-class' ),
626 'ID and class are allowed'
627 );
628
629 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
630 assert.htmlEqual(
631 formatParse( 'jquerymsg-italics-onclick' ),
632 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
633 'element with onclick is escaped because it is not allowed'
634 );
635
636 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
637 assert.htmlEqual(
638 formatParse( 'jquerymsg-script-msg' ),
639 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
640 'Tag outside whitelist escaped in parse mode'
641 );
642
643 assert.equal(
644 formatText( 'jquerymsg-script-msg' ),
645 mw.messages.get( 'jquerymsg-script-msg' ),
646 'Tag outside whitelist unchanged in text mode'
647 );
648
649 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
650 assert.htmlEqual(
651 formatParse( 'jquerymsg-script-link-msg' ),
652 '&lt;script&gt;<a title="Foo" href="' + mw.html.escape( mw.util.getUrl( 'Foo' ) ) + '">bar</a>&lt;/script&gt;',
653 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
654 );
655
656 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
657 assert.htmlEqual(
658 formatParse( 'jquerymsg-mismatched-html' ),
659 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
660 'Mismatched HTML start and end tag treated as text'
661 );
662
663 // TODO (mattflaschen, 2013-03-18): It's not a security issue, but there's no real
664 // reason the htmlEmitter span needs to be here. It's an artifact of how emitting works.
665 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
666 assert.htmlEqual(
667 formatParse( 'jquerymsg-script-and-external-link' ),
668 '&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>',
669 'HTML tags in external links not interfering with escaping of other tags'
670 );
671
672 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
673 assert.htmlEqual(
674 formatParse( 'jquerymsg-link-script' ),
675 '<a href="http://example.com"><span class="mediaWiki_htmlEmitter">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</span></a>',
676 'Non-whitelisted HTML tag in external link anchor treated as text'
677 );
678
679 // Intentionally not using htmlEqual for the quote tests
680 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
681 assert.equal(
682 formatParse( 'jquerymsg-double-quotes-preserved' ),
683 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
684 'Attributes with double quotes are preserved as such'
685 );
686
687 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
688 assert.equal(
689 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
690 '<i id="single">Single</i>',
691 'Attributes with single quotes are normalized to double'
692 );
693
694 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:&quot;Arial&quot;">Styled</i>' );
695 assert.htmlEqual(
696 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
697 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
698 'Escaped attributes are parsed correctly'
699 );
700
701 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:&#039;Arial&#039;\'>Styled</i>' );
702 assert.htmlEqual(
703 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
704 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
705 'Escaped attributes are parsed correctly'
706 );
707
708 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
709 assert.htmlEqual(
710 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
711 '<i><a href="http://example.com">Example</a></i>',
712 'Contents of valid tag are treated as wikitext, so external link is parsed'
713 );
714
715 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
716 assert.htmlEqual(
717 formatParse( 'jquerymsg-wikitext-contents-script' ),
718 '<i><span class="mediaWiki_htmlEmitter">&lt;script&gt;Script inside&lt;/script&gt;</span></i>',
719 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
720 );
721
722 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
723 assert.htmlEqual(
724 formatParse( 'jquerymsg-unclosed-tag' ),
725 'Foo&lt;tag&gt;bar',
726 'Nonsupported unclosed tags are escaped'
727 );
728
729 mw.messages.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
730 assert.htmlEqual(
731 formatParse( 'jquerymsg-self-closing-tag' ),
732 'Foo&lt;tag/&gt;bar',
733 'Self-closing tags don\'t cause a parse error'
734 );
735 } );
736
737 QUnit.test( 'Behavior in case of invalid wikitext', 3, function ( assert ) {
738 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
739
740 this.suppressWarnings();
741 var logSpy = this.sandbox.spy( mw.log, 'warn' );
742
743 assert.equal(
744 formatParse( 'invalid-wikitext' ),
745 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
746 'Invalid wikitext: \'parse\' format'
747 );
748
749 assert.equal(
750 formatText( 'invalid-wikitext' ),
751 '<b>{{FAIL}}</b>',
752 'Invalid wikitext: \'text\' format'
753 );
754
755 assert.equal( logSpy.callCount, 2, 'mw.log.warn calls' );
756 } );
757
758 }( mediaWiki, jQuery ) );