From d2827bfa76801d0e8b104ab28703ce86164d5253 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Fri, 24 Jan 2014 11:04:13 -0500 Subject: [PATCH] Add strip, lstrip, rstrip filters --- History.md | 1 + lib/liquid/standardfilters.rb | 12 ++++++++++++ test/liquid/standard_filter_test.rb | 15 +++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/History.md b/History.md index 6cd27fb..b57c71a 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ ## 3.0.0 / not yet released / branch "master" * ... +* Add strip, lstrip, and rstrip to standard filters [Florian Weingarten, fw42] * Make if, for & case tags return complete and consistent nodelists, see #250 [Nick Jones, dntj] * Prevent arbitrary method invocation on condition objects, see #274 [Dylan Thacker-Smith, dylanahsmith] * Don't call to_sym when creating conditions for security reasons, see #273 [Bouke van der Bijl, bouk] diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 027c0b3..d48c408 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -63,6 +63,18 @@ module Liquid input.split(pattern) end + def strip(input) + input.to_s.strip + end + + def lstrip(input) + input.to_s.lstrip + end + + def rstrip(input) + input.to_s.rstrip + end + def strip_html(input) input.to_s.gsub(//m, '').gsub(//m, '').gsub(//m, '').gsub(/<.*?>/m, '') end diff --git a/test/liquid/standard_filter_test.rb b/test/liquid/standard_filter_test.rb index b03f605..d5eb78b 100644 --- a/test/liquid/standard_filter_test.rb +++ b/test/liquid/standard_filter_test.rb @@ -209,6 +209,21 @@ class StandardFiltersTest < Test::Unit::TestCase assert_template_result 'foobar', "{{ 'foo|bar' | remove: '|' }}" end + 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" + 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_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_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"