diff --git a/CHANGELOG.md b/CHANGELOG.md index 096b43c..7c1557b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ -0.1.6: +0.1.7: -* square brackets are also special strings, because of HTTParty; +* percent-encode square brackets in :url_encoded part of the result, because of HTTParty; 0.1.5: diff --git a/lib/linkhum/url.rb b/lib/linkhum/url.rb index 47a93ea..fd11819 100644 --- a/lib/linkhum/url.rb +++ b/lib/linkhum/url.rb @@ -65,7 +65,7 @@ def self.generate_url(parts) end def self.encode_component(string, skip_percent_sign = true) - chars_to_keep_unencoded = Addressable::URI::CharacterClasses::RESERVED + Addressable::URI::CharacterClasses::UNRESERVED + chars_to_keep_unencoded = "\\:\\/\\?\\#\\@\\!\\$\\&\\'\\(\\)\\*\\+\\,\\;\\=" + Addressable::URI::CharacterClasses::UNRESERVED chars_to_keep_unencoded << '\\%' if skip_percent_sign Addressable::URI.encode_component(string, chars_to_keep_unencoded) end diff --git a/linkhum-url.gemspec b/linkhum-url.gemspec index c3abb98..10c811c 100644 --- a/linkhum-url.gemspec +++ b/linkhum-url.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'linkhum-url' - s.version = '0.1.6' + s.version = '0.1.7' s.date = '2017-10-17' s.summary = "Linkhum-URL creates both URL-encoded and readable versions of URLs" s.description = "Input URL could be either human-readable, or URL-encoded. Two URLs are returned as result: human-readable and URL-encoded." diff --git a/spec/linkhum-url.spec b/spec/linkhum-url.spec index eba23ef..7f5be5e 100644 --- a/spec/linkhum-url.spec +++ b/spec/linkhum-url.spec @@ -56,6 +56,12 @@ describe Linkhum::URL do expect(lu[:url_encoded]).to eql("https://example.org/search.html?q=%D0%BF%D0%B8%D0%B2%D0%B1%D0%B0%D1%80") end + it "percent-encodes square brackets in paths" do + lu = Linkhum::URL.parse("http://www.example.com/image[2].jpg") + expect(lu[:human_readable]).to eql("http://www.example.com/image[2].jpg") + expect(lu[:url_encoded]).to eql("http://www.example.com/image%5B2%5D.jpg") + end + it "handles non-ASCII fragments" do lu = Linkhum::URL.parse("https://example.org/venues.html#пивбар") expect(lu[:human_readable]).to eql("https://example.org/venues.html#пивбар")