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

Commit

Permalink
Make resolvePath a little smarter
Browse files Browse the repository at this point in the history
Use a "relative path" approach:
- if paths have common parts, make sure to not duplicate those parts
- if there are no common parts, just append

Inspired by the functionality of Node's path.relative
  • Loading branch information
dfreedm committed Nov 21, 2013
1 parent d03aad8 commit 7a9cff6
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/declaration/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,47 @@
*/

(function(scope) {

var path = {
addResolvePathApi: function() {
var root = this.elementPath();
// let assetpath attribute modify the resolve path
var assetPath = this.getAttribute('assetpath') || '';
var relPath = this.relPath;
this.prototype.resolvePath = function(inPath) {
return root + assetPath + inPath;
var to = inPath;
if (assetPath) {
// assetPath is always a folder, drop the trailing '/'
var from = assetPath.slice(0, -1);
to = relPath(from, to);
}

return root + assetPath + to;
};
},
elementPath: function() {
return this.urlToPath(HTMLImports.getDocumentUrl(this.ownerDocument));
},
relPath: function(from, to) {
var fromParts = from.split('/');
var toParts = to.split('/');

// chop to common length
var common = false;
while(fromParts.length && toParts.length && fromParts[0] === toParts[0]) {
fromParts.shift();
toParts.shift();
common = true;
}

// if there were some commonalities, add '../' for differences
if (common) {
for (var i = 0; i < fromParts.length; i++) {
toParts.unshift('..');
}
}
return toParts.join('/');
},
urlToPath: function(url) {
if (!url) {
return '';
Expand All @@ -29,7 +57,7 @@
}
}
};

// exports
scope.api.declaration.path = path;

Expand Down

0 comments on commit 7a9cff6

Please sign in to comment.