Skip to content

Commit

Permalink
Merge pull request #1783 from Shopify/contains-encoding
Browse files Browse the repository at this point in the history
Fallback to binary comparison when `contains` RHS is  UTF8 encoded
  • Loading branch information
ianks committed Jan 31, 2024
2 parents 9b38a15 + 1b2b629 commit 02ecaab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/liquid/condition.rb
Expand Up @@ -24,6 +24,9 @@ class Condition # :nodoc:
else
false
end
rescue Encoding::CompatibilityError
# "✅".b.include?("✅") raises Encoding::CompatibilityError despite being materially equal
left.b.include?(right.b)
end,
}

Expand Down
9 changes: 7 additions & 2 deletions test/unit/condition_unit_test.rb
Expand Up @@ -55,6 +55,11 @@ def test_contains_works_on_strings
assert_evaluates_false('bob', 'contains', '---')
end

def test_contains_binary_encoding_compatibility_with_utf8
assert_evaluates_true('🙈'.b, 'contains', '🙈')
assert_evaluates_true('🙈', 'contains', '🙈'.b)
end

def test_invalid_comparation_operator
assert_evaluates_argument_error(1, '~~', 0)
end
Expand Down Expand Up @@ -166,14 +171,14 @@ def test_default_context_is_deprecated
def assert_evaluates_true(left, op, right)
assert(
Condition.new(left, op, right).evaluate(@context),
"Evaluated false: #{left} #{op} #{right}",
"Evaluated false: #{left.inspect} #{op} #{right.inspect}",
)
end

def assert_evaluates_false(left, op, right)
assert(
!Condition.new(left, op, right).evaluate(@context),
"Evaluated true: #{left} #{op} #{right}",
"Evaluated true: #{left.inspect} #{op} #{right.inspect}",
)
end

Expand Down

0 comments on commit 02ecaab

Please sign in to comment.