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

Add url_add_param and url_remove_param filters #1642

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
31 changes: 31 additions & 0 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'cgi'
require 'base64'
require 'bigdecimal'
require 'uri'

module Liquid
module StandardFilters
Expand Down Expand Up @@ -99,6 +100,36 @@ def escape_once(input)
input.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP, HTML_ESCAPE)
end

def url_add_param(input, key, value)
begin
uri = URI.parse(input)
uri.query = URI.encode_www_form(
URI.decode_www_form(uri.query || '') << [key, value]
)

uri.to_s
rescue URI::InvalidURIError
raise_property_error(input)
end
end

def url_remove_param(input, key)
begin
uri = URI.parse(input)
query = URI.decode_www_form(uri.query || '').to_h
query.delete(key)
uri.query = if query.empty?
''
else
URI.encode_www_form(query)
end

uri.to_s
rescue URI::InvalidURIError
raise_property_error(input)
end
end

# @liquid_public_docs
# @liquid_type filter
# @liquid_category string
Expand Down
24 changes: 24 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ def test_base64_url_safe_decode
assert_equal('Liquid error: invalid base64 provided to base64_url_safe_decode', exception.message)
end

def test_url_add_param
assert_equal('https://shopify.com?foo=bar', @filters.url_add_param('https://shopify.com', 'foo', 'bar'))
assert_equal('https://shopify.com?foo=bar&baz=qux', @filters.url_add_param('https://shopify.com?foo=bar', 'baz', 'qux'))
assert_equal('https://shopify.com?foo=bar#baz', @filters.url_add_param('https://shopify.com#baz', 'foo', 'bar'))
assert_equal('not-an-url?foo=bar', @filters.url_add_param('not-an-url', 'foo', 'bar'))
assert_equal('not-an-url?foo=bar&baz=qux', @filters.url_add_param('not-an-url?foo=bar', 'baz', 'qux'))
assert_equal('not-an-url?foo=bar#baz', @filters.url_add_param('not-an-url#baz', 'foo', 'bar'))
assert_equal('?foo=bar', @filters.url_add_param('', 'foo', 'bar'))
assert_equal('?foo=bar&baz=qux', @filters.url_add_param('?foo=bar', 'baz', 'qux'))
assert_equal('?foo=bar#baz', @filters.url_add_param('#baz', 'foo', 'bar'))
end

def test_url_remove_param
assert_equal('https://shopify.com?', @filters.url_remove_param('https://shopify.com?foo=bar', 'foo'))
assert_equal('https://shopify.com?foo=bar', @filters.url_remove_param('https://shopify.com?foo=bar&baz=qux', 'baz'))
assert_equal('https://shopify.com?#baz', @filters.url_remove_param('https://shopify.com?foo=bar#baz', 'foo'))
assert_equal('not-an-url?', @filters.url_remove_param('not-an-url', 'foo'))
assert_equal('not-an-url?foo=bar', @filters.url_remove_param('not-an-url?foo=bar&baz=qux', 'baz'))
assert_equal('not-an-url?#baz', @filters.url_remove_param('not-an-url#baz', 'foo'))
assert_equal('?', @filters.url_remove_param('?foo=bar', 'foo'))
assert_equal('?foo=bar', @filters.url_remove_param('?foo=bar&baz=qux', 'baz'))
assert_equal('?#baz', @filters.url_remove_param('?foo=bar#baz', 'foo'))
end

def test_url_encode
assert_equal('foo%2B1%40example.com', @filters.url_encode('[email protected]'))
assert_equal('1', @filters.url_encode(1))
Expand Down