From: Timo Tijhof Date: Sat, 20 Jun 2015 05:46:24 +0000 (+0100) Subject: Introduce mediawiki.RegExp module X-Git-Tag: 1.31.0-rc.0~11024^2~1 X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=commitdiff_plain;h=8bee338d1cc83a4e0e52cc8530b2352169c1afb8;p=lhc%2Fweb%2Fwiklou.git Introduce mediawiki.RegExp module Simple module based on the $.escapeRE method of the old "jquery.mwExtension" module. Change-Id: I9e108a3f8c24d87bb239b63a14807a691c25aa3e --- diff --git a/maintenance/jsduck/categories.json b/maintenance/jsduck/categories.json index 7e73e64c58..96c05a1e8d 100644 --- a/maintenance/jsduck/categories.json +++ b/maintenance/jsduck/categories.json @@ -23,6 +23,7 @@ "classes": [ "mw.Title", "mw.Uri", + "mw.RegExp", "mw.messagePoster.*", "mw.notification", "mw.Notification_", diff --git a/resources/Resources.php b/resources/Resources.php index c280770856..0f2fc945e5 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1020,6 +1020,10 @@ return array( 'scripts' => 'resources/src/mediawiki/mediawiki.notify.js', 'targets' => array( 'desktop', 'mobile' ), ), + 'mediawiki.RegExp' => array( + 'scripts' => 'resources/src/mediawiki/mediawiki.RegExp.js', + 'targets' => array( 'desktop', 'mobile' ), + ), 'mediawiki.pager.tablePager' => array( 'styles' => 'resources/src/mediawiki/mediawiki.pager.tablePager.less', 'position' => 'top', diff --git a/resources/src/mediawiki/mediawiki.RegExp.js b/resources/src/mediawiki/mediawiki.RegExp.js new file mode 100644 index 0000000000..1da4ab4c22 --- /dev/null +++ b/resources/src/mediawiki/mediawiki.RegExp.js @@ -0,0 +1,22 @@ +( function ( mw ) { + /** + * @class mw.RegExp + */ + mw.RegExp = { + /** + * Escape string for safe inclusion in regular expression + * + * The following characters are escaped: + * + * \ { } ( ) | . ? * + - ^ $ [ ] + * + * @since 1.26 + * @static + * @param {string} str String to escape + * @return {string} Escaped string + */ + escape: function ( str ) { + return str.replace( /([\\{}()|.?*+\-\^$\[\]])/g, '\\$1' ); + } + }; +}( mediaWiki ) ); diff --git a/tests/qunit/QUnitTestResources.php b/tests/qunit/QUnitTestResources.php index 9093797aaa..c6f3a2311a 100644 --- a/tests/qunit/QUnitTestResources.php +++ b/tests/qunit/QUnitTestResources.php @@ -66,6 +66,7 @@ return array( 'tests/qunit/suites/resources/mediawiki/mediawiki.jqueryMsg.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.jscompat.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.messagePoster.factory.test.js', + 'tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.template.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.test.js', 'tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js', @@ -108,6 +109,7 @@ return array( 'mediawiki.api.watch', 'mediawiki.jqueryMsg', 'mediawiki.messagePoster', + 'mediawiki.RegExp', 'mediawiki.Title', 'mediawiki.toc', 'mediawiki.Uri', diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js new file mode 100644 index 0000000000..2388497a45 --- /dev/null +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.RegExp.test.js @@ -0,0 +1,38 @@ +( function ( mw, $ ) { + QUnit.module( 'mediawiki.RegExp' ); + + QUnit.test( 'escape', 16, function ( assert ) { + var specials, normal; + + specials = [ + '\\', + '{', + '}', + '(', + ')', + '[', + ']', + '|', + '.', + '?', + '*', + '+', + '-', + '^', + '$' + ]; + + normal = [ + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', + 'abcdefghijklmnopqrstuvwxyz', + '0123456789' + ].join( '' ); + + $.each( specials, function ( i, str ) { + assert.propEqual( str.match( new RegExp( mw.RegExp.escape( str ) ) ), [ str ], 'Match ' + str ); + } ); + + assert.equal( mw.RegExp.escape( normal ), normal, 'Alphanumerals are left alone' ); + } ); + +}( mediaWiki, jQuery ) );