From 4f082a96bc33ac9b8740aa8b5be7cade6a13b81b Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 8 Aug 2023 12:20:03 -0400 Subject: [PATCH] Support negated pattern for `*[pP]ropsToMatch` values in scriptlets --- assets/resources/scriptlets.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/assets/resources/scriptlets.js b/assets/resources/scriptlets.js index f14d8006c..55dabc892 100644 --- a/assets/resources/scriptlets.js +++ b/assets/resources/scriptlets.js @@ -66,13 +66,14 @@ function safeSelf() { if ( pattern === '' ) { return { matchAll: true }; } - const expect = (options.canNegate && pattern.startsWith('!') === false); + const expect = (options.canNegate === true && pattern.startsWith('!') === false); if ( expect === false ) { pattern = pattern.slice(1); } const match = /^\/(.+)\/([gimsu]*)$/.exec(pattern); if ( match !== null ) { return { + pattern, re: new this.RegExp( match[1], match[2] || options.flags @@ -81,6 +82,7 @@ function safeSelf() { }; } return { + pattern, re: new this.RegExp(pattern.replace( /[.*+?^${}()|[\]\\]/g, '\\$&'), options.flags @@ -882,13 +884,14 @@ function parsePropertiesToMatch(propsToMatch, implicit = '') { const safe = safeSelf(); const needles = new Map(); if ( propsToMatch === undefined || propsToMatch === '' ) { return needles; } + const options = { canNegate: true }; for ( const needle of propsToMatch.split(/\s+/) ) { const [ prop, pattern ] = needle.split(':'); if ( prop === '' ) { continue; } if ( pattern !== undefined ) { - needles.set(prop, { pattern, re: safe.patternToRegex(pattern) }); + needles.set(prop, safe.initPattern(pattern, options)); } else if ( implicit !== '' ) { - needles.set(implicit, { pattern: prop, re: safe.patternToRegex(prop) }); + needles.set(implicit, safe.initPattern(prop, options)); } } return needles; @@ -899,6 +902,9 @@ function parsePropertiesToMatch(propsToMatch, implicit = '') { builtinScriptlets.push({ name: 'match-object-properties.fn', fn: matchObjectProperties, + dependencies: [ + 'safe-self.fn', + ], }); function matchObjectProperties(propNeedles, ...objs) { if ( matchObjectProperties.extractProperties === undefined ) { @@ -910,6 +916,7 @@ function matchObjectProperties(propNeedles, ...objs) { } }; } + const safe = safeSelf(); const haystack = {}; const props = Array.from(propNeedles.keys()); for ( const obj of objs ) { @@ -922,9 +929,9 @@ function matchObjectProperties(propNeedles, ...objs) { if ( typeof value !== 'string' ) { try { value = JSON.stringify(value); } catch(ex) { } + if ( typeof value !== 'string' ) { continue; } } - if ( typeof value !== 'string' ) { continue; } - if ( details.re.test(value) ) { continue; } + if ( safe.testPattern(details, value) ) { continue; } return false; } return true;