2 var formatText
, formatParse
, formatnumTests
, specialCharactersPageName
, expectedListUsers
,
3 expectedListUsersSitename
, expectedEntrypoints
,
5 hasOwn
= Object
.hasOwnProperty
;
7 // When the expected result is the same in both modes
8 function assertBothModes( assert
, parserArguments
, expectedResult
, assertMessage
) {
9 assert
.equal( formatText
.apply( null, parserArguments
), expectedResult
, assertMessage
+ ' when format is \'text\'' );
10 assert
.equal( formatParse
.apply( null, parserArguments
), expectedResult
, assertMessage
+ ' when format is \'parse\'' );
13 QUnit
.module( 'mediawiki.jqueryMsg', QUnit
.newMwEnvironment( {
15 this.originalMwLanguage
= mw
.language
;
17 specialCharactersPageName
= '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
19 expectedListUsers
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
20 expectedListUsersSitename
= '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
21 mw
.config
.get( 'wgSiteName' ) + '</a>';
23 expectedEntrypoints
= '<a href="https://www.mediawiki.org/wiki/Manual:index.php">index.php</a>';
25 formatText
= mw
.jqueryMsg
.getMessageFunction( {
29 formatParse
= mw
.jqueryMsg
.getMessageFunction( {
33 teardown: function () {
34 mw
.language
= this.originalMwLanguage
;
37 wgArticlePath
: '/wiki/$1'
39 // Messages that are reused in multiple tests
41 // The values for gender are not significant,
42 // what matters is which of the values is choosen by the parser
43 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
44 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
46 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
47 // See https://phabricator.wikimedia.org/T71993
48 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
49 // Assume the grammar form grammar_case_foo is not valid in any language
50 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
52 'formatnum-msg': '{{formatnum:$1}}',
54 'portal-url': 'Project:Community portal',
55 'see-portal-url': '{{Int:portal-url}} is an important community page.',
57 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
58 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
60 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
62 'external-link-replace': 'Foo [$1 bar]',
63 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
64 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
69 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
70 * to transport the module back to the test. It musn't be overwritten concurrentely.
72 * This function caches the mw.language data to avoid having to request the same module
73 * multiple times. There is more than one test case for any given language.
75 function getMwLanguage( langCode
) {
76 if ( !hasOwn
.call( mwLanguageCache
, langCode
) ) {
77 mwLanguageCache
[ langCode
] = $.ajax( {
78 url
: mw
.util
.wikiScript( 'load' ),
80 skin
: mw
.config
.get( 'skin' ),
82 debug
: mw
.config
.get( 'debug' ),
84 'mediawiki.language.data',
91 } ).then( function () {
95 return mwLanguageCache
[ langCode
];
99 * @param {Function[]} tasks List of functions that perform tasks
100 * that may be asynchronous. Invoke the callback parameter when done.
101 * @param {Function} complete Called when all tasks are done, or when the sequence is aborted.
103 function process( tasks
, complete
) {
104 /*jshint latedef:false */
106 tasks
.splice( 0, tasks
.length
);
111 // This happens if after the process is completed, one of our callbacks is
112 // invoked. This can happen if a test timed out but the process was still
113 // running. In that case, ignore it. Don't invoke complete() a second time.
116 var task
= tasks
.shift();
120 // Remove tasks list to indicate the process is final.
128 QUnit
.test( 'Replace', 16, function ( assert
) {
129 mw
.messages
.set( 'simple', 'Foo $1 baz $2' );
131 assert
.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
132 assert
.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
133 assert
.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
135 mw
.messages
.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
138 formatParse( 'plain-input', 'bar' ),
139 '<foo foo="foo">xbary&lt;</foo>z',
140 'Input is not considered html'
143 mw
.messages
.set( 'plain-replace', 'Foo $1' );
146 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
147 'Foo <bar bar="bar">&gt;</bar>',
148 'Replacement is not considered html'
151 mw
.messages
.set( 'object-replace', 'Foo $1' );
154 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
155 'Foo <div class="bar">></div>',
156 'jQuery objects are preserved as raw html'
160 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
161 'Foo <div class="bar">></div>',
162 'HTMLElement objects are preserved as raw html'
166 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
167 'Foo <div class="bar">></div>',
168 'HTMLElement[] arrays are preserved as raw html'
172 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
173 'Foo <a href="http://example.org/?x=y&z">bar</a>',
174 'Href is not double-escaped in wikilink function'
177 formatParse( 'external-link-plural', 1, 'http://example.org' ),
178 'Foo is <a href="http://example.org">one</a> things.',
179 'Link is expanded inside plural and is not escaped html'
182 formatParse( 'external-link-plural', 2, 'http://example.org' ),
183 'Foo <a href=\"http://example.org\">two</a> things.',
184 'Link is expanded inside an explicit plural form and is not escaped html'
187 formatParse( 'external-link-plural', 3 ),
189 'A simple explicit plural form co-existing with complex explicit plural forms'
192 formatParse( 'external-link-plural', 4, 'http://example.org' ),
194 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
197 formatParse( 'external-link-plural', 5, 'http://example.org' ),
198 'Foo are <a href="http://example.org">some</a> things.',
199 'Invalid explicit plural form. Plural fallback to the "other" plural form'
202 formatParse( 'external-link-plural', 6, 'http://example.org' ),
203 'Foo are <a href="http://example.org">some</a> things.',
204 'Plural fallback to the "other" plural form'
207 formatParse( 'plural-only-explicit-forms', 2 ),
208 'It is a double room.',
209 'Plural with explicit forms alone.'
213 QUnit
.test( 'Plural', 6, function ( assert
) {
214 assert
.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
215 assert
.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
216 assert
.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
217 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
218 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}}' );
219 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
222 QUnit
.test( 'Gender', 15, function ( assert
) {
223 var originalGender
= mw
.user
.options
.get( 'gender' );
225 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
226 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
227 mw
.user
.options
.set( 'gender', 'male' );
229 formatParse( 'gender-msg', 'Bob', 'male' ),
231 'Masculine from string "male"'
234 formatParse( 'gender-msg', 'Bob', mw
.user
),
236 'Masculine from mw.user object'
239 formatParse( 'gender-msg-currentuser' ),
241 'Masculine for current user'
244 mw
.user
.options
.set( 'gender', 'female' );
246 formatParse( 'gender-msg', 'Alice', 'female' ),
248 'Feminine from string "female"' );
250 formatParse( 'gender-msg', 'Alice', mw
.user
),
252 'Feminine from mw.user object'
255 formatParse( 'gender-msg-currentuser' ),
257 'Feminine for current user'
260 mw
.user
.options
.set( 'gender', 'unknown' );
262 formatParse( 'gender-msg', 'Foo', mw
.user
),
264 'Neutral from mw.user object' );
266 formatParse( 'gender-msg', 'User' ),
268 'Neutral when no parameter given' );
270 formatParse( 'gender-msg', 'User', 'unknown' ),
272 'Neutral from string "unknown"'
275 formatParse( 'gender-msg-currentuser' ),
277 'Neutral for current user'
280 mw
.messages
.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
283 formatParse( 'gender-msg-one-form', 'male', 10 ),
285 'Gender neutral and plural form'
288 formatParse( 'gender-msg-one-form', 'female', 1 ),
290 'Gender neutral and singular form'
293 mw
.messages
.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
295 formatParse( 'gender-msg-lowercase', 'male' ),
300 formatParse( 'gender-msg-lowercase', 'female' ),
305 mw
.messages
.set( 'gender-msg-wrong', '{{gender}} test' );
307 formatParse( 'gender-msg-wrong', 'female' ),
309 'Invalid syntax should result in {{gender}} simply being stripped away'
312 mw
.user
.options
.set( 'gender', originalGender
);
315 QUnit
.test( 'Grammar', 2, function ( assert
) {
316 assert
.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'Grammar Test with sitename' );
318 mw
.messages
.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
319 assert
.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
322 QUnit
.test( 'Match PHP parser', mw
.libs
.phpParserData
.tests
.length
, function ( assert
) {
323 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
324 var tasks
= $.map( mw
.libs
.phpParserData
.tests
, function ( test
) {
325 return function ( next
, abort
) {
326 getMwLanguage( test
.lang
)
327 .then( function ( langClass
) {
328 mw
.config
.set( 'wgUserLanguage', test
.lang
);
329 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
331 parser
.parse( test
.key
, test
.args
).html(),
336 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load.' );
338 .then( next
, abort
);
343 process( tasks
, QUnit
.start
);
346 QUnit
.test( 'Links', 7, function ( assert
) {
347 var expectedDisambiguationsText
,
348 expectedMultipleBars
,
349 expectedSpecialCharacters
;
351 // The below three are all identical to or based on real messages. For disambiguations-text,
352 // the bold was removed because it is not yet implemented.
355 formatParse( 'jquerymsg-test-statistics-users' ),
360 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 ' +
361 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
363 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]].' );
365 formatParse( 'disambiguations-text' ),
366 expectedDisambiguationsText
,
367 'Wikilink without pipe'
371 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
376 // Pipe trick is not supported currently, but should not parse as text either.
377 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
378 this.suppressWarnings();
380 formatParse( 'pipe-trick' ),
381 '[[Tampa, Florida|]]',
382 'Pipe trick should not be parsed.'
384 this.restoreWarnings();
386 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
387 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
389 formatParse( 'multiple-bars' ),
390 expectedMultipleBars
,
394 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>';
396 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
398 formatParse( 'special-characters' ),
399 expectedSpecialCharacters
,
404 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
405 expectedListUsersSitename
,
406 'Piped wikilink with parser function in the text'
410 // Tests that {{-transformation vs. general parsing are done as requested
411 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
412 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
414 assertBothModes( assert
, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
416 assertBothModes( assert
, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
418 assertBothModes( assert
, [ 'grammar-msg' ], 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'grammar is resolved' );
420 mw
.config
.set( 'wgUserLanguage', 'en' );
421 assertBothModes( assert
, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
423 // Test non-{{ wikitext, where behavior differs
427 formatText( 'jquerymsg-test-statistics-users' ),
428 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
429 'Internal link message unchanged when format is \'text\''
432 formatParse( 'jquerymsg-test-statistics-users' ),
434 'Internal link message parsed when format is \'parse\''
439 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
440 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
441 'External link message unchanged when format is \'text\''
444 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
446 'External link message processed when format is \'parse\''
449 // External link with parameter
451 formatText( 'external-link-replace', 'http://example.com' ),
452 'Foo [http://example.com bar]',
453 'External link message only substitutes parameter when format is \'text\''
456 formatParse( 'external-link-replace', 'http://example.com' ),
457 'Foo <a href="http://example.com">bar</a>',
458 'External link message processed when format is \'parse\''
461 formatParse( 'external-link-replace', $( '<i>' ) ),
463 'External link message processed as jQuery object when format is \'parse\''
466 formatParse( 'external-link-replace', function () {} ),
467 'Foo <a href="#">bar</a>',
468 'External link message processed as function when format is \'parse\''
471 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
474 QUnit
.test( 'Int', 4, function ( assert
) {
475 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.',
476 expectedNewarticletext
,
477 helpPageTitle
= 'Help:Foobar';
479 mw
.messages
.set( 'foobar', helpPageTitle
);
481 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 ' +
482 '<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.';
484 mw
.messages
.set( 'newarticletext', newarticletextSource
);
487 formatParse( 'newarticletext' ),
488 expectedNewarticletext
,
489 'Link with nested message'
493 formatParse( 'see-portal-url' ),
494 'Project:Community portal is an important community page.',
498 mw
.messages
.set( 'newarticletext-lowercase',
499 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
502 formatParse( 'newarticletext-lowercase' ),
503 expectedNewarticletext
,
504 'Link with nested message, lowercase include'
507 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
510 formatParse( 'uses-missing-int' ),
512 'int: where nested message does not exist'
516 // Tests that getMessageFunction is used for non-plain messages with curly braces or
517 // square brackets, but not otherwise.
518 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
519 var oldGMF
, outerCalled
, innerCalled
;
522 'curly-brace': '{{int:message}}',
523 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
524 'double-square-bracket': '[[Some page]]',
525 regular
: 'Other message'
528 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
530 mw
.jqueryMsg
.getMessageFunction = function () {
537 function verifyGetMessageFunction( key
, format
, shouldCall
) {
541 message
= mw
.message( key
);
543 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
544 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
547 verifyGetMessageFunction( 'curly-brace', 'parse', true );
548 verifyGetMessageFunction( 'curly-brace', 'plain', false );
550 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
551 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
553 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
554 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
556 verifyGetMessageFunction( 'regular', 'parse', false );
557 verifyGetMessageFunction( 'regular', 'plain', false );
559 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
560 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
561 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
563 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
569 number
: 987654321.654321,
570 result
: '987,654,321.654',
571 description
: 'formatnum test for English, decimal separator'
575 number
: 987654321.654321,
576 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
577 description
: 'formatnum test for Arabic, with decimal separator'
581 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
584 description
: 'formatnum test for Arabic, with decimal separator, reverse'
590 description
: 'formatnum test for Arabic, negative number'
597 description
: 'formatnum test for Arabic, negative number, reverse'
601 number
: 987654321.654321,
602 result
: '987.654.321,654',
603 description
: 'formatnum test for Nederlands, decimal separator'
609 description
: 'formatnum test for Nederlands, negative number'
615 description
: 'formatnum test for Nederlands'
619 number
: 'invalidnumber',
620 result
: 'invalidnumber',
621 description
: 'formatnum test for Nederlands, invalid number'
625 number
: '1000000000',
626 result
: '1,00,00,00,000',
627 description
: 'formatnum test for Malayalam'
631 number
: '-1000000000',
632 result
: '-1,00,00,00,000',
633 description
: 'formatnum test for Malayalam, negative number'
636 * This will fail because of wrong pattern for ml in MW(different from CLDR)
639 number: '1000000000.000',
640 result: '1,00,00,00,000.000',
641 description: 'formatnum test for Malayalam with decimal place'
646 number
: '123456789.123456789',
647 result
: '१२,३४,५६,७८९',
648 description
: 'formatnum test for Hindi'
652 number
: '१२,३४,५६,७८९',
653 result
: '१२,३४,५६,७८९',
654 description
: 'formatnum test for Hindi, Devanagari digits passed'
658 number
: '१२३४५६,७८९',
661 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
665 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
666 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
667 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
668 var queue
= $.map( formatnumTests
, function ( test
) {
669 return function ( next
, abort
) {
670 getMwLanguage( test
.lang
)
671 .then( function ( langClass
) {
672 mw
.config
.set( 'wgUserLanguage', test
.lang
);
673 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
675 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
676 [ test
.number
] ).html(),
681 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
683 .then( next
, abort
);
687 process( queue
, QUnit
.start
);
691 QUnit
.test( 'HTML', 26, function ( assert
) {
692 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
694 assertBothModes( assert
, [ 'jquerymsg-italics-msg' ], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
696 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
697 assertBothModes( assert
, [ 'jquerymsg-bold-msg' ], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
699 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
700 assertBothModes( assert
, [ 'jquerymsg-bold-italics-msg' ], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
702 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
703 assertBothModes( assert
, [ 'jquerymsg-italics-bold-msg' ], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
705 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
708 formatParse( 'jquerymsg-italics-with-link' ),
709 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
710 'Italics with link inside in parse mode'
714 formatText( 'jquerymsg-italics-with-link' ),
715 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
716 'Italics with link unchanged in text mode'
719 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
721 formatParse( 'jquerymsg-italics-id-class' ),
722 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
723 'ID and class are allowed'
726 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
728 formatParse( 'jquerymsg-italics-onclick' ),
729 '<i onclick="alert(\'foo\')">Foo</i>',
730 'element with onclick is escaped because it is not allowed'
733 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
735 formatParse( 'jquerymsg-script-msg' ),
736 '<script >alert( "Who put this tag here?" );</script>',
737 'Tag outside whitelist escaped in parse mode'
741 formatText( 'jquerymsg-script-msg' ),
742 mw
.messages
.get( 'jquerymsg-script-msg' ),
743 'Tag outside whitelist unchanged in text mode'
746 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
748 formatParse( 'jquerymsg-script-link-msg' ),
749 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
750 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
753 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
755 formatParse( 'jquerymsg-mismatched-html' ),
756 '<i class="important">test</b>',
757 'Mismatched HTML start and end tag treated as text'
760 // TODO (mattflaschen, 2013-03-18): It's not a security issue, but there's no real
761 // reason the htmlEmitter span needs to be here. It's an artifact of how emitting works.
762 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
764 formatParse( 'jquerymsg-script-and-external-link' ),
765 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><span class="mediaWiki_htmlEmitter"><i>Foo</i> bar</span></a>',
766 'HTML tags in external links not interfering with escaping of other tags'
769 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
771 formatParse( 'jquerymsg-link-script' ),
772 '<a href="http://example.com"><span class="mediaWiki_htmlEmitter"><script>alert( "jquerymsg-link-script test" );</script></span></a>',
773 'Non-whitelisted HTML tag in external link anchor treated as text'
776 // Intentionally not using htmlEqual for the quote tests
777 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
779 formatParse( 'jquerymsg-double-quotes-preserved' ),
780 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
781 'Attributes with double quotes are preserved as such'
784 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
786 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
787 '<i id="single">Single</i>',
788 'Attributes with single quotes are normalized to double'
791 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
793 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
794 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
795 'Escaped attributes are parsed correctly'
798 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
800 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
801 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
802 'Escaped attributes are parsed correctly'
805 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
807 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
808 '<i><a href="http://example.com">Example</a></i>',
809 'Contents of valid tag are treated as wikitext, so external link is parsed'
812 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
814 formatParse( 'jquerymsg-wikitext-contents-script' ),
815 '<i><span class="mediaWiki_htmlEmitter"><script>Script inside</script></span></i>',
816 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
819 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
821 formatParse( 'jquerymsg-unclosed-tag' ),
823 'Nonsupported unclosed tags are escaped'
826 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
828 formatParse( 'jquerymsg-self-closing-tag' ),
829 'Foo<tag/>bar',
830 'Self-closing tags don\'t cause a parse error'
834 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
835 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
837 this.suppressWarnings();
838 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
841 formatParse( 'invalid-wikitext' ),
842 '<b>{{FAIL}}</b>',
843 'Invalid wikitext: \'parse\' format'
847 formatText( 'invalid-wikitext' ),
849 'Invalid wikitext: \'text\' format'
852 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
855 }( mediaWiki
, jQuery
) );