Fix overzealous matching in (remove|replace)-node-text scriptlets

Related issue:
https://github.com/uBlockOrigin/uBlock-issues/issues/2868
This commit is contained in:
Raymond Hill 2023-10-13 21:51:13 -04:00
parent be60ac33c2
commit e5c1b63315
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -100,11 +100,15 @@ function safeSelf() {
if ( details.matchAll ) { return true; }
return this.RegExp_test.call(details.re, haystack) === details.expect;
},
patternToRegex(pattern, flags = undefined) {
patternToRegex(pattern, flags = undefined, verbatim = false) {
if ( pattern === '' ) { return /^/; }
const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern);
if ( match === null ) {
return new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), flags);
const reStr = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
if ( verbatim ) {
return new RegExp(`^${reStr}$`, flags);
}
return new RegExp(reStr, flags);
}
try {
return new RegExp(match[1], match[2] || flags);
@ -552,7 +556,7 @@ function replaceNodeTextCore(
replacement = ''
) {
const safe = safeSelf();
const reNodeName = safe.patternToRegex(nodeName, 'i');
const reNodeName = safe.patternToRegex(nodeName, 'i', true);
const rePattern = safe.patternToRegex(pattern, 'gms');
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
const shouldLog = scriptletGlobals.has('canDebug') && extraArgs.log || 0;