Skip to content

Commit

Permalink
Fix overzealous matching in (remove|replace)-node-text scriptlets
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 14, 2023
1 parent be60ac3 commit e5c1b63
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e5c1b63

Please sign in to comment.