Preserve existing strip_html behaviour for weird inputs

This commit is contained in:
Justin Li
2019-02-22 13:00:36 -05:00
parent 89c1ba2b0e
commit ed73794f82
2 changed files with 10 additions and 4 deletions
+7 -4
View File
@@ -11,12 +11,12 @@ module Liquid
"'".freeze => '''.freeze
}
HTML_ESCAPE_ONCE_REGEXP = /["><']|&(?!([a-zA-Z]+|(#\d+));)/
STRIP_HTML = Regexp.union(
STRIP_HTML_BLOCKS = Regexp.union(
/<script.*?<\/script>/m,
/<!--.*?-->/m,
/<style.*?<\/style>/m,
/<.*?>/m
/<style.*?<\/style>/m
)
STRIP_HTML_TAGS = /<.*?>/m
# Return the size of an array or of an string
def size(input)
@@ -108,7 +108,10 @@ module Liquid
end
def strip_html(input)
input.to_s.gsub(STRIP_HTML, ''.freeze)
empty = ''.freeze
result = input.to_s.gsub(STRIP_HTML_BLOCKS, empty)
result.gsub!(STRIP_HTML_TAGS, empty)
result
end
# Remove all newlines from the string
+3
View File
@@ -177,6 +177,9 @@ class StandardFiltersTest < Minitest::Test
assert_equal 'test', @filters.strip_html("<div\nclass='multiline'>test</div>")
assert_equal 'test', @filters.strip_html("<!-- foo bar \n test -->test")
assert_equal '', @filters.strip_html(nil)
# Quirk of the existing implementation
assert_equal 'foo;', @filters.strip_html("<<<script </script>script>foo;</script>")
end
def test_join