From 7a9cff6644d1c15a8bb64b742d96331ee81330f0 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Wed, 20 Nov 2013 17:41:01 -0800 Subject: [PATCH] Make resolvePath a little smarter 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 --- src/declaration/path.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/declaration/path.js b/src/declaration/path.js index 78c08e3..3c46bba 100644 --- a/src/declaration/path.js +++ b/src/declaration/path.js @@ -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 ''; @@ -29,7 +57,7 @@ } } }; - + // exports scope.api.declaration.path = path;