2 var mwLanguageCache
= {}, formatText
, formatParse
, formatnumTests
, specialCharactersPageName
,
3 expectedListUsers
, expectedEntrypoints
;
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\'' );
11 QUnit
.module( 'mediawiki.jqueryMsg', QUnit
.newMwEnvironment( {
13 this.originalMwLanguage
= mw
.language
;
15 specialCharactersPageName
= '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
17 expectedListUsers
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
19 expectedEntrypoints
= '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
21 formatText
= mw
.jqueryMsg
.getMessageFunction( {
25 formatParse
= mw
.jqueryMsg
.getMessageFunction( {
29 teardown: function () {
30 mw
.language
= this.originalMwLanguage
;
33 wgArticlePath
: '/wiki/$1'
35 // Messages that are reused in multiple tests
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}}',
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}}}}',
48 'formatnum-msg': '{{formatnum:$1}}',
50 'portal-url': 'Project:Community portal',
51 'see-portal-url': '{{Int:portal-url}} is an important community page.',
53 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
55 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
57 'external-link-replace': 'Foo [$1 bar]',
58 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
59 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
63 function getMwLanguage( langCode
, cb
) {
64 if ( mwLanguageCache
[langCode
] !== undefined ) {
65 mwLanguageCache
[langCode
].add( cb
);
68 mwLanguageCache
[langCode
] = $.Callbacks( 'once memory' );
69 mwLanguageCache
[langCode
].add( cb
);
71 url
: mw
.util
.wikiScript( 'load' ),
73 skin
: mw
.config
.get( 'skin' ),
75 debug
: mw
.config
.get( 'debug' ),
77 'mediawiki.language.data',
83 } ).done(function () {
84 mwLanguageCache
[langCode
].fire( mw
.language
);
85 } ).fail( function () {
86 mwLanguageCache
[langCode
].fire( false );
90 QUnit
.test( 'Replace', 16, function ( assert
) {
91 mw
.messages
.set( 'simple', 'Foo $1 baz $2' );
93 assert
.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
94 assert
.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
95 assert
.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
97 mw
.messages
.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
100 formatParse( 'plain-input', 'bar' ),
101 '<foo foo="foo">xbary&lt;</foo>z',
102 'Input is not considered html'
105 mw
.messages
.set( 'plain-replace', 'Foo $1' );
108 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
109 'Foo <bar bar="bar">&gt;</bar>',
110 'Replacement is not considered html'
113 mw
.messages
.set( 'object-replace', 'Foo $1' );
116 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
117 'Foo <div class="bar">></div>',
118 'jQuery objects are preserved as raw html'
122 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
123 'Foo <div class="bar">></div>',
124 'HTMLElement objects are preserved as raw html'
128 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
129 'Foo <div class="bar">></div>',
130 'HTMLElement[] arrays are preserved as raw html'
134 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
135 'Foo <a href="http://example.org/?x=y&z">bar</a>',
136 'Href is not double-escaped in wikilink function'
139 formatParse( 'external-link-plural', 1, 'http://example.org' ),
140 'Foo is <a href="http://example.org">one</a> things.',
141 'Link is expanded inside plural and is not escaped html'
144 formatParse( 'external-link-plural', 2, 'http://example.org' ),
145 'Foo <a href=\"http://example.org\">two</a> things.',
146 'Link is expanded inside an explicit plural form and is not escaped html'
149 formatParse( 'external-link-plural', 3 ),
151 'A simple explicit plural form co-existing with complex explicit plural forms'
154 formatParse( 'external-link-plural', 4, 'http://example.org' ),
156 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
159 formatParse( 'external-link-plural', 5, 'http://example.org' ),
160 'Foo are <a href="http://example.org">some</a> things.',
161 'Invalid explicit plural form. Plural fallback to the "other" plural form'
164 formatParse( 'external-link-plural', 6, 'http://example.org' ),
165 'Foo are <a href="http://example.org">some</a> things.',
166 'Plural fallback to the "other" plural form'
169 formatParse( 'plural-only-explicit-forms', 2 ),
170 'It is a double room.',
171 'Plural with explicit forms alone.'
175 QUnit
.test( 'Plural', 6, function ( assert
) {
176 assert
.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
177 assert
.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
178 assert
.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
179 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
180 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}}' );
181 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
184 QUnit
.test( 'Gender', 15, function ( assert
) {
185 var originalGender
= mw
.user
.options
.get( 'gender' );
187 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
188 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
189 mw
.user
.options
.set( 'gender', 'male' );
191 formatParse( 'gender-msg', 'Bob', 'male' ),
193 'Masculine from string "male"'
196 formatParse( 'gender-msg', 'Bob', mw
.user
),
198 'Masculine from mw.user object'
201 formatParse( 'gender-msg-currentuser' ),
203 'Masculine for current user'
206 mw
.user
.options
.set( 'gender', 'female' );
208 formatParse( 'gender-msg', 'Alice', 'female' ),
210 'Feminine from string "female"' );
212 formatParse( 'gender-msg', 'Alice', mw
.user
),
214 'Feminine from mw.user object'
217 formatParse( 'gender-msg-currentuser' ),
219 'Feminine for current user'
222 mw
.user
.options
.set( 'gender', 'unknown' );
224 formatParse( 'gender-msg', 'Foo', mw
.user
),
226 'Neutral from mw.user object' );
228 formatParse( 'gender-msg', 'User' ),
230 'Neutral when no parameter given' );
232 formatParse( 'gender-msg', 'User', 'unknown' ),
234 'Neutral from string "unknown"'
237 formatParse( 'gender-msg-currentuser' ),
239 'Neutral for current user'
242 mw
.messages
.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
245 formatParse( 'gender-msg-one-form', 'male', 10 ),
247 'Gender neutral and plural form'
250 formatParse( 'gender-msg-one-form', 'female', 1 ),
252 'Gender neutral and singular form'
255 mw
.messages
.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
257 formatParse( 'gender-msg-lowercase', 'male' ),
262 formatParse( 'gender-msg-lowercase', 'female' ),
267 mw
.messages
.set( 'gender-msg-wrong', '{{gender}} test' );
269 formatParse( 'gender-msg-wrong', 'female' ),
271 'Invalid syntax should result in {{gender}} simply being stripped away'
274 mw
.user
.options
.set( 'gender', originalGender
);
277 QUnit
.test( 'Grammar', 2, function ( assert
) {
278 assert
.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'Grammar Test with sitename' );
280 mw
.messages
.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
281 assert
.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
284 QUnit
.test( 'Match PHP parser', mw
.libs
.phpParserData
.tests
.length
, function ( assert
) {
285 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
286 $.each( mw
.libs
.phpParserData
.tests
, function ( i
, test
) {
288 getMwLanguage( test
.lang
, function ( langClass
) {
291 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
294 mw
.config
.set( 'wgUserLanguage', test
.lang
);
295 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
297 parser
.parse( test
.key
, test
.args
).html(),
305 QUnit
.test( 'Links', 6, function ( assert
) {
306 var expectedDisambiguationsText
,
307 expectedMultipleBars
,
308 expectedSpecialCharacters
;
310 // The below three are all identical to or based on real messages. For disambiguations-text,
311 // the bold was removed because it is not yet implemented.
314 formatParse( 'jquerymsg-test-statistics-users' ),
319 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 ' +
320 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
322 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]].' );
324 formatParse( 'disambiguations-text' ),
325 expectedDisambiguationsText
,
326 'Wikilink without pipe'
330 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
335 // Pipe trick is not supported currently, but should not parse as text either.
336 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
337 this.suppressWarnings();
339 formatParse( 'pipe-trick' ),
340 '[[Tampa, Florida|]]',
341 'Pipe trick should not be parsed.'
343 this.restoreWarnings();
345 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
346 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
348 formatParse( 'multiple-bars' ),
349 expectedMultipleBars
,
353 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>';
355 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
357 formatParse( 'special-characters' ),
358 expectedSpecialCharacters
,
363 // Tests that {{-transformation vs. general parsing are done as requested
364 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
365 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
367 assertBothModes( assert
, ['gender-msg', 'Bob', 'male'], 'Bob: blue', 'gender is resolved' );
369 assertBothModes( assert
, ['plural-msg', 5], 'Found 5 items', 'plural is resolved' );
371 assertBothModes( assert
, ['grammar-msg'], 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'grammar is resolved' );
373 mw
.config
.set( 'wgUserLanguage', 'en' );
374 assertBothModes( assert
, ['formatnum-msg', '987654321.654321'], '987,654,321.654', 'formatnum is resolved' );
376 // Test non-{{ wikitext, where behavior differs
380 formatText( 'jquerymsg-test-statistics-users' ),
381 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
382 'Internal link message unchanged when format is \'text\''
385 formatParse( 'jquerymsg-test-statistics-users' ),
387 'Internal link message parsed when format is \'parse\''
392 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
393 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
394 'External link message unchanged when format is \'text\''
397 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
399 'External link message processed when format is \'parse\''
402 // External link with parameter
404 formatText( 'external-link-replace', 'http://example.com' ),
405 'Foo [http://example.com bar]',
406 'External link message only substitutes parameter when format is \'text\''
409 formatParse( 'external-link-replace', 'http://example.com' ),
410 'Foo <a href="http://example.com">bar</a>',
411 'External link message processed when format is \'parse\''
414 formatParse( 'external-link-replace', $( '<i>' ) ),
416 'External link message processed as jQuery object when format is \'parse\''
419 formatParse( 'external-link-replace', function () {} ),
420 'Foo <a href="#">bar</a>',
421 'External link message processed as function when format is \'parse\''
424 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
427 QUnit
.test( 'Int', 4, function ( assert
) {
428 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.',
429 expectedNewarticletext
,
430 helpPageTitle
= 'Help:Foobar';
432 mw
.messages
.set( 'foobar', helpPageTitle
);
434 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 ' +
435 '<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.';
437 mw
.messages
.set( 'newarticletext', newarticletextSource
);
440 formatParse( 'newarticletext' ),
441 expectedNewarticletext
,
442 'Link with nested message'
446 formatParse( 'see-portal-url' ),
447 'Project:Community portal is an important community page.',
451 mw
.messages
.set( 'newarticletext-lowercase',
452 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
455 formatParse( 'newarticletext-lowercase' ),
456 expectedNewarticletext
,
457 'Link with nested message, lowercase include'
460 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
463 formatParse( 'uses-missing-int' ),
465 'int: where nested message does not exist'
469 // Tests that getMessageFunction is used for non-plain messages with curly braces or
470 // square brackets, but not otherwise.
471 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
472 var oldGMF
, outerCalled
, innerCalled
;
475 'curly-brace': '{{int:message}}',
476 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
477 'double-square-bracket': '[[Some page]]',
478 'regular': 'Other message'
481 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
483 mw
.jqueryMsg
.getMessageFunction = function () {
490 function verifyGetMessageFunction( key
, format
, shouldCall
) {
494 message
= mw
.message( key
);
496 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
497 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
500 verifyGetMessageFunction( 'curly-brace', 'parse', true );
501 verifyGetMessageFunction( 'curly-brace', 'plain', false );
503 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
504 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
506 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
507 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
509 verifyGetMessageFunction( 'regular', 'parse', false );
510 verifyGetMessageFunction( 'regular', 'plain', false );
512 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
513 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
514 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
516 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
522 number
: 987654321.654321,
523 result
: '987,654,321.654',
524 description
: 'formatnum test for English, decimal seperator'
528 number
: 987654321.654321,
529 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
530 description
: 'formatnum test for Arabic, with decimal seperator'
534 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
537 description
: 'formatnum test for Arabic, with decimal seperator, reverse'
543 description
: 'formatnum test for Arabic, negative number'
550 description
: 'formatnum test for Arabic, negative number, reverse'
554 number
: 987654321.654321,
555 result
: '987.654.321,654',
556 description
: 'formatnum test for Nederlands, decimal seperator'
562 description
: 'formatnum test for Nederlands, negative number'
568 description
: 'formatnum test for Nederlands'
572 number
: 'invalidnumber',
573 result
: 'invalidnumber',
574 description
: 'formatnum test for Nederlands, invalid number'
578 number
: '1000000000',
579 result
: '1,00,00,00,000',
580 description
: 'formatnum test for Malayalam'
584 number
: '-1000000000',
585 result
: '-1,00,00,00,000',
586 description
: 'formatnum test for Malayalam, negative number'
589 * This will fail because of wrong pattern for ml in MW(different from CLDR)
592 number: '1000000000.000',
593 result: '1,00,00,00,000.000',
594 description: 'formatnum test for Malayalam with decimal place'
599 number
: '123456789.123456789',
600 result
: '१२,३४,५६,७८९',
601 description
: 'formatnum test for Hindi'
605 number
: '१२,३४,५६,७८९',
606 result
: '१२,३४,५६,७८९',
607 description
: 'formatnum test for Hindi, Devanagari digits passed'
611 number
: '१२३४५६,७८९',
614 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
618 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
619 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
620 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
621 $.each( formatnumTests
, function ( i
, test
) {
623 getMwLanguage( test
.lang
, function ( langClass
) {
626 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
629 mw
.messages
.set(test
.message
);
630 mw
.config
.set( 'wgUserLanguage', test
.lang
);
631 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
633 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
634 [ test
.number
] ).html(),
643 QUnit
.test( 'HTML', 26, function ( assert
) {
644 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
646 assertBothModes( assert
, ['jquerymsg-italics-msg'], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
648 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
649 assertBothModes( assert
, ['jquerymsg-bold-msg'], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
651 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
652 assertBothModes( assert
, ['jquerymsg-bold-italics-msg'], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
654 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
655 assertBothModes( assert
, ['jquerymsg-italics-bold-msg'], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
657 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
660 formatParse( 'jquerymsg-italics-with-link' ),
661 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
662 'Italics with link inside in parse mode'
666 formatText( 'jquerymsg-italics-with-link' ),
667 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
668 'Italics with link unchanged in text mode'
671 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
673 formatParse( 'jquerymsg-italics-id-class' ),
674 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
675 'ID and class are allowed'
678 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
680 formatParse( 'jquerymsg-italics-onclick' ),
681 '<i onclick="alert(\'foo\')">Foo</i>',
682 'element with onclick is escaped because it is not allowed'
685 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
687 formatParse( 'jquerymsg-script-msg' ),
688 '<script >alert( "Who put this tag here?" );</script>',
689 'Tag outside whitelist escaped in parse mode'
693 formatText( 'jquerymsg-script-msg' ),
694 mw
.messages
.get( 'jquerymsg-script-msg' ),
695 'Tag outside whitelist unchanged in text mode'
698 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
700 formatParse( 'jquerymsg-script-link-msg' ),
701 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
702 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
705 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
707 formatParse( 'jquerymsg-mismatched-html' ),
708 '<i class="important">test</b>',
709 'Mismatched HTML start and end tag treated as text'
712 // TODO (mattflaschen, 2013-03-18): It's not a security issue, but there's no real
713 // reason the htmlEmitter span needs to be here. It's an artifact of how emitting works.
714 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
716 formatParse( 'jquerymsg-script-and-external-link' ),
717 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><span class="mediaWiki_htmlEmitter"><i>Foo</i> bar</span></a>',
718 'HTML tags in external links not interfering with escaping of other tags'
721 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
723 formatParse( 'jquerymsg-link-script' ),
724 '<a href="http://example.com"><span class="mediaWiki_htmlEmitter"><script>alert( "jquerymsg-link-script test" );</script></span></a>',
725 'Non-whitelisted HTML tag in external link anchor treated as text'
728 // Intentionally not using htmlEqual for the quote tests
729 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
731 formatParse( 'jquerymsg-double-quotes-preserved' ),
732 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
733 'Attributes with double quotes are preserved as such'
736 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
738 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
739 '<i id="single">Single</i>',
740 'Attributes with single quotes are normalized to double'
743 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
745 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
746 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
747 'Escaped attributes are parsed correctly'
750 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
752 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
753 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
754 'Escaped attributes are parsed correctly'
757 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
759 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
760 '<i><a href="http://example.com">Example</a></i>',
761 'Contents of valid tag are treated as wikitext, so external link is parsed'
764 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
766 formatParse( 'jquerymsg-wikitext-contents-script' ),
767 '<i><span class="mediaWiki_htmlEmitter"><script>Script inside</script></span></i>',
768 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
771 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
773 formatParse( 'jquerymsg-unclosed-tag' ),
775 'Nonsupported unclosed tags are escaped'
778 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
780 formatParse( 'jquerymsg-self-closing-tag' ),
781 'Foo<tag/>bar',
782 'Self-closing tags don\'t cause a parse error'
786 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
787 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
789 this.suppressWarnings();
790 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
793 formatParse( 'invalid-wikitext' ),
794 '<b>{{FAIL}}</b>',
795 'Invalid wikitext: \'parse\' format'
799 formatText( 'invalid-wikitext' ),
801 'Invalid wikitext: \'text\' format'
804 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
807 }( mediaWiki
, jQuery
) );