Encode and unencode urls only as required (#7654)

Merge pull request 7654
This commit is contained in:
Ashwin Maroli 2019-05-15 21:44:08 +05:30 committed by jekyllbot
parent 8c5ee73661
commit 20c9d0957a
1 changed files with 7 additions and 3 deletions

View File

@ -129,6 +129,8 @@ module Jekyll
#
# Returns the escaped path.
def self.escape_path(path)
return path if path.empty? || %r!^[a-zA-Z0-9./-]+$!.match?(path)
# Because URI.escape doesn't escape "?", "[" and "]" by default,
# specify unsafe string (except unreserved, sub-delims, ":", "@" and "/").
#
@ -139,8 +141,7 @@ module Jekyll
# pct-encoded = "%" HEXDIG HEXDIG
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
# / "*" / "+" / "," / ";" / "="
path = Addressable::URI.encode(path)
path.encode("utf-8").sub("#", "%23")
Addressable::URI.encode(path).encode("utf-8").sub("#", "%23")
end
# Unescapes a URL path segment
@ -154,7 +155,10 @@ module Jekyll
#
# Returns the unescaped path.
def self.unescape_path(path)
Addressable::URI.unencode(path.encode("utf-8"))
path = path.encode("utf-8")
return path unless path.include?("%")
Addressable::URI.unencode(path)
end
end
end