fix: handle undefined replacement argument in replace filter (#864)

This commit is contained in:
Joe Cottam
2026-03-26 01:18:40 +08:00
committed by GitHub
parent 97d829116c
commit 0ad2b11ab1
2 changed files with 9 additions and 0 deletions
+1
View File
@@ -137,6 +137,7 @@ export function capitalize (this: FilterImpl, str: string) {
export function replace (this: FilterImpl, v: string, pattern: string, replacement: string) {
const str = stringify(v)
pattern = stringify(pattern)
replacement = stringify(replacement)
this.context.memoryLimit.use(str.length + pattern.length + replacement.length)
return str.split(pattern).join(replacement)
}
+8
View File
@@ -109,6 +109,14 @@ describe('filters/string', function () {
return test('{{ "Take my protein pills and put my helmet on" | replace: "my", "your" }}',
'Take your protein pills and put your helmet on')
})
it('should support replace with undefined replacement', function () {
return test('{{ "Take my protein pills and put my helmet on" | replace: "my" }}',
'Take protein pills and put helmet on')
})
it('should support replace with undefined variable as replacement', function () {
return test('{{ "Take my protein pills and put my helmet on" | replace: "my", missing_variable }}',
'Take protein pills and put helmet on')
})
it('should support replace_first', function () {
return test('{% assign my_string = "Take my protein pills and put my helmet on" %}\n' +
'{{ my_string | replace_first: "my", "your" }}',