Skip to content

Commit 384bf45

Browse files
committed
Fix compatiblity with newer uri gem
1 parent 0487291 commit 384bf45

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

lib/sprockets/legacy.rb

+4-12
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,10 @@ def matches_filter(filters, logical_path, filename)
154154
end
155155
end
156156

157-
# URI.unescape is deprecated on 1.9. We need to use URI::Parser
158-
# if its available.
159-
if defined? URI::DEFAULT_PARSER
160-
def unescape(str)
161-
str = URI::DEFAULT_PARSER.unescape(str)
162-
str.force_encoding(Encoding.default_internal) if Encoding.default_internal
163-
str
164-
end
165-
else
166-
def unescape(str)
167-
URI.unescape(str)
168-
end
157+
def unescape(str)
158+
URIUtils::URI_PARSER.unescape(str)
159+
str.force_encoding(Encoding.default_internal) if Encoding.default_internal
160+
str
169161
end
170162
end
171163

lib/sprockets/uri_utils.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module Sprockets
2020
module URIUtils
2121
extend self
2222

23+
URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::RFC2396_Parser.new
24+
2325
# Internal: Parse URI into component parts.
2426
#
2527
# uri - String uri
@@ -44,7 +46,7 @@ def join_uri(scheme, userinfo, host, port, registry, path, opaque, query, fragme
4446
def split_file_uri(uri)
4547
scheme, _, host, _, _, path, _, query, _ = URI.split(uri)
4648

47-
path = URI::Generic::DEFAULT_PARSER.unescape(path)
49+
path = URI_PARSER.unescape(path)
4850
path.force_encoding(Encoding::UTF_8)
4951

5052
# Hack for parsing Windows "file:///C:/Users/IEUser" paths
@@ -63,7 +65,7 @@ def join_file_uri(scheme, host, path, query)
6365
str = "#{scheme}://"
6466
str << host if host
6567
path = "/#{path}" unless path.start_with?("/")
66-
str << URI::Generic::DEFAULT_PARSER.escape(path)
68+
str << URI_PARSER.escape(path)
6769
str << "?#{query}" if query
6870
str
6971
end
@@ -162,7 +164,7 @@ def encode_uri_query_params(params)
162164
when Integer
163165
query << "#{key}=#{value}"
164166
when String, Symbol
165-
query << "#{key}=#{URI::Generic::DEFAULT_PARSER.escape(value.to_s)}"
167+
query << "#{key}=#{URI_PARSER.escape(value.to_s)}"
166168
when TrueClass
167169
query << "#{key}"
168170
when FalseClass, NilClass
@@ -182,7 +184,7 @@ def encode_uri_query_params(params)
182184
def parse_uri_query_params(query)
183185
query.to_s.split('&').reduce({}) do |h, p|
184186
k, v = p.split('=', 2)
185-
v = URI::Generic::DEFAULT_PARSER.unescape(v) if v
187+
v = URI_PARSER.unescape(v) if v
186188
h[k.to_sym] = v || true
187189
h
188190
end

0 commit comments

Comments
 (0)