diff --git a/src/polyfills/custom-elements.js b/src/polyfills/custom-elements.js index 4278de9d1f06..80c5e44ef0d9 100644 --- a/src/polyfills/custom-elements.js +++ b/src/polyfills/custom-elements.js @@ -227,11 +227,6 @@ class Registry { */ this.win_ = win; - /** - * @private @const - */ - this.doc_ = win.document; - /** * @type {!Object} * @private @@ -260,12 +255,11 @@ class Registry { this.mutationObserver_ = null; /** - * All the observed DOM trees, including shadow trees. This is cleared out - * when the mutation observer is created. + * All the observed DOM trees, including shadow trees. * * @private @const {!Array} */ - this.observed_ = [win.document]; + this.roots_ = [win.document]; } /** @@ -346,7 +340,9 @@ class Registry { }; this.observe_(name); - this.upgrade(this.doc_, name); + this.roots_.forEach(tree => { + this.upgrade(tree, name); + }); } /** @@ -510,10 +506,12 @@ class Registry { }); this.mutationObserver_ = mo; - this.observed_.forEach(tree => { + // I would love to not have to hold onto all of the roots, since it's a + // memory leak. Unfortunately, there's no way to iterate a list and hold + // onto its contents weakly. + this.roots_.forEach(tree => { mo.observe(tree, TRACK_SUBTREE); }); - this.observed_.length = 0; installPatches(this.win_, this); } @@ -524,10 +522,9 @@ class Registry { * @param {!Node} tree */ observe(tree) { + this.roots_.push(tree); if (this.mutationObserver_) { this.mutationObserver_.observe(tree, TRACK_SUBTREE); - } else { - this.observed_.push(tree); } }