1 module( 'mediawiki.util.js' );
3 test( '-- Initial check', function() {
6 ok( mw
.util
, 'mw.util defined' );
9 test( 'rawurlencode', function() {
12 equal( mw
.util
.rawurlencode( 'Test:A & B/Here' ), 'Test%3AA%20%26%20B%2FHere' );
15 test( 'wikiUrlencode', function() {
18 equal( mw
.util
.wikiUrlencode( 'Test:A & B/Here' ), 'Test:A_%26_B/Here' );
21 test( 'wikiGetlink', function() {
24 // Not part of startUp module
25 mw
.config
.set( 'wgArticlePath', '/wiki/$1' );
27 var hrefA
= mw
.util
.wikiGetlink( 'Sandbox' );
28 equal( hrefA
, '/wiki/Sandbox', 'Simple title; Get link for "Sandbox"' );
30 var hrefB
= mw
.util
.wikiGetlink( 'Foo:Sandbox ? 5+5=10 ! (test)/subpage' );
31 equal( hrefB
, '/wiki/Foo:Sandbox_%3F_5%2B5%3D10_%21_%28test%29/subpage',
32 'Advanced title; Get link for "Foo:Sandbox ? 5+5=10 ! (test)/subpage"' );
35 test( 'wikiScript', function() {
39 'wgScript': '/w/index.php',
41 'wgScriptExtension': '.php'
44 equal( mw
.util
.wikiScript(), mw
.config
.get( 'wgScript' ), 'Defaults to index.php and is equal to wgScript' );
45 equal( mw
.util
.wikiScript( 'api' ), '/w/api.php', 'API path' );
49 test( 'addCSS', function() {
52 var a
= mw
.util
.addCSS( '#bodyContent { visibility: hidden; }' );
53 equal( typeof a
, 'object', 'addCSS returned an object' );
54 strictEqual( a
.disabled
, false, 'property "disabled" is available and set to false' );
56 var $b
= $('#bodyContent');
57 equal( $b
.css('visibility'), 'hidden', 'Added style properties are in effect' );
60 $( a
.ownerNode
).remove();
63 test( 'toggleToc', function() {
66 strictEqual( mw
.util
.toggleToc(), null, 'Return null if there is no table of contents on the page.' );
69 '<table id="toc" class="toc"><tr><td>' +
70 '<div id="toctitle">' +
72 '<span class="toctoggle"> [<a href="#" class="internal" id="togglelink">Hide</a> ]</span>' +
74 '<ul><li></li></ul>' +
76 var $toc
= $(tocHtml
).appendTo( 'body' );
77 var $toggleLink
= $( '#togglelink' );
79 // Toggle animation is asynchronous
80 // QUnit should not finish this test() untill they are all done
83 var actionC = function() {
89 var actionB = function() {
90 strictEqual( mw
.util
.toggleToc( $toggleLink
, actionC
), true, 'Return boolean true if the TOC is now visible.' );
92 var actionA = function() {
93 strictEqual( mw
.util
.toggleToc( $toggleLink
, actionB
), false, 'Return boolean false if the TOC is now hidden.' );
99 test( 'getParamValue', function() {
102 var url
= 'http://mediawiki.org/?foo=wrong&foo=right#&foo=bad';
104 equal( mw
.util
.getParamValue( 'foo', url
), 'right', 'Use latest one, ignore hash' );
105 strictEqual( mw
.util
.getParamValue( 'bar', url
), null, 'Return null when not found' );
108 test( 'tooltipAccessKey', function() {
111 equal( typeof mw
.util
.tooltipAccessKeyPrefix
, 'string', 'mw.util.tooltipAccessKeyPrefix must be a string' );
112 ok( mw
.util
.tooltipAccessKeyRegexp
instanceof RegExp
, 'mw.util.tooltipAccessKeyRegexp instance of RegExp' );
113 ok( mw
.util
.updateTooltipAccessKeys
, 'mw.util.updateTooltipAccessKeys' );
116 test( '$content', function() {
119 ok( mw
.util
.$content
instanceof jQuery
, 'mw.util.$content instance of jQuery' );
120 strictEqual( mw
.util
.$content
.length
, 1, 'mw.util.$content must have length of 1' );
123 test( 'addPortletLink', function() {
126 var A
= mw
.util
.addPortletLink( 'p-tb', 'http://mediawiki.org/wiki/ResourceLoader',
127 'ResourceLoader', 't-rl', 'More info about ResourceLoader on MediaWiki.org ', 'l', '#t-specialpages' );
129 ok( $.isDomElement( A
), 'addPortletLink returns a valid DOM Element according to $.isDomElement' );
131 var B
= mw
.util
.addPortletLink( "p-tb", "http://mediawiki.org/",
132 "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", A
);
134 equal( $( B
).attr( 'id' ), 't-mworg', 'Link has correct ID set' );
135 equal( $( B
).closest( '.portal' ).attr( 'id' ), 'p-tb', 'Link was inserted within correct portlet' );
136 equal( $( B
).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
138 var C
= mw
.util
.addPortletLink( "p-tb", "http://mediawiki.org/wiki/RL/DM",
139 "Default modules", "t-rldm", "List of all default modules ", "d", "#t-rl" );
141 equal( $( C
).next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing CSS selector)' );
144 $( [A
, B
, C
] ).remove();
147 test( 'jsMessage', function() {
150 var a
= mw
.util
.jsMessage( "MediaWiki is <b>Awesome</b>." );
151 ok( a
, 'Basic checking of return value' );
154 $( '#mw-js-message' ).remove();
157 test( 'validateEmail', function() {
160 strictEqual( mw
.util
.validateEmail( "" ), null, 'Should return null for empty string ' );
161 strictEqual( mw
.util
.validateEmail( "user@localhost" ), true, 'Return true for a valid e-mail address' );
163 // testEmailWithCommasAreInvalids
164 strictEqual( mw
.util
.validateEmail( "user,foo@example.org" ), false, 'Emails with commas are invalid' );
165 strictEqual( mw
.util
.validateEmail( "userfoo@ex,ample.org" ), false, 'Emails with commas are invalid' );
167 // testEmailWithHyphens
168 strictEqual( mw
.util
.validateEmail( "user-foo@example.org" ), true, 'Emails may contain a hyphen' );
169 strictEqual( mw
.util
.validateEmail( "userfoo@ex-ample.org" ), true, 'Emails may contain a hyphen' );
172 test( 'isIPv6Address', function() {
176 var assertFalseIPv6 = function( addy
, summary
) {
177 return strictEqual( mw
.util
.isIPv6Address( addy
), false, summary
);
179 assertTrueIPv6 = function( addy
, summary
) {
180 return strictEqual( mw
.util
.isIPv6Address( addy
), true, summary
);
183 // Based on IPTest.php > testisIPv6
184 assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
185 assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
186 assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
187 assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
195 'fc:100:a:d:1:e:ac::'], function( i
, addy
){
196 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
199 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words ending with "::"' );
200 assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words ending with "::"' );
202 assertFalseIPv6( ':::' );
203 assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
205 assertTrueIPv6( '::', 'IPv6 zero address' );
214 '::fc:100:a:d:1:e:ac',
216 'fc:100:a:d:1:e:ac:0'], function( i
, addy
){
217 assertTrueIPv6( addy
, addy
+ ' is a valid IP' );
220 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
221 assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
223 assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
224 assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
225 assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
227 assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
228 assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
229 assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
230 assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
231 assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words' );
232 assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7 words' );
233 assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
234 assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5 words' );
235 assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6 words' );
237 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8 words' );
238 assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words' );
241 test( 'isIPv4Address', function() {
245 var assertFalseIPv4 = function( addy
, summary
) {
246 return strictEqual( mw
.util
.isIPv4Address( addy
), false, summary
);
248 assertTrueIPv4 = function( addy
, summary
) {
249 return strictEqual( mw
.util
.isIPv4Address( addy
), true, summary
);
252 // Based on IPTest.php > testisIPv4
253 assertFalseIPv4( false, 'Boolean false is not an IP' );
254 assertFalseIPv4( true, 'Boolean true is not an IP' );
255 assertFalseIPv4( '', 'Empty string is not an IP' );
256 assertFalseIPv4( 'abc', '"abc" is not an IP' );
257 assertFalseIPv4( ':', 'Colon is not an IP' );
258 assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
259 assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
260 assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
262 assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
263 assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
264 assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not recogzized as valid IPs' );