Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ module StandardFilters
%r{<style.*?</style>}m,
)
STRIP_HTML_TAGS = /<.*?>/m
# Use POSIX whitespace matching so filters handle whitespace beyond Ruby String#strip's ASCII set.
WHITESPACE_LEFT = /\A[[:space:]]+/
WHITESPACE_RIGHT = /[[:space:]]+\z/
WHITESPACE_EDGES = Regexp.union(WHITESPACE_LEFT, WHITESPACE_RIGHT)
# Optimized runs regex to find 2 or more [[:space:]] OR a single [[:space:]]
# that isn't already `" " `.
WHITESPACE_RUNS = /([[:space:]]{2,}|[[[:space:]]&&[^ ]])/
private_constant(
:WHITESPACE_EDGES,
:WHITESPACE_LEFT,
:WHITESPACE_RIGHT,
:WHITESPACE_RUNS,
)

class << self
def try_coerce_encoding(input, encoding:)
Expand Down Expand Up @@ -312,7 +325,7 @@ def split(input, pattern)
def squish(input)
return if input.nil?

Utils.to_s(input).strip.gsub(/\s+/, ' ')
Utils.to_s(input).gsub(WHITESPACE_RUNS, ' ').strip
end

# @liquid_public_docs
Expand All @@ -324,7 +337,7 @@ def squish(input)
# @liquid_return [string]
def strip(input)
input = Utils.to_s(input)
input.strip
input.gsub(WHITESPACE_EDGES, ' ').strip
end

# @liquid_public_docs
Expand All @@ -336,7 +349,7 @@ def strip(input)
# @liquid_return [string]
def lstrip(input)
input = Utils.to_s(input)
input.lstrip
input.gsub(WHITESPACE_LEFT, ' ').lstrip
end

# @liquid_public_docs
Expand All @@ -348,7 +361,7 @@ def lstrip(input)
# @liquid_return [string]
def rstrip(input)
input = Utils.to_s(input)
input.rstrip
input.gsub(WHITESPACE_RIGHT, ' ').rstrip
end

# @liquid_public_docs
Expand Down
35 changes: 35 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ def test_squish_filter
\t boo " | squish }})).render)
assert_equal("", Liquid::Template.parse('{{ nil | squish }}').render)
assert_equal("", Liquid::Template.parse('{{ " " | squish }}').render)

unicode_spaces = "\u00A0\u202F\u2009\u2007"

assert_template_result(
"foo bar boo",
"{{ source | squish }}",
{ 'source' => "#{unicode_spaces}foo\u202F\u2009bar\t\n\u2007boo#{unicode_spaces}" },
)
assert_template_result("\u200Bfoo\u200B", "{{ source | squish }}", { 'source' => "\u200Bfoo\u200B" })
end

def test_escape
Expand Down Expand Up @@ -703,16 +712,42 @@ def test_pipes_in_string_arguments
def test_strip
assert_template_result('ab c', "{{ source | strip }}", { 'source' => " ab c " })
assert_template_result('ab c', "{{ source | strip }}", { 'source' => " \tab c \n \t" })

unicode_spaces = "\u00A0\u202F\u2009\u2007"

assert_template_result(
'ab c',
"{{ source | strip }}",
{ 'source' => "#{unicode_spaces}ab c#{unicode_spaces}" },
)
assert_template_result("a\u00A0b\u202Fc", "{{ source | strip }}", { 'source' => "a\u00A0b\u202Fc" })
assert_template_result("\u200Bfoo\u200B", "{{ source | strip }}", { 'source' => "\u200Bfoo\u200B" })
end

def test_lstrip
assert_template_result('ab c ', "{{ source | lstrip }}", { 'source' => " ab c " })
assert_template_result("ab c \n \t", "{{ source | lstrip }}", { 'source' => " \tab c \n \t" })

unicode_spaces = "\u00A0\u202F\u2009\u2007"

assert_template_result(
"ab c#{unicode_spaces}",
"{{ source | lstrip }}",
{ 'source' => "#{unicode_spaces}ab c#{unicode_spaces}" },
)
end

def test_rstrip
assert_template_result(" ab c", "{{ source | rstrip }}", { 'source' => " ab c " })
assert_template_result(" \tab c", "{{ source | rstrip }}", { 'source' => " \tab c \n \t" })

unicode_spaces = "\u00A0\u202F\u2009\u2007"

assert_template_result(
"#{unicode_spaces}ab c",
"{{ source | rstrip }}",
{ 'source' => "#{unicode_spaces}ab c#{unicode_spaces}" },
)
end

def test_strip_newlines
Expand Down
Loading