Skip to content

Commit

Permalink
* allow setting rootPath
Browse files Browse the repository at this point in the history
* disallow setting `importPath` (this is supported in 2.x but not 1.x)
  • Loading branch information
Steven Orvell committed Mar 4, 2017
1 parent daaf460 commit ac06765
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/lib/resolve-url.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
};

// NOTE: IE11 does not support baseURI
Polymer.rootPath = pathFromUrl(document.baseURI || window.location.href);
Polymer.rootPath = Polymer.Settings.rootPath ||
pathFromUrl(document.baseURI || window.location.href);

})();

Expand Down
18 changes: 9 additions & 9 deletions src/mini/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
module = Polymer.DomModule.import(this.is);
this._template = module && module.querySelector('template');
}
if (!this._importPath) {
if (module) {
var assetPath = module.getAttribute('assetpath') || '';
var importURL = Polymer.ResolveUrl.resolveUrl(assetPath,
module.ownerDocument.baseURI);
this._importPath = Polymer.ResolveUrl.pathFromUrl(importURL);
} else {
this._importPath = '';
}
// NOTE: users setting `_importPath` is supported in Polymer 2.x but not
// 1.x.
if (module) {
var assetPath = module.getAttribute('assetpath') || '';
var importURL = Polymer.ResolveUrl.resolveUrl(assetPath,
module.ownerDocument.baseURI);
this._importPath = Polymer.ResolveUrl.pathFromUrl(importURL);
} else {
this._importPath = '';
}
// stick finger in footgun
if (this._template && this._template.hasAttribute('is')) {
Expand Down
36 changes: 35 additions & 1 deletion test/unit/resolveurl.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<meta charset="UTF-8">
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<script>
Polymer = {
rootPath: 'earlyRootPath/'
}
</script>
<link rel="import" href="../../polymer.html">
<link id="elements" rel="import" href="sub/resolveurl-elements.html">
</head>
Expand All @@ -34,6 +39,26 @@
</template>
</dom-module>

<dom-module id="x-early">
<template>
<style>
:host {
background: url('foo.png');
}
</style>
<img id="root" src$="[[rootPath]]foo.png">
<img id="import" src$="[[importPath]]foo.png">
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({ is: 'x-early'})
});
</script>
</dom-module>

<dom-module id="x-resolve">
</dom-module>

<script>
addEventListener('HTMLImportsLoaded', function() {
Polymer({is: 'x-resolve'});
Expand Down Expand Up @@ -129,17 +154,26 @@
document.body.removeChild(el);
});

test('rootPath set early', function() {
var el = document.createElement('x-early');
document.body.appendChild(el);
var matchRoot = /earlyRootPath\//i;
assert.match(el.$.root.getAttribute('src'), matchRoot);
document.body.removeChild(el);
});

test('Hybrid-url: changes via setting importPath/rootPath when defining element', function() {
Polymer.rootPath = 'defineRoot/';
Polymer({
is: 'x-late',
// NOTE: setting importPath is not supported in Polymer 1.x.
_importPath: 'defineImport/'
});
var el = document.createElement('x-late');
document.body.appendChild(el);
var matchRoot = /defineRoot\//i;
var matchImport = /defineImport\//i;
assert.match(el.$.import.getAttribute('src'), matchImport);
assert.notMatch(el.$.import.getAttribute('src'), matchImport);
assert.match(el.$.root.getAttribute('src'), matchRoot);
document.body.removeChild(el);
});
Expand Down

0 comments on commit ac06765

Please sign in to comment.