Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Improve relative path resolution; fixes issue #31
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Aug 23, 2013
1 parent badd01d commit a92ff74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 17 additions & 9 deletions src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function makeDocument(resource, url) {
doc._URL = url;
// establish a relative path via <base>
var base = doc.createElement('base');
base.setAttribute('href', document.baseURI);
base.setAttribute('href', document.baseURI || document.URL);
doc.head.appendChild(base);
// TODO(sorvell): MDV Polyfill intrusion: boostrap template polyfill
if (window.HTMLTemplateElement && HTMLTemplateElement.bootstrap) {
Expand Down Expand Up @@ -293,7 +293,7 @@ var path = {
if (this.isAbsUrl(url)) {
return url;
}
return path.makeRelPath(path.documentURL, this.resolveUrl(baseUrl, url));
return this.makeDocumentRelPath(this.resolveUrl(baseUrl, url));
},
isAbsUrl: function(url) {
return /(^data:)|(^http[s]?:)|(^\/)/.test(url);
Expand Down Expand Up @@ -322,15 +322,22 @@ var path = {
}
return parts.join('/') + search;
},
makeDocumentRelPath: function(url) {
// test url against document to see if we can construct a relative path
path.urlElt.href = url;
// IE does not set host if same as document
if (!path.urlElt.host ||
(path.urlElt.host === window.location.host &&
path.urlElt.protocol === window.location.protocol)) {
return this.makeRelPath(path.documentURL, path.urlElt.href);
} else {
return url;
}
},
// make a relative path from source to target
makeRelPath: function(source, target) {
var s, t;
s = this.compressUrl(source).split("/");
t = this.compressUrl(target).split("/");
// bail if target is not relative to source
if (!s.length || s[0] !== t[0]) {
return target;
}
var s = source.split("/");
var t = target.split("/");
while (s.length && s[0] === t[0]){
s.shift();
t.shift();
Expand Down Expand Up @@ -397,6 +404,7 @@ var path = {
};

path.documentURL = path.getDocumentUrl(document);
path.urlElt = document.createElement('a');

xhr = xhr || {
async: true,
Expand Down
12 changes: 11 additions & 1 deletion test/html/path.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var absBase = parts.join('/');
var l = document.createElement('link');
l.setAttribute('rel', 'import');
l.href = absBase + '/imports/abs.html';
l.setAttribute('href', absBase + '/imports/abs.html');
document.head.appendChild(l);
</script>
</head>
Expand All @@ -25,6 +25,16 @@
url = 'http://foo/bar?baz="foo/../bar"';
chai.assert.equal(path.compressUrl(url), url, 'query string is not counted in path compression');

url = '../foo.png';
chai.assert.equal(path.makeDocumentRelPath(url), url, 'document relative path correctly resolves');

url = window.location.host + '/zonk/zim/foo.png';
chai.assert.equal(path.makeDocumentRelPath(url), url, 'abs / url is relative to document');

url = window.location.protocol + '//' + window.location.hostname + ':9999/zonk/zim/foo.png';
chai.assert.equal(path.makeDocumentRelPath(url), url, 'other host url is absolute');


url = '/foo/bar/baz"';
chai.assert.equal(path.compressUrl(url), url, 'compressUrl handles url\'s starting with / as abs');
document.addEventListener('HTMLImportsLoaded', function() {
Expand Down

0 comments on commit a92ff74

Please sign in to comment.