Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions src/BaseIRI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { escapeRegex } from './Util';
const BASE_UNSUPPORTED = /^:?[^:?#]*(?:[?#]|$)|^file:|^[^:]*:\/*[^?#]+?\/(?:\.\.?(?:\/|$)|\/)/i;
const SUFFIX_SUPPORTED = /^(?:(?:[^/?#]{3,}|\.?[^/?#.]\.?)(?:\/[^/?#]{3,}|\.?[^/?#.]\.?)*\/?)?(?:[?#]|$)/;
const CURRENT = './';
const ORIGIN = '/';
const PARENT = '../';
const QUERY = '?';
const FRAGMENT = '#';
Expand Down Expand Up @@ -57,8 +58,17 @@ export default class BaseIRI {
}

// Precalculate parent path substitutions
for (let i = 0; i < segments.length; i++)
this._pathReplacements[segments[i]] = PARENT.repeat(segments.length - i - 1);
for (let i = 0; i < segments.length; i++) {
const parentLength = 3 * (segments.length - i - 1);
const baseLength = segments[i] - segments[0];

if (parentLength < baseLength) {
this._pathReplacements[segments[i]] = PARENT.repeat(segments.length - i - 1);
}
else {
this._pathReplacements[segments[i]] = ORIGIN + this.base.slice(segments[0], segments[i]);
}
}
this._pathReplacements[segments[segments.length - 1]] = CURRENT;

// Add the remainder of the base IRI (without fragment) to the regex
Expand Down
9 changes: 9 additions & 0 deletions test/BaseIRI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ describe('BaseIRI', () => {

relativizes('an IRI containing ../ in its fragment', 'http://example.org/foo/',
'http://example.org/foo/baz#bar/../baz', 'baz#bar/../baz');

relativizes('an IRI directly relative to the base', 'http://example.org/foo/',
'http://example.org/foo/baz', 'baz');

relativizes('an IRI where it is better to use a / path', 'http://example.org/foo/baz/nook/task/tar/',
'http://example.org/foo/bar', '/foo/bar');

relativizes('an IRI where it is better to use a / path [/x/y]', 'http://example.org/x/q/r/n/m/',
'http://example.org/x/y', '/x/y');
});
});

Expand Down
Loading