mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Merge pull request #1774 from Shopify/check-utf8-validity
check template UTF8 validity before parsing
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
include: "Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]"
|
include: "Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]"
|
||||||
inline_comment_invalid: "Syntax error in tag '#' - Each line of comments must be prefixed by the '#' character"
|
inline_comment_invalid: "Syntax error in tag '#' - Each line of comments must be prefixed by the '#' character"
|
||||||
invalid_delimiter: "'%{tag}' is not a valid delimiter for %{block_name} tags. use %{block_delimiter}"
|
invalid_delimiter: "'%{tag}' is not a valid delimiter for %{block_name} tags. use %{block_delimiter}"
|
||||||
|
invalid_template_encoding: "Invalid template encoding"
|
||||||
render: "Syntax error in tag 'render' - Template name must be a quoted string"
|
render: "Syntax error in tag 'render' - Template name must be a quoted string"
|
||||||
table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3"
|
table_row: "Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3"
|
||||||
tag_never_closed: "'%{block_name}' tag was never closed"
|
tag_never_closed: "'%{block_name}' tag was never closed"
|
||||||
|
|||||||
@@ -107,6 +107,11 @@ module Liquid
|
|||||||
# Returns self for easy chaining
|
# Returns self for easy chaining
|
||||||
def parse(source, options = {})
|
def parse(source, options = {})
|
||||||
parse_context = configure_options(options)
|
parse_context = configure_options(options)
|
||||||
|
|
||||||
|
unless source.valid_encoding?
|
||||||
|
raise SyntaxError, parse_context.locale.t("errors.syntax.invalid_template_encoding")
|
||||||
|
end
|
||||||
|
|
||||||
tokenizer = parse_context.new_tokenizer(source, start_line_number: @line_numbers && 1)
|
tokenizer = parse_context.new_tokenizer(source, start_line_number: @line_numbers && 1)
|
||||||
@root = Document.parse(tokenizer, parse_context)
|
@root = Document.parse(tokenizer, parse_context)
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -337,4 +337,16 @@ class TemplateTest < Minitest::Test
|
|||||||
assert_equal("x=2", output)
|
assert_equal("x=2", output)
|
||||||
assert_instance_of(String, output)
|
assert_instance_of(String, output)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_raises_error_with_invalid_utf8
|
||||||
|
e = assert_raises(SyntaxError) do
|
||||||
|
Template.parse(<<~LIQUID)
|
||||||
|
{% comment %}
|
||||||
|
\xC0
|
||||||
|
{% endcomment %}
|
||||||
|
LIQUID
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal('Liquid syntax error: Invalid template encoding', e.message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user