From 0ad2b11ab15e7da608a9ef936b2a00a6a6517038 Mon Sep 17 00:00:00 2001 From: Joe Cottam <44173086+joecottam@users.noreply.github.com> Date: Wed, 25 Mar 2026 17:18:40 +0000 Subject: [PATCH] fix: handle undefined replacement argument in replace filter (#864) --- src/filters/string.ts | 1 + test/integration/filters/string.spec.ts | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/filters/string.ts b/src/filters/string.ts index fe012fb2f..3ef570566 100644 --- a/src/filters/string.ts +++ b/src/filters/string.ts @@ -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) } diff --git a/test/integration/filters/string.spec.ts b/test/integration/filters/string.spec.ts index 9ea5bd088..1f0a93a33 100644 --- a/test/integration/filters/string.spec.ts +++ b/test/integration/filters/string.spec.ts @@ -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" }}',