Skip to content
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

Unexpected Range Behaviors #1890

Open
microalps opened this issue Jan 14, 2025 · 2 comments
Open

Unexpected Range Behaviors #1890

microalps opened this issue Jan 14, 2025 · 2 comments

Comments

@microalps
Copy link

microalps commented Jan 14, 2025

There are two main issues being reported here:

  1. Strict mode vs not does not give the same behavior in simple ranges, where strict mode works and lax does not. In this example, there are extra spaces in the range.
    assert_equal '12345',
     Liquid::Template.parse('{%for item in (1 .. 5)%}{{item}}{%endfor%}').render
  1. Both strict and lax mode do not support range in a variable outside of for loop. This works in LiquidJs and DotLiquid
assert_equal '12345',
     Liquid::Template.parse('{{(1..5)}}', error_mode: :strict).render
  1. Variables with double dots trip the regex method of parsing ranges
  def test_range_dotted_variable
    nativedata = { 'paths' => { '..' => 1, '...' => 5 } }
    assert_equal '1 5',
     Liquid::Template.parse('{{ paths[".."] }} {{ paths["..."] }}', error_mode: :strict).render(nativedata)
    assert_equal '12345',
      Liquid::Template.parse('{%for item in (1..paths["..."]) %}{{item}}{%endfor%}', error_mode: :strict).render(nativedata)
    assert_equal '12345',
      Liquid::Template.parse('{%for item in (paths[".."]..paths["..."]) %}{{item}}{%endfor%}', error_mode: :strict).render(nativedata)
  end
@microalps
Copy link
Author

See https://replit.com/@microalps/LiquidUnitTests#main.rb for more detailed tests. Some issues are similar to dotliquid/dotliquid#565

@jg-rp
Copy link
Contributor

jg-rp commented Jan 14, 2025

  1. Both strict and lax mode do not support range in a variable outside of for loop. This works in LiquidJs and DotLiquid

Range expressions can appear outside a for loop, but they don't render as an array would.

require 'liquid'

source = <<LIQUID
{% assign x = 5 -%}
Some range: {{ (1..x) }}
LIQUID

template = Liquid::Template.parse(source, error_mode: :strict)
puts template.render!

output

Some range: 1..5

We can of course filter a range expression and it will behave like an array.

require 'liquid'

source = <<LIQUID
{% assign x = 5 -%}
Joined range: {{ (1..x) | join: '#' }}
LIQUID

template = Liquid::Template.parse(source, error_mode: :strict)
puts template.render!

output

Joined range: 1#2#3#4#5

There are some range test cases in the Golden Liquid test suite that might interest you, but it does not yet cover the other issues you've reported here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants