2 var util
= require( 'mediawiki.util' ),
3 // Based on IPTest.php > testisIPv4
5 [ false, false, 'Boolean false is not an IP' ],
6 [ false, true, 'Boolean true is not an IP' ],
7 [ false, '', 'Empty string is not an IP' ],
8 [ false, 'abc', '"abc" is not an IP' ],
9 [ false, ':', 'Colon is not an IP' ],
10 [ false, '124.24.52', 'IPv4 not enough quads' ],
11 [ false, '24.324.52.13', 'IPv4 out of range' ],
12 [ false, '.24.52.13', 'IPv4 starts with period' ],
14 [ true, '124.24.52.13', '124.24.52.134 is a valid IP' ],
15 [ true, '1.24.52.13', '1.24.52.13 is a valid IP' ],
16 [ false, '74.24.52.13/20', 'IPv4 ranges are not recognized as valid IPs' ]
19 // Based on IPTest.php > testisIPv6
21 [ false, ':fc:100::', 'IPv6 starting with lone ":"' ],
22 [ false, 'fc:100:::', 'IPv6 ending with a ":::"' ],
23 [ false, 'fc:300', 'IPv6 with only 2 words' ],
24 [ false, 'fc:100:300', 'IPv6 with only 3 words' ],
26 [ false, 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' ],
27 [ false, 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' ],
30 [ false, '::0:', 'IPv6 ending in a lone ":"' ],
32 [ true, '::', 'IPv6 zero address' ],
34 [ false, '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' ],
35 [ false, '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' ],
37 [ false, ':fc::100', 'IPv6 starting with lone ":"' ],
38 [ false, 'fc::100:', 'IPv6 ending with lone ":"' ],
39 [ false, 'fc:::100', 'IPv6 with ":::" in the middle' ],
41 [ true, 'fc::100', 'IPv6 with "::" and 2 words' ],
42 [ true, 'fc::100:a', 'IPv6 with "::" and 3 words' ],
43 [ true, 'fc::100:a:d', 'IPv6 with "::" and 4 words' ],
44 [ true, 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' ],
45 [ true, 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' ],
46 [ true, 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' ],
47 [ true, '2001::df', 'IPv6 with "::" and 2 words' ],
48 [ true, '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' ],
49 [ true, '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' ],
51 [ false, 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' ],
52 [ false, 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' ]
55 Array
.prototype.push
.apply( IPV6_CASES
,
62 'fc:100:a:d:1:e:ac::',
70 '::fc:100:a:d:1:e:ac',
72 ].map( function ( el
) {
73 return [ true, el
, el
+ ' is a valid IP' ];
77 QUnit
.module( 'mediawiki.util', QUnit
.newMwEnvironment( {
79 $.fn
.updateTooltipAccessKeys
.setTestMode( true );
81 teardown: function () {
82 $.fn
.updateTooltipAccessKeys
.setTestMode( false );
83 mw
.util
.resetOptionsForTest();
86 // Used by accessKeyLabel in test for addPortletLink
92 QUnit
.test( 'rawurlencode', function ( assert
) {
93 assert
.strictEqual( util
.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
96 QUnit
.test( 'escapeIdForAttribute', function ( assert
) {
97 // Test cases are kept in sync with SanitizerTest.php
98 var text
= 'foo тест_#%!\'()[]:<>',
99 legacyEncoded
= 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
100 html5Encoded
= 'foo_тест_#%!\'()[]:<>',
101 // Settings: this is $wgFragmentMode
102 legacy
= [ 'legacy' ],
103 legacyNew
= [ 'legacy', 'html5' ],
104 newLegacy
= [ 'html5', 'legacy' ],
105 allNew
= [ 'html5' ];
107 // Test cases are kept in sync with SanitizerTest.php
109 // Pure legacy: how MW worked before 2017
110 [ legacy
, text
, legacyEncoded
],
111 // Transition to a new world: legacy links with HTML5 fallback
112 [ legacyNew
, text
, legacyEncoded
],
113 // New world: HTML5 links, legacy fallbacks
114 [ newLegacy
, text
, html5Encoded
],
115 // Distant future: no legacy fallbacks
116 [ allNew
, text
, html5Encoded
]
117 ].forEach( function ( testCase
) {
118 mw
.util
.setOptionsForTest( { FragmentMode
: testCase
[ 0 ] } );
120 assert
.strictEqual( util
.escapeIdForAttribute( testCase
[ 1 ] ), testCase
[ 2 ] );
124 QUnit
.test( 'escapeIdForLink', function ( assert
) {
125 // Test cases are kept in sync with SanitizerTest.php
126 var text
= 'foo тест_#%!\'()[]:<>',
127 legacyEncoded
= 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
128 html5Encoded
= 'foo_тест_#%!\'()[]:<>',
129 // Settings: this is wgFragmentMode
130 legacy
= [ 'legacy' ],
131 legacyNew
= [ 'legacy', 'html5' ],
132 newLegacy
= [ 'html5', 'legacy' ],
133 allNew
= [ 'html5' ];
136 // Pure legacy: how MW worked before 2017
137 [ legacy
, text
, legacyEncoded
],
138 // Transition to a new world: legacy links with HTML5 fallback
139 [ legacyNew
, text
, legacyEncoded
],
140 // New world: HTML5 links, legacy fallbacks
141 [ newLegacy
, text
, html5Encoded
],
142 // Distant future: no legacy fallbacks
143 [ allNew
, text
, html5Encoded
]
144 ].forEach( function ( testCase
) {
145 mw
.util
.setOptionsForTest( { FragmentMode
: testCase
[ 0 ] } );
147 assert
.strictEqual( util
.escapeIdForLink( testCase
[ 1 ] ), testCase
[ 2 ] );
151 QUnit
.test( 'wikiUrlencode', function ( assert
) {
152 assert
.strictEqual( util
.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
153 // See also wfUrlencodeTest.php#provideURLS
154 // eslint-disable-next-line no-jquery/no-each-util
160 ';@$-_.!*': ';@$-_.!*',
166 }, function ( input
, output
) {
167 assert
.strictEqual( util
.wikiUrlencode( input
), output
);
171 QUnit
.test( 'getUrl', function ( assert
) {
174 wgScript
: '/w/index.php',
175 wgArticlePath
: '/wiki/$1',
179 href
= util
.getUrl( 'Sandbox' );
180 assert
.strictEqual( href
, '/wiki/Sandbox', 'simple title' );
182 href
= util
.getUrl( 'Foo:Sandbox? 5+5=10! (test)/sub ' );
183 assert
.strictEqual( href
, '/wiki/Foo:Sandbox%3F_5%2B5%3D10!_(test)/sub_', 'complex title' );
186 href
= util
.getUrl( 'My$$test$$$$$title' );
187 assert
.strictEqual( href
, '/wiki/My$$test$$$$$title', 'title with multiple consecutive dollar signs' );
189 href
= util
.getUrl();
190 assert
.strictEqual( href
, '/wiki/Foobar', 'default title' );
192 href
= util
.getUrl( null, { action
: 'edit' } );
193 assert
.strictEqual( href
, '/w/index.php?title=Foobar&action=edit', 'default title with query string' );
195 href
= util
.getUrl( 'Sandbox', { action
: 'edit' } );
196 assert
.strictEqual( href
, '/w/index.php?title=Sandbox&action=edit', 'simple title with query string' );
199 href
= util
.getUrl( 'Foo:Sandbox#Fragment', { action
: 'edit' } );
200 assert
.strictEqual( href
, '/w/index.php?title=Foo:Sandbox&action=edit#Fragment', 'namespaced title with query string and fragment' );
202 href
= util
.getUrl( 'Sandbox#', { action
: 'edit' } );
203 assert
.strictEqual( href
, '/w/index.php?title=Sandbox&action=edit', 'title with query string and empty fragment' );
205 href
= util
.getUrl( 'Sandbox', {} );
206 assert
.strictEqual( href
, '/wiki/Sandbox', 'title with empty query string' );
208 href
= util
.getUrl( '#Fragment' );
209 assert
.strictEqual( href
, '/wiki/#Fragment', 'empty title with fragment' );
211 href
= util
.getUrl( '#Fragment', { action
: 'edit' } );
212 assert
.strictEqual( href
, '/w/index.php?action=edit#Fragment', 'empty title with query string and fragment' );
214 mw
.util
.setOptionsForTest( { FragmentMode
: [ 'legacy' ] } );
215 href
= util
.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action
: 'edit' } );
216 assert
.strictEqual( href
, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
218 mw
.util
.setOptionsForTest( { FragmentMode
: [ 'html5' ] } );
219 href
= util
.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action
: 'edit' } );
220 assert
.strictEqual( href
, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_Ä', 'title with query string, fragment, and special characters' );
222 href
= util
.getUrl( 'Foo:%23#Fragment', { action
: 'edit' } );
223 assert
.strictEqual( href
, '/w/index.php?title=Foo:%2523&action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
225 mw
.util
.setOptionsForTest( { FragmentMode
: [ 'legacy' ] } );
226 href
= util
.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action
: 'edit' } );
227 assert
.strictEqual( href
, '/w/index.php?action=edit#.2B.26.3D:.3B.40.24-_..21.2A.2F.5B.5D.3C.3E.27.C2.A7', 'fragment with various characters' );
229 mw
.util
.setOptionsForTest( { FragmentMode
: [ 'html5' ] } );
230 href
= util
.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action
: 'edit' } );
231 assert
.strictEqual( href
, '/w/index.php?action=edit#+&=:;@$-_.!*/[]<>\'§', 'fragment with various characters' );
234 QUnit
.test( 'wikiScript', function ( assert
) {
236 // customized wgScript for T41103
237 wgScript
: '/w/i.php',
238 // customized wgLoadScript for T41103
239 wgLoadScript
: '/w/l.php',
243 assert
.strictEqual( util
.wikiScript(), mw
.config
.get( 'wgScript' ),
244 'wikiScript() returns wgScript'
246 assert
.strictEqual( util
.wikiScript( 'index' ), mw
.config
.get( 'wgScript' ),
247 'wikiScript( index ) returns wgScript'
249 assert
.strictEqual( util
.wikiScript( 'load' ), mw
.config
.get( 'wgLoadScript' ),
250 'wikiScript( load ) returns wgLoadScript'
252 assert
.strictEqual( util
.wikiScript( 'api' ), '/w/api.php', 'API path' );
255 QUnit
.test( 'addCSS', function ( assert
) {
257 $el
= $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
259 style
= util
.addCSS( '#mw-addcsstest { visibility: hidden; }' );
260 assert
.strictEqual( typeof style
, 'object', 'addCSS returned an object' );
261 assert
.strictEqual( style
.disabled
, false, 'property "disabled" is available and set to false' );
263 assert
.strictEqual( $el
.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
266 $( style
.ownerNode
).remove();
269 QUnit
.test( 'getParamValue', function ( assert
) {
272 url
= 'http://example.org/?foo=wrong&foo=right#&foo=bad';
273 assert
.strictEqual( util
.getParamValue( 'foo', url
), 'right', 'Use latest one, ignore hash' );
274 assert
.strictEqual( util
.getParamValue( 'bar', url
), null, 'Return null when not found' );
276 url
= 'http://example.org/#&foo=bad';
277 assert
.strictEqual( util
.getParamValue( 'foo', url
), null, 'Ignore hash if param is not in querystring but in hash (T29427)' );
279 url
= 'example.org?' + $.param( { TEST
: 'a b+c' } );
280 assert
.strictEqual( util
.getParamValue( 'TEST', url
), 'a b+c', 'T32441: getParamValue must understand "+" encoding of space' );
282 url
= 'example.org?' + $.param( { TEST
: 'a b+c d' } ); // check for sloppy code from r95332 :)
283 assert
.strictEqual( util
.getParamValue( 'TEST', url
), 'a b+c d', 'T32441: getParamValue must understand "+" encoding of space (multiple spaces)' );
286 QUnit
.test( '$content', function ( assert
) {
287 assert
.ok( util
.$content
instanceof $, 'mw.util.$content instance of jQuery' );
288 assert
.strictEqual( util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
292 * Portlet names are prefixed with 'p-test' to avoid conflict with core
293 * when running the test suite under a wiki page.
294 * Previously, test elements where invisible to the selector since only
295 * one element can have a given id.
297 QUnit
.test( 'addPortletLink', function ( assert
) {
298 var tbRL
, cuQuux
, $cuQuux
, tbMW
, $tbMW
, tbRLDM
, caFoo
,
299 addedAfter
, tbRLDMnonexistentid
, tbRLDMemptyjquery
;
301 $( '#qunit-fixture' ).append(
302 '<div class="portlet" id="p-test-tb">' +
304 '<ul class="body"></ul>' +
306 '<div class="portlet" id="p-test-custom">' +
308 '<ul class="body">' +
309 '<li id="c-foo"><a href="#">Foo</a></li>' +
310 '<li id="c-barmenu">' +
312 '<li id="c-bar-baz"><a href="#">Baz</a></a>' +
317 '<div id="p-test-views" class="vectorTabs">' +
323 tbRL
= util
.addPortletLink( 'p-test-tb', 'https://example.org/next',
324 'Next', 't-rl', 'More info about Example Next ', 'l'
326 assert
.strictEqual( tbRL
.nodeType
, 1, 'returns a DOM Node' );
327 assert
.strictEqual( tbRL
.nodeName
, 'LI', 'returns a list item element' );
329 tbMW
= util
.addPortletLink( 'p-test-tb', '//example.org/',
330 'Example.org', 't-xmp', 'Go to Example', 'x', tbRL
);
337 'List item attributes'
340 $tbMW
.find( 'a' ).getAttrs(),
342 href
: '//example.org/',
343 title
: 'Go to Example [test-x]',
346 'Anchor link attributes'
349 $tbMW
.closest( '.portlet' ).attr( 'id' ),
356 'Next node (set as Node object)'
359 $tbMW
.find( 'span' ).length
,
361 'No <span> wrap for porlets without vectorTabs class'
364 cuQuux
= util
.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
365 $cuQuux
= $( cuQuux
);
367 $cuQuux
.find( 'a' ).attr( 'title' ),
369 'Title has new accesskey and label'
372 $( '#p-test-custom #c-barmenu ul li' ).length
,
374 'No items added to unrelated <ul> elsewhere in the portlet (T37082)'
377 tbRLDM
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
378 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
379 assert
.strictEqual( $( tbRLDM
).next()[ 0 ], tbRL
, 'Next node (set as CSS selector)' );
381 caFoo
= util
.addPortletLink( 'p-test-views', '#', 'Foo' );
382 assert
.strictEqual( $( caFoo
).find( 'span' ).length
, 1, 'Added <span> element for porlet with vectorTabs class' );
384 addedAfter
= util
.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL
) );
385 assert
.strictEqual( $( addedAfter
).next()[ 0 ], tbRL
, 'Next node (set as jQuery object)' );
387 tbRLDMnonexistentid
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
388 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' );
391 $( '#p-test-tb li' ).last()[ 0 ],
392 'Next node as non-matching CSS selector falls back to appending'
395 tbRLDMemptyjquery
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
396 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) );
399 $( '#p-test-tb li' ).last()[ 0 ],
400 'Next node as empty jQuery object falls back to appending'
404 QUnit
.test( 'validateEmail', function ( assert
) {
405 assert
.strictEqual( util
.validateEmail( '' ), null, 'Should return null for empty string ' );
406 assert
.strictEqual( util
.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
408 // testEmailWithCommasAreInvalids
409 assert
.strictEqual( util
.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
410 assert
.strictEqual( util
.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
412 // testEmailWithHyphens
413 assert
.strictEqual( util
.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
414 assert
.strictEqual( util
.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
417 QUnit
.test( 'isIPv6Address', function ( assert
) {
418 IPV6_CASES
.forEach( function ( ipCase
) {
419 assert
.strictEqual( util
.isIPv6Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
423 QUnit
.test( 'isIPv4Address', function ( assert
) {
424 IPV4_CASES
.forEach( function ( ipCase
) {
425 assert
.strictEqual( util
.isIPv4Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
429 QUnit
.test( 'isIPAddress', function ( assert
) {
430 IPV4_CASES
.forEach( function ( ipCase
) {
431 assert
.strictEqual( util
.isIPv4Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
434 IPV6_CASES
.forEach( function ( ipCase
) {
435 assert
.strictEqual( util
.isIPv6Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );