Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.05k stars 1.38k forks source link

Fallback to binary comparison when `contains` RHS is UTF8 encoded #1783

Closed ianks closed 8 months ago

ianks commented 8 months ago

Base64 decoding of strings always results in a Encoding::BINARY string result. Unfortunately, this means the contains operator will throw an Encoding::CompatibilityError whenever we attempt to compare the decoded result against a UTF8 string.

{% assign cuties = "🙈 🙉" | base64_encode | base64_decode %}

{% if cuties contains "🙈" %}
  😻
{% endif %}

Today, the result of the above templates is:

Liquid error (templates/index line 3): internal

But what about this template?

{% assign alphabet = "abcdef 🔤" | base64_encode | base64_decode %}

{% if alphabet contains "abc" %}
  Next time won't you sing with me?
{% endif %}

If you don't know off the top of your head, you're in good company. Neither did I.

  Next time won't you sing with me?

Obviously, throwing an internal error is not ideal. Instead, let's fallback to binary comparison of the string since that seems to me like the most "unsuprising" thing to do here.