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

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

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/liquid/condition.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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