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