jenkins-bot has submitted this change and it was merged.
Change subject: mediawiki.util: Add mw.util.isIPAddress
......................................................................
mediawiki.util: Add mw.util.isIPAddress
Test it by reusing all the IPv4 and IPv6 cases.
Also, spelling fix
Change-Id: I57e1f00d18a74147da3200be133a4ceb989b1b9d
---
M resources/src/mediawiki.special/mediawiki.special.block.js
M resources/src/mediawiki/mediawiki.util.js
M tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js
3 files changed, 103 insertions(+), 86 deletions(-)
Approvals:
Krinkle: Looks good to me, approved
Mattflaschen: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/resources/src/mediawiki.special/mediawiki.special.block.js
b/resources/src/mediawiki.special/mediawiki.special.block.js
index 8579e05..aca335e 100644
--- a/resources/src/mediawiki.special/mediawiki.special.block.js
+++ b/resources/src/mediawiki.special/mediawiki.special.block.js
@@ -12,7 +12,7 @@
function updateBlockOptions( instant ) {
var blocktarget = $.trim( $blockTarget.val() ),
isEmpty = blocktarget === '',
- isIp = mw.util.isIPv4Address( blocktarget, true
) || mw.util.isIPv6Address( blocktarget, true ),
+ isIp = mw.util.isIPAddress( blocktarget, true ),
isIpRange = isIp && blocktarget.match( /\/\d+$/
);
if ( isIp && !isEmpty ) {
diff --git a/resources/src/mediawiki/mediawiki.util.js
b/resources/src/mediawiki/mediawiki.util.js
index 2c3c90f..6723e5f 100644
--- a/resources/src/mediawiki/mediawiki.util.js
+++ b/resources/src/mediawiki/mediawiki.util.js
@@ -443,6 +443,19 @@
return address.search( new RegExp( '^' + RE_IPV6_ADD +
block + '$' ) ) !== -1
&& address.search( /::/ ) !== -1 &&
address.search( /::.*::/ ) === -1;
+ },
+
+ /**
+ * Check whether a string is an IP address
+ *
+ * @since 1.25
+ * @param {string} address String to check
+ * @param {boolean} allowBlock True if a block of IPs should be
allowed
+ * @return {boolean}
+ */
+ isIPAddress: function ( address, allowBlock ) {
+ return util.isIPv4Address( address, allowBlock ) ||
+ util.isIPv6Address( address, allowBlock );
}
};
diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js
b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js
index 3f19a64..0b42af4 100644
--- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js
+++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js
@@ -1,4 +1,79 @@
( function ( mw, $ ) {
+ var
+ // Based on IPTest.php > testisIPv4
+ IPV4_CASES = [
+ [false, false, 'Boolean false is not an IP'],
+ [false, true, 'Boolean true is not an IP'],
+ [false, '', 'Empty string is not an IP'],
+ [false, 'abc', '"abc" is not an IP'],
+ [false, ':', 'Colon is not an IP'],
+ [false, '124.24.52', 'IPv4 not enough quads'],
+ [false, '24.324.52.13', 'IPv4 out of range'],
+ [false, '.24.52.13', 'IPv4 starts with period'],
+
+ [true, '124.24.52.13', '124.24.52.134 is a valid IP'],
+ [true, '1.24.52.13', '1.24.52.13 is a valid IP'],
+ [false, '74.24.52.13/20', 'IPv4 ranges are not
recognized as valid IPs']
+ ],
+
+ // Based on IPTest.php > testisIPv6
+ IPV6_CASES = [
+ [false, ':fc:100::', 'IPv6 starting with lone ":"'],
+ [false, 'fc:100:::', 'IPv6 ending with a ":::"'],
+ [false, 'fc:300', 'IPv6 with only 2 words'],
+ [false, 'fc:100:300', 'IPv6 with only 3 words'],
+
+ [false, 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words
ending with "::"'],
+ [false, 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words
ending with "::"'],
+
+ [false, ':::'],
+ [false, '::0:', 'IPv6 ending in a lone ":"'],
+
+ [true, '::', 'IPv6 zero address'],
+
+ [false, '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8
words'],
+ [false, '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words'],
+
+ [false, ':fc::100', 'IPv6 starting with lone ":"'],
+ [false, 'fc::100:', 'IPv6 ending with lone ":"'],
+ [false, 'fc:::100', 'IPv6 with ":::" in the middle'],
+
+ [true, 'fc::100', 'IPv6 with "::" and 2 words'],
+ [true, 'fc::100:a', 'IPv6 with "::" and 3 words'],
+ [true, 'fc::100:a:d', 'IPv6 with "::" and 4 words'],
+ [true, 'fc::100:a:d:1', 'IPv6 with "::" and 5 words'],
+ [true, 'fc::100:a:d:1:e', 'IPv6 with "::" and 6
words'],
+ [true, 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7
words'],
+ [true, '2001::df', 'IPv6 with "::" and 2 words'],
+ [true, '2001:5c0:1400:a::df', 'IPv6 with "::" and 5
words'],
+ [true, '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6
words'],
+
+ [false, 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8
words'],
+ [false, 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words']
+ ];
+
+ Array.prototype.push.apply( IPV6_CASES,
+ $.map( [
+ 'fc:100::',
+ 'fc:100:a::',
+ 'fc:100:a:d::',
+ 'fc:100:a:d:1::',
+ 'fc:100:a:d:1:e::',
+ 'fc:100:a:d:1:e:ac::',
+ '::0',
+ '::fc',
+ '::fc:100',
+ '::fc:100:a',
+ '::fc:100:a:d',
+ '::fc:100:a:d:1',
+ '::fc:100:a:d:1:e',
+ '::fc:100:a:d:1:e:ac',
+ 'fc:100:a:d:1:e:ac:0'
+ ], function ( el ) {
+ return [[ true, el, el + ' is a valid IP' ]];
+ } )
+ );
+
QUnit.module( 'mediawiki.util', QUnit.newMwEnvironment( {
setup: function () {
$.fn.updateTooltipAccessKeys.setTestMode( true );
@@ -243,95 +318,24 @@
} );
QUnit.test( 'isIPv6Address', 40, function ( assert ) {
- // Shortcuts
- function assertFalseIPv6( addy, summary ) {
- return assert.strictEqual( mw.util.isIPv6Address( addy
), false, summary );
- }
-
- function assertTrueIPv6( addy, summary ) {
- return assert.strictEqual( mw.util.isIPv6Address( addy
), true, summary );
- }
-
- // Based on IPTest.php > testisIPv6
- assertFalseIPv6( ':fc:100::', 'IPv6 starting with lone ":"' );
- assertFalseIPv6( 'fc:100:::', 'IPv6 ending with a ":::"' );
- assertFalseIPv6( 'fc:300', 'IPv6 with only 2 words' );
- assertFalseIPv6( 'fc:100:300', 'IPv6 with only 3 words' );
-
- $.each(
- ['fc:100::',
- 'fc:100:a::',
- 'fc:100:a:d::',
- 'fc:100:a:d:1::',
- 'fc:100:a:d:1:e::',
- 'fc:100:a:d:1:e:ac::'], function ( i, addy ) {
- assertTrueIPv6( addy, addy + ' is a valid IP' );
- } );
-
- assertFalseIPv6( 'fc:100:a:d:1:e:ac:0::', 'IPv6 with 8 words
ending with "::"' );
- assertFalseIPv6( 'fc:100:a:d:1:e:ac:0:1::', 'IPv6 with 9 words
ending with "::"' );
-
- assertFalseIPv6( ':::' );
- assertFalseIPv6( '::0:', 'IPv6 ending in a lone ":"' );
-
- assertTrueIPv6( '::', 'IPv6 zero address' );
- $.each(
- ['::0',
- '::fc',
- '::fc:100',
- '::fc:100:a',
- '::fc:100:a:d',
- '::fc:100:a:d:1',
- '::fc:100:a:d:1:e',
- '::fc:100:a:d:1:e:ac',
-
- 'fc:100:a:d:1:e:ac:0'], function ( i, addy ) {
- assertTrueIPv6( addy, addy + ' is a valid IP' );
- } );
-
- assertFalseIPv6( '::fc:100:a:d:1:e:ac:0', 'IPv6 with "::" and 8
words' );
- assertFalseIPv6( '::fc:100:a:d:1:e:ac:0:1', 'IPv6 with 9 words'
);
-
- assertFalseIPv6( ':fc::100', 'IPv6 starting with lone ":"' );
- assertFalseIPv6( 'fc::100:', 'IPv6 ending with lone ":"' );
- assertFalseIPv6( 'fc:::100', 'IPv6 with ":::" in the middle' );
-
- assertTrueIPv6( 'fc::100', 'IPv6 with "::" and 2 words' );
- assertTrueIPv6( 'fc::100:a', 'IPv6 with "::" and 3 words' );
- assertTrueIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' );
- assertTrueIPv6( 'fc::100:a:d:1', 'IPv6 with "::" and 5 words' );
- assertTrueIPv6( 'fc::100:a:d:1:e', 'IPv6 with "::" and 6 words'
);
- assertTrueIPv6( 'fc::100:a:d:1:e:ac', 'IPv6 with "::" and 7
words' );
- assertTrueIPv6( '2001::df', 'IPv6 with "::" and 2 words' );
- assertTrueIPv6( '2001:5c0:1400:a::df', 'IPv6 with "::" and 5
words' );
- assertTrueIPv6( '2001:5c0:1400:a::df:2', 'IPv6 with "::" and 6
words' );
-
- assertFalseIPv6( 'fc::100:a:d:1:e:ac:0', 'IPv6 with "::" and 8
words' );
- assertFalseIPv6( 'fc::100:a:d:1:e:ac:0:1', 'IPv6 with 9 words'
);
+ $.each( IPV6_CASES, function ( i, ipCase ) {
+ assert.strictEqual( mw.util.isIPv6Address( ipCase[1] ),
ipCase[0], ipCase[2] );
+ } );
} );
QUnit.test( 'isIPv4Address', 11, function ( assert ) {
- // Shortcuts
- function assertFalseIPv4( addy, summary ) {
- assert.strictEqual( mw.util.isIPv4Address( addy ),
false, summary );
- }
+ $.each( IPV4_CASES, function ( i, ipCase ) {
+ assert.strictEqual( mw.util.isIPv4Address( ipCase[1] ),
ipCase[0], ipCase[2] );
+ } );
+ } );
- function assertTrueIPv4( addy, summary ) {
- assert.strictEqual( mw.util.isIPv4Address( addy ),
true, summary );
- }
+ QUnit.test( 'isIPAddress', 51, function ( assert ) {
+ $.each( IPV4_CASES, function ( i, ipCase ) {
+ assert.strictEqual( mw.util.isIPv4Address( ipCase[1] ),
ipCase[0], ipCase[2] );
+ } );
- // Based on IPTest.php > testisIPv4
- assertFalseIPv4( false, 'Boolean false is not an IP' );
- assertFalseIPv4( true, 'Boolean true is not an IP' );
- assertFalseIPv4( '', 'Empty string is not an IP' );
- assertFalseIPv4( 'abc', '"abc" is not an IP' );
- assertFalseIPv4( ':', 'Colon is not an IP' );
- assertFalseIPv4( '124.24.52', 'IPv4 not enough quads' );
- assertFalseIPv4( '24.324.52.13', 'IPv4 out of range' );
- assertFalseIPv4( '.24.52.13', 'IPv4 starts with period' );
-
- assertTrueIPv4( '124.24.52.13', '124.24.52.134 is a valid IP' );
- assertTrueIPv4( '1.24.52.13', '1.24.52.13 is a valid IP' );
- assertFalseIPv4( '74.24.52.13/20', 'IPv4 ranges are not
recogzized as valid IPs' );
+ $.each( IPV6_CASES, function ( i, ipCase ) {
+ assert.strictEqual( mw.util.isIPv6Address( ipCase[1] ),
ipCase[0], ipCase[2] );
+ } );
} );
}( mediaWiki, jQuery ) );
--
To view, visit https://gerrit.wikimedia.org/r/197464
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I57e1f00d18a74147da3200be133a4ceb989b1b9d
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>
Gerrit-Reviewer: Daniel Friesen <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Mattflaschen <[email protected]>
Gerrit-Reviewer: Ricordisamoa <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits