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

Commit

Permalink
Make paths relative only if they start the same.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Aug 22, 2013
1 parent 830760c commit 701d348
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/HTMLImports.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ var path = {
return url;
}
url = this.compressUrl(this.urlToPath(baseUrl) + url);
if (relativeToDocument && !this.isAbsUrl(url)) {
if (relativeToDocument) {
url = path.makeRelPath(path.getDocumentUrl(document), url);
}
return url;
Expand Down Expand Up @@ -325,6 +325,10 @@ var path = {
var s, t;
s = this.compressUrl(inSource).split("/");
t = this.compressUrl(inTarget).split("/");
// bail if target is not relative to source
if (!s.length || s[0] !== t[0]) {
return inTarget;
}
while (s.length && s[0] === t[0]){
s.shift();
t.shift();
Expand Down
1 change: 1 addition & 0 deletions test/html/imports/abs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img src="../../foo.png">
22 changes: 21 additions & 1 deletion test/html/path.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
<script src="../../tools/test/htmltest.js"></script>
<script src="../../node_modules/chai/chai.js"></script>
<script src="../../html-imports.js"></script>
<script>
var parts = window.location.pathname.split('/');
parts.pop();
var absBase = parts.join('/');
var l = document.createElement('link');
l.setAttribute('rel', 'import');
l.href = absBase + '/imports/abs.html';
document.head.appendChild(l);
</script>
</head>
<body>
<script>
Expand All @@ -16,7 +25,18 @@
url = 'http://foo/bar?baz="foo/../bar"';
chai.assert.equal(path.compressUrl(url), url, 'query string is not counted in path compression');

done();
url = '/foo/bar/baz"';
chai.assert.equal(path.compressUrl(url), url, 'compressUrl handles url\'s starting with / as abs');
document.addEventListener('HTMLImportsLoaded', function() {
var i = document.querySelector('[rel=import]');
var importDoc = i.import.content;
var parts = window.location.href.split('/');
parts.pop();
parts.pop();
var expectedPath = parts.join('/') + '/foo.png';
chai.assert.equal(importDoc.querySelector('img').src, expectedPath);
done();
});
</script>
</body>
</html>

0 comments on commit 701d348

Please sign in to comment.