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

Allow use of curlies in a properly quoted string #623

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Liquid
QuotedFragment = /#{QuotedString}|(?:[^\s,\|'"]|#{QuotedString})+/o
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
AnyStartingTag = /\{\{|\{\%/
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/om
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}(?:(?:[^'"{}]+|#{QuotedString})*?|.*?)#{VariableIncompleteEnd}/om
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is [^'"{}]+| needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean it could be (?:#{QuotedString})*?|.*?)? No, that would only save a quoted string at the beginning of a variable block (which normally begins with a variable name) and the following .*? would stop at the next } that might be in the next quoted string.

I included {} in the character class because it looked like the tokenizer has to be generous with curly brace mismatch for the "lax" parser to work.

TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/om
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/o

Expand Down
1 change: 1 addition & 0 deletions test/unit/tokenizer_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def test_tokenize_variables
assert_equal [' ', '{{funk}}', ' '], tokenize(' {{funk}} ')
assert_equal [' ', '{{funk}}', ' ', '{{so}}', ' ', '{{brother}}', ' '], tokenize(' {{funk}} {{so}} {{brother}} ')
assert_equal [' ', '{{ funk }}', ' '], tokenize(' {{ funk }} ')
assert_equal ['{{funk | replace: "}", \'}}\' }}'], tokenize('{{funk | replace: "}", \'}}\' }}')
end

def test_tokenize_blocks
Expand Down
6 changes: 6 additions & 0 deletions test/unit/variable_unit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def test_filters_without_whitespace
assert_equal [['replace', ['foo', 'bar']], ['textileze', []]], var.filters
end

def test_filters_with_properly_quoted_curlies
var = create_variable("hello | replace: \"}\", '}}'")
assert_equal VariableLookup.new('hello'), var.name
assert_equal [['replace', ['}', '}}']]], var.filters
end

def test_symbol
var = create_variable("http://disney.com/logo.gif | image: 'med' ", error_mode: :lax)
assert_equal VariableLookup.new('http://disney.com/logo.gif'), var.name
Expand Down