Skip to content

Commit

Permalink
Merge pull request #119 from PolymerLabs/dom-module-hax
Browse files Browse the repository at this point in the history
Remove dom-module hacks now that upgrades work in imports again.
  • Loading branch information
Steve Orvell committed Sep 9, 2016
2 parents a212747 + 3cb93f9 commit 7408a9d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 64 deletions.
18 changes: 5 additions & 13 deletions src/elements/element.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
// observedAttributes must be finalized at registration time
this._observedAttributes = this._addPropertiesToAttributes(
this._flattenedProperties, []);
// TODO(kschaaf): revisit: capture import document, to aid finding dom-module
let currentScript = document._currentScript || document.currentScript;
this.__importDoc = currentScript && currentScript.ownerDocument;
}
return this._observedAttributes;
}
Expand Down Expand Up @@ -133,18 +130,13 @@

static get template() {
if (!this.hasOwnProperty('_template')) {
// TODO(sorvell): `__importDoc` may not be set if super class
// has not run defined... falling back to document here is
// incorrect. This gambit cannot work as is since if the superclass
// document cannot be discovered via the subclass.
// TODO(sorvell): support more ways to acquire template.
// this requires `is` on constructor...
this._template = Polymer.DomModule.import(this.is,
'template', this.__importDoc || document) ||
// note: implemented so a subclass can retrieve the super
// template; call the super impl this way so that `this` points
// to the superclass.
Object.getPrototypeOf(this.prototype).constructor.template;
this._template = Polymer.DomModule.import(this.is, 'template') ||
// note: implemented so a subclass can retrieve the super
// template; call the super impl this way so that `this` points
// to the superclass.
Object.getPrototypeOf(this.prototype).constructor.template;
}
return this._template;
}
Expand Down
54 changes: 4 additions & 50 deletions src/legacy/dom-module.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,71 +67,25 @@
* @return {Object} Returns the dom which matches `selector` in the module
* at the specified `id`.
*/
__import(id, selector, doc) {
import(id, selector) {
if (id) {
var m = findModule(id);
// TODO(kschaaf): Fallback for V1 CE's that don't currently upgrade in
// HTML Imports: querySelect into the import document (which must be captured
// in a script in that file)! This is not sufficient to solve the CSP case
// where scripts & DOM are separated
if (!m && doc) {
m = doc.querySelector('dom-module[id=' + id + ']');
}
if (!m) {
// If polyfilling, a script can run before a dom-module element
// is upgraded. We force the containing document to upgrade
// dom-modules and try again to workaround this polyfill limitation.
forceDomModulesUpgrade();
m = findModule(id);
}
if (m && selector) {
m = m.querySelector(selector);
return m.querySelector(selector);
}
return m;
}
}

}

// NOTE: Safari can't deal with an ES6 style function named `import`
DomModule.prototype.import = DomModule.prototype.__import;

customElements.define('dom-module', DomModule);

// NOTE: HTMLImports polyfill does not
// block scripts on upgrading elements. However, we want to ensure that
// any dom-module in the tree is available prior to a subsequent script
// processing.
// Therefore, we force any dom-modules in the tree to upgrade when dom-module
// is registered by temporarily setting CE polyfill to crawl the entire
// imports tree. (Note: this should only upgrade any imports that have been
// loaded by this point. In addition the HTMLImports polyfill should be
// changed to upgrade elements prior to running any scripts.)
var cePolyfill = window.CustomElements &&
CustomElements.upgrade &&
!CustomElements.useNative;

function forceDomModulesUpgrade() {
if (cePolyfill) {
var script = document._currentScript || document.currentScript;
var doc = script && script.ownerDocument || document;
// find all dom-modules
var modules = doc.querySelectorAll('dom-module');
// minimize work by going backwards and stopping if we find an
// upgraded module.
for (var i= modules.length-1, m; (i >=0) && (m=modules[i]); i--) {
if (m.__upgraded__) {
return;
} else {
CustomElements.upgrade(m);
}
}
}
}

// export
Polymer.DomModule = new DomModule();

Polymer.DomModule.modules = modules;

})();

</script>
2 changes: 1 addition & 1 deletion test/unit/resolveurl.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</dom-module>

<script>
addEventListener('HTMLImportsLoaded', function() {
HTMLImports.whenReady(function() {
Polymer({is: 'x-resolve'});
});
</script>
Expand Down

0 comments on commit 7408a9d

Please sign in to comment.