Skip to content

Commit e8623e8

Browse files
Disambiguate relative paths containing colons.
1 parent 3f81c79 commit e8623e8

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/protocol/url/path.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ def relative(from)
386386
# An empty reference identifies the current document, so identify the current directory explicitly:
387387
if relative_segments == [""]
388388
relative_segments = [".", ""]
389+
elsif relative_segments.first&.include?(":")
390+
# A colon in the first segment would be interpreted as a URI scheme:
391+
relative_segments.unshift(".")
389392
end
390393

391394
return Path.new(nil, relative_segments)

test/protocol/url/path.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@
521521
expect(Protocol::URL::Path.relative("/docs", "/docs/index.html")).to be == "../docs"
522522
end
523523

524+
it "disambiguates a colon in the first segment" do
525+
expect(Protocol::URL::Path.relative("/docs/this:that", "/docs/index.html")).to be == "./this:that"
526+
end
527+
524528
it "calculates relative path with multiple levels up" do
525529
expect(Protocol::URL::Path.relative("/a/file.txt", "/x/y/z/")).to be == "../../../a/file.txt"
526530
end

test/protocol/url/relative.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@
224224
expect(url.relative_to("/index").to_s).to be == "./?q=ruby#examples"
225225
end
226226

227+
it "disambiguates a colon in the first segment" do
228+
url = Protocol::URL::Relative.new("/docs/this:that")
229+
relative_url = url.relative_to("/docs/index")
230+
231+
expect(relative_url.to_s).to be == "./this:that"
232+
expect(Protocol::URL[relative_url.to_s]).to be_a(Protocol::URL::Relative)
233+
end
234+
227235
it "returns already-relative URLs unchanged" do
228236
url = Protocol::URL::Relative.new("../guide", "q=ruby", "examples")
229237

0 commit comments

Comments
 (0)