mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 19:30:47 -07:00
Use replacement string for replace filters literally (#924)
This commit is contained in:
committed by
GitHub
parent
44eaa4b9d8
commit
27c91203ab
@@ -201,12 +201,14 @@ module Liquid
|
|||||||
|
|
||||||
# Replace occurrences of a string with another
|
# Replace occurrences of a string with another
|
||||||
def replace(input, string, replacement = ''.freeze)
|
def replace(input, string, replacement = ''.freeze)
|
||||||
input.to_s.gsub(string.to_s, replacement.to_s)
|
replacement = replacement.to_s
|
||||||
|
input.to_s.gsub(string.to_s) { replacement }
|
||||||
end
|
end
|
||||||
|
|
||||||
# Replace the first occurrences of a string with another
|
# Replace the first occurrences of a string with another
|
||||||
def replace_first(input, string, replacement = ''.freeze)
|
def replace_first(input, string, replacement = ''.freeze)
|
||||||
input.to_s.sub(string.to_s, replacement.to_s)
|
replacement = replacement.to_s
|
||||||
|
input.to_s.sub(string.to_s) { replacement }
|
||||||
end
|
end
|
||||||
|
|
||||||
# remove a substring
|
# remove a substring
|
||||||
|
|||||||
@@ -362,8 +362,10 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
def test_replace
|
def test_replace
|
||||||
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', '1', 2)
|
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', '1', 2)
|
||||||
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', 1, 2)
|
assert_equal '2 2 2 2', @filters.replace('1 1 1 1', 1, 2)
|
||||||
|
assert_equal "\\& \\& \\& \\&", @filters.replace('1 1 1 1', '1', "\\&")
|
||||||
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', '1', 2)
|
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', '1', 2)
|
||||||
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2)
|
assert_equal '2 1 1 1', @filters.replace_first('1 1 1 1', 1, 2)
|
||||||
|
assert_equal '\\& 1 1 1', @filters.replace_first("1 1 1 1", '1', "\\&")
|
||||||
assert_template_result '2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}"
|
assert_template_result '2 1 1 1', "{{ '1 1 1 1' | replace_first: '1', 2 }}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user