From a3369ffb21171365a254205f7999164904d427c7 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Thu, 28 Feb 2013 15:49:42 -0500 Subject: [PATCH] Fix bugs with hashes and queries when compacting to relative IRIs. --- jsonld.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/jsonld.php b/jsonld.php index 65ff924..db1211f 100644 --- a/jsonld.php +++ b/jsonld.php @@ -478,9 +478,12 @@ function jsonld_remove_base($base, $iri) { return $iri; } + // remove root from IRI + $rel = jsonld_parse_url(substr($iri, strlen($root))); + // remove path segments that match $base_segments = explode('/', $base['path']); - $iri_segments = explode('/', substr($iri, strlen($root))); + $iri_segments = explode('/', $rel['path']); while(count($base_segments) > 0 && count($iri_segments) > 0) { if($base_segments[0] !== $iri_segments[0]) { break; @@ -504,6 +507,18 @@ function jsonld_remove_base($base, $iri) { // prepend remaining segments $rval .= implode('/', $iri_segments); + // add query and hash + if(isset($rel['query'])) { + $rval .= "?{$rel['query']}"; + } + if(isset($rel['fragment'])) { + $rval .= "#{$rel['fragment']}"; + } + + if($rval === '') { + $rval = './'; + } + return $rval; }