-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Prevent SelfDrop context mutation across render boundaries
#2082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karreiro
wants to merge
5
commits into
main
Choose a base branch
from
self-drop-renderer-mutation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−6
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c698474
Prevent `SelfDrop` context mutation across render boundaries
karreiro fd49bba
Renamed: test/integration/self_drop_test.rb -> test/integration/self_…
karreiro bc7f1ca
Remove support for contextualization
karreiro 8d8105a
Update lib/liquid/self_drop.rb
karreiro 3f5a7fa
Adopt 'undef context='
karreiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| class SelfDropContextTest < Minitest::Test | ||
| include Liquid | ||
|
|
||
| def test_self_drop_passed_as_render_param_preserves_original_scope | ||
| source = <<~LIQUID | ||
| {%- assign var = 42 -%} | ||
| {%- assign s = self -%} | ||
| {%- render "snippet1", other_self: s -%} | ||
| LIQUID | ||
|
|
||
| partials = { | ||
| 'snippet1' => <<~LIQUID, | ||
| {%- assign var = 43 -%} | ||
| {{- other_self.var }}|{{ self.var -}} | ||
| LIQUID | ||
| } | ||
|
|
||
| assert_template_result('42|43', source, partials: partials) | ||
| end | ||
|
|
||
| def test_self_drop_in_render_without_passing_resolves_inner_scope | ||
| source = <<~LIQUID | ||
| {%- assign var = 42 -%} | ||
| {%- render "snippet1" -%} | ||
| LIQUID | ||
|
|
||
| partials = { | ||
| 'snippet1' => <<~LIQUID, | ||
| {%- assign var = 99 -%} | ||
| {{- self.var -}} | ||
| LIQUID | ||
| } | ||
|
|
||
| assert_template_result('99', source, partials: partials) | ||
| end | ||
|
|
||
| def test_self_drop_passed_to_nested_renders_preserves_each_level | ||
| source = <<~LIQUID | ||
| {%- assign a = 1 -%} | ||
| {%- assign s1 = self -%} | ||
| {%- render "snippet1", outer: s1 -%} | ||
| LIQUID | ||
|
|
||
| partials = { | ||
| 'snippet1' => <<~LIQUID, | ||
| {%- assign a = 2 -%} | ||
| {%- assign s2 = self -%} | ||
| {%- render "snippet2", outer: outer, middle: s2 -%} | ||
| LIQUID | ||
| 'snippet2' => <<~LIQUID, | ||
| {%- assign a = 3 -%} | ||
| {{- outer.a }}|{{ middle.a }}|{{ self.a -}} | ||
| LIQUID | ||
| } | ||
|
|
||
| assert_template_result('1|2|3', source, partials: partials) | ||
| end | ||
|
|
||
| def test_self_drop_reflects_variables_assigned_after_creation | ||
| source = <<~LIQUID | ||
| {%- assign s = self -%} | ||
| {%- assign x = 42 %}{{ s.x -}} | ||
| LIQUID | ||
|
|
||
| assert_template_result('42', source) | ||
| end | ||
|
|
||
| def test_self_drop_context_setter_is_undefined | ||
| context = Context.new | ||
| drop = SelfDrop.new(context) | ||
| refute(drop.respond_to?(:context=)) | ||
|
|
||
| assert_template_result('42', '{{ self.x }}', { 'x' => 42 }) | ||
| end | ||
|
|
||
| def test_self_drop_with_strict_variables_does_not_raise_for_defined_var | ||
| t = Template.parse('{{ self.x }}') | ||
| result = t.render({ 'x' => 42 }, strict_variables: true) | ||
| assert_equal('42', result) | ||
| end | ||
|
|
||
| def test_self_drop_with_strict_variables_returns_nil_for_undefined_var | ||
| t = Template.parse('{{ self.x }}') | ||
| result = t.render({}, strict_variables: true) | ||
| assert_equal('', result) | ||
| end | ||
|
|
||
| def test_self_drop_can_be_passed_as_bare_drop_to_render | ||
| t = Template.parse('{{ self.x }}') | ||
| drop = SelfDrop.new(Context.new({ 'x' => 42 })) | ||
| result = t.render(drop) | ||
| assert_equal('42', result) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.