var IRIResolver = (function () { function IRIResolver (meta) { if (!(this instanceof IRIResolver)) return new IRIResolver(options); this.meta = meta; this._setBase(meta.base); }; var absoluteIRI = /^[a-z][a-z0-9+.-]*:/i, schemeAuthority = /^(?:([a-z][a-z0-9+.-]*:))?(?:\/\/[^\/]*)?/i, dotSegments = /(?:^|\/)\.\.?(?:$|[\/#?])/; IRIResolver.prototype = { // vvv stolen from Ruben Vergorgh's N3Parser.js vvv // ### `_setBase` sets the base IRI to resolve relative IRIs _setBase: function (baseIRI) { if (!baseIRI) this._base = null; else { // Remove fragment if present var fragmentPos = baseIRI.indexOf('#'); if (fragmentPos >= 0) baseIRI = baseIRI.substr(0, fragmentPos); // Set base IRI and its components this._base = baseIRI; this._basePath = baseIRI.indexOf('/') < 0 ? baseIRI : baseIRI.replace(/[^\/?]*(?:\?.*)?$/, ''); baseIRI = baseIRI.match(schemeAuthority); this._baseRoot = baseIRI[0]; this._baseScheme = baseIRI[1]; } }, // ### `_resolveIRI` resolves a relative IRI token against the base path, // assuming that a base path has been set and that the IRI is indeed relative _resolveIRI: function (token) { var iri = token.value; switch (iri[0]) { // An empty relative IRI indicates the base IRI case undefined: return this._base; // Resolve relative fragment IRIs against the base IRI case '#': return this._base + iri; // Resolve relative query string IRIs by replacing the query string case '?': return this._base.replace(/(?:\?.*)?$/, iri); // Resolve root-relative IRIs at the root of the base IRI case '/': // Resolve scheme-relative IRIs to the scheme return (iri[1] === '/' ? this._baseScheme : this._baseRoot) + this._removeDotSegments(iri); // Resolve all other IRIs at the base IRI's path default: return this._removeDotSegments(this._basePath + iri); } }, // ### `_removeDotSegments` resolves './' and '../' path segments in an IRI as per RFC3986 _removeDotSegments: function (iri) { // Don't modify the IRI if it does not contain any dot segments if (!dotSegments.test(iri)) return iri; // Start with an imaginary slash before the IRI in order to resolve trailing './' and '../' var result = '', length = iri.length, i = -1, pathStart = -1, segmentStart = 0, next = '/'; while (i < length) { switch (next) { // The path starts with the first slash after the authority case ':': if (pathStart < 0) { // Skip two slashes before the authority if (iri[++i] === '/' && iri[++i] === '/') // Skip to slash after the authority while ((pathStart = i + 1) < length && iri[pathStart] !== '/') i = pathStart; } break; // Don't modify a query string or fragment case '?': case '#': i = length; break; // Handle '/.' or '/..' path segments case '/': if (iri[i + 1] === '.') { next = iri[++i + 1]; switch (next) { // Remove a '/.' segment case '/': result += iri.substring(segmentStart, i - 1); segmentStart = i + 1; break; // Remove a trailing '/.' segment case undefined: case '?': case '#': return result + iri.substring(segmentStart, i) + iri.substr(i + 1); // Remove a '/..' segment case '.': next = iri[++i + 1]; if (next === undefined || next === '/' || next === '?' || next === '#') { result += iri.substring(segmentStart, i - 2); // Try to remove the parent path from result if ((segmentStart = result.lastIndexOf('/')) >= pathStart) result = result.substr(0, segmentStart); // Remove a trailing '/..' segment if (next !== '/') return result + '/' + iri.substr(i + 1); segmentStart = i + 1; } } } } next = iri[++i]; } return result + iri.substring(segmentStart); }, _resolveAbsoluteIRI: function (token) { return (this._base === null || absoluteIRI.test(token.value)) ? token.value : this._resolveIRI(token); } }; return IRIResolver; })(); if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') module.exports = IRIResolver;