2 var formatText
, formatParse
, formatnumTests
, specialCharactersPageName
, expectedListUsers
, expectedEntrypoints
,
4 hasOwn
= Object
.hasOwnProperty
;
6 // When the expected result is the same in both modes
7 function assertBothModes( assert
, parserArguments
, expectedResult
, assertMessage
) {
8 assert
.equal( formatText
.apply( null, parserArguments
), expectedResult
, assertMessage
+ ' when format is \'text\'' );
9 assert
.equal( formatParse
.apply( null, parserArguments
), expectedResult
, assertMessage
+ ' when format is \'parse\'' );
12 QUnit
.module( 'mediawiki.jqueryMsg', QUnit
.newMwEnvironment( {
14 this.originalMwLanguage
= mw
.language
;
16 specialCharactersPageName
= '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
18 expectedListUsers
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
20 expectedEntrypoints
= '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
22 formatText
= mw
.jqueryMsg
.getMessageFunction( {
26 formatParse
= mw
.jqueryMsg
.getMessageFunction( {
30 teardown: function () {
31 mw
.language
= this.originalMwLanguage
;
34 wgArticlePath
: '/wiki/$1'
36 // Messages that are reused in multiple tests
38 // The values for gender are not significant,
39 // what matters is which of the values is choosen by the parser
40 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
41 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
43 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
44 // See https://bugzilla.wikimedia.org/69993
45 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
46 // Assume the grammar form grammar_case_foo is not valid in any language
47 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
49 'formatnum-msg': '{{formatnum:$1}}',
51 'portal-url': 'Project:Community portal',
52 'see-portal-url': '{{Int:portal-url}} is an important community page.',
54 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
56 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
58 'external-link-replace': 'Foo [$1 bar]',
59 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
60 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
65 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
66 * to transport the module back to the test. It musn't be overwritten concurrentely.
68 * This function caches the mw.language data to avoid having to request the same module
69 * multiple times. There is more than one test case for any given language.
71 function getMwLanguage( langCode
) {
72 if ( !hasOwn
.call( mwLanguageCache
, langCode
) ) {
73 mwLanguageCache
[langCode
] = $.ajax( {
74 url
: mw
.util
.wikiScript( 'load' ),
76 skin
: mw
.config
.get( 'skin' ),
78 debug
: mw
.config
.get( 'debug' ),
80 'mediawiki.language.data',
87 } ).then( function () {
91 return mwLanguageCache
[langCode
];
95 * @param {Function[]} tasks List of functions that perform tasks
96 * that may be asynchronous. Invoke the callback parameter when done.
97 * @param {Function} done When all tasks are done.
100 function process( tasks
, done
) {
102 var task
= tasks
.shift();
112 QUnit
.test( 'Replace', 16, function ( assert
) {
113 mw
.messages
.set( 'simple', 'Foo $1 baz $2' );
115 assert
.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
116 assert
.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
117 assert
.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
119 mw
.messages
.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
122 formatParse( 'plain-input', 'bar' ),
123 '<foo foo="foo">xbary&lt;</foo>z',
124 'Input is not considered html'
127 mw
.messages
.set( 'plain-replace', 'Foo $1' );
130 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
131 'Foo <bar bar="bar">&gt;</bar>',
132 'Replacement is not considered html'
135 mw
.messages
.set( 'object-replace', 'Foo $1' );
138 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
139 'Foo <div class="bar">></div>',
140 'jQuery objects are preserved as raw html'
144 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
145 'Foo <div class="bar">></div>',
146 'HTMLElement objects are preserved as raw html'
150 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
151 'Foo <div class="bar">></div>',
152 'HTMLElement[] arrays are preserved as raw html'
156 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
157 'Foo <a href="http://example.org/?x=y&z">bar</a>',
158 'Href is not double-escaped in wikilink function'
161 formatParse( 'external-link-plural', 1, 'http://example.org' ),
162 'Foo is <a href="http://example.org">one</a> things.',
163 'Link is expanded inside plural and is not escaped html'
166 formatParse( 'external-link-plural', 2, 'http://example.org' ),
167 'Foo <a href=\"http://example.org\">two</a> things.',
168 'Link is expanded inside an explicit plural form and is not escaped html'
171 formatParse( 'external-link-plural', 3 ),
173 'A simple explicit plural form co-existing with complex explicit plural forms'
176 formatParse( 'external-link-plural', 4, 'http://example.org' ),
178 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
181 formatParse( 'external-link-plural', 5, 'http://example.org' ),
182 'Foo are <a href="http://example.org">some</a> things.',
183 'Invalid explicit plural form. Plural fallback to the "other" plural form'
186 formatParse( 'external-link-plural', 6, 'http://example.org' ),
187 'Foo are <a href="http://example.org">some</a> things.',
188 'Plural fallback to the "other" plural form'
191 formatParse( 'plural-only-explicit-forms', 2 ),
192 'It is a double room.',
193 'Plural with explicit forms alone.'
197 QUnit
.test( 'Plural', 6, function ( assert
) {
198 assert
.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
199 assert
.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
200 assert
.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
201 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
202 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}}' );
203 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
206 QUnit
.test( 'Gender', 15, function ( assert
) {
207 var originalGender
= mw
.user
.options
.get( 'gender' );
209 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
210 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
211 mw
.user
.options
.set( 'gender', 'male' );
213 formatParse( 'gender-msg', 'Bob', 'male' ),
215 'Masculine from string "male"'
218 formatParse( 'gender-msg', 'Bob', mw
.user
),
220 'Masculine from mw.user object'
223 formatParse( 'gender-msg-currentuser' ),
225 'Masculine for current user'
228 mw
.user
.options
.set( 'gender', 'female' );
230 formatParse( 'gender-msg', 'Alice', 'female' ),
232 'Feminine from string "female"' );
234 formatParse( 'gender-msg', 'Alice', mw
.user
),
236 'Feminine from mw.user object'
239 formatParse( 'gender-msg-currentuser' ),
241 'Feminine for current user'
244 mw
.user
.options
.set( 'gender', 'unknown' );
246 formatParse( 'gender-msg', 'Foo', mw
.user
),
248 'Neutral from mw.user object' );
250 formatParse( 'gender-msg', 'User' ),
252 'Neutral when no parameter given' );
254 formatParse( 'gender-msg', 'User', 'unknown' ),
256 'Neutral from string "unknown"'
259 formatParse( 'gender-msg-currentuser' ),
261 'Neutral for current user'
264 mw
.messages
.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
267 formatParse( 'gender-msg-one-form', 'male', 10 ),
269 'Gender neutral and plural form'
272 formatParse( 'gender-msg-one-form', 'female', 1 ),
274 'Gender neutral and singular form'
277 mw
.messages
.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
279 formatParse( 'gender-msg-lowercase', 'male' ),
284 formatParse( 'gender-msg-lowercase', 'female' ),
289 mw
.messages
.set( 'gender-msg-wrong', '{{gender}} test' );
291 formatParse( 'gender-msg-wrong', 'female' ),
293 'Invalid syntax should result in {{gender}} simply being stripped away'
296 mw
.user
.options
.set( 'gender', originalGender
);
299 QUnit
.test( 'Grammar', 2, function ( assert
) {
300 assert
.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'Grammar Test with sitename' );
302 mw
.messages
.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
303 assert
.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
306 QUnit
.test( 'Match PHP parser', mw
.libs
.phpParserData
.tests
.length
, function ( assert
) {
307 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
308 var tasks
= $.map( mw
.libs
.phpParserData
.tests
, function ( test
) {
309 return function ( next
) {
310 getMwLanguage( test
.lang
)
311 .done( function ( langClass
) {
312 mw
.config
.set( 'wgUserLanguage', test
.lang
);
313 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
315 parser
.parse( test
.key
, test
.args
).html(),
321 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load.' );
328 process( tasks
, QUnit
.start
);
331 QUnit
.test( 'Links', 6, function ( assert
) {
332 var expectedDisambiguationsText
,
333 expectedMultipleBars
,
334 expectedSpecialCharacters
;
336 // The below three are all identical to or based on real messages. For disambiguations-text,
337 // the bold was removed because it is not yet implemented.
340 formatParse( 'jquerymsg-test-statistics-users' ),
345 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 ' +
346 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
348 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]].' );
350 formatParse( 'disambiguations-text' ),
351 expectedDisambiguationsText
,
352 'Wikilink without pipe'
356 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
361 // Pipe trick is not supported currently, but should not parse as text either.
362 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
363 this.suppressWarnings();
365 formatParse( 'pipe-trick' ),
366 '[[Tampa, Florida|]]',
367 'Pipe trick should not be parsed.'
369 this.restoreWarnings();
371 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
372 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
374 formatParse( 'multiple-bars' ),
375 expectedMultipleBars
,
379 expectedSpecialCharacters
= '<a title=""Who" wants to be a millionaire & live on 'Exotic Island'?" href="/wiki/%22Who%22_wants_to_be_a_millionaire_%26_live_on_%27Exotic_Island%27%3F">"Who" wants to be a millionaire & live on 'Exotic Island'?</a>';
381 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
383 formatParse( 'special-characters' ),
384 expectedSpecialCharacters
,
389 // Tests that {{-transformation vs. general parsing are done as requested
390 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
391 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
393 assertBothModes( assert
, ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' );
395 assertBothModes( assert
, ['plural-msg', 5], 'Found 5 items', 'plural is resolved' );
397 assertBothModes( assert
, ['grammar-msg'], 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'grammar is resolved' );
399 mw
.config
.set( 'wgUserLanguage', 'en' );
400 assertBothModes( assert
, ['formatnum-msg', '987654321.654321'], '987,654,321.654', 'formatnum is resolved' );
402 // Test non-{{ wikitext, where behavior differs
406 formatText( 'jquerymsg-test-statistics-users' ),
407 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
408 'Internal link message unchanged when format is \'text\''
411 formatParse( 'jquerymsg-test-statistics-users' ),
413 'Internal link message parsed when format is \'parse\''
418 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
419 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
420 'External link message unchanged when format is \'text\''
423 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
425 'External link message processed when format is \'parse\''
428 // External link with parameter
430 formatText( 'external-link-replace', 'http://example.com' ),
431 'Foo [http://example.com bar]',
432 'External link message only substitutes parameter when format is \'text\''
435 formatParse( 'external-link-replace', 'http://example.com' ),
436 'Foo <a href="http://example.com">bar</a>',
437 'External link message processed when format is \'parse\''
440 formatParse( 'external-link-replace', $( '<i>' ) ),
442 'External link message processed as jQuery object when format is \'parse\''
445 formatParse( 'external-link-replace', function () {} ),
446 'Foo <a href="#">bar</a>',
447 'External link message processed as function when format is \'parse\''
450 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
453 QUnit
.test( 'Int', 4, function ( assert
) {
454 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.',
455 expectedNewarticletext
,
456 helpPageTitle
= 'Help:Foobar';
458 mw
.messages
.set( 'foobar', helpPageTitle
);
460 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 ' +
461 '<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.';
463 mw
.messages
.set( 'newarticletext', newarticletextSource
);
466 formatParse( 'newarticletext' ),
467 expectedNewarticletext
,
468 'Link with nested message'
472 formatParse( 'see-portal-url' ),
473 'Project:Community portal is an important community page.',
477 mw
.messages
.set( 'newarticletext-lowercase',
478 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
481 formatParse( 'newarticletext-lowercase' ),
482 expectedNewarticletext
,
483 'Link with nested message, lowercase include'
486 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
489 formatParse( 'uses-missing-int' ),
491 'int: where nested message does not exist'
495 // Tests that getMessageFunction is used for non-plain messages with curly braces or
496 // square brackets, but not otherwise.
497 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
498 var oldGMF
, outerCalled
, innerCalled
;
501 'curly-brace': '{{int:message}}',
502 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
503 'double-square-bracket': '[[Some page]]',
504 'regular': 'Other message'
507 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
509 mw
.jqueryMsg
.getMessageFunction = function () {
516 function verifyGetMessageFunction( key
, format
, shouldCall
) {
520 message
= mw
.message( key
);
522 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
523 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
526 verifyGetMessageFunction( 'curly-brace', 'parse', true );
527 verifyGetMessageFunction( 'curly-brace', 'plain', false );
529 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
530 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
532 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
533 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
535 verifyGetMessageFunction( 'regular', 'parse', false );
536 verifyGetMessageFunction( 'regular', 'plain', false );
538 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
539 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
540 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
542 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
548 number
: 987654321.654321,
549 result
: '987,654,321.654',
550 description
: 'formatnum test for English, decimal seperator'
554 number
: 987654321.654321,
555 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
556 description
: 'formatnum test for Arabic, with decimal seperator'
560 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
563 description
: 'formatnum test for Arabic, with decimal seperator, reverse'
569 description
: 'formatnum test for Arabic, negative number'
576 description
: 'formatnum test for Arabic, negative number, reverse'
580 number
: 987654321.654321,
581 result
: '987.654.321,654',
582 description
: 'formatnum test for Nederlands, decimal seperator'
588 description
: 'formatnum test for Nederlands, negative number'
594 description
: 'formatnum test for Nederlands'
598 number
: 'invalidnumber',
599 result
: 'invalidnumber',
600 description
: 'formatnum test for Nederlands, invalid number'
604 number
: '1000000000',
605 result
: '1,00,00,00,000',
606 description
: 'formatnum test for Malayalam'
610 number
: '-1000000000',
611 result
: '-1,00,00,00,000',
612 description
: 'formatnum test for Malayalam, negative number'
615 * This will fail because of wrong pattern for ml in MW(different from CLDR)
618 number: '1000000000.000',
619 result: '1,00,00,00,000.000',
620 description: 'formatnum test for Malayalam with decimal place'
625 number
: '123456789.123456789',
626 result
: '१२,३४,५६,७८९',
627 description
: 'formatnum test for Hindi'
631 number
: '१२,३४,५६,७८९',
632 result
: '१२,३४,५६,७८९',
633 description
: 'formatnum test for Hindi, Devanagari digits passed'
637 number
: '१२३४५६,७८९',
640 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
644 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
645 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
646 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
647 var queue
= $.map( formatnumTests
, function ( test
) {
648 return function ( next
) {
649 getMwLanguage( test
.lang
)
650 .done( function ( langClass
) {
651 mw
.config
.set( 'wgUserLanguage', test
.lang
);
652 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
654 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
655 [ test
.number
] ).html(),
661 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
667 process( queue
, QUnit
.start
);
671 QUnit
.test( 'HTML', 26, function ( assert
) {
672 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
674 assertBothModes( assert
, ['jquerymsg-italics-msg'], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
676 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
677 assertBothModes( assert
, ['jquerymsg-bold-msg'], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
679 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
680 assertBothModes( assert
, ['jquerymsg-bold-italics-msg'], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
682 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
683 assertBothModes( assert
, ['jquerymsg-italics-bold-msg'], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
685 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
688 formatParse( 'jquerymsg-italics-with-link' ),
689 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
690 'Italics with link inside in parse mode'
694 formatText( 'jquerymsg-italics-with-link' ),
695 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
696 'Italics with link unchanged in text mode'
699 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
701 formatParse( 'jquerymsg-italics-id-class' ),
702 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
703 'ID and class are allowed'
706 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
708 formatParse( 'jquerymsg-italics-onclick' ),
709 '<i onclick="alert(\'foo\')">Foo</i>',
710 'element with onclick is escaped because it is not allowed'
713 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
715 formatParse( 'jquerymsg-script-msg' ),
716 '<script >alert( "Who put this tag here?" );</script>',
717 'Tag outside whitelist escaped in parse mode'
721 formatText( 'jquerymsg-script-msg' ),
722 mw
.messages
.get( 'jquerymsg-script-msg' ),
723 'Tag outside whitelist unchanged in text mode'
726 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
728 formatParse( 'jquerymsg-script-link-msg' ),
729 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
730 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
733 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
735 formatParse( 'jquerymsg-mismatched-html' ),
736 '<i class="important">test</b>',
737 'Mismatched HTML start and end tag treated as text'
740 // TODO (mattflaschen, 2013-03-18): It's not a security issue, but there's no real
741 // reason the htmlEmitter span needs to be here. It's an artifact of how emitting works.
742 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
744 formatParse( 'jquerymsg-script-and-external-link' ),
745 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><span class="mediaWiki_htmlEmitter"><i>Foo</i> bar</span></a>',
746 'HTML tags in external links not interfering with escaping of other tags'
749 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
751 formatParse( 'jquerymsg-link-script' ),
752 '<a href="http://example.com"><span class="mediaWiki_htmlEmitter"><script>alert( "jquerymsg-link-script test" );</script></span></a>',
753 'Non-whitelisted HTML tag in external link anchor treated as text'
756 // Intentionally not using htmlEqual for the quote tests
757 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
759 formatParse( 'jquerymsg-double-quotes-preserved' ),
760 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
761 'Attributes with double quotes are preserved as such'
764 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
766 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
767 '<i id="single">Single</i>',
768 'Attributes with single quotes are normalized to double'
771 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
773 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
774 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
775 'Escaped attributes are parsed correctly'
778 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
780 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
781 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
782 'Escaped attributes are parsed correctly'
785 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
787 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
788 '<i><a href="http://example.com">Example</a></i>',
789 'Contents of valid tag are treated as wikitext, so external link is parsed'
792 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
794 formatParse( 'jquerymsg-wikitext-contents-script' ),
795 '<i><span class="mediaWiki_htmlEmitter"><script>Script inside</script></span></i>',
796 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
799 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
801 formatParse( 'jquerymsg-unclosed-tag' ),
803 'Nonsupported unclosed tags are escaped'
806 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
808 formatParse( 'jquerymsg-self-closing-tag' ),
809 'Foo<tag/>bar',
810 'Self-closing tags don\'t cause a parse error'
814 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
815 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
817 this.suppressWarnings();
818 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
821 formatParse( 'invalid-wikitext' ),
822 '<b>{{FAIL}}</b>',
823 'Invalid wikitext: \'parse\' format'
827 formatText( 'invalid-wikitext' ),
829 'Invalid wikitext: \'text\' format'
832 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
835 }( mediaWiki
, jQuery
) );