Debian Codegraph @

tests/qunit/resources/mediawiki.jqueryMsg.test.js

raw symbols SCIP
1 ( function () {
2 const jqueryMsg = require( 'mediawiki.jqueryMsg' ).test;
3
4 /* eslint-disable camelcase */
5 let formatText, formatParse, specialCharactersPageName, expectedListUsers,
6 expectedListUsersSitename, expectedLinkPagenamee, expectedEntrypoints;
7 const testData = require( 'mediawiki.language.testdata' ),
8 phpParserData = testData.phpParserData;
9
10 // When the expected result is the same in both modes
11 function assertBothModes( assert, parserArguments, expectedResult, assertMessage ) {
12 assert.strictEqual( formatText( ...parserArguments ), expectedResult, assertMessage + ' when format is \'text\'' );
13 assert.strictEqual( formatParse( ...parserArguments ), expectedResult, assertMessage + ' when format is \'parse\'' );
14 }
15
16 QUnit.module( 'mediawiki.jqueryMsg', QUnit.newMwEnvironment( {
17 beforeEach: function () {
18 this.originalMwLanguage = mw.language;
19 this.getMwLanguage = function ( langCode ) {
20 mw.language = Object.create( this.originalMwLanguage );
21 mw.language.setData( langCode, phpParserData.jsData[ langCode ] );
22 testData.initLang( langCode );
23 return mw.language;
24 };
25 this.parserDefaults = jqueryMsg.getParserDefaults();
26 jqueryMsg.setParserDefaults( {
27 magic: {
28 SITENAME: 'Wiki',
29 // Repeat parserDefaults.magic from mediawiki.jqueryMsg.js. The original
30 // runs before the mock config is set up.
31 PAGENAME: mw.config.get( 'wgPageName' ),
32 PAGENAMEE: mw.util.wikiUrlencode( mw.config.get( 'wgPageName' ) ),
33 SERVERNAME: mw.config.get( 'wgServerName' ),
34 CONTENTLANGUAGE: mw.config.get( 'wgContentLanguage' )
35 }
36 } );
37
38 specialCharactersPageName = '"Who" wants to be a millionaire & live on \'Exotic Island\'?';
39
40 expectedListUsers = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户</a>';
41 expectedListUsersSitename = '注册<a title="Special:ListUsers" href="/wiki/Special:ListUsers">用户' +
42 'Wiki</a>';
43 expectedLinkPagenamee = '<a href="https://example.org/wiki/Foo?bar=baz#val/2_%2B_2" class="external">Test</a>';
44
45 expectedEntrypoints = '<a href="https://www.mediawiki.org/wiki/Manual:index.php" class="external">index.php</a>';
46
47 formatText = jqueryMsg.getMessageFunction( {
48 format: 'text'
49 } );
50
51 formatParse = jqueryMsg.getMessageFunction( {
52 format: 'parse'
53 } );
54 },
55 afterEach: function () {
56 mw.language = this.originalMwLanguage;
57 jqueryMsg.setParserDefaults( this.parserDefaults );
58 },
59 config: {
60 wgPageName: '2 + 2',
61 wgServerName: 'wiki.xyz',
62 wgContentLanguage: 'sjn',
63 wgArticlePath: '/wiki/$1',
64 wgNamespaceIds: {
65 template: 10,
66 template_talk: 11,
67 // Localised
68 szablon: 10,
69 dyskusja_szablonu: 11
70 },
71 wgFormattedNamespaces: {
72 // Localised
73 10: 'Szablon',
74 11: 'Dyskusja szablonu'
75 },
76 wgCaseSensitiveNamespaces: []
77 },
78 // Messages that are reused in multiple tests
79 messages: {
80 // The values for gender are not significant,
81 // what matters is which of the values is choosen by the parser
82 'gender-msg': '$1: {{GENDER:$2|blue|pink|green}}',
83 'gender-msg-currentuser': '{{GENDER:|blue|pink|green}}',
84
85 'plural-msg': 'Found $1 {{PLURAL:$1|item|items}}',
86 // See https://phabricator.wikimedia.org/T71993
87 'plural-msg-explicit-forms-nested': 'Found {{PLURAL:$1|$1 results|0=no results in {{SITENAME}}|1=$1 result}}',
88 // Assume the grammar form grammar_case_foo is not valid in any language
89 'grammar-msg': 'Przeszukaj {{GRAMMAR:grammar_case_foo|{{SITENAME}}}}',
90
91 'formatnum-msg': '{{formatnum:$1}}',
92
93 'bidi-msg': 'Welcome {{BIDI:$1}}!',
94
95 'portal-url': 'Project:Community portal',
96 'see-portal-url': '{{Int:portal-url}} is an important community page.',
97
98 'jquerymsg-test-statistics-users': '注册[[Special:ListUsers|用户]]',
99 'jquerymsg-test-statistics-users-sitename': '注册[[Special:ListUsers|用户{{SITENAME}}]]',
100 'jquerymsg-test-link-pagenamee': '[https://example.org/wiki/Foo?bar=baz#val/{{PAGENAMEE}} Test]',
101
102 'jquerymsg-test-version-entrypoints-index-php': '[https://www.mediawiki.org/wiki/Manual:index.php index.php]',
103
104 'external-link-replace': 'Foo [$1 bar]',
105 'external-link-plural': 'Foo {{PLURAL:$1|is [$2 one]|are [$2 some]|2=[$2 two]|3=three|4=a=b}} things.',
106 'plural-only-explicit-forms': 'It is a {{PLURAL:$1|1=single|2=double}} room.',
107 'plural-empty-explicit-form': 'There is me{{PLURAL:$1|0=| and other people}}.'
108 }
109 } ) );
110
111 QUnit.test( 'Replace', ( assert ) => {
112 mw.messages.set( 'simple', 'Foo $1 baz $2' );
113
114 assert.strictEqual( formatParse( 'simple' ), 'Foo $1 baz $2', 'Replacements with no substitutes' );
115 assert.strictEqual( formatParse( 'simple', 'bar' ), 'Foo bar baz $2', 'Replacements with less substitutes' );
116 assert.strictEqual( formatParse( 'simple', 'bar', 'quux' ), 'Foo bar baz quux', 'Replacements with all substitutes' );
117
118 mw.messages.set( 'plain-input', '<foo foo="foo">x$1y&lt;</foo>z' );
119
120 assert.strictEqual(
121 formatParse( 'plain-input', 'bar' ),
122 '&lt;foo foo="foo"&gt;xbary&amp;lt;&lt;/foo&gt;z',
123 'Input is not considered html'
124 );
125
126 mw.messages.set( 'plain-replace', 'Foo $1' );
127
128 assert.strictEqual(
129 formatParse( 'plain-replace', '<bar bar="bar">&gt;</bar>' ),
130 'Foo &lt;bar bar="bar"&gt;&amp;gt;&lt;/bar&gt;',
131 'Replacement is not considered html'
132 );
133
134 mw.messages.set( 'object-replace', 'Foo $1' );
135
136 assert.strictEqual(
137 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ) ),
138 'Foo <div class="bar">&gt;</div>',
139 'jQuery objects are preserved as raw html'
140 );
141
142 assert.strictEqual(
143 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
144 'Foo <div class="bar">&gt;</div>',
145 'HTMLElement objects are preserved as raw html'
146 );
147
148 assert.strictEqual(
149 formatParse( 'object-replace', $( '<div class="bar">&gt;</div>' ).toArray() ),
150 'Foo <div class="bar">&gt;</div>',
151 'HTMLElement[] arrays are preserved as raw html'
152 );
153
154 mw.messages.set( 'simple-double-replace', 'Foo 1: $1 2: $1' );
155 assert.strictEqual(
156 formatParse( 'simple-double-replace', 'bar' ),
157 'Foo 1: bar 2: bar',
158 'string params can be used multiple times'
159 );
160
161 mw.messages.set( 'object-double-replace', 'Foo 1: $1 2: $1' );
162 assert.strictEqual(
163 formatParse( 'object-double-replace', $( '<div class="bar">&gt;</div>' ) ),
164 'Foo 1: <div class="bar">&gt;</div> 2: <div class="bar">&gt;</div>',
165 'jQuery objects can be used multiple times'
166 );
167
168 assert.strictEqual(
169 formatParse( 'object-double-replace', $( '<div class="bar">&gt;</div>' ).get( 0 ) ),
170 'Foo 1: <div class="bar">&gt;</div> 2: <div class="bar">&gt;</div>',
171 'HTMLElement can be used multiple times'
172 );
173
174 assert.strictEqual(
175 formatParse( 'external-link-replace', 'http://example.org/?x=y&z' ),
176 'Foo <a href="http://example.org/?x=y&amp;z" class="external">bar</a>',
177 'Href is not double-escaped in wikilink function'
178 );
179 assert.strictEqual(
180 formatParse( 'external-link-plural', 1, 'http://example.org' ),
181 'Foo is <a href="http://example.org" class="external">one</a> things.',
182 'Link is expanded inside plural and is not escaped html'
183 );
184 assert.strictEqual(
185 formatParse( 'external-link-plural', 2, 'http://example.org' ),
186 'Foo <a href="http://example.org" class="external">two</a> things.',
187 'Link is expanded inside an explicit plural form and is not escaped html'
188 );
189 assert.strictEqual(
190 formatParse( 'external-link-plural', 3 ),
191 'Foo three things.',
192 'A simple explicit plural form co-existing with complex explicit plural forms'
193 );
194 assert.strictEqual(
195 formatParse( 'external-link-plural', 4, 'http://example.org' ),
196 'Foo a=b things.',
197 'Only first equal sign is used as delimiter for explicit plural form. Repeated equal signs does not create issue'
198 );
199 assert.strictEqual(
200 formatParse( 'external-link-plural', 6, 'http://example.org' ),
201 'Foo are <a href="http://example.org" class="external">some</a> things.',
202 'Plural fallback to the "other" plural form'
203 );
204 assert.strictEqual(
205 formatParse( 'plural-only-explicit-forms', 2 ),
206 'It is a double room.',
207 'Plural with explicit forms alone.'
208 );
209 } );
210
211 QUnit.test( 'Plural', ( assert ) => {
212 assert.strictEqual( formatParse( 'plural-msg', 0 ), 'Found 0 items', 'Plural test for english with zero as count' );
213 assert.strictEqual( formatParse( 'plural-msg', 1 ), 'Found 1 item', 'Singular test for english' );
214 assert.strictEqual( formatParse( 'plural-msg', 2 ), 'Found 2 items', 'Plural test for english' );
215 assert.strictEqual( formatParse( 'plural-msg-explicit-forms-nested', 6 ), 'Found 6 results', 'Plural message with explicit plural forms' );
216 assert.strictEqual( formatParse( 'plural-msg-explicit-forms-nested', 0 ), 'Found no results in Wiki', 'Plural message with explicit plural forms, with nested {{SITENAME}}' );
217 assert.strictEqual( formatParse( 'plural-msg-explicit-forms-nested', 1 ), 'Found 1 result', 'Plural message with explicit plural forms with placeholder nested' );
218 assert.strictEqual( formatParse( 'plural-empty-explicit-form', 0 ), 'There is me.' );
219 assert.strictEqual( formatParse( 'plural-empty-explicit-form', 1 ), 'There is me and other people.' );
220 assert.strictEqual( formatParse( 'plural-empty-explicit-form', 2 ), 'There is me and other people.' );
221 } );
222
223 QUnit.test( 'Gender', ( assert ) => {
224 const originalGender = mw.user.options.get( 'gender' );
225
226 // TODO: These tests should be for mw.msg once mw.msg integrated with jqueryMsg
227 // TODO: English may not be the best language for these tests. Use a language like Arabic or Russian
228 mw.user.options.set( 'gender', 'male' );
229 assert.strictEqual(
230 formatParse( 'gender-msg', 'Bob', 'male' ),
231 'Bob: blue',
232 'Masculine from string "male"'
233 );
234 assert.strictEqual(
235 formatParse( 'gender-msg', 'Bob', mw.user ),
236 'Bob: blue',
237 'Masculine from mw.user object'
238 );
239 assert.strictEqual(
240 formatParse( 'gender-msg-currentuser' ),
241 'blue',
242 'Masculine for current user'
243 );
244
245 mw.user.options.set( 'gender', 'female' );
246 assert.strictEqual(
247 formatParse( 'gender-msg', 'Alice', 'female' ),
248 'Alice: pink',
249 'Feminine from string "female"' );
250 assert.strictEqual(
251 formatParse( 'gender-msg', 'Alice', mw.user ),
252 'Alice: pink',
253 'Feminine from mw.user object'
254 );
255 assert.strictEqual(
256 formatParse( 'gender-msg-currentuser' ),
257 'pink',
258 'Feminine for current user'
259 );
260
261 mw.user.options.set( 'gender', 'unknown' );
262 assert.strictEqual(
263 formatParse( 'gender-msg', 'Foo', mw.user ),
264 'Foo: green',
265 'Neutral from mw.user object' );
266 assert.strictEqual(
267 formatParse( 'gender-msg', 'User' ),
268 'User: green',
269 'Neutral when no parameter given' );
270 assert.strictEqual(
271 formatParse( 'gender-msg', 'User', 'unknown' ),
272 'User: green',
273 'Neutral from string "unknown"'
274 );
275 assert.strictEqual(
276 formatParse( 'gender-msg-currentuser' ),
277 'green',
278 'Neutral for current user'
279 );
280
281 mw.messages.set( 'gender-msg-one-form', '{{GENDER:$1|User}}: $2 {{PLURAL:$2|edit|edits}}' );
282
283 assert.strictEqual(
284 formatParse( 'gender-msg-one-form', 'male', 10 ),
285 'User: 10 edits',
286 'Gender neutral and plural form'
287 );
288 assert.strictEqual(
289 formatParse( 'gender-msg-one-form', 'female', 1 ),
290 'User: 1 edit',
291 'Gender neutral and singular form'
292 );
293
294 mw.messages.set( 'gender-msg-lowercase', '{{gender:$1|he|she}} is awesome' );
295 assert.strictEqual(
296 formatParse( 'gender-msg-lowercase', 'male' ),
297 'he is awesome',
298 'Gender masculine'
299 );
300 assert.strictEqual(
301 formatParse( 'gender-msg-lowercase', 'female' ),
302 'she is awesome',
303 'Gender feminine'
304 );
305
306 mw.messages.set( 'gender-msg-wrong', '{{gender}} test' );
307 assert.strictEqual(
308 formatParse( 'gender-msg-wrong', 'female' ),
309 ' test',
310 'Invalid syntax should result in {{gender}} simply being stripped away'
311 );
312
313 mw.user.options.set( 'gender', originalGender );
314 } );
315
316 QUnit.test( 'Case changing', ( assert ) => {
317 mw.messages.set( 'to-lowercase', '{{lc:thIS hAS MEsSed uP CapItaliZatiON}}' );
318 assert.strictEqual( formatParse( 'to-lowercase' ), 'this has messed up capitalization', 'To lowercase' );
319
320 mw.messages.set( 'to-caps', '{{uc:thIS hAS MEsSed uP CapItaliZatiON}}' );
321 assert.strictEqual( formatParse( 'to-caps' ), 'THIS HAS MESSED UP CAPITALIZATION', 'To caps' );
322
323 mw.messages.set( 'uc-to-lcfirst', '{{lcfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
324 mw.messages.set( 'lc-to-lcfirst', '{{lcfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
325 assert.strictEqual( formatParse( 'uc-to-lcfirst' ), 'tHis hAS MEsSed uP CapItaliZatiON', 'Lcfirst caps' );
326 assert.strictEqual( formatParse( 'lc-to-lcfirst' ), 'thIS hAS MEsSed uP CapItaliZatiON', 'Lcfirst lowercase' );
327
328 mw.messages.set( 'uc-to-ucfirst', '{{ucfirst:THis hAS MEsSed uP CapItaliZatiON}}' );
329 mw.messages.set( 'lc-to-ucfirst', '{{ucfirst:thIS hAS MEsSed uP CapItaliZatiON}}' );
330 assert.strictEqual( formatParse( 'uc-to-ucfirst' ), 'THis hAS MEsSed uP CapItaliZatiON', 'Ucfirst caps' );
331 assert.strictEqual( formatParse( 'lc-to-ucfirst' ), 'ThIS hAS MEsSed uP CapItaliZatiON', 'Ucfirst lowercase' );
332
333 mw.messages.set( 'mixed-to-sentence', '{{ucfirst:{{lc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
334 assert.strictEqual( formatParse( 'mixed-to-sentence' ), 'This has messed up capitalization', 'To sentence case' );
335 mw.messages.set( 'all-caps-except-first', '{{lcfirst:{{uc:thIS hAS MEsSed uP CapItaliZatiON}}}}' );
336 assert.strictEqual( formatParse( 'all-caps-except-first' ), 'tHIS HAS MESSED UP CAPITALIZATION', 'To opposite sentence case' );
337
338 mw.messages.set( 'ucfirst-outside-BMP', '{{ucfirst:\uD803\uDCC0 is U+10CC0 (OLD HUNGARIAN SMALL LETTER A)}}' );
339 assert.strictEqual( formatParse( 'ucfirst-outside-BMP' ), '\uD803\uDC80 is U+10CC0 (OLD HUNGARIAN SMALL LETTER A)', 'Ucfirst outside BMP' );
340
341 mw.messages.set( 'lcfirst-outside-BMP', '{{lcfirst:\uD803\uDC80 is U+10C80 (OLD HUNGARIAN CAPITAL LETTER A)}}' );
342 assert.strictEqual( formatParse( 'lcfirst-outside-BMP' ), '\uD803\uDCC0 is U+10C80 (OLD HUNGARIAN CAPITAL LETTER A)', 'Lcfirst outside BMP' );
343
344 } );
345
346 QUnit.test( 'Grammar', ( assert ) => {
347 assert.strictEqual( formatParse( 'grammar-msg' ), 'Przeszukaj Wiki', 'Grammar Test with sitename' );
348
349 mw.messages.set( 'grammar-msg-wrong-syntax', 'Przeszukaj {{GRAMMAR:grammar_case_xyz}}' );
350 assert.strictEqual( formatParse( 'grammar-msg-wrong-syntax' ), 'Przeszukaj ', 'Grammar Test with wrong grammar template syntax' );
351 } );
352
353 QUnit.test( 'Formal', ( assert ) => {
354 mw.language.setData( mw.config.get( 'wgUserLanguage' ), 'formalityIndex', 0 );
355 mw.messages.set( 'formal-msg', '{{#FORMAL:Du hast|Sie haben}}' );
356 assert.strictEqual( formatParse( 'formal-msg' ), 'Du hast', 'Formal Test ' );
357
358 mw.messages.set( 'formal-msg-no-arguments', 'a{{#FORMAL:}}b' );
359 assert.strictEqual( formatParse( 'formal-msg-no-arguments' ), 'ab', 'Formal Test with wrong formal template syntax' );
360
361 mw.messages.set( 'formal-msg-one-argument', '{{#FORMAL:Single form}}' );
362 assert.strictEqual( formatParse( 'formal-msg-one-argument' ), 'Single form', 'Formal Test with a single argument' );
363
364 mw.messages.set( 'formal-msg-complex1', '{{#FORMAL:Informal hello|{{GENDER:|Formal}} hello}}' );
365 assert.strictEqual( formatParse( 'formal-msg-complex1' ), 'Informal hello', 'Complex first parameter' );
366 mw.messages.set( 'formal-msg-complex2', '{{#FORMAL:{{GENDER:|Informal}} hello|Formal hello}}' );
367 assert.strictEqual( formatParse( 'formal-msg-complex2' ), 'Informal hello', 'Complex second parameter' );
368 mw.messages.set( 'formal-msg-complex3', '{{#FORMAL:{{GENDER:|Informal}} hello|{{GENDER:|Formal}} hello}}' );
369 assert.strictEqual( formatParse( 'formal-msg-complex3' ), 'Informal hello', 'Complex both parameters' );
370
371 mw.language.setData( mw.config.get( 'wgUserLanguage' ), 'formalityIndex', 1 );
372 assert.strictEqual( formatParse( 'formal-msg' ), 'Sie haben', 'Formal Test' );
373 assert.strictEqual( formatParse( 'formal-msg-one-argument' ), 'Single form', 'Formal Test with a single argument when the second argument should be chosen' );
374
375 mw.messages.set( 'formal-msg-complex1', '{{#FORMAL:Informal hello|{{GENDER:|Formal}} hello}}' );
376 assert.strictEqual( formatParse( 'formal-msg-complex1' ), 'Formal hello', 'Complex first parameter' );
377 mw.messages.set( 'formal-msg-complex2', '{{#FORMAL:{{GENDER:|Informal}} hello|Formal hello}}' );
378 assert.strictEqual( formatParse( 'formal-msg-complex2' ), 'Formal hello', 'Complex second parameter' );
379 mw.messages.set( 'formal-msg-complex3', '{{#FORMAL:{{GENDER:|Informal}} hello|{{GENDER:|Formal}} hello}}' );
380 assert.strictEqual( formatParse( 'formal-msg-complex3' ), 'Formal hello', 'Complex both parameters' );
381
382 } );
383
384 QUnit.test( 'Variables', ( assert ) => {
385 mw.messages.set( 'variables-pagename', '{{PAGENAME}}' );
386 assert.strictEqual( formatParse( 'variables-pagename' ), '2 + 2', 'PAGENAME' );
387 mw.messages.set( 'variables-pagenamee', '{{PAGENAMEE}}' );
388 assert.strictEqual( formatParse( 'variables-pagenamee' ), mw.util.wikiUrlencode( '2 + 2' ), 'PAGENAMEE' );
389 mw.messages.set( 'variables-sitename', '{{SITENAME}}' );
390 assert.strictEqual( formatParse( 'variables-sitename' ), 'Wiki', 'SITENAME' );
391 mw.messages.set( 'variables-servername', '{{SERVERNAME}}' );
392 assert.strictEqual( formatParse( 'variables-servername' ), 'wiki.xyz', 'SERVERNAME' );
393 mw.messages.set( 'variables-contentlanguage', '{{CONTENTLANGUAGE}}' );
394 assert.strictEqual( formatParse( 'variables-contentlanguage' ), 'sjn', 'CONTENTLANGUAGE' );
395 } );
396
397 QUnit.test( 'Bi-di', ( assert ) => {
398 assert.strictEqual( formatParse( 'bidi-msg', 'Bob' ), 'Welcome \u202ABob\u202C!', 'Bidi test (LTR)' );
399 assert.strictEqual( formatParse( 'bidi-msg', 'בוב' ), 'Welcome \u202Bבוב\u202C!', 'Bidi test (RTL)' );
400 } );
401
402 QUnit.test( 'Match PHP parser', function ( assert ) {
403 const self = this;
404 mw.messages.set( phpParserData.messages );
405 phpParserData.tests.forEach( ( test ) => {
406 const langClass = self.getMwLanguage( test.lang );
407 mw.config.set( 'wgUserLanguage', test.lang );
408 const parser = new jqueryMsg.Parser( { language: langClass } );
409 assert.strictEqual(
410 parser.parse( test.key, test.args ).html(),
411 test.result,
412 test.name
413 );
414 } );
415 } );
416
417 QUnit.test( 'Links', function ( assert ) {
418 // The below three are all identical to or based on real messages. For disambiguations-text,
419 // the bold was removed because it is not yet implemented.
420
421 assert.htmlEqual(
422 formatParse( 'jquerymsg-test-statistics-users' ),
423 expectedListUsers,
424 'Piped wikilink'
425 );
426
427 const 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 ' +
428 '<a title="MediaWiki:Disambiguationspage" href="/wiki/MediaWiki:Disambiguationspage">MediaWiki:Disambiguationspage</a>.';
429
430 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]].' );
431 assert.htmlEqual(
432 formatParse( 'disambiguations-text' ),
433 expectedDisambiguationsText,
434 'Wikilink without pipe'
435 );
436
437 assert.htmlEqual(
438 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
439 expectedEntrypoints,
440 'External link'
441 );
442
443 // Pipe trick is not supported currently, but should not parse as text either.
444 mw.messages.set( 'pipe-trick', '[[Tampa, Florida|]]' );
445 mw.messages.set( 'reverse-pipe-trick', '[[|Tampa, Florida]]' );
446 mw.messages.set( 'empty-link', '[[]]' );
447 this.suppressWarnings();
448 assert.strictEqual(
449 formatParse( 'pipe-trick' ),
450 '[[Tampa, Florida|]]',
451 'Pipe trick should not be parsed.'
452 );
453 assert.strictEqual(
454 formatParse( 'reverse-pipe-trick' ),
455 '[[|Tampa, Florida]]',
456 'Reverse pipe trick should not be parsed.'
457 );
458 assert.strictEqual(
459 formatParse( 'empty-link' ),
460 '[[]]',
461 'Empty link should not be parsed.'
462 );
463 this.restoreWarnings();
464
465 const expectedMultipleBars = '<a title="Main Page" href="/wiki/Main_Page">Main|Page</a>';
466 mw.messages.set( 'multiple-bars', '[[Main Page|Main|Page]]' );
467 assert.htmlEqual(
468 formatParse( 'multiple-bars' ),
469 expectedMultipleBars,
470 'Bar in anchor'
471 );
472
473 const expectedSpecialCharacters = '<a title="&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?" href="/wiki/%22Who%22_wants_to_be_a_millionaire_%26_live_on_%27Exotic_Island%27%3F">&quot;Who&quot; wants to be a millionaire &amp; live on &#039;Exotic Island&#039;?</a>';
474
475 mw.messages.set( 'special-characters', '[[' + specialCharactersPageName + ']]' );
476 assert.htmlEqual(
477 formatParse( 'special-characters' ),
478 expectedSpecialCharacters,
479 'Special characters'
480 );
481
482 mw.messages.set( 'leading-colon', '[[:File:Foo.jpg]]' );
483 assert.htmlEqual(
484 formatParse( 'leading-colon' ),
485 '<a title="File:Foo.jpg" href="/wiki/File:Foo.jpg">File:Foo.jpg</a>',
486 'Leading colon in links is stripped'
487 );
488
489 assert.htmlEqual(
490 formatParse( 'jquerymsg-test-statistics-users-sitename' ),
491 expectedListUsersSitename,
492 'Piped wikilink with parser function in the text'
493 );
494
495 assert.htmlEqual(
496 formatParse( 'jquerymsg-test-link-pagenamee' ),
497 expectedLinkPagenamee,
498 'External link with parser function in the URL'
499 );
500
501 const testCases = [
502 [
503 'extlink-html-full',
504 'asd [http://example.org <strong>Example</strong>] asd',
505 'asd <a href="http://example.org" class="external"><strong>Example</strong></a> asd'
506 ],
507 [
508 'extlink-html-partial',
509 'asd [http://example.org foo <strong>Example</strong> bar] asd',
510 'asd <a href="http://example.org" class="external">foo <strong>Example</strong> bar</a> asd'
511 ],
512 [
513 'wikilink-html-full',
514 'asd [[Example|<strong>Example</strong>]] asd',
515 'asd <a title="Example" href="/wiki/Example"><strong>Example</strong></a> asd'
516 ],
517 [
518 'wikilink-html-partial',
519 'asd [[Example|foo <strong>Example</strong> bar]] asd',
520 'asd <a title="Example" href="/wiki/Example">foo <strong>Example</strong> bar</a> asd'
521 ]
522 ];
523
524 testCases.forEach( ( testCase ) => {
525 const
526 key = testCase[ 0 ],
527 input = testCase[ 1 ],
528 output = testCase[ 2 ];
529 mw.messages.set( key, input );
530 assert.htmlEqual(
531 formatParse( key ),
532 output,
533 'HTML in links: ' + key
534 );
535 } );
536
537 mw.messages.set( 'jquerymsg-title-fragment', 'Link to [[Title#Fragment]]' );
538 assert.htmlEqual(
539 formatParse( 'jquerymsg-title-fragment' ),
540 'Link to <a title="Title" href="/wiki/Title#Fragment">Title#Fragment</a>',
541 'Link with title and fragment'
542 );
543
544 mw.messages.set( 'jquerymsg-fragment', 'Link to [[#Fragment]]' );
545 assert.htmlEqual(
546 formatParse( 'jquerymsg-fragment' ),
547 'Link to <a href="#Fragment">#Fragment</a>',
548 'Link with fragment only'
549 );
550 } );
551
552 // Test case sensitive namespaces
553 QUnit.test( 'CaseSensitiveNamespaces', ( assert ) => {
554 const oldCaseSensitiveNamespaces = mw.config.get( 'wgCaseSensitiveNamespaces' );
555 mw.config.set( 'wgCaseSensitiveNamespaces', [ 0 ] );
556
557 mw.messages.set( 'jquerymsg-lowercase-link', 'A [[lowercase]] and [[Uppercase]] wiki-link' );
558 assert.htmlEqual(
559 formatParse( 'jquerymsg-lowercase-link' ),
560 'A <a title="lowercase" href="/wiki/lowercase">lowercase</a> and <a title="Uppercase" href="/wiki/Uppercase">Uppercase</a> wiki-link',
561 'wgCaseSensitiveNamespaces allows lowercase namespaces.'
562 );
563
564 mw.config.set( 'wgCaseSensitiveNamespaces', oldCaseSensitiveNamespaces );
565 } );
566
567 // Test localized namespace names
568 QUnit.test( 'LocalizedNamespaces', ( assert ) => {
569 mw.messages.set( 'jquerymsg-localized-link', 'Link to [[template:foo|template foo]]' );
570 assert.htmlEqual(
571 formatParse( 'jquerymsg-localized-link' ),
572 'Link to <a title="Szablon:Foo" href="/wiki/Szablon:Foo">template foo</a>',
573 'Links have localized namespace names.'
574 );
575 } );
576
577 QUnit.test( 'Replacements in links', ( assert ) => {
578 const testCases = [
579 [
580 'extlink-param-href-full',
581 'asd [$1 Example] asd',
582 'asd <a href="http://example.com" class="external">Example</a> asd'
583 ],
584 [
585 'extlink-param-href-partial',
586 'asd [$1/example Example] asd',
587 'asd <a href="http://example.com/example" class="external">Example</a> asd'
588 ],
589 [
590 'extlink-param-text-full',
591 'asd [http://example.org $2] asd',
592 'asd <a href="http://example.org" class="external">Text</a> asd'
593 ],
594 [
595 'extlink-param-text-partial',
596 'asd [http://example.org Example $2] asd',
597 'asd <a href="http://example.org" class="external">Example Text</a> asd'
598 ],
599 [
600 'extlink-param-both-full',
601 'asd [$1 $2] asd',
602 'asd <a href="http://example.com" class="external">Text</a> asd'
603 ],
604 [
605 'extlink-param-both-partial',
606 'asd [$1/example Example $2] asd',
607 'asd <a href="http://example.com/example" class="external">Example Text</a> asd'
608 ],
609 [
610 'wikilink-param-href-full',
611 'asd [[$1|Example]] asd',
612 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
613 ],
614 [
615 'wikilink-param-href-partial',
616 'asd [[$1/Test|Example]] asd',
617 'asd <a title="Example/Test" href="/wiki/Example/Test">Example</a> asd'
618 ],
619 [
620 'wikilink-param-text-full',
621 'asd [[Example|$2]] asd',
622 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
623 ],
624 [
625 'wikilink-param-text-partial',
626 'asd [[Example|Example $2]] asd',
627 'asd <a title="Example" href="/wiki/Example">Example Text</a> asd'
628 ],
629 [
630 'wikilink-param-both-full',
631 'asd [[$1|$2]] asd',
632 'asd <a title="Example" href="/wiki/Example">Text</a> asd'
633 ],
634 [
635 'wikilink-param-both-partial',
636 'asd [[$1/Test|Example $2]] asd',
637 'asd <a title="Example/Test" href="/wiki/Example/Test">Example Text</a> asd'
638 ],
639 [
640 'wikilink-param-unpiped-full',
641 'asd [[$1]] asd',
642 'asd <a title="Example" href="/wiki/Example">Example</a> asd'
643 ],
644 [
645 'wikilink-param-unpiped-partial',
646 'asd [[$1/Test]] asd',
647 'asd <a title="Example/Test" href="/wiki/Example/Test">Example/Test</a> asd'
648 ]
649 ];
650
651 testCases.forEach( ( testCase ) => {
652 const
653 key = testCase[ 0 ],
654 input = testCase[ 1 ],
655 output = testCase[ 2 ],
656 paramHref = key.slice( 0, 8 ) === 'wikilink' ? 'Example' : 'http://example.com',
657 paramText = 'Text';
658 mw.messages.set( key, input );
659 assert.htmlEqual(
660 formatParse( key, paramHref, paramText ),
661 output,
662 'Replacements in links: ' + key
663 );
664 } );
665 } );
666
667 // Tests that {{-transformation vs. general parsing are done as requested
668 QUnit.test( 'Curly brace transformation', ( assert ) => {
669 const oldUserLang = mw.config.get( 'wgUserLanguage' );
670
671 assertBothModes( assert, [ 'gender-msg', 'Bob', 'male' ], 'Bob: blue', 'gender is resolved' );
672
673 assertBothModes( assert, [ 'plural-msg', 5 ], 'Found 5 items', 'plural is resolved' );
674
675 assertBothModes( assert, [ 'grammar-msg' ], 'Przeszukaj Wiki', 'grammar is resolved' );
676
677 mw.config.set( 'wgUserLanguage', 'en' );
678 assertBothModes( assert, [ 'formatnum-msg', '987654321.654321' ], '987,654,321.654', 'formatnum is resolved' );
679
680 // Test non-{{ wikitext, where behavior differs
681
682 // Wikilink
683 assert.strictEqual(
684 formatText( 'jquerymsg-test-statistics-users' ),
685 mw.messages.get( 'jquerymsg-test-statistics-users' ),
686 'Internal link message unchanged when format is \'text\''
687 );
688 assert.htmlEqual(
689 formatParse( 'jquerymsg-test-statistics-users' ),
690 expectedListUsers,
691 'Internal link message parsed when format is \'parse\''
692 );
693
694 // External link
695 assert.strictEqual(
696 formatText( 'jquerymsg-test-version-entrypoints-index-php' ),
697 mw.messages.get( 'jquerymsg-test-version-entrypoints-index-php' ),
698 'External link message unchanged when format is \'text\''
699 );
700 assert.htmlEqual(
701 formatParse( 'jquerymsg-test-version-entrypoints-index-php' ),
702 expectedEntrypoints,
703 'External link message processed when format is \'parse\''
704 );
705
706 // External link with parameter
707 assert.strictEqual(
708 formatText( 'external-link-replace', 'http://example.com' ),
709 'Foo [http://example.com bar]',
710 'External link message only substitutes parameter when format is \'text\''
711 );
712 assert.htmlEqual(
713 formatParse( 'external-link-replace', 'http://example.com' ),
714 'Foo <a href="http://example.com" class="external">bar</a>',
715 'External link message processed when format is \'parse\''
716 );
717 assert.htmlEqual(
718 formatParse( 'external-link-replace', '/wiki/Special:Version' ),
719 'Foo <a href="/wiki/Special:Version">bar</a>',
720 'External link message allows relative URL when processed'
721 );
722 assert.htmlEqual(
723 formatParse( 'external-link-replace', '//example.com' ),
724 'Foo <a href="//example.com" class="external">bar</a>',
725 'External link message allows protocol-relative URL when processed'
726 );
727 assert.htmlEqual(
728 formatParse( 'external-link-replace', $( '<i>' ) ),
729 'Foo <i>bar</i>',
730 'External link message processed as jQuery object when format is \'parse\''
731 );
732 assert.htmlEqual(
733 formatParse( 'external-link-replace', () => {} ),
734 'Foo <a role="button" tabindex="0">bar</a>',
735 'External link message processed as function when format is \'parse\''
736 );
737
738 mw.config.set( 'wgUserLanguage', oldUserLang );
739 } );
740
741 QUnit.test( 'Int', ( assert ) => {
742 const 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.',
743 helpPageTitle = 'Help:Foobar';
744
745 const 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 ' +
746 '<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.';
747
748 mw.config.set( 'wgUserLanguage', 'en' );
749 mw.messages.set( {
750 foobar: helpPageTitle,
751 newarticletext: newarticletextSource
752 } );
753
754 assert.htmlEqual(
755 formatParse( 'newarticletext' ),
756 expectedNewarticletext,
757 'Link with nested message'
758 );
759
760 assert.strictEqual(
761 formatParse( 'see-portal-url' ),
762 'Project:Community portal is an important community page.',
763 'Nested message'
764 );
765
766 mw.messages.set( 'newarticletext-lowercase',
767 newarticletextSource.replace( 'Int:Helppage', 'int:helppage' ) );
768
769 assert.htmlEqual(
770 formatParse( 'newarticletext-lowercase' ),
771 expectedNewarticletext,
772 'Link with nested message, lowercase include'
773 );
774
775 mw.messages.set( 'uses-missing-int', '{{int:doesnt-exist}}' );
776
777 assert.strictEqual(
778 formatParse( 'uses-missing-int' ),
779 '⧼doesnt-exist⧽',
780 'int: where nested message does not exist'
781 );
782 } );
783
784 QUnit.test( 'Ns', ( assert ) => {
785 mw.messages.set( 'ns-template-talk', '{{ns:Template talk}}' );
786 assert.strictEqual(
787 formatParse( 'ns-template-talk' ),
788 'Dyskusja szablonu',
789 'ns: returns localised namespace when used with a canonical namespace name'
790 );
791
792 mw.messages.set( 'ns-10', '{{ns:10}}' );
793 assert.strictEqual(
794 formatParse( 'ns-10' ),
795 'Szablon',
796 'ns: returns localised namespace when used with a namespace number'
797 );
798
799 mw.messages.set( 'ns-unknown', '{{ns:doesnt-exist}}' );
800 assert.strictEqual(
801 formatParse( 'ns-unknown' ),
802 '',
803 'ns: returns empty string for unknown namespace name'
804 );
805
806 mw.messages.set( 'ns-in-a-link', '[[{{ns:template}}:Foo]]' );
807 assert.strictEqual(
808 formatParse( 'ns-in-a-link' ),
809 '<a title="Szablon:Foo" href="/wiki/Szablon:Foo">Szablon:Foo</a>',
810 'ns: works when used inside a wikilink'
811 );
812 } );
813
814 // Tests that getMessageFunction is used for non-plain messages with curly braces or
815 // square brackets, but not otherwise.
816 QUnit.test( 'mw.Message.prototype.parser monkey-patch', ( assert ) => {
817 let outerCalled, innerCalled;
818
819 mw.messages.set( {
820 'curly-brace': '{{int:message}}',
821 'single-square-bracket': '[https://www.mediawiki.org/ MediaWiki]',
822 'double-square-bracket': '[[Some page]]',
823 regular: 'Other message'
824 } );
825
826 const restore = jqueryMsg.setMessageFunction( () => {
827 outerCalled = true;
828 return function () {
829 innerCalled = true;
830 };
831 } );
832
833 function verifyGetMessageFunction( key, format, shouldCall ) {
834 outerCalled = false;
835 innerCalled = false;
836 // eslint-disable-next-line mediawiki/msg-doc
837 const message = mw.message( key );
838 message[ format ]();
839 assert.strictEqual( outerCalled, shouldCall, 'Outer function called for ' + key );
840 assert.strictEqual( innerCalled, shouldCall, 'Inner function called for ' + key );
841 delete mw.messages[ format ];
842 }
843
844 verifyGetMessageFunction( 'curly-brace', 'parse', true );
845 verifyGetMessageFunction( 'curly-brace', 'plain', false );
846
847 verifyGetMessageFunction( 'single-square-bracket', 'parse', true );
848 verifyGetMessageFunction( 'single-square-bracket', 'plain', false );
849
850 verifyGetMessageFunction( 'double-square-bracket', 'parse', true );
851 verifyGetMessageFunction( 'double-square-bracket', 'plain', false );
852
853 verifyGetMessageFunction( 'regular', 'parse', false );
854 verifyGetMessageFunction( 'regular', 'plain', false );
855
856 verifyGetMessageFunction( 'jquerymsg-test-pagetriage-del-talk-page-notify-summary', 'plain', false );
857 verifyGetMessageFunction( 'jquerymsg-test-categorytree-collapse-bullet', 'plain', false );
858 verifyGetMessageFunction( 'jquerymsg-test-wikieditor-toolbar-help-content-signature-result', 'plain', false );
859
860 restore();
861 } );
862
863 // Tests that HTML in message parameters is escaped,
864 // whether the message looks like wikitext or not.
865 QUnit.test( 'mw.Message.prototype.parser monkey-patch HTML-escape', ( assert ) => {
866 mw.messages.set( '1x-wikitext', '<span>$1</span>' );
867 assert.htmlEqual(
868 mw.message( '1x-wikitext', '<script>alert( "1x-wikitext test" )</script>' ).parse(),
869 '<span>&lt;script&gt;alert( &quot;1x-wikitext test&quot; )&lt;/script&gt;</span>',
870 'Message parameters are escaped if message contains wikitext'
871 );
872
873 mw.messages.set( '1x-plain', '$1' );
874 assert.htmlEqual(
875 mw.message( '1x-plain', '<script>alert( "1x-plain test" )</script>' ).parse(),
876 '&lt;script&gt;alert( &quot;1x-plain test&quot; )&lt;/script&gt;',
877 'Message parameters are still escaped if message contains no wikitext'
878 );
879 } );
880
881 const formatnumTests = [
882 {
883 lang: 'en',
884 number: 987654321.654321,
885 result: '987,654,321.654',
886 description: 'formatnum test for English, decimal separator'
887 },
888 {
889 lang: 'ar',
890 number: 987654321.654321,
891 result: '٩٨٧٬٦٥٤٬٣٢١٫٦٥٤',
892 description: 'formatnum test for Arabic, with decimal separator'
893 },
894 {
895 lang: 'ar',
896 number: '٩٨٧٦٥٤٣٢١٫٦٥٤٣٢١',
897 result: '987654321',
898 integer: true,
899 description: 'formatnum test for Arabic, with decimal separator, reverse'
900 },
901 {
902 lang: 'ar',
903 number: -12.89,
904 result: '-١٢٫٨٩',
905 description: 'formatnum test for Arabic, negative number'
906 },
907 {
908 lang: 'ar',
909 number: '-١٢٫٨٩',
910 result: '-12',
911 integer: true,
912 description: 'formatnum test for Arabic, negative number, reverse'
913 },
914 {
915 lang: 'nl',
916 number: 987654321.654321,
917 result: '987.654.321,654',
918 description: 'formatnum test for Nederlands, decimal separator'
919 },
920 {
921 lang: 'nl',
922 number: -12.89,
923 result: '-12,89',
924 description: 'formatnum test for Nederlands, negative number'
925 },
926 {
927 lang: 'nl',
928 number: '.89',
929 result: '0,89',
930 description: 'formatnum test for Nederlands'
931 },
932 {
933 lang: 'nl',
934 number: 'invalidnumber',
935 result: 'invalidnumber',
936 description: 'formatnum test for Nederlands, invalid number'
937 },
938 {
939 lang: 'ml',
940 number: '1000000000',
941 result: '1,00,00,00,000',
942 description: 'formatnum test for Malayalam'
943 },
944 {
945 lang: 'ml',
946 number: '-1000000000',
947 result: '-1,00,00,00,000',
948 description: 'formatnum test for Malayalam, negative number'
949 },
950 /*
951 * This will fail because of wrong pattern for ml in MW(different from CLDR)
952 {
953 lang: 'ml',
954 number: '1000000000.000',
955 result: '1,00,00,00,000.000',
956 description: 'formatnum test for Malayalam with decimal place'
957 },
958 */
959 {
960 lang: 'hi',
961 number: '123456789.123456789',
962 result: '१२,३४,५६,७८९.१२३',
963 description: 'formatnum test for Hindi'
964 },
965 {
966 lang: 'hi',
967 number: '१२,३४,५६,७८९',
968 result: '१२,३४,५६,७८९',
969 description: 'formatnum test for Hindi, Devanagari digits passed'
970 },
971 {
972 lang: 'hi',
973 number: '१,२३,४५६',
974 result: '123456',
975 integer: true,
976 description: 'formatnum test for Hindi, Devanagari digits passed to get integer value'
977 }
978 ];
979
980 QUnit.test( 'formatnum', function ( assert ) {
981 const self = this;
982 mw.messages.set( 'formatnum-msg', '{{formatnum:$1}}' );
983 mw.messages.set( 'formatnum-msg-int', '{{formatnum:$1|R}}' );
984 formatnumTests.forEach( ( test ) => {
985 const langClass = self.getMwLanguage( test.lang );
986 mw.config.set( 'wgUserLanguage', test.lang );
987 const parser = new jqueryMsg.Parser( { language: langClass } );
988 assert.strictEqual(
989 parser.parse( test.integer ? 'formatnum-msg-int' : 'formatnum-msg',
990 [ test.number ] ).html(),
991 test.result,
992 test.description
993 );
994 } );
995 } );
996
997 // HTML in wikitext
998 QUnit.test( 'HTML', ( assert ) => {
999 mw.messages.set( 'jquerymsg-italics-msg', '<i>Very</i> important' );
1000
1001 assertBothModes( assert, [ 'jquerymsg-italics-msg' ], mw.messages.get( 'jquerymsg-italics-msg' ), 'Simple italics unchanged' );
1002
1003 mw.messages.set( 'jquerymsg-bold-msg', '<b>Strong</b> speaker' );
1004 assertBothModes( assert, [ 'jquerymsg-bold-msg' ], mw.messages.get( 'jquerymsg-bold-msg' ), 'Simple bold unchanged' );
1005
1006 mw.messages.set( 'jquerymsg-bold-italics-msg', 'It is <b><i>key</i></b>' );
1007 assertBothModes( assert, [ 'jquerymsg-bold-italics-msg' ], mw.messages.get( 'jquerymsg-bold-italics-msg' ), 'Bold and italics nesting order preserved' );
1008
1009 mw.messages.set( 'jquerymsg-italics-bold-msg', 'It is <i><b>vital</b></i>' );
1010 assertBothModes( assert, [ 'jquerymsg-italics-bold-msg' ], mw.messages.get( 'jquerymsg-italics-bold-msg' ), 'Italics and bold nesting order preserved' );
1011
1012 mw.messages.set( 'jquerymsg-italics-with-link', 'An <i>italicized [[link|wiki-link]]</i>' );
1013
1014 assert.htmlEqual(
1015 formatParse( 'jquerymsg-italics-with-link' ),
1016 'An <i>italicized <a title="Link" href="/wiki/Link">wiki-link</i>',
1017 'Italics with link inside in parse mode'
1018 );
1019
1020 assert.strictEqual(
1021 formatText( 'jquerymsg-italics-with-link' ),
1022 mw.messages.get( 'jquerymsg-italics-with-link' ),
1023 'Italics with link unchanged in text mode'
1024 );
1025
1026 mw.messages.set( 'jquerymsg-italics-id-class', '<i id="foo" class="bar">Foo</i>' );
1027 assert.htmlEqual(
1028 formatParse( 'jquerymsg-italics-id-class' ),
1029 mw.messages.get( 'jquerymsg-italics-id-class' ),
1030 'ID and class are allowed'
1031 );
1032
1033 mw.messages.set( 'jquerymsg-italics-onclick', '<i onclick="alert(\'foo\')">Foo</i>' );
1034 assert.htmlEqual(
1035 formatParse( 'jquerymsg-italics-onclick' ),
1036 '&lt;i onclick=&quot;alert(\'foo\')&quot;&gt;Foo&lt;/i&gt;',
1037 'element with onclick is escaped because it is not allowed'
1038 );
1039
1040 mw.messages.set( 'jquerymsg-script-msg', '<script >alert( "Who put this tag here?" );</script>' );
1041 assert.htmlEqual(
1042 formatParse( 'jquerymsg-script-msg' ),
1043 '&lt;script &gt;alert( &quot;Who put this tag here?&quot; );&lt;/script&gt;',
1044 'Tag outside list of allowed ones is escaped in parse mode'
1045 );
1046
1047 assert.strictEqual(
1048 formatText( 'jquerymsg-script-msg' ),
1049 mw.messages.get( 'jquerymsg-script-msg' ),
1050 'Tag outside list of allowed ones is unchanged in text mode'
1051 );
1052
1053 mw.messages.set( 'jquerymsg-script-link-msg', '<script>[[Foo|bar]]</script>' );
1054 assert.htmlEqual(
1055 formatParse( 'jquerymsg-script-link-msg' ),
1056 '&lt;script&gt;<a title="Foo" href="/wiki/Foo">bar</a>&lt;/script&gt;',
1057 'Script tag text is escaped because that element is not allowed, but link inside is still HTML'
1058 );
1059
1060 mw.messages.set( 'jquerymsg-mismatched-html', '<i class="important">test</b>' );
1061 assert.htmlEqual(
1062 formatParse( 'jquerymsg-mismatched-html' ),
1063 '&lt;i class=&quot;important&quot;&gt;test&lt;/b&gt;',
1064 'Mismatched HTML start and end tag treated as text'
1065 );
1066
1067 mw.messages.set( 'jquerymsg-script-and-external-link', '<script>alert( "jquerymsg-script-and-external-link test" );</script> [http://example.com <i>Foo</i> bar]' );
1068 assert.htmlEqual(
1069 formatParse( 'jquerymsg-script-and-external-link' ),
1070 '&lt;script&gt;alert( "jquerymsg-script-and-external-link test" );&lt;/script&gt; <a href="http://example.com" class="external"><i>Foo</i> bar</a>',
1071 'HTML tags in external links not interfering with escaping of other tags'
1072 );
1073
1074 mw.messages.set( 'jquerymsg-link-script', '[http://example.com <script>alert( "jquerymsg-link-script test" );</script>]' );
1075 assert.htmlEqual(
1076 formatParse( 'jquerymsg-link-script' ),
1077 '<a href="http://example.com" class="external">&lt;script&gt;alert( "jquerymsg-link-script test" );&lt;/script&gt;</a>',
1078 'HTML tag not is list of allowed ones in external link anchor is treated as text'
1079 );
1080
1081 // Intentionally not using htmlEqual for the quote tests
1082 mw.messages.set( 'jquerymsg-double-quotes-preserved', '<i id="double">Double</i>' );
1083 assert.strictEqual(
1084 formatParse( 'jquerymsg-double-quotes-preserved' ),
1085 mw.messages.get( 'jquerymsg-double-quotes-preserved' ),
1086 'Attributes with double quotes are preserved as such'
1087 );
1088
1089 mw.messages.set( 'jquerymsg-single-quotes-normalized-to-double', '<i id=\'single\'>Single</i>' );
1090 assert.strictEqual(
1091 formatParse( 'jquerymsg-single-quotes-normalized-to-double' ),
1092 '<i id="single">Single</i>',
1093 'Attributes with single quotes are normalized to double'
1094 );
1095
1096 mw.messages.set( 'jquerymsg-escaped-double-quotes-attribute', '<i title="Hello &quot;World&quot;">Title</i>' );
1097 assert.htmlEqual(
1098 formatParse( 'jquerymsg-escaped-double-quotes-attribute' ),
1099 mw.messages.get( 'jquerymsg-escaped-double-quotes-attribute' ),
1100 'Escaped attributes are parsed correctly'
1101 );
1102
1103 mw.messages.set( 'jquerymsg-escaped-single-quotes-attribute', '<i title=\'Hello &#039;World&#039;\'>Title</i>' );
1104 assert.htmlEqual(
1105 formatParse( 'jquerymsg-escaped-single-quotes-attribute' ),
1106 mw.messages.get( 'jquerymsg-escaped-single-quotes-attribute' ),
1107 'Escaped attributes are parsed correctly'
1108 );
1109
1110 mw.messages.set( 'jquerymsg-wikitext-contents-parsed', '<i>[http://example.com Example]</i>' );
1111 assert.htmlEqual(
1112 formatParse( 'jquerymsg-wikitext-contents-parsed' ),
1113 '<i><a href="http://example.com" class="external">Example</a></i>',
1114 'Contents of valid tag are treated as wikitext, so external link is parsed'
1115 );
1116
1117 mw.messages.set( 'jquerymsg-wikitext-contents-script', '<i><script>Script inside</script></i>' );
1118 assert.htmlEqual(
1119 formatParse( 'jquerymsg-wikitext-contents-script' ),
1120 '<i>&lt;script&gt;Script inside&lt;/script&gt;</i>',
1121 'Contents of valid tag are treated as wikitext, so invalid HTML element is treated as text'
1122 );
1123
1124 mw.messages.set( 'jquerymsg-unclosed-tag', 'Foo<tag>bar' );
1125 assert.htmlEqual(
1126 formatParse( 'jquerymsg-unclosed-tag' ),
1127 'Foo&lt;tag&gt;bar',
1128 'Nonsupported unclosed tags are escaped'
1129 );
1130
1131 mw.messages.set( 'jquerymsg-asciialphabetliteral-regression', '<b >>>="dir">asd</b>' );
1132 assert.htmlEqual(
1133 formatParse( 'jquerymsg-asciialphabetliteral-regression' ),
1134 '<b>&gt;&gt;="dir"&gt;asd</b>',
1135 'Regression test for bad "asciiAlphabetLiteral" definition'
1136 );
1137
1138 mw.messages.set( 'jquerymsg-entities1', 'A&B' );
1139 mw.messages.set( 'jquerymsg-entities2', 'A&gt;B' );
1140 mw.messages.set( 'jquerymsg-entities3', 'A&rarr;B' );
1141 assert.htmlEqual(
1142 formatParse( 'jquerymsg-entities1' ),
1143 'A&amp;B',
1144 'Lone "&" is escaped in text'
1145 );
1146 assert.htmlEqual(
1147 formatParse( 'jquerymsg-entities2' ),
1148 'A&amp;gt;B',
1149 '"&gt;" entity is double-escaped in text' // (WHY?)
1150 );
1151 assert.htmlEqual(
1152 formatParse( 'jquerymsg-entities3' ),
1153 'A&amp;rarr;B',
1154 '"&rarr;" entity is double-escaped in text'
1155 );
1156
1157 mw.messages.set( 'jquerymsg-entities-attr1', '<i title="A&B"></i>' );
1158 mw.messages.set( 'jquerymsg-entities-attr2', '<i title="A&gt;B"></i>' );
1159 mw.messages.set( 'jquerymsg-entities-attr3', '<i title="A&rarr;B"></i>' );
1160 assert.htmlEqual(
1161 formatParse( 'jquerymsg-entities-attr1' ),
1162 '<i title="A&amp;B"></i>',
1163 'Lone "&" is escaped in attribute'
1164 );
1165 assert.htmlEqual(
1166 formatParse( 'jquerymsg-entities-attr2' ),
1167 '<i title="A&gt;B"></i>',
1168 '"&gt;" entity is not double-escaped in attribute' // (WHY?)
1169 );
1170 assert.htmlEqual(
1171 formatParse( 'jquerymsg-entities-attr3' ),
1172 '<i title="A&amp;rarr;B"></i>',
1173 '"&rarr;" entity is double-escaped in attribute'
1174 );
1175
1176 mw.messages.set( 'jquerymsg-valid-self-closing-tags-1', '<br><wbr><hr>' );
1177 mw.messages.set( 'jquerymsg-valid-self-closing-tags-2', '<br/><wbr/><hr/>' );
1178 mw.messages.set( 'jquerymsg-valid-self-closing-tags-3', '<br /><wbr /><hr />' );
1179 mw.messages.set( 'jquerymsg-valid-self-closing-tags-4', '<br class="test" />' );
1180 assert.htmlEqual(
1181 formatParse( 'jquerymsg-valid-self-closing-tags-1' ),
1182 '<br><wbr><hr>',
1183 'Valid self-closing tags without slashes are turned into HTML'
1184 );
1185 assert.htmlEqual(
1186 formatParse( 'jquerymsg-valid-self-closing-tags-2' ),
1187 '<br><wbr><hr>',
1188 'Valid self-closing tags with slashes are turned into HTML'
1189 );
1190 assert.htmlEqual(
1191 formatParse( 'jquerymsg-valid-self-closing-tags-3' ),
1192 '<br><wbr><hr>',
1193 'Valid self-closing tags with whitespace and slashes are turned into HTML'
1194 );
1195 assert.htmlEqual(
1196 formatParse( 'jquerymsg-valid-self-closing-tags-4' ),
1197 '<br class="test">',
1198 'Valid self-closing tags with attributes are turned into HTML'
1199 );
1200
1201 mw.messages.set( 'jquerymsg-invalid-self-closing-tags-1', '<div/>' );
1202 mw.messages.set( 'jquerymsg-invalid-self-closing-tags-2', '<foo/>' );
1203 mw.messages.set( 'jquerymsg-invalid-self-closing-tags-3', '<br onclick="alert(1)">' );
1204 assert.htmlEqual(
1205 formatParse( 'jquerymsg-invalid-self-closing-tags-1' ),
1206 '&lt;div/&gt;',
1207 'Self-closing syntax on an allowed tag that is not self-closing escapes the tag'
1208 );
1209 assert.htmlEqual(
1210 formatParse( 'jquerymsg-invalid-self-closing-tags-2' ),
1211 '&lt;foo/&gt;',
1212 'Self-closing syntax on a disallowed tag escapes the tag'
1213 );
1214 assert.htmlEqual(
1215 formatParse( 'jquerymsg-invalid-self-closing-tags-3' ),
1216 '&lt;br onclick="alert(1)"&gt;',
1217 'Self-closing tags with disallowed attributes are escaped'
1218 );
1219
1220 mw.messages.set( 'jquery-mixed-self-closing-tags', '<div><br></div>' );
1221 assert.htmlEqual(
1222 formatParse( 'jquery-mixed-self-closing-tags' ),
1223 '<div><br></div>',
1224 'Self-closing tags inside other tags are turned into HTML'
1225 );
1226 } );
1227
1228 QUnit.test( 'Nowiki', ( assert ) => {
1229 mw.messages.set( 'jquerymsg-nowiki-link', 'Foo <nowiki>[[bar]]</nowiki> baz.' );
1230 assert.strictEqual(
1231 formatParse( 'jquerymsg-nowiki-link' ),
1232 'Foo [[bar]] baz.',
1233 'Link inside nowiki is not parsed'
1234 );
1235
1236 mw.messages.set( 'jquerymsg-nowiki-htmltag', 'Foo <nowiki><b>bar</b></nowiki> baz.' );
1237 assert.strictEqual(
1238 formatParse( 'jquerymsg-nowiki-htmltag' ),
1239 'Foo &lt;b&gt;bar&lt;/b&gt; baz.',
1240 'HTML inside nowiki is not parsed and escaped'
1241 );
1242
1243 mw.messages.set( 'jquerymsg-nowiki-template', 'Foo <nowiki>{{bar}}</nowiki> baz.' );
1244 assert.strictEqual(
1245 formatParse( 'jquerymsg-nowiki-template' ),
1246 'Foo {{bar}} baz.',
1247 'Template inside nowiki is not parsed and does not cause a parse error'
1248 );
1249 } );
1250
1251 QUnit.test( 'Behavior in case of invalid wikitext', function ( assert ) {
1252 mw.messages.set( 'invalid-wikitext', '<b>{{FAIL}}</b>' );
1253
1254 this.suppressWarnings();
1255 const logSpy = this.sandbox.spy( mw.log, 'warn' );
1256
1257 assert.strictEqual(
1258 mw.message( 'invalid-wikitext' ).isParseable(),
1259 false,
1260 'Invalid wikitext: reported as not parseable'
1261 );
1262
1263 assert.strictEqual(
1264 formatParse( 'invalid-wikitext' ),
1265 '&lt;b&gt;{{FAIL}}&lt;/b&gt;',
1266 'Invalid wikitext: \'parse\' format'
1267 );
1268
1269 assert.strictEqual(
1270 formatText( 'invalid-wikitext' ),
1271 '<b>{{FAIL}}</b>',
1272 'Invalid wikitext: \'text\' format'
1273 );
1274
1275 assert.strictEqual( logSpy.callCount, 2, 'mw.log.warn calls' );
1276 } );
1277
1278 QUnit.test( 'Non-string parameters to various functions', ( assert ) => {
1279 // For jquery-param-int
1280 mw.messages.set( 'x', 'y' );
1281 // For jquery-param-grammar
1282 mw.language.setData( 'en', 'grammarTransformations', {
1283 test: [
1284 [ 'x', 'y' ]
1285 ]
1286 } );
1287
1288 const cases = [
1289 {
1290 key: 'jquery-param-wikilink',
1291 msg: '[[$1]] [[$1|a]]',
1292 expected: '<a title="X" href="/wiki/X">x</a> <a title="X" href="/wiki/X">a</a>'
1293 },
1294 {
1295 key: 'jquery-param-plural',
1296 msg: '{{PLURAL:$1|a|b}}',
1297 expected: 'b'
1298 },
1299 {
1300 key: 'jquery-param-gender',
1301 msg: '{{GENDER:$1|a|b}}',
1302 expected: 'a'
1303 },
1304 {
1305 key: 'jquery-param-grammar',
1306 msg: '{{GRAMMAR:test|$1}}',
1307 expected: '<b>x</b>'
1308 },
1309 {
1310 key: 'jquery-param-int',
1311 msg: '{{int:$1}}',
1312 expected: 'y'
1313 },
1314 {
1315 key: 'jquery-param-ns',
1316 msg: '{{ns:$1}}',
1317 expected: ''
1318 },
1319 {
1320 key: 'jquery-param-formatnum',
1321 msg: '{{formatnum:$1}}',
1322 expected: '<b>x</b>'
1323 },
1324 {
1325 key: 'jquery-param-case',
1326 msg: '{{lc:$1}} {{uc:$1}} {{lcfirst:$1}} {{ucfirst:$1}}',
1327 expected: 'x X x X'
1328 }
1329 ];
1330
1331 for ( let i = 0; i < cases.length; i++ ) {
1332 mw.messages.set( cases[ i ].key, cases[ i ].msg );
1333 assert.strictEqual(
1334 // eslint-disable-next-line mediawiki/msg-doc
1335 mw.message( cases[ i ].key, $( '<b>' ).text( 'x' ) ).parse(),
1336 cases[ i ].expected,
1337 cases[ i ].key
1338 );
1339 }
1340 } );
1341
1342 QUnit.test( 'Do not allow javascript: urls', function ( assert ) {
1343 mw.messages.set( 'illegal-url', '[javascript:alert(1) foo]' );
1344 mw.messages.set( 'illegal-url-param', '[$1 foo]' );
1345
1346 this.suppressWarnings();
1347
1348 assert.strictEqual(
1349 formatParse( 'illegal-url' ),
1350 '[javascript:alert(1) foo]',
1351 'illegal-url: \'parse\' format'
1352 );
1353
1354 assert.strictEqual(
1355 // eslint-disable-next-line no-script-url
1356 formatParse( 'illegal-url-param', 'javascript:alert(1)' ),
1357 '[javascript:alert(1) foo]',
1358 'illegal-url-param: \'parse\' format'
1359 );
1360 } );
1361
1362 QUnit.test( 'Do not allow style attribute (T251032)', function ( assert ) {
1363 mw.messages.set( 'unsafe-style', '<span style="background-image:url( http://example.com )">bar</span>' );
1364 mw.messages.set( 'safe-style', '<span style="color:red">bar</span>' );
1365
1366 this.suppressWarnings();
1367
1368 assert.strictEqual(
1369 formatParse( 'unsafe-style' ),
1370 '&lt;span style="background-image:url( http://example.com )"&gt;bar&lt;/span&gt;',
1371 'unsafe-style: \'parse\' format'
1372 );
1373
1374 assert.strictEqual(
1375 formatParse( 'safe-style' ),
1376 '&lt;span style="color:red"&gt;bar&lt;/span&gt;',
1377 'safe-style: \'parse\' format (all styles are disallowed now, T251032)'
1378 );
1379 } );
1380
1381 QUnit.test( 'Integration', ( assert ) => {
1382 const expected = '<b><a title="Bold" href="/wiki/Bold">Bold</a>!</b>';
1383 mw.messages.set( 'integration-test', '<b>[[Bold]]!</b>' );
1384 mw.messages.set( 'param-test', 'Hello $1' );
1385 mw.messages.set( 'param-test-with-link', 'Hello $1 [[$2|$3]]' );
1386
1387 assert.strictEqual(
1388 mw.message( 'integration-test' ).parse(),
1389 expected,
1390 'mw.message().parse() works correctly'
1391 );
1392
1393 assert.strictEqual(
1394 $( '<span>' ).append( mw.message( 'integration-test' ).parseDom() ).html(),
1395 expected,
1396 'mw.message().parseDom() works correctly'
1397 );
1398
1399 assert.strictEqual(
1400 $( '<span>' ).msg( 'integration-test' ).html(),
1401 expected,
1402 'jQuery plugin $.fn.msg() works correctly'
1403 );
1404
1405 assert.strictEqual(
1406 mw.message( 'integration-test' ).isParseable(),
1407 true,
1408 'mw.message().isParseable() works correctly'
1409 );
1410
1411 assert.strictEqual(
1412 mw.message( 'param-test', $( '<span>' ).text( 'World' ) ).parse(),
1413 'Hello <span>World</span>',
1414 'Passing a jQuery object as a parameter to a message without wikitext works correctly'
1415 );
1416
1417 mw.messages.set( 'object-double-replace', 'Foo 1: $1 2: $1' );
1418 const $messageArgument = $( '<div class="bar">&gt;</div>' );
1419 const $message = $( '<span>' ).msg( 'object-double-replace', $messageArgument );
1420 assert.true(
1421 $message[ 0 ].contains( $messageArgument[ 0 ] ),
1422 'The original jQuery object is actually in the DOM'
1423 );
1424
1425 assert.strictEqual(
1426 mw.message( 'param-test', $( '<span>' ).text( 'World' ).get( 0 ) ).parse(),
1427 'Hello <span>World</span>',
1428 'Passing a DOM node as a parameter to a message without wikitext works correctly'
1429 );
1430
1431 assert.strictEqual(
1432 mw.message( 'param-test', undefined ).parse(),
1433 'Hello $1',
1434 'Passing undefined as a parameter to a message does not throw an exception'
1435 );
1436
1437 assert.strictEqual(
1438 mw.message(
1439 'param-test-with-link',
1440 $( '<span>' ).text( 'cruel' ),
1441 'Globe',
1442 'world'
1443 ).parse(),
1444 'Hello <span>cruel</span> <a title="Globe" href="/wiki/Globe">world</a>',
1445 'Message with a jQuery parameter and a parsed link'
1446 );
1447
1448 mw.messages.set( 'integration-test-extlink', '[$1 Link]' );
1449 const msg = mw.message(
1450 'integration-test-extlink',
1451 $( '<a>' ).attr( 'href', 'http://example.com/' )
1452 );
1453 msg.parse(); // Not a no-op
1454 assert.strictEqual(
1455 msg.parse(),
1456 '<a href="http://example.com/">Link</a>',
1457 'Calling .parse() multiple times does not duplicate link contents'
1458 );
1459
1460 mw.config.set( 'wgUserLanguage', 'qqx' );
1461
1462 const $bar = $( '<b>' ).text( 'bar' );
1463 mw.messages.set( 'qqx-message', '(qqx-message)' );
1464 mw.messages.set( 'non-qqx-message', '<b>hello world</b>' );
1465
1466 assert.strictEqual( mw.message( 'missing-message' ).parse(), '(missing-message)', 'qqx message (missing)' );
1467 assert.strictEqual( mw.message( 'missing-message', $bar, 'baz' ).parse(), '(missing-message: <b>bar</b>, baz)', 'qqx message (missing) with parameters' );
1468 assert.strictEqual( mw.message( 'qqx-message' ).parse(), '(qqx-message)', 'qqx message (defined)' );
1469 assert.strictEqual( mw.message( 'qqx-message', $bar, 'baz' ).parse(), '(qqx-message: <b>bar</b>, baz)', 'qqx message (defined) with parameters' );
1470 assert.strictEqual( mw.message( 'non-qqx-message' ).parse(), '<b>hello world</b>', 'non-qqx message in qqx mode' );
1471 } );
1472
1473 QUnit.test( 'setParserDefaults', ( assert ) => {
1474 jqueryMsg.setParserDefaults( {
1475 magic: {
1476 FOO: 'foo',
1477 BAR: 'bar'
1478 }
1479 } );
1480
1481 assert.deepEqual(
1482 jqueryMsg.getParserDefaults().magic,
1483 {
1484 FOO: 'foo',
1485 BAR: 'bar'
1486 },
1487 'setParserDefaults updates the parser defaults'
1488 );
1489 } );
1490 }() );