Skip to content
Closed
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
60 changes: 60 additions & 0 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ module StandardFilters
%r{<style.*?</style>}m,
)
STRIP_HTML_TAGS = /<.*?>/m
UNICODE_WHITESPACE_LEFT = /\A[[:space:]]+/
UNICODE_WHITESPACE_RIGHT = /[[:space:]]+\z/
UNICODE_WHITESPACE_EDGES = Regexp.union(UNICODE_WHITESPACE_LEFT, UNICODE_WHITESPACE_RIGHT)
UNICODE_WHITESPACE_RUNS = /[[:space:]]+/
private_constant(
:UNICODE_WHITESPACE_EDGES,
:UNICODE_WHITESPACE_LEFT,
:UNICODE_WHITESPACE_RIGHT,
:UNICODE_WHITESPACE_RUNS,
)

class << self
def try_coerce_encoding(input, encoding:)
Expand Down Expand Up @@ -315,6 +325,56 @@ def squish(input)
Utils.to_s(input).strip.gsub(/\s+/, ' ')
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
# @liquid_summary
# Strips whitespace, including Unicode whitespace, from the left and right of a string.
# @liquid_syntax string | strip_whitespace
# @liquid_return [string]
def strip_whitespace(input)
input = Utils.to_s(input)
input.gsub(UNICODE_WHITESPACE_EDGES, ' ').strip
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
# @liquid_summary
# Strips whitespace, including Unicode whitespace, from the left of a string.
# @liquid_syntax string | lstrip_whitespace
# @liquid_return [string]
def lstrip_whitespace(input)
input = Utils.to_s(input)
input.gsub(UNICODE_WHITESPACE_LEFT, ' ').lstrip
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
# @liquid_summary
# Strips whitespace, including Unicode whitespace, from the right of a string.
# @liquid_syntax string | rstrip_whitespace
# @liquid_return [string]
def rstrip_whitespace(input)
input = Utils.to_s(input)
input.gsub(UNICODE_WHITESPACE_RIGHT, ' ').rstrip
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
# @liquid_summary
# Strips whitespace, including Unicode whitespace, from a string and collapses consecutive whitespace
# to a single ASCII space.
# @liquid_syntax string | squish_whitespace
# @liquid_return [string]
def squish_whitespace(input)
return if input.nil?

Utils.to_s(input).gsub(UNICODE_WHITESPACE_RUNS, ' ').strip
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
Expand Down
49 changes: 49 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ def test_squish_filter
assert_equal("", Liquid::Template.parse('{{ " " | squish }}').render)
end

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

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

def test_escape
assert_equal('&lt;strong&gt;', @filters.escape('<strong>'))
assert_equal('1', @filters.escape(1))
Expand Down Expand Up @@ -705,16 +718,52 @@ def test_strip
assert_template_result('ab c', "{{ source | strip }}", { 'source' => " \tab c \n \t" })
end

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

assert_template_result(
'ab c',
"{{ source | strip_whitespace }}",
{ 'source' => "#{unicode_spaces}ab c#{unicode_spaces}" },
)
assert_template_result("a\u00A0b\u202Fc", "{{ source | strip_whitespace }}", { 'source' => "a\u00A0b\u202Fc" })
assert_template_result("\u200Bfoo\u200B", "{{ source | strip_whitespace }}", { '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" })
end

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

assert_template_result(
"ab c#{unicode_spaces}",
"{{ source | lstrip_whitespace }}",
{ 'source' => "#{unicode_spaces}ab c#{unicode_spaces}" },
)
assert_template_result("a\u00A0b\u202Fc", "{{ source | lstrip_whitespace }}", { 'source' => "a\u00A0b\u202Fc" })
assert_template_result("\u200Bfoo\u200B", "{{ source | lstrip_whitespace }}", { 'source' => "\u200Bfoo\u200B" })
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" })
end

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

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

def test_strip_newlines
assert_template_result('abc', "{{ source | strip_newlines }}", { 'source' => "a\nb\nc" })
assert_template_result('abc', "{{ source | strip_newlines }}", { 'source' => "a\r\nb\nc" })
Expand Down
Loading