forked from Shopify/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable rendering of tag based on register (Shopify#1162)
* Disable rendering of tag based on register * Improvements to disable tag * Resolve disbale tag tests * Test disable_tags register * disabled_tags is now always avaiable * Allow multiple tags to be disabled at once * Move disabled check to block_body * Code improvements * Remove redundant nil check * Improve disabled tag error output * Improve disable tag API * Code improvements * Switch disabled? to not mutate output * Fix array handling shortcut in disable_tags
- Loading branch information
Showing
12 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Liquid | ||
class Register | ||
end | ||
end |
This file contains 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,32 @@ | ||
# frozen_string_literal: true | ||
module Liquid | ||
class DisabledTags < Register | ||
def initialize | ||
@disabled_tags = {} | ||
end | ||
|
||
def disabled?(tag) | ||
@disabled_tags.key?(tag) && @disabled_tags[tag] > 0 | ||
end | ||
|
||
def disable(tags) | ||
tags.each(&method(:increment)) | ||
yield | ||
ensure | ||
tags.each(&method(:decrement)) | ||
end | ||
|
||
private | ||
|
||
def increment(tag) | ||
@disabled_tags[tag] ||= 0 | ||
@disabled_tags[tag] += 1 | ||
end | ||
|
||
def decrement(tag) | ||
@disabled_tags[tag] -= 1 | ||
end | ||
end | ||
|
||
Template.add_register('disabled_tags', DisabledTags.new) | ||
end |
This file contains 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 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 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 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 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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class DisabledTagsTest < Minitest::Test | ||
include Liquid | ||
|
||
class DisableRaw < Block | ||
disable_tags "raw" | ||
end | ||
|
||
class DisableRawEcho < Block | ||
disable_tags "raw", "echo" | ||
end | ||
|
||
def test_disables_raw | ||
with_custom_tag('disable', DisableRaw) do | ||
assert_template_result 'raw usage is not allowed in this contextfoo', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}' | ||
end | ||
end | ||
|
||
def test_disables_echo_and_raw | ||
with_custom_tag('disable', DisableRawEcho) do | ||
assert_template_result 'raw usage is not allowed in this contextecho usage is not allowed in this context', '{% disable %}{% raw %}Foobar{% endraw %}{% echo "foo" %}{% enddisable %}' | ||
end | ||
end | ||
end |
This file contains 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 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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class DisabledTagsUnitTest < Minitest::Test | ||
include Liquid | ||
|
||
def test_disables_tag_specified | ||
register = DisabledTags.new | ||
register.disable(%w(foo bar)) do | ||
assert_equal true, register.disabled?("foo") | ||
assert_equal true, register.disabled?("bar") | ||
assert_equal false, register.disabled?("unknown") | ||
end | ||
end | ||
|
||
def test_disables_nested_tags | ||
register = DisabledTags.new | ||
register.disable(["foo"]) do | ||
register.disable(["foo"]) do | ||
assert_equal true, register.disabled?("foo") | ||
assert_equal false, register.disabled?("bar") | ||
end | ||
register.disable(["bar"]) do | ||
assert_equal true, register.disabled?("foo") | ||
assert_equal true, register.disabled?("bar") | ||
register.disable(["foo"]) do | ||
assert_equal true, register.disabled?("foo") | ||
assert_equal true, register.disabled?("bar") | ||
end | ||
end | ||
assert_equal true, register.disabled?("foo") | ||
assert_equal false, register.disabled?("bar") | ||
end | ||
end | ||
end |