convert template source to string to ensure encoding validity

This commit is contained in:
Michael Go
2024-01-10 16:50:31 -04:00
parent 730ad3684a
commit 6a0fe3f7e3
3 changed files with 7 additions and 5 deletions
+2 -1
View File
@@ -107,8 +107,9 @@ module Liquid
# Returns self for easy chaining
def parse(source, options = {})
parse_context = configure_options(options)
source = source.to_s.to_str
if source.is_a?(String) && !source.valid_encoding?
unless source.valid_encoding?
raise SyntaxError, parse_context.locale.t("errors.syntax.invalid_template_encoding")
end
+1 -1
View File
@@ -5,7 +5,7 @@ module Liquid
attr_reader :line_number, :for_liquid_tag
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
@source = source.to_s.to_str
@source = source
@line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag
@offset = 0
+4 -3
View File
@@ -350,8 +350,9 @@ class TemplateTest < Minitest::Test
assert_equal('Liquid syntax error: Invalid template encoding', e.message)
end
def test_allows_nil_as_source
template = Template.parse(nil)
assert_equal('', template.render)
def test_allows_non_string_values_as_source
assert_equal('', Template.parse(nil).render)
assert_equal('1', Template.parse(1).render)
assert_equal('true', Template.parse(true).render)
end
end