Skip to content

Commit

Permalink
Fix: locomotivecms#171 looping was modifing the original attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Jan 27, 2020
1 parent 9be88e1 commit 50648b0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/locomotive/steam/liquid/tags/concerns/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ module Attributes
private

def parse_attributes(markup, default = {})
@attributes = default || {}
@raw_attributes = default || {}
attribute_markup = ""
if markup =~ /^ *([a-zA-Z0-9_.]*:.*)$/
attribute_markup = $1
elsif markup =~ /^[a-zA-Z0-9 _"']*, *(.*)$/
attribute_markup = $1
end
unless attribute_markup.blank?
@attributes.merge!(AttributeParser.parse(attribute_markup))
@raw_attributes.merge!(AttributeParser.parse(attribute_markup))
end
@attributes
@raw_attributes
end

def context_evaluate_array(vals)
Expand All @@ -41,7 +41,7 @@ def context_evaluate(vals)

def evaluate_attributes(context, lax: false)
@attributes = HashWithIndifferentAccess.new.tap do |hash|
attributes.each do |key, value|
raw_attributes.each do |key, value|
hash[evaluate_value(context, key, lax: lax)] = evaluate_value(context, value, lax: lax)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/locomotive/steam/liquid/tags/consume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def initialize(tag_name, markup, options)
super

if markup =~ Syntax
@variable_name, @url, attributes = $1.to_s, ::Liquid::Expression.parse($2), $3
@variable_name, @url, _attributes = $1.to_s, ::Liquid::Expression.parse($2), $3

parse_attributes(attributes)
parse_attributes(_attributes)
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
end
Expand Down
7 changes: 7 additions & 0 deletions spec/unit/liquid/tags/path_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@

it { is_expected.to eq '/fr/a-notre-sujet' }

context 'loop on several locale from variable' do
let(:assigns) { { 'about_us' => drop, 'langs' => ['en', 'fr'] } }
let(:source) { "{% for lang in langs %}{% path_to about_us, locale: lang %}|{% endfor %}" }

it { is_expected.to eq '/about-us|/fr/a-notre-sujet|' }
end

end

end
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/liquid/tags/with_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@

end

describe 'In a loop context, each scope should be evaluated correctly' do
let(:assigns) { {'list' => ['A', 'B', 'C']} }

let(:source) { "{% for key in list %}{% with_scope foo: key %}{% assign conditions = with_scope %}{% endwith_scope %}{{ conditions }}{% endfor %}" }

it { expect(output).to eq '{"foo"=>"A"}{"foo"=>"B"}{"foo"=>"C"}' }

end

end

end

0 comments on commit 50648b0

Please sign in to comment.