Use boolean RegExp.test() instead of .match() where possible

.test() is the dedicated syntax for a boolean "does match? yes/no?"
check. .match() returns an array of matches, or null. This is just not
needed in these situations.

Change-Id: Ibb996ab843d1a6c7d7af98d6a112990665d543b2
This commit is contained in:
Thiemo Kreuz 2022-01-13 12:10:02 +01:00
parent dbf21a69c1
commit c8af207681
10 changed files with 20 additions and 21 deletions

View file

@ -115,7 +115,7 @@ ve.ce.MWTransclusionNode.static.filterRendering = function ( contentNodes ) {
} );
function isWhitespaceNode( node ) {
return node && node.nodeType === Node.TEXT_NODE && !!node.data.match( whitespaceRegex );
return node && node.nodeType === Node.TEXT_NODE && whitespaceRegex.test( node.data );
}
while ( isWhitespaceNode( contentNodes[ 0 ] ) ) {

View file

@ -325,34 +325,32 @@ ve.dm.MWTransclusionNode.static.escapeParameter = function ( param ) {
if ( inNowiki ) {
if ( match[ 0 ] === '</nowiki>' ) {
inNowiki = false;
output += match[ 0 ];
} else {
output += match[ 0 ];
}
output += match[ 0 ];
} else {
var needsNowiki = true;
if ( match[ 0 ] === '<nowiki>' ) {
inNowiki = true;
needsNowiki = false;
} else if ( match[ 0 ] === '</nowiki>' || match[ 0 ].match( /<nowiki\s*\/>/ ) ) {
} else if ( match[ 0 ] === '</nowiki>' || /<nowiki\s*\/>/.test( match[ 0 ] ) ) {
needsNowiki = false;
} else if ( match[ 0 ].match( /(?:\[\[)/ ) ) {
} else if ( /(?:\[\[)/.test( match[ 0 ] ) ) {
linkStack++;
needsNowiki = false;
} else if ( match[ 0 ].match( /(?:\]\])/ ) ) {
} else if ( /(?:\]\])/.test( match[ 0 ] ) ) {
if ( linkStack > 0 ) {
linkStack--;
needsNowiki = false;
}
} else if ( match[ 0 ].match( /(?:\{\{)/ ) ) {
} else if ( /(?:\{\{)/.test( match[ 0 ] ) ) {
bracketStack++;
needsNowiki = false;
} else if ( match[ 0 ].match( /(?:\}\})/ ) ) {
} else if ( /(?:\}\})/.test( match[ 0 ] ) ) {
if ( bracketStack > 0 ) {
bracketStack--;
needsNowiki = false;
}
} else if ( match[ 0 ].match( /\|+/ ) ) {
} else if ( /\|+/.test( match[ 0 ] ) ) {
if ( bracketStack > 0 || linkStack > 0 ) {
needsNowiki = false;
}

View file

@ -36,7 +36,8 @@ ve.dm.MWWikitextSurfaceFragment.prototype.hasMatchingAncestor = function ( type,
// TODO: Use a registry to do this matching
switch ( type ) {
case 'paragraph':
all = !text.match( /^ |^=|^<blockquote>/ );
// Anything but what's matched below
all = !/^ |^=|^<blockquote>/.test( text );
break;
case 'mwPreformatted':
all = text.slice( 0, 1 ) === ' ';
@ -45,8 +46,8 @@ ve.dm.MWWikitextSurfaceFragment.prototype.hasMatchingAncestor = function ( type,
all = text.slice( 0, 12 ) === '<blockquote>';
break;
case 'mwHeading':
all = text.match( new RegExp( '^={' + attributes.level + '}[^=]' ) ) &&
text.match( new RegExp( '[^=]={' + attributes.level + '}$' ) );
all = new RegExp( '^={' + attributes.level + '}[^=]' ).test( text ) &&
new RegExp( '[^=]={' + attributes.level + '}$' ).test( text );
break;
default:
all = false;

View file

@ -475,7 +475,7 @@
// We can detect that case by `content` being empty, and not retry.
if ( useRestbase && resp.visualeditor.content && (
!resp.visualeditor.etag ||
!resp.visualeditor.etag.match( etagRegexp )
!etagRegexp.test( resp.visualeditor.etag )
) ) {
// Direct request to RESTBase returned a mangled or missing etag.
// Retry via the MediaWiki API.

View file

@ -87,7 +87,7 @@ mw.libs.ve.restbaseIdRegExp = /^mw[a-zA-Z0-9\-_]{2,6}$/;
mw.libs.ve.stripRestbaseIds = function ( doc ) {
var restbaseIdRegExp = mw.libs.ve.restbaseIdRegExp;
Array.prototype.forEach.call( doc.querySelectorAll( '[id^="mw"]' ), function ( element ) {
if ( element.id.match( restbaseIdRegExp ) ) {
if ( restbaseIdRegExp.test( element.id ) ) {
element.removeAttribute( 'id' );
}
} );

View file

@ -813,9 +813,9 @@ ve.ui.MWMediaDialog.prototype.getLicenseIcon = function ( license ) {
// FIXME: Structured data from Commons will make this properly
// multilingual. For now, this is the limit of what is sensible.
if ( normalized.match( /^((cc )?pd|public domain)/ ) ) {
if ( /^((cc )?pd|public domain)/.test( normalized ) ) {
return 'public-domain';
} else if ( normalized.match( /^cc (by|sa)?/ ) ) {
} else if ( /^cc (by|sa)?/.test( normalized ) ) {
return 'logoCC';
} else {
return 'info';

View file

@ -221,7 +221,7 @@ ve.ui.MWSaveDialog.prototype.showPreview = function ( docOrMsg, baseDoc ) {
} );
// Remove skin-specific modules (T187075 / T185284)
modules = modules.filter( function ( module ) {
return ( module.match( /^(skins|mediawiki\.skinning)\./ ) === null );
return !/^(skins|mediawiki\.skinning)\./.test( module );
} );
mw.loader.using( modules );
var body = docOrMsg.body;

View file

@ -87,7 +87,7 @@ ve.ui.wikitextCommandRegistry.register(
/* unwrapOffsetsCallback */
function ( text ) {
/* Text is only italic if there are 2 or 5+ apostrophes */
var matches = text.match( /^(''([^'].*|.*[^'])''|'{5,}([^'].*|.*[^'])'{5,})$/ );
var matches = /^(''([^'].*|.*[^'])''|'{5,}([^'].*|.*[^'])'{5,})$/.test( text );
return matches ? [ 2, 2 ] : null;
}

View file

@ -89,7 +89,7 @@ ve.ui.MWEditSummaryWidget.static.getMatchingSummaries = function ( summaries, qu
summaryPrefixMatches.push( summary );
}
} else if ( index !== -1 ) {
if ( lowerSummary[ index - 1 ].match( /\s/ ) ) {
if ( /\s/.test( lowerSummary[ index - 1 ] ) ) {
// Character before match is whitespace
wordPrefixMatches.push( summary );
} else {

View file

@ -60,7 +60,7 @@ ve.ui.MWMediaInfoFieldWidget = function VeUiMWMediaInfoFieldWidget( content, con
// For the cases where we get urls that are "local"
// without http(s) prefix, we will add that prefix
// ourselves
!config.href.match( /^(https?:)?\/\// ) ?
!/^(https?:)?\/\//.test( config.href ) ?
'//' + config.href :
config.href
)