liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Escape charactors in string literals

Open tuchida opened this issue 3 years ago • 2 comments

Checklist

  • [x] I have searched the issue list
  • [x] I have tested my example against Shopify Liquid. (This isn't necessary if the actual behavior is a panic, or an error for which IsTemplateError returns false.)

Expected Behavior

{{ "a\"b\\c" }}

output

a"b\c

Actual Behavior

Syntax Error

Detailed Description

ref. #5

The Shopify specification does not allow escaping of characters in string literals. https://shopify.github.io/liquid/basics/types/#string

Liquid does not convert escape sequences into special characters.

But this is inconvenient for me.

Possible Solution

The proposal makes it escapable when ``. https://github.com/Shopify/liquid/pull/1174

This my branch uses strconv.Unquote to escape only double-quoted. https://github.com/osteele/liquid/compare/master...tuchida:escape-string

In this implementation, it seems that only \' and \" can be escaped. https://github.com/acstech/liquid/blob/5bcb5c9b2d87dbd09e8a08889477167721e09edf/core/parser.go#L135-L136

If you want to support escaping, you can decide on the specification and I will create a Pull Request.

tuchida avatar Jun 16 '21 00:06 tuchida

I'm wary of introducing a difference from the Shopify specification. Aside from this, this seems like an improvement, that would be useful for users who are not concerned about compatibility with the specification and with other implementations.

How about if this is controlled by a flag or option, that defaults to false?

This could be a (new) configuration parameter on NewEngine, or ParseString / ParseTemplate / ParseTemplateLocation.

osteele avatar Jun 17 '21 06:06 osteele

I'm not familiar with Ragel, but it looks like it would probably be difficult to branch here. https://github.com/osteele/liquid/blob/5d67c47c6c28f6a0beec7094775f55246f81acea/expressions/scanner.rl#L92-L93

I can implement it if it can enclose it in backtick and allow backtick here only when the flag is valid. https://github.com/osteele/liquid/blob/5d67c47c6c28f6a0beec7094775f55246f81acea/expressions/scanner.rl#L71

tuchida avatar Jun 17 '21 09:06 tuchida