Skip to content

Commit d22e026

Browse files
committed
Update URI#relativize based on JSON-LD Context#remove_base. This covers more cases where the base URI can be used.
1 parent edf4291 commit d22e026

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

lib/rdf/model/uri.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,22 @@ def relative?; !absolute?; end
311311
# @param [#to_s] base_uri
312312
# @return [RDF::URI]
313313
def relativize(base_uri)
314-
if base_uri.to_s.end_with?("/", "#") &&
314+
if self.to_s.start_with?(base_uri.to_s) && %w(# ?).include?(self.to_s[base_uri.to_s.length, 1]) ||
315+
base_uri.to_s.end_with?("/", "#") &&
315316
self.to_s.start_with?(base_uri.to_s)
316-
RDF::URI(self.to_s[base_uri.to_s.length..-1])
317+
return RDF::URI(self.to_s[base_uri.to_s.length..-1])
317318
else
318-
self
319+
# Create a list of parents, for which this IRI may be relative.
320+
u = RDF::URI(base_uri)
321+
iri_set = u.to_s.end_with?('/') ? [u.to_s] : []
322+
iri_set << u.to_s while (u = u.parent)
323+
iri_set.each_with_index do |bb, index|
324+
next unless self.to_s.start_with?(bb)
325+
rel = "../" * index + self.to_s[bb.length..-1]
326+
return rel.empty? ? "./" : rel
327+
end
319328
end
329+
self
320330
end
321331

322332
##

spec/model_uri_spec.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,33 @@
778778
{
779779
"prefix with #" => ["http://example.com/#", "http://example.com/#foo", "foo"],
780780
"prefix with /" => ["http://example.com/", "http://example.com/#foo", "#foo"],
781-
"prefix without / or #" => ["http://example.com/f", "http://example.com/foo", "http://example.com/foo"],
781+
"prefix without / or #" => ["http://example.com/f", "http://example.com/foo", "foo"],
782782
}.each do |name, (base_uri,orig,result)|
783783
it "<#{base_uri}> + <#{orig}>: <#{result}>" do
784784
expect(described_class.new(orig).relativize(base_uri)).to eq result
785785
end
786786
end
787+
788+
context "json-ld compact#t0066" do
789+
{
790+
"https://w3c.github.io/json-ld-api/tests/compact/link" => "link",
791+
"https://w3c.github.io/json-ld-api/tests/compact/0066-in.jsonld#fragment-works" => "#fragment-works",
792+
"https://w3c.github.io/json-ld-api/tests/compact/0066-in.jsonld?query=works" => "?query=works",
793+
"https://w3c.github.io/json-ld-api/tests/" => "../",
794+
"https://w3c.github.io/json-ld-api/" => "../../",
795+
"https://w3c.github.io/json-ld-api/parent" => "../../parent",
796+
"https://w3c.github.io/json-ld-api/parent#fragment" => "../../parent#fragment",
797+
"https://w3c.github.io/parent-parent-eq-root" => "../../../parent-parent-eq-root",
798+
"https://w3c.github.io/still-root" => "../../../still-root",
799+
"https://w3c.github.io/too-many-dots" => "../../../too-many-dots",
800+
"https://w3c.github.io/absolute" => "../../../absolute",
801+
"http://example.org/scheme-relative" => "http://example.org/scheme-relative"
802+
}.each do |orig, result|
803+
it "relativises #{orig} to #{result}" do
804+
expect(described_class.new(orig).relativize("https://w3c.github.io/json-ld-api/tests/compact/0066-in.jsonld")).to eq result
805+
end
806+
end
807+
end
787808
end
788809

789810
describe "#request_uri" do
@@ -950,8 +971,8 @@
950971
end
951972

952973
it "#=~" do
953-
expect(RDF::URI('http://example.org/')).to match /example/
954-
expect(RDF::URI('http://example.org/')).not_to match /foobar/
974+
expect(RDF::URI('http://example.org/')).to match(/example/)
975+
expect(RDF::URI('http://example.org/')).not_to match(/foobar/)
955976
end
956977

957978
it "#to_str" do

0 commit comments

Comments
 (0)