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',
38 // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
46 // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
47 wgFormattedNamespaces
: {
50 11: 'Dyskusja szablonu'
53 // Messages that are reused in multiple tests
55 // The values for gender are not significant,
56 // what matters is which of the values is choosen by the parser
57 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
58 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
60 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
61 // See https://phabricator.wikimedia.org/T71993
62 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
63 // Assume the grammar form grammar_case_foo is not valid in any language
64 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
66 'formatnum-msg': '{{formatnum:$1}}',
68 'portal-url': 'Project:Community portal',
69 'see-portal-url': '{{Int:portal-url}} is an important community page.',
71 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
72 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
74 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
76 'external-link-replace': 'Foo [$1 bar]',
77 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b|5=}} things.',
78 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.'
83 * Be careful to no run this in parallel as it uses a global identifier (mw.language)
84 * to transport the module back to the test. It musn't be overwritten concurrentely.
86 * This function caches the mw.language data to avoid having to request the same module
87 * multiple times. There is more than one test case for any given language.
89 function getMwLanguage( langCode
) {
90 if ( !hasOwn
.call( mwLanguageCache
, langCode
) ) {
91 mwLanguageCache
[ langCode
] = $.ajax( {
92 url
: mw
.util
.wikiScript( 'load' ),
94 skin
: mw
.config
.get( 'skin' ),
96 debug
: mw
.config
.get( 'debug' ),
98 'mediawiki.language.data',
105 } ).then( function () {
109 return mwLanguageCache
[ langCode
];
113 * @param {Function[]} tasks List of functions that perform tasks
114 * that may be asynchronous. Invoke the callback parameter when done.
115 * @param {Function} complete Called when all tasks are done, or when the sequence is aborted.
117 function process( tasks
, complete
) {
118 /*jshint latedef:false */
120 tasks
.splice( 0, tasks
.length
);
125 // This happens if after the process is completed, one of our callbacks is
126 // invoked. This can happen if a test timed out but the process was still
127 // running. In that case, ignore it. Don't invoke complete() a second time.
130 var task
= tasks
.shift();
134 // Remove tasks list to indicate the process is final.
142 QUnit
.test( 'Replace', 16, function ( assert
) {
143 mw
.messages
.set( 'simple', 'Foo $1 baz $2' );
145 assert
.equal( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
146 assert
.equal( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
147 assert
.equal( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
149 mw
.messages
.set( 'plain-input', '<foo foo="foo">x$1y<</foo>z' );
152 formatParse( 'plain-input', 'bar' ),
153 '<foo foo="foo">xbary&lt;</foo>z',
154 'Input is not considered html'
157 mw
.messages
.set( 'plain-replace', 'Foo $1' );
160 formatParse( 'plain-replace', '<bar bar="bar">></bar>' ),
161 'Foo <bar bar="bar">&gt;</bar>',
162 'Replacement is not considered html'
165 mw
.messages
.set( 'object-replace', 'Foo $1' );
168 formatParse( 'object-replace', $( '<div class="bar">></div>' ) ),
169 'Foo <div class="bar">></div>',
170 'jQuery objects are preserved as raw html'
174 formatParse( 'object-replace', $( '<div class="bar">></div>' ).get( 0 ) ),
175 'Foo <div class="bar">></div>',
176 'HTMLElement objects are preserved as raw html'
180 formatParse( 'object-replace', $( '<div class="bar">></div>' ).toArray() ),
181 'Foo <div class="bar">></div>',
182 'HTMLElement[] arrays are preserved as raw html'
186 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
187 'Foo <a href="http://example.org/?x=y&z">bar</a>',
188 'Href is not double-escaped in wikilink function'
191 formatParse( 'external-link-plural', 1, 'http://example.org' ),
192 'Foo is <a href="http://example.org">one</a> things.',
193 'Link is expanded inside plural and is not escaped html'
196 formatParse( 'external-link-plural', 2, 'http://example.org' ),
197 'Foo <a href=\"http://example.org\">two</a> things.',
198 'Link is expanded inside an explicit plural form and is not escaped html'
201 formatParse( 'external-link-plural', 3 ),
203 'A simple explicit plural form co-existing with complex explicit plural forms'
206 formatParse( 'external-link-plural', 4, 'http://example.org' ),
208 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
211 formatParse( 'external-link-plural', 5, 'http://example.org' ),
212 'Foo are <a href="http://example.org">some</a> things.',
213 'Invalid explicit plural form. Plural fallback to the "other" plural form'
216 formatParse( 'external-link-plural', 6, 'http://example.org' ),
217 'Foo are <a href="http://example.org">some</a> things.',
218 'Plural fallback to the "other" plural form'
221 formatParse( 'plural-only-explicit-forms', 2 ),
222 'It is a double room.',
223 'Plural with explicit forms alone.'
227 QUnit
.test( 'Plural', 6, function ( assert
) {
228 assert
.equal( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
229 assert
.equal( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
230 assert
.equal( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
231 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
232 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}}' );
233 assert
.equal( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
236 QUnit
.test( 'Gender', 15, function ( assert
) {
237 var originalGender
= mw
.user
.options
.get( 'gender' );
239 // TODO: These tests should be for mw.msg once mw.msg integrated with mw.jqueryMsg
240 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
241 mw
.user
.options
.set( 'gender', 'male' );
243 formatParse( 'gender-msg', 'Bob', 'male' ),
245 'Masculine from string "male"'
248 formatParse( 'gender-msg', 'Bob', mw
.user
),
250 'Masculine from mw.user object'
253 formatParse( 'gender-msg-currentuser' ),
255 'Masculine for current user'
258 mw
.user
.options
.set( 'gender', 'female' );
260 formatParse( 'gender-msg', 'Alice', 'female' ),
262 'Feminine from string "female"' );
264 formatParse( 'gender-msg', 'Alice', mw
.user
),
266 'Feminine from mw.user object'
269 formatParse( 'gender-msg-currentuser' ),
271 'Feminine for current user'
274 mw
.user
.options
.set( 'gender', 'unknown' );
276 formatParse( 'gender-msg', 'Foo', mw
.user
),
278 'Neutral from mw.user object' );
280 formatParse( 'gender-msg', 'User' ),
282 'Neutral when no parameter given' );
284 formatParse( 'gender-msg', 'User', 'unknown' ),
286 'Neutral from string "unknown"'
289 formatParse( 'gender-msg-currentuser' ),
291 'Neutral for current user'
294 mw
.messages
.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
297 formatParse( 'gender-msg-one-form', 'male', 10 ),
299 'Gender neutral and plural form'
302 formatParse( 'gender-msg-one-form', 'female', 1 ),
304 'Gender neutral and singular form'
307 mw
.messages
.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
309 formatParse( 'gender-msg-lowercase', 'male' ),
314 formatParse( 'gender-msg-lowercase', 'female' ),
319 mw
.messages
.set( 'gender-msg-wrong', '{{gender}} test' );
321 formatParse( 'gender-msg-wrong', 'female' ),
323 'Invalid syntax should result in {{gender}} simply being stripped away'
326 mw
.user
.options
.set( 'gender', originalGender
);
329 QUnit
.test( 'Grammar', 2, function ( assert
) {
330 assert
.equal( formatParse( 'grammar-msg' ), 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'Grammar Test with sitename' );
332 mw
.messages
.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
333 assert
.equal( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
336 QUnit
.test( 'Match PHP parser', mw
.libs
.phpParserData
.tests
.length
, function ( assert
) {
337 mw
.messages
.set( mw
.libs
.phpParserData
.messages
);
338 var tasks
= $.map( mw
.libs
.phpParserData
.tests
, function ( test
) {
339 return function ( next
, abort
) {
340 getMwLanguage( test
.lang
)
341 .then( function ( langClass
) {
342 mw
.config
.set( 'wgUserLanguage', test
.lang
);
343 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
345 parser
.parse( test
.key
, test
.args
).html(),
350 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load.' );
352 .then( next
, abort
);
357 process( tasks
, QUnit
.start
);
360 QUnit
.test( 'Links', 14, function ( assert
) {
362 expectedDisambiguationsText
,
363 expectedMultipleBars
,
364 expectedSpecialCharacters
;
366 // The below three are all identical to or based on real messages. For disambiguations-text,
367 // the bold was removed because it is not yet implemented.
370 formatParse( 'jquerymsg-test-statistics-users' ),
375 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 ' +
376 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
378 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]].' );
380 formatParse( 'disambiguations-text' ),
381 expectedDisambiguationsText
,
382 'Wikilink without pipe'
386 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
391 // Pipe trick is not supported currently, but should not parse as text either.
392 mw
.messages
.set( 'pipe-trick', '[[Tampa, Florida|]]' );
393 mw
.messages
.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
394 mw
.messages
.set( 'empty-link', '[[]]' );
395 this.suppressWarnings();
397 formatParse( 'pipe-trick' ),
398 '[[Tampa, Florida|]]',
399 'Pipe trick should not be parsed.'
402 formatParse( 'reverse-pipe-trick' ),
403 '[[|Tampa, Florida]]',
404 'Reverse pipe trick should not be parsed.'
407 formatParse( 'empty-link' ),
409 'Empty link should not be parsed.'
411 this.restoreWarnings();
413 expectedMultipleBars
= '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
414 mw
.messages
.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
416 formatParse( 'multiple-bars' ),
417 expectedMultipleBars
,
421 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>';
423 mw
.messages
.set( 'special-characters', '[[' + specialCharactersPageName
+ ']]' );
425 formatParse( 'special-characters' ),
426 expectedSpecialCharacters
,
430 mw
.messages
.set( 'leading-colon', '[[:File:Foo.jpg]]' );
432 formatParse( 'leading-colon' ),
433 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
434 'Leading colon in links is stripped'
438 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
439 expectedListUsersSitename
,
440 'Piped wikilink with parser function in the text'
446 'asd [http://example.org <strong>Example</strong>] asd',
447 'asd <a href="http://example.org"><strong>Example</strong></a> asd'
450 'extlink-html-partial',
451 'asd [http://example.org foo <strong>Example</strong> bar] asd',
452 'asd <a href="http://example.org">foo <strong>Example</strong> bar</a> asd'
455 'wikilink-html-full',
456 'asd [[Example|<strong>Example</strong>]] asd',
457 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
460 'wikilink-html-partial',
461 'asd [[Example|foo <strong>Example</strong> bar]] asd',
462 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
466 $.each( testCases
, function () {
471 mw
.messages
.set( key
, input
);
475 'HTML in links: ' + key
480 QUnit
.test( 'Replacements in links', 14, function ( assert
) {
483 'extlink-param-href-full',
484 'asd [$1 Example] asd',
485 'asd <a href="http://example.com">Example</a> asd'
488 'extlink-param-href-partial',
489 'asd [$1/example Example] asd',
490 'asd <a href="http://example.com/example">Example</a> asd'
493 'extlink-param-text-full',
494 'asd [http://example.org $2] asd',
495 'asd <a href="http://example.org">Text</a> asd'
498 'extlink-param-text-partial',
499 'asd [http://example.org Example $2] asd',
500 'asd <a href="http://example.org">Example Text</a> asd'
503 'extlink-param-both-full',
505 'asd <a href="http://example.com">Text</a> asd'
508 'extlink-param-both-partial',
509 'asd [$1/example Example $2] asd',
510 'asd <a href="http://example.com/example">Example Text</a> asd'
513 'wikilink-param-href-full',
514 'asd [[$1|Example]] asd',
515 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
518 'wikilink-param-href-partial',
519 'asd [[$1/Test|Example]] asd',
520 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
523 'wikilink-param-text-full',
524 'asd [[Example|$2]] asd',
525 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
528 'wikilink-param-text-partial',
529 'asd [[Example|Example $2]] asd',
530 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
533 'wikilink-param-both-full',
535 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
538 'wikilink-param-both-partial',
539 'asd [[$1/Test|Example $2]] asd',
540 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
543 'wikilink-param-unpiped-full',
545 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
548 'wikilink-param-unpiped-partial',
549 'asd [[$1/Test]] asd',
550 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
554 $.each( testCases
, function () {
559 paramHref
= key
.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
561 mw
.messages
.set( key
, input
);
563 formatParse( key
, paramHref
, paramText
),
565 'Replacements in links: ' + key
570 // Tests that {{-transformation vs. general parsing are done as requested
571 QUnit
.test( 'Curly brace transformation', 16, function ( assert
) {
572 var oldUserLang
= mw
.config
.get( 'wgUserLanguage' );
574 assertBothModes( assert
, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
576 assertBothModes( assert
, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
578 assertBothModes( assert
, [ 'grammar-msg' ], 'Przeszukaj ' + mw
.config
.get( 'wgSiteName' ), 'grammar is resolved' );
580 mw
.config
.set( 'wgUserLanguage', 'en' );
581 assertBothModes( assert
, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
583 // Test non-{{ wikitext, where behavior differs
587 formatText( 'jquerymsg-test-statistics-users' ),
588 mw
.messages
.get( 'jquerymsg-test-statistics-users' ),
589 'Internal link message unchanged when format is \'text\''
592 formatParse( 'jquerymsg-test-statistics-users' ),
594 'Internal link message parsed when format is \'parse\''
599 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
600 mw
.messages
.get( 'jquerymsg-test-version-entrypoints-index-php' ),
601 'External link message unchanged when format is \'text\''
604 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
606 'External link message processed when format is \'parse\''
609 // External link with parameter
611 formatText( 'external-link-replace', 'http://example.com' ),
612 'Foo [http://example.com bar]',
613 'External link message only substitutes parameter when format is \'text\''
616 formatParse( 'external-link-replace', 'http://example.com' ),
617 'Foo <a href="http://example.com">bar</a>',
618 'External link message processed when format is \'parse\''
621 formatParse( 'external-link-replace', $( '<i>' ) ),
623 'External link message processed as jQuery object when format is \'parse\''
626 formatParse( 'external-link-replace', function () {} ),
627 'Foo <a href="#">bar</a>',
628 'External link message processed as function when format is \'parse\''
631 mw
.config
.set( 'wgUserLanguage', oldUserLang
);
634 QUnit
.test( 'Int', 4, function ( assert
) {
635 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.',
636 expectedNewarticletext
,
637 helpPageTitle
= 'Help:Foobar';
639 mw
.messages
.set( 'foobar', helpPageTitle
);
641 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 ' +
642 '<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.';
644 mw
.messages
.set( 'newarticletext', newarticletextSource
);
647 formatParse( 'newarticletext' ),
648 expectedNewarticletext
,
649 'Link with nested message'
653 formatParse( 'see-portal-url' ),
654 'Project:Community portal is an important community page.',
658 mw
.messages
.set( 'newarticletext-lowercase',
659 newarticletextSource
.replace( 'Int:Helppage', 'int:helppage' ) );
662 formatParse( 'newarticletext-lowercase' ),
663 expectedNewarticletext
,
664 'Link with nested message, lowercase include'
667 mw
.messages
.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
670 formatParse( 'uses-missing-int' ),
672 'int: where nested message does not exist'
676 QUnit
.test( 'Ns', 4, function ( assert
) {
677 mw
.messages
.set( 'ns-template-talk', '{{ns:Template talk}}' );
679 formatParse( 'ns-template-talk' ),
681 'ns: returns localised namespace when used with a canonical namespace name'
684 mw
.messages
.set( 'ns-10', '{{ns:10}}' );
686 formatParse( 'ns-10' ),
688 'ns: returns localised namespace when used with a namespace number'
691 mw
.messages
.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
693 formatParse( 'ns-unknown' ),
695 'ns: returns empty string for unknown namespace name'
698 mw
.messages
.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
700 formatParse( 'ns-in-a-link' ),
701 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
702 'ns: works when used inside a wikilink'
706 // Tests that getMessageFunction is used for non-plain messages with curly braces or
707 // square brackets, but not otherwise.
708 QUnit
.test( 'mw.Message.prototype.parser monkey-patch', 22, function ( assert
) {
709 var oldGMF
, outerCalled
, innerCalled
;
712 'curly-brace': '{{int:message}}',
713 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
714 'double-square-bracket': '[[Some page]]',
715 regular
: 'Other message'
718 oldGMF
= mw
.jqueryMsg
.getMessageFunction
;
720 mw
.jqueryMsg
.getMessageFunction = function () {
727 function verifyGetMessageFunction( key
, format
, shouldCall
) {
731 message
= mw
.message( key
);
733 assert
.strictEqual( outerCalled
, shouldCall
, 'Outer function called for ' + key
);
734 assert
.strictEqual( innerCalled
, shouldCall
, 'Inner function called for ' + key
);
737 verifyGetMessageFunction( 'curly-brace', 'parse', true );
738 verifyGetMessageFunction( 'curly-brace', 'plain', false );
740 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
741 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
743 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
744 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
746 verifyGetMessageFunction( 'regular', 'parse', false );
747 verifyGetMessageFunction( 'regular', 'plain', false );
749 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
750 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
751 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
753 mw
.jqueryMsg
.getMessageFunction
= oldGMF
;
759 number
: 987654321.654321,
760 result
: '987,654,321.654',
761 description
: 'formatnum test for English, decimal separator'
765 number
: 987654321.654321,
766 result
: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
767 description
: 'formatnum test for Arabic, with decimal separator'
771 number
: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
774 description
: 'formatnum test for Arabic, with decimal separator, reverse'
780 description
: 'formatnum test for Arabic, negative number'
787 description
: 'formatnum test for Arabic, negative number, reverse'
791 number
: 987654321.654321,
792 result
: '987.654.321,654',
793 description
: 'formatnum test for Nederlands, decimal separator'
799 description
: 'formatnum test for Nederlands, negative number'
805 description
: 'formatnum test for Nederlands'
809 number
: 'invalidnumber',
810 result
: 'invalidnumber',
811 description
: 'formatnum test for Nederlands, invalid number'
815 number
: '1000000000',
816 result
: '1,00,00,00,000',
817 description
: 'formatnum test for Malayalam'
821 number
: '-1000000000',
822 result
: '-1,00,00,00,000',
823 description
: 'formatnum test for Malayalam, negative number'
826 * This will fail because of wrong pattern for ml in MW(different from CLDR)
829 number: '1000000000.000',
830 result: '1,00,00,00,000.000',
831 description: 'formatnum test for Malayalam with decimal place'
836 number
: '123456789.123456789',
837 result
: '१२,३४,५६,७८९',
838 description
: 'formatnum test for Hindi'
842 number
: '१२,३४,५६,७८९',
843 result
: '१२,३४,५६,७८९',
844 description
: 'formatnum test for Hindi, Devanagari digits passed'
848 number
: '१२३४५६,७८९',
851 description
: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
855 QUnit
.test( 'formatnum', formatnumTests
.length
, function ( assert
) {
856 mw
.messages
.set( 'formatnum-msg', '{{formatnum:$1}}' );
857 mw
.messages
.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
858 var queue
= $.map( formatnumTests
, function ( test
) {
859 return function ( next
, abort
) {
860 getMwLanguage( test
.lang
)
861 .then( function ( langClass
) {
862 mw
.config
.set( 'wgUserLanguage', test
.lang
);
863 var parser
= new mw
.jqueryMsg
.parser( { language
: langClass
} );
865 parser
.parse( test
.integer
? 'formatnum-msg-int' : 'formatnum-msg',
866 [ test
.number
] ).html(),
871 assert
.ok( false, 'Language "' + test
.lang
+ '" failed to load' );
873 .then( next
, abort
);
877 process( queue
, QUnit
.start
);
881 QUnit
.test( 'HTML', 32, function ( assert
) {
882 mw
.messages
.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
884 assertBothModes( assert
, [ 'jquerymsg-italics-msg' ], mw
.messages
.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
886 mw
.messages
.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
887 assertBothModes( assert
, [ 'jquerymsg-bold-msg' ], mw
.messages
.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
889 mw
.messages
.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
890 assertBothModes( assert
, [ 'jquerymsg-bold-italics-msg' ], mw
.messages
.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
892 mw
.messages
.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
893 assertBothModes( assert
, [ 'jquerymsg-italics-bold-msg' ], mw
.messages
.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
895 mw
.messages
.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
898 formatParse( 'jquerymsg-italics-with-link' ),
899 'An <i>italicized <a title="link" href="' + mw
.html
.escape( mw
.util
.getUrl( 'link' ) ) + '">wiki-link</i>',
900 'Italics with link inside in parse mode'
904 formatText( 'jquerymsg-italics-with-link' ),
905 mw
.messages
.get( 'jquerymsg-italics-with-link' ),
906 'Italics with link unchanged in text mode'
909 mw
.messages
.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
911 formatParse( 'jquerymsg-italics-id-class' ),
912 mw
.messages
.get( 'jquerymsg-italics-id-class' ),
913 'ID and class are allowed'
916 mw
.messages
.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
918 formatParse( 'jquerymsg-italics-onclick' ),
919 '<i onclick="alert(\'foo\')">Foo</i>',
920 'element with onclick is escaped because it is not allowed'
923 mw
.messages
.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
925 formatParse( 'jquerymsg-script-msg' ),
926 '<script >alert( "Who put this tag here?" );</script>',
927 'Tag outside whitelist escaped in parse mode'
931 formatText( 'jquerymsg-script-msg' ),
932 mw
.messages
.get( 'jquerymsg-script-msg' ),
933 'Tag outside whitelist unchanged in text mode'
936 mw
.messages
.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
938 formatParse( 'jquerymsg-script-link-msg' ),
939 '<script><a title="Foo" href="' + mw
.html
.escape( mw
.util
.getUrl( 'Foo' ) ) + '">bar</a></script>',
940 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
943 mw
.messages
.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
945 formatParse( 'jquerymsg-mismatched-html' ),
946 '<i class="important">test</b>',
947 'Mismatched HTML start and end tag treated as text'
950 mw
.messages
.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
952 formatParse( 'jquerymsg-script-and-external-link' ),
953 '<script>alert( "jquerymsg-script-and-external-link test" );</script> <a href="http://example.com"><i>Foo</i> bar</a>',
954 'HTML tags in external links not interfering with escaping of other tags'
957 mw
.messages
.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
959 formatParse( 'jquerymsg-link-script' ),
960 '<a href="http://example.com"><script>alert( "jquerymsg-link-script test" );</script></a>',
961 'Non-whitelisted HTML tag in external link anchor treated as text'
964 // Intentionally not using htmlEqual for the quote tests
965 mw
.messages
.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
967 formatParse( 'jquerymsg-double-quotes-preserved' ),
968 mw
.messages
.get( 'jquerymsg-double-quotes-preserved' ),
969 'Attributes with double quotes are preserved as such'
972 mw
.messages
.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
974 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
975 '<i id="single">Single</i>',
976 'Attributes with single quotes are normalized to double'
979 mw
.messages
.set( 'jquerymsg-escaped-double-quotes-attribute', '<i style="font-family:"Arial"">Styled</i>' );
981 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
982 mw
.messages
.get( 'jquerymsg-escaped-double-quotes-attribute' ),
983 'Escaped attributes are parsed correctly'
986 mw
.messages
.set( 'jquerymsg-escaped-single-quotes-attribute', '<i style=\'font-family:'Arial'\'>Styled</i>' );
988 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
989 mw
.messages
.get( 'jquerymsg-escaped-single-quotes-attribute' ),
990 'Escaped attributes are parsed correctly'
993 mw
.messages
.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
995 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
996 '<i><a href="http://example.com">Example</a></i>',
997 'Contents of valid tag are treated as wikitext, so external link is parsed'
1000 mw
.messages
.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1002 formatParse( 'jquerymsg-wikitext-contents-script' ),
1003 '<i><script>Script inside</script></i>',
1004 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1007 mw
.messages
.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1009 formatParse( 'jquerymsg-unclosed-tag' ),
1010 'Foo<tag>bar',
1011 'Nonsupported unclosed tags are escaped'
1014 mw
.messages
.set( 'jquerymsg-self-closing-tag', 'Foo<tag/>bar' );
1016 formatParse( 'jquerymsg-self-closing-tag' ),
1017 'Foo<tag/>bar',
1018 'Self-closing tags don\'t cause a parse error'
1021 mw
.messages
.set( 'jquerymsg-entities1', 'A&B' );
1022 mw
.messages
.set( 'jquerymsg-entities2', 'A>B' );
1023 mw
.messages
.set( 'jquerymsg-entities3', 'A→B' );
1025 formatParse( 'jquerymsg-entities1' ),
1027 'Lone "&" is escaped in text'
1030 formatParse( 'jquerymsg-entities2' ),
1032 '">" entity is double-escaped in text' // (WHY?)
1035 formatParse( 'jquerymsg-entities3' ),
1037 '"→" entity is double-escaped in text'
1040 mw
.messages
.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1041 mw
.messages
.set( 'jquerymsg-entities-attr2', '<i title="A>B"></i>' );
1042 mw
.messages
.set( 'jquerymsg-entities-attr3', '<i title="A→B"></i>' );
1044 formatParse( 'jquerymsg-entities-attr1' ),
1045 '<i title="A&B"></i>',
1046 'Lone "&" is escaped in attribute'
1049 formatParse( 'jquerymsg-entities-attr2' ),
1050 '<i title="A>B"></i>',
1051 '">" entity is not double-escaped in attribute' // (WHY?)
1054 formatParse( 'jquerymsg-entities-attr3' ),
1055 '<i title="A&rarr;B"></i>',
1056 '"→" entity is double-escaped in attribute'
1060 QUnit
.test( 'Behavior in case of invalid wikitext', 3, function ( assert
) {
1061 mw
.messages
.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1063 this.suppressWarnings();
1064 var logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1067 formatParse( 'invalid-wikitext' ),
1068 '<b>{{FAIL}}</b>',
1069 'Invalid wikitext: \'parse\' format'
1073 formatText( 'invalid-wikitext' ),
1075 'Invalid wikitext: \'text\' format'
1078 assert
.equal( logSpy
.callCount
, 2, 'mw.log.warn calls' );
1081 QUnit
.test( 'Integration', 5, function ( assert
) {
1082 var expected
, logSpy
, msg
;
1084 expected
= '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1085 mw
.messages
.set( 'integration-test', '<b>[[Bold]]!</b>' );
1087 this.suppressWarnings();
1088 logSpy
= this.sandbox
.spy( mw
.log
, 'warn' );
1090 window
.gM( 'integration-test' ),
1092 'Global function gM() works correctly'
1094 assert
.equal( logSpy
.callCount
, 1, 'mw.log.warn called' );
1095 this.restoreWarnings();
1098 mw
.message( 'integration-test' ).parse(),
1100 'mw.message().parse() works correctly'
1104 $( '<span>' ).msg( 'integration-test' ).html(),
1106 'jQuery plugin $.fn.msg() works correctly'
1109 mw
.messages
.set( 'integration-test-extlink', '[$1 Link]' );
1111 'integration-test-extlink',
1112 $( '<a>' ).attr( 'href', 'http://example.com/' )
1114 msg
.parse(); // Not a no-op
1117 '<a href="http://example.com/">Link</a>',
1118 'Calling .parse() multiple times does not duplicate link contents'
1122 }( mediaWiki
, jQuery
) );