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 );
85 // Used by accessKeyLabel in test for addPortletLink
91 QUnit
.test( 'rawurlencode', function ( assert
) {
92 assert
.equal( util
.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
95 QUnit
.test( 'escapeIdForAttribute', function ( assert
) {
96 // Test cases are kept in sync with SanitizerTest.php
97 var text
= 'foo тест_#%!\'()[]:<>',
98 legacyEncoded
= 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
99 html5Encoded
= 'foo_тест_#%!\'()[]:<>',
100 // Settings: this is $wgFragmentMode
101 legacy
= [ 'legacy' ],
102 legacyNew
= [ 'legacy', 'html5' ],
103 newLegacy
= [ 'html5', 'legacy' ],
104 allNew
= [ 'html5' ];
106 // Test cases are kept in sync with SanitizerTest.php
108 // Pure legacy: how MW worked before 2017
109 [ legacy
, text
, legacyEncoded
],
110 // Transition to a new world: legacy links with HTML5 fallback
111 [ legacyNew
, text
, legacyEncoded
],
112 // New world: HTML5 links, legacy fallbacks
113 [ newLegacy
, text
, html5Encoded
],
114 // Distant future: no legacy fallbacks
115 [ allNew
, text
, html5Encoded
]
116 ].forEach( function ( testCase
) {
117 mw
.config
.set( 'wgFragmentMode', testCase
[ 0 ] );
119 assert
.equal( util
.escapeIdForAttribute( testCase
[ 1 ] ), testCase
[ 2 ] );
123 QUnit
.test( 'escapeIdForLink', function ( assert
) {
124 // Test cases are kept in sync with SanitizerTest.php
125 var text
= 'foo тест_#%!\'()[]:<>',
126 legacyEncoded
= 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E',
127 html5Encoded
= 'foo_тест_#%!\'()[]:<>',
128 // Settings: this is wgFragmentMode
129 legacy
= [ 'legacy' ],
130 legacyNew
= [ 'legacy', 'html5' ],
131 newLegacy
= [ 'html5', 'legacy' ],
132 allNew
= [ 'html5' ];
135 // Pure legacy: how MW worked before 2017
136 [ legacy
, text
, legacyEncoded
],
137 // Transition to a new world: legacy links with HTML5 fallback
138 [ legacyNew
, text
, legacyEncoded
],
139 // New world: HTML5 links, legacy fallbacks
140 [ newLegacy
, text
, html5Encoded
],
141 // Distant future: no legacy fallbacks
142 [ allNew
, text
, html5Encoded
]
143 ].forEach( function ( testCase
) {
144 mw
.config
.set( 'wgFragmentMode', testCase
[ 0 ] );
146 assert
.equal( util
.escapeIdForLink( testCase
[ 1 ] ), testCase
[ 2 ] );
150 QUnit
.test( 'wikiUrlencode', function ( assert
) {
151 assert
.equal( util
.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
152 // See also wfUrlencodeTest.php#provideURLS
158 ';@$-_.!*': ';@$-_.!*',
164 }, function ( input
, output
) {
165 assert
.equal( util
.wikiUrlencode( input
), output
);
169 QUnit
.test( 'getUrl', function ( assert
) {
172 wgScript
: '/w/index.php',
173 wgArticlePath
: '/wiki/$1',
177 href
= util
.getUrl( 'Sandbox' );
178 assert
.equal( href
, '/wiki/Sandbox', 'simple title' );
180 href
= util
.getUrl( 'Foo:Sandbox? 5+5=10! (test)/sub ' );
181 assert
.equal( href
, '/wiki/Foo:Sandbox%3F_5%2B5%3D10!_(test)/sub_', 'complex title' );
184 href
= util
.getUrl( 'My$$test$$$$$title' );
185 assert
.equal( href
, '/wiki/My$$test$$$$$title', 'title with multiple consecutive dollar signs' );
187 href
= util
.getUrl();
188 assert
.equal( href
, '/wiki/Foobar', 'default title' );
190 href
= util
.getUrl( null, { action
: 'edit' } );
191 assert
.equal( href
, '/w/index.php?title=Foobar&action=edit', 'default title with query string' );
193 href
= util
.getUrl( 'Sandbox', { action
: 'edit' } );
194 assert
.equal( href
, '/w/index.php?title=Sandbox&action=edit', 'simple title with query string' );
197 href
= util
.getUrl( 'Foo:Sandbox#Fragment', { action
: 'edit' } );
198 assert
.equal( href
, '/w/index.php?title=Foo:Sandbox&action=edit#Fragment', 'namespaced title with query string and fragment' );
200 href
= util
.getUrl( 'Sandbox#', { action
: 'edit' } );
201 assert
.equal( href
, '/w/index.php?title=Sandbox&action=edit', 'title with query string and empty fragment' );
203 href
= util
.getUrl( 'Sandbox', {} );
204 assert
.equal( href
, '/wiki/Sandbox', 'title with empty query string' );
206 href
= util
.getUrl( '#Fragment' );
207 assert
.equal( href
, '/wiki/#Fragment', 'empty title with fragment' );
209 href
= util
.getUrl( '#Fragment', { action
: 'edit' } );
210 assert
.equal( href
, '/w/index.php?action=edit#Fragment', 'empty title with query string and fragment' );
212 mw
.config
.set( 'wgFragmentMode', [ 'legacy' ] );
213 href
= util
.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action
: 'edit' } );
214 assert
.equal( href
, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_.C3.84', 'title with query string, fragment, and special characters' );
216 mw
.config
.set( 'wgFragmentMode', [ 'html5' ] );
217 href
= util
.getUrl( 'Foo:Sandbox \xC4#Fragment \xC4', { action
: 'edit' } );
218 assert
.equal( href
, '/w/index.php?title=Foo:Sandbox_%C3%84&action=edit#Fragment_Ä', 'title with query string, fragment, and special characters' );
220 href
= util
.getUrl( 'Foo:%23#Fragment', { action
: 'edit' } );
221 assert
.equal( href
, '/w/index.php?title=Foo:%2523&action=edit#Fragment', 'title containing %23 (#), fragment, and a query string' );
223 mw
.config
.set( 'wgFragmentMode', [ 'legacy' ] );
224 href
= util
.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action
: 'edit' } );
225 assert
.equal( 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' );
227 mw
.config
.set( 'wgFragmentMode', [ 'html5' ] );
228 href
= util
.getUrl( '#+&=:;@$-_.!*/[]<>\'§', { action
: 'edit' } );
229 assert
.equal( href
, '/w/index.php?action=edit#+&=:;@$-_.!*/[]<>\'§', 'fragment with various characters' );
232 QUnit
.test( 'wikiScript', function ( assert
) {
234 // customized wgScript for T41103
235 wgScript
: '/w/i.php',
236 // customized wgLoadScript for T41103
237 wgLoadScript
: '/w/l.php',
241 assert
.equal( util
.wikiScript(), mw
.config
.get( 'wgScript' ),
242 'wikiScript() returns wgScript'
244 assert
.equal( util
.wikiScript( 'index' ), mw
.config
.get( 'wgScript' ),
245 'wikiScript( index ) returns wgScript'
247 assert
.equal( util
.wikiScript( 'load' ), mw
.config
.get( 'wgLoadScript' ),
248 'wikiScript( load ) returns wgLoadScript'
250 assert
.equal( util
.wikiScript( 'api' ), '/w/api.php', 'API path' );
253 QUnit
.test( 'addCSS', function ( assert
) {
255 $el
= $( '<div>' ).attr( 'id', 'mw-addcsstest' ).appendTo( '#qunit-fixture' );
257 style
= util
.addCSS( '#mw-addcsstest { visibility: hidden; }' );
258 assert
.equal( typeof style
, 'object', 'addCSS returned an object' );
259 assert
.strictEqual( style
.disabled
, false, 'property "disabled" is available and set to false' );
261 assert
.equal( $el
.css( 'visibility' ), 'hidden', 'Added style properties are in effect' );
264 $( style
.ownerNode
).remove();
267 QUnit
.test( 'getParamValue', function ( assert
) {
270 url
= 'http://example.org/?foo=wrong&foo=right#&foo=bad';
271 assert
.equal( util
.getParamValue( 'foo', url
), 'right', 'Use latest one, ignore hash' );
272 assert
.strictEqual( util
.getParamValue( 'bar', url
), null, 'Return null when not found' );
274 url
= 'http://example.org/#&foo=bad';
275 assert
.strictEqual( util
.getParamValue( 'foo', url
), null, 'Ignore hash if param is not in querystring but in hash (T29427)' );
277 url
= 'example.org?' + $.param( { TEST
: 'a b+c' } );
278 assert
.strictEqual( util
.getParamValue( 'TEST', url
), 'a b+c', 'T32441: getParamValue must understand "+" encoding of space' );
280 url
= 'example.org?' + $.param( { TEST
: 'a b+c d' } ); // check for sloppy code from r95332 :)
281 assert
.strictEqual( util
.getParamValue( 'TEST', url
), 'a b+c d', 'T32441: getParamValue must understand "+" encoding of space (multiple spaces)' );
284 QUnit
.test( '$content', function ( assert
) {
285 assert
.ok( util
.$content
instanceof jQuery
, 'mw.util.$content instance of jQuery' );
286 assert
.strictEqual( util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
290 * Portlet names are prefixed with 'p-test' to avoid conflict with core
291 * when running the test suite under a wiki page.
292 * Previously, test elements where invisible to the selector since only
293 * one element can have a given id.
295 QUnit
.test( 'addPortletLink', function ( assert
) {
296 var pTestTb
, pCustom
, vectorTabs
, tbRL
, cuQuux
, $cuQuux
, tbMW
, $tbMW
, tbRLDM
, caFoo
,
297 addedAfter
, tbRLDMnonexistentid
, tbRLDMemptyjquery
;
300 '<div class="portlet" id="p-test-tb">' +
302 '<ul class="body"></ul>' +
305 '<div class="portlet" id="p-test-custom">' +
307 '<ul class="body">' +
308 '<li id="c-foo"><a href="#">Foo</a></li>' +
309 '<li id="c-barmenu">' +
311 '<li id="c-bar-baz"><a href="#">Baz</a></a>' +
317 '<div id="p-test-views" class="vectorTabs">' +
322 $( '#qunit-fixture' ).append( pTestTb
, pCustom
, vectorTabs
);
324 tbRL
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/ResourceLoader',
325 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l'
328 assert
.ok( tbRL
&& tbRL
.nodeType
, 'addPortletLink returns a DOM Node' );
330 tbMW
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/',
331 'MediaWiki.org', 't-mworg', 'Go to MediaWiki.org', 'm', tbRL
);
339 'Validate attributes of created element'
343 $tbMW
.find( 'a' ).getAttrs(),
345 href
: '//mediawiki.org/',
346 title
: 'Go to MediaWiki.org [test-m]',
349 'Validate attributes of anchor tag in created element'
352 assert
.equal( $tbMW
.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
353 assert
.strictEqual( $tbMW
.next()[ 0 ], tbRL
, 'Link is in the correct position (nextnode as Node object)' );
355 cuQuux
= util
.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
356 $cuQuux
= $( cuQuux
);
358 assert
.equal( $cuQuux
.find( 'a' ).attr( 'title' ), 'Example [test-q]', 'Existing accesskey is stripped and updated' );
361 $( '#p-test-custom #c-barmenu ul li' ).length
,
363 'addPortletLink did not add the item to all <ul> elements in the portlet (T37082)'
366 tbRLDM
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
367 'Default modules', 't-rldm', 'List of all default modules ', 'd', '#t-rl' );
369 assert
.strictEqual( $( tbRLDM
).next()[ 0 ], tbRL
, 'Link is in the correct position (CSS selector as nextnode)' );
371 caFoo
= util
.addPortletLink( 'p-test-views', '#', 'Foo' );
373 assert
.strictEqual( $tbMW
.find( 'span' ).length
, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
374 assert
.strictEqual( $( caFoo
).find( 'span' ).length
, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
376 addedAfter
= util
.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL
) );
377 assert
.strictEqual( $( addedAfter
).next()[ 0 ], tbRL
, 'Link is in the correct position (jQuery object as nextnode)' );
379 // test case - nonexistent id as next node
380 tbRLDMnonexistentid
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
381 'Default modules', 't-rldm-nonexistent', 'List of all default modules ', 'd', '#t-rl-nonexistent' );
383 assert
.equal( tbRLDMnonexistentid
, $( '#p-test-tb li:last' )[ 0 ], 'Fallback to adding at the end (nextnode non-matching CSS selector)' );
385 // test case - empty jquery object as next node
386 tbRLDMemptyjquery
= util
.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM',
387 'Default modules', 't-rldm-empty-jquery', 'List of all default modules ', 'd', $( '#t-rl-nonexistent' ) );
389 assert
.equal( tbRLDMemptyjquery
, $( '#p-test-tb li:last' )[ 0 ], 'Fallback to adding at the end (nextnode as empty jQuery object)' );
392 QUnit
.test( 'validateEmail', function ( assert
) {
393 assert
.strictEqual( util
.validateEmail( '' ), null, 'Should return null for empty string ' );
394 assert
.strictEqual( util
.validateEmail( 'user@localhost' ), true, 'Return true for a valid e-mail address' );
396 // testEmailWithCommasAreInvalids
397 assert
.strictEqual( util
.validateEmail( 'user,foo@example.org' ), false, 'Emails with commas are invalid' );
398 assert
.strictEqual( util
.validateEmail( 'userfoo@ex,ample.org' ), false, 'Emails with commas are invalid' );
400 // testEmailWithHyphens
401 assert
.strictEqual( util
.validateEmail( 'user-foo@example.org' ), true, 'Emails may contain a hyphen' );
402 assert
.strictEqual( util
.validateEmail( 'userfoo@ex-ample.org' ), true, 'Emails may contain a hyphen' );
405 QUnit
.test( 'isIPv6Address', function ( assert
) {
406 IPV6_CASES
.forEach( function ( ipCase
) {
407 assert
.strictEqual( util
.isIPv6Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
411 QUnit
.test( 'isIPv4Address', function ( assert
) {
412 IPV4_CASES
.forEach( function ( ipCase
) {
413 assert
.strictEqual( util
.isIPv4Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
417 QUnit
.test( 'isIPAddress', function ( assert
) {
418 IPV4_CASES
.forEach( function ( ipCase
) {
419 assert
.strictEqual( util
.isIPv4Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
422 IPV6_CASES
.forEach( function ( ipCase
) {
423 assert
.strictEqual( util
.isIPv6Address( ipCase
[ 1 ] ), ipCase
[ 0 ], ipCase
[ 2 ] );
426 }( mediaWiki
, jQuery
) );