Skip to content

Commit

Permalink
fix pattern arg usage
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 24, 2022
1 parent d02e1fe commit 43b3d8d
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/scriptlets/trusted-replace-xhr-response.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
hit,
toRegExp,
objectToString,
getWildcardSymbol,
matchRequestProps,
Expand All @@ -9,7 +10,6 @@ import {
getMatchPropsData,
validateParsedData,
parseMatchProps,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
Expand All @@ -31,9 +31,9 @@ import {
*
* - pattern - optional, argument for matching contents of responseText that should be replaced. If set, `replacement` is required;
* possible values:
* - '*' to match all text content
* - string
* - regular expression
* - '*' to match all text content
* - replacement — optional, should be set if `pattern` is set. String to replace matched content with. Empty string to remove content.
* - propsToMatch — optional, string of space-separated properties to match for extra condition; possible props:
* - string or regular expression for matching the URL passed to `.open()` call;
Expand Down Expand Up @@ -87,8 +87,6 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
const nativeOpen = window.XMLHttpRequest.prototype.open;
const nativeSend = window.XMLHttpRequest.prototype.send;

const MATCH_ALL_CHARACTERS_REGEX = toRegExp();

let shouldReplace = false;
let xhrData;
let requestHeaders = [];
Expand Down Expand Up @@ -156,11 +154,11 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
return;
}

const parsedPattern = pattern === getWildcardSymbol()
? MATCH_ALL_CHARACTERS_REGEX
: pattern;
const patternRegexp = pattern === getWildcardSymbol()
? toRegExp
: toRegExp(pattern);

const modifiedContent = content.replace(parsedPattern, replacement);
const modifiedContent = content.replace(patternRegexp, replacement);

// Manually put required values into target XHR object
// as thisArg can't be redefined and XHR objects can't be (re)assigned or copied
Expand Down Expand Up @@ -228,14 +226,14 @@ trustedReplaceXhrResponse.names = [

trustedReplaceXhrResponse.injections = [
hit,
toRegExp,
objectToString,
getWildcardSymbol,
matchRequestProps,
getXhrData,
getMatchPropsData,
validateParsedData,
parseMatchProps,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
Expand Down

0 comments on commit 43b3d8d

Please sign in to comment.