Skip to content

Commit

Permalink
Fix #49. Track link element, get href, and use anchor element to get …
Browse files Browse the repository at this point in the history
…the current asset pathname
  • Loading branch information
t2ym committed Mar 2, 2017
1 parent 2387caa commit 7514629
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
34 changes: 28 additions & 6 deletions define-element.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@

current = (!window.HTMLImports || HTMLImports.useNative) ? document.currentScript
: (document._currentScript || document.currentScript);
var _tmpNode = current;
var ownerDocument = current ? current.ownerDocument : document;
var baseURI = ownerDocument.baseURI;
if (current && current.ownerDocument && current.ownerDocument.nodeType === current.ownerDocument.DOCUMENT_NODE) {
while (_tmpNode && _tmpNode.tagName !== 'LINK' &&
_tmpNode.nodeType !== _tmpNode.DOCUMENT_FRAGMENT_NODE &&
_tmpNode.nodeType !== _tmpNode.DOCUMENT_NODE) {
_tmpNode = _tmpNode.parentNode;
}
if (_tmpNode &&
(_tmpNode.nodeType === _tmpNode.DOCUMENT_FRAGMENT_NODE ||
_tmpNode.nodeType === _tmpNode.DOCUMENT_NODE)) {
ownerDocument = _tmpNode;
baseURI = ownerDocument.baseURI;
}
else if (_tmpNode && _tmpNode.import === _tmpNode) {
ownerDocument = _tmpNode;
baseURI = ownerDocument.href; // link node
}
}

previous = current.previousSibling;
while (previous && !previous.tagName) {
Expand Down Expand Up @@ -118,8 +138,7 @@
}
if (!rawTemplate && !template) {
// Pattern b), c)
template = current.ownerDocument
.querySelector('template[id=' + id + ']') ||
template = ownerDocument.querySelector('template[id=' + id + ']') ||
document.querySelector('template[id=' + id + ']');
}
if (!rawTemplate && !template && previous && !previous.id) {
Expand All @@ -139,16 +158,19 @@
// register dom-module
if (template) {
var domModule = document.createElement('dom-module');
let assetUrl = new URL(current.baseURI ||
(window.currentImport ? window.currentImport.baseURI : null) ||
current.ownerDocument.baseURI);
var assetpath = typeof URL === 'function' && URL.name === 'URL'
? new URL((current ? current.baseURI : null) ||
(window.currentImport ? window.currentImport.baseURI : null) ||
baseURI ||
document.baseURI).pathname
: (uri => { let a = document.createElement('a'); a.href = uri; return ('/' + a.pathname).replace(/^\/\//, '/'); })(baseURI);
domModule.appendChild(template);
domModule.setAttribute('assetpath',
template.hasAttribute('basepath') ?
template.getAttribute('basepath') :
template.hasAttribute('assetpath') ?
template.getAttribute('assetpath') :
assetUrl.pathname);
assetpath);
domModule.register(id);
proto._template = template;
proto.finalize();
Expand Down
37 changes: 29 additions & 8 deletions i18n-element.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,28 @@
let template = Polymer.DomModule.import(id, 'template');
if (id && !template) {
let current = (!window.HTMLImports || HTMLImports.useNative) ? document.currentScript
: document._currentScript;
template = (current ? current.ownerDocument
.querySelector('template[id=' + id + ']') : null) ||
: document._currentScript || document.currentScript;
let _tmpNode = current;
let ownerDocument = current ? current.ownerDocument : document;
let baseURI = ownerDocument.baseURI;
if (current && current.ownerDocument && current.ownerDocument.nodeType === current.ownerDocument.DOCUMENT_NODE) {
while (_tmpNode && _tmpNode.tagName !== 'LINK' &&
_tmpNode.nodeType !== _tmpNode.DOCUMENT_FRAGMENT_NODE &&
_tmpNode.nodeType !== _tmpNode.DOCUMENT_NODE) {
_tmpNode = _tmpNode.parentNode;
}
if (_tmpNode &&
(_tmpNode.nodeType === _tmpNode.DOCUMENT_FRAGMENT_NODE ||
_tmpNode.nodeType === _tmpNode.DOCUMENT_NODE)) {
ownerDocument = _tmpNode;
baseURI = ownerDocument.baseURI;
}
else if (_tmpNode && _tmpNode.import === _tmpNode) {
ownerDocument = _tmpNode;
baseURI = ownerDocument.href; // link node
}
}
template = ownerDocument.querySelector('template[id=' + id + ']') ||
document.querySelector('template[id=' + id + ']');
if (!template && id !== 'i18n-dom-bind') {
template = document.createElement('template');
Expand All @@ -43,17 +62,19 @@
}
if (template) {
let domModule = document.createElement('dom-module');
let assetUrl = new URL((current ? current.baseURI : null) ||
(window.currentImport ? window.currentImport.baseURI : null) ||
(current && current.ownerDocument ? current.ownerDocument.baseURI : null) ||
document.baseURI);
let assetpath = typeof URL === 'function' && URL.name === 'URL'
? new URL((current ? current.baseURI : null) ||
(window.currentImport ? window.currentImport.baseURI : null) ||
baseURI ||
document.baseURI).pathname
: (uri => { let a = document.createElement('a'); a.href = uri; return ('/' + a.pathname).replace(/^\/\//, '/'); })(baseURI);
domModule.appendChild(template);
domModule.setAttribute('assetpath',
template.hasAttribute('basepath') ?
template.getAttribute('basepath') :
template.hasAttribute('assetpath') ?
template.getAttribute('assetpath') :
assetUrl.pathname);
assetpath);
domModule.register(id);
this._template = template;
}
Expand Down

0 comments on commit 7514629

Please sign in to comment.