Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 39 additions & 26 deletions js/jsonld.js
Original file line number Diff line number Diff line change
Expand Up @@ -6116,22 +6116,22 @@ function _prependBase(base, iri) {
var path = base.path;

// append relative path to the end of the last directory from base
if(rel.path !== '') {
path = path.substr(0, path.lastIndexOf('/') + 1);
if(path.length > 0 && path.substr(-1) !== '/') {
path += '/';
}
path += rel.path;
path = path.substr(0, path.lastIndexOf('/') + 1);
if(path.length > 0 && path.substr(-1) !== '/') {
path += '/';
}
path += rel.path;

transform.path = path;
}
transform.query = rel.query;
}
}

if(rel.path !== '') {
// remove slashes and dots in path
transform.path = _removeDotSegments(transform.path, !!transform.authority);
transform.path = _removeDotSegments(transform.path);
}

// construct URL
var rval = transform.protocol;
Expand Down Expand Up @@ -8120,46 +8120,59 @@ jsonld.url.parse = function(str, parser) {
parsed.port = null;
}

parsed.normalizedPath = _removeDotSegments(parsed.path, !!parsed.authority);
parsed.normalizedPath = _removeDotSegments(parsed.path);
return parsed;
};

/**
* Removes dot segments from a URL path.
*
* @param path the path to remove dot segments from.
* @param hasAuthority true if the URL has an authority, false if not.
*/
function _removeDotSegments(path, hasAuthority) {
var rval = '';
function _removeDotSegments(path) {
// RFC 3986 5.2.4 (reworked)

if(path.indexOf('/') === 0) {
rval = '/';
// empty path shortcut
if(path.length === 0) {
return '';
}

// RFC 3986 5.2.4 (reworked)
var input = path.split('/');
var output = [];

while(input.length > 0) {
if(input[0] === '.' || (input[0] === '' && input.length > 1)) {
input.shift();
var next = input.shift();
var done = input.length === 0;

if(next === '.') {
if(done) {
// ensure output has trailing /
output.push('');
}
continue;
}
if(input[0] === '..') {
input.shift();
if(hasAuthority ||
(output.length > 0 && output[output.length - 1] !== '..')) {
output.pop();
} else {
// leading relative URL '..'
output.push('..');

if(next === '..') {
output.pop();
if(done) {
// ensure output has trailing /
output.push('');
}
continue;
}
output.push(input.shift());

output.push(next);
}

// ensure output has leading /
if(output.length > 0 && output[0] !== '') {
output.unshift('');
}
if(output.length === 1 && output[0] === '') {
return '/';
}

return rval + output.join('/');
return output.join('/');
}

if(_nodejs) {
Expand Down
4 changes: 1 addition & 3 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ var TEST_TYPES = {
compare: compareExpectedNQuads
},
'jld:ToRDFTest': {
skip: {
regex: [/RFC3986/]
},
skip: {},
fn: 'toRDF',
params: [
readTestUrl('input'),
Expand Down