diff --git a/externs/customelements.js b/externs/customelements.js new file mode 100644 index 0000000..11c89e5 --- /dev/null +++ b/externs/customelements.js @@ -0,0 +1,5 @@ +/** @constructor */ +var CustomElementsRegistry; +// +// /** @type string */ +CustomElementsRegistry.prototype.currentTag; diff --git a/gulpfile.js b/gulpfile.js index c0f5977..946777a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -139,7 +139,7 @@ gulp.task('CustomElementsV1', function () { language_in: 'ECMASCRIPT6_STRICT', language_out: 'ECMASCRIPT5_STRICT', output_wrapper: '(function(){\n%output%\n}).call(this)', - externs: 'externs/html5.js', + externs: ['externs/html5.js','externs/customelements.js'], js_output_file: 'CustomElementsV1.min.js' })) .pipe(gulp.dest('./dist')); diff --git a/src/CustomElements/v1/CustomElements.js b/src/CustomElements/v1/CustomElements.js index 475a9a4..534f3a5 100644 --- a/src/CustomElements/v1/CustomElements.js +++ b/src/CustomElements/v1/CustomElements.js @@ -61,27 +61,18 @@ var CustomElementDefinition; return reservedTagList.indexOf(name) !== -1; } - /** - * @constructor - * @property {Map} _defintions - * @property {MutationObserver} _observer - * @property {MutationObserver} _attributeObserver - * @property {HTMLElement} _newInstance - * @property {string} _newTagName - * @property {boolean} polyfilled - */ - function CustomElementsRegistry() { - this._definitions = new Map(); - this._observer = this._observeRoot(document); - this._attributeObserver = - new MutationObserver(this._handleAttributeChange.bind(this)); - this._newInstance = null; - this._newTagName = null; - this.polyfilled = true; - } - - /** @lends {CustomElementsRegistry.prototype} */ - CustomElementsRegistry.prototype = { + /** @export */ + class CustomElementsRegistry { + constructor() { + this._definitions = new Map(); + this._observer = this._observeRoot(document); + this._attributeObserver = + new MutationObserver(this._handleAttributeChange.bind(this)); + this._newInstance = null; + this._newTagName = null; + this.polyfilled = true; + } + /** @export */ define(name, constructor, options) { // 5.1.1 if (typeof constructor !== 'function') { @@ -158,15 +149,17 @@ var CustomElementDefinition; // 5.1.22 // this causes an upgrade of the document this._addNodes(doc.childNodes); - }, - + } + set currentTag(tagName) { + this._newTagName = this._newTagName || tagName; + } flush() { this._handleMutations(this._observer.takeRecords()); - }, + } _setNewInstance(instance) { this._newInstance = instance; - }, + } _observeRoot(root) { if (!root.__observer) { @@ -175,7 +168,7 @@ var CustomElementDefinition; root.__observer = observer; } return root.__observer; - }, + } _handleMutations(mutations) { for (var i = 0; i < mutations.length; i++) { @@ -185,7 +178,7 @@ var CustomElementDefinition; this._removeNodes(mutation.removedNodes); } } - }, + } /** * @param {NodeList} nodeList @@ -211,7 +204,7 @@ var CustomElementDefinition; } } while (walker.nextNode()) } - }, + } /** * @param {NodeList} nodeList @@ -231,7 +224,7 @@ var CustomElementDefinition; } } while (walker.nextNode()) } - }, + } /** * @param {HTMLElement} element @@ -254,7 +247,7 @@ var CustomElementDefinition; attributeFilter: definition.observedAttributes, }); } - }, + } /** * @private @@ -271,21 +264,17 @@ var CustomElementDefinition; target['attributeChangedCallback'](name, oldValue, newValue, namespace); } } - }, + } }; - // TODO: Figure out how to export a setter w/o using defineProperty - Object.defineProperty(CustomElementsRegistry.prototype, 'currentTag', { - set(tagName) { - this._newTagName = this._newTagName || tagName; - }, - }); - // Closure Compiler Exports window['CustomElementsRegistry'] = CustomElementsRegistry; CustomElementsRegistry.prototype['define'] = CustomElementsRegistry.prototype.define; CustomElementsRegistry.prototype['flush'] = CustomElementsRegistry.prototype.flush; CustomElementsRegistry.prototype['polyfilled'] = CustomElementsRegistry.prototype.polyfilled; + /** @export */ + window['customElements'] = new CustomElementsRegistry(); + // patch window.HTMLElement // TODO: patch up all built-in subclasses of HTMLElement to use the fake @@ -341,7 +330,4 @@ var CustomElementDefinition; return _origCreateElementNS.call(document, namespaceURI, qualifiedName); } }; - - /** @type {CustomElementsRegistry} */ - window['customElements'] = new CustomElementsRegistry(); })();