mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
Fix support for using a String subclass for the liquid source (#1421)
This commit is contained in:
@@ -5,7 +5,7 @@ module Liquid
|
|||||||
attr_reader :line_number, :for_liquid_tag
|
attr_reader :line_number, :for_liquid_tag
|
||||||
|
|
||||||
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
|
def initialize(source, line_numbers = false, line_number: nil, for_liquid_tag: false)
|
||||||
@source = source
|
@source = source.to_s.to_str
|
||||||
@line_number = line_number || (line_numbers ? 1 : nil)
|
@line_number = line_number || (line_numbers ? 1 : nil)
|
||||||
@for_liquid_tag = for_liquid_tag
|
@for_liquid_tag = for_liquid_tag
|
||||||
@tokens = tokenize
|
@tokens = tokenize
|
||||||
@@ -24,7 +24,7 @@ module Liquid
|
|||||||
private
|
private
|
||||||
|
|
||||||
def tokenize
|
def tokenize
|
||||||
return [] if @source.to_s.empty?
|
return [] if @source.empty?
|
||||||
|
|
||||||
return @source.split("\n") if @for_liquid_tag
|
return @source.split("\n") if @for_liquid_tag
|
||||||
|
|
||||||
|
|||||||
@@ -323,4 +323,18 @@ class TemplateTest < Minitest::Test
|
|||||||
result = t.render('x' => 1, 'y' => 5)
|
result = t.render('x' => 1, 'y' => 5)
|
||||||
assert_equal('12345', result)
|
assert_equal('12345', result)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_source_string_subclass
|
||||||
|
string_subclass = Class.new(String) do
|
||||||
|
# E.g. ActiveSupport::SafeBuffer does this, so don't just rely on to_s to return a String
|
||||||
|
def to_s
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
source = string_subclass.new("{% assign x = 2 -%} x= {{- x }}")
|
||||||
|
assert_instance_of(string_subclass, source)
|
||||||
|
output = Template.parse(source).render!
|
||||||
|
assert_equal("x=2", output)
|
||||||
|
assert_instance_of(String, output)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user