-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplace.js
44 lines (42 loc) · 1.6 KB
/
replace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const isSpecialRegex = /(\$\$)|(\$&)|(\$`)|(\$')|(\$\d)/
function getReplace (addon) {
function isSpecialReplacement (replacer) {
return replacer.indexOf('$') > -1 && !!replacer.match(isSpecialRegex)
}
function shouldBePropagated (transactionId, thisArg, replacer) {
return addon.isTainted(transactionId, thisArg, replacer) && !isSpecialReplacement(replacer)
}
if (addon.replace) {
return addon.replace
}
return function replace (transactionId, result, thisArg, matcher, replacer) {
if (transactionId && typeof thisArg === 'string' && typeof replacer === 'string') {
if (typeof matcher === 'string') {
if (shouldBePropagated(transactionId, thisArg, replacer)) {
const index = thisArg.indexOf(matcher)
if (index > -1) {
return addon.replaceStringByString(transactionId, result, thisArg, matcher, replacer, index)
}
}
} else if (matcher instanceof RegExp) {
if (shouldBePropagated(transactionId, thisArg, replacer)) {
const replacements = []
let lastIndex = -1
for (let match = matcher.exec(thisArg), i = 0; match != null; match = matcher.exec(thisArg), i++) {
const index = match.index
if (index !== lastIndex) {
replacements.push([index, match[0]])
lastIndex = index
} else {
break
}
}
return addon.replaceStringByStringUsingRegex(transactionId, result, thisArg, matcher,
replacer, replacements)
}
}
}
return result
}
}
module.exports = getReplace