diff --git a/src/CustomElements/v1/CustomElements.js b/src/CustomElements/v1/CustomElements.js index 8c97199..65c96a4 100644 --- a/src/CustomElements/v1/CustomElements.js +++ b/src/CustomElements/v1/CustomElements.js @@ -143,9 +143,8 @@ this._definitions.set(localName, definition); // 5.1.22 - // The spec says we should upgrade all existing elements now, but if we - // defer we can do less tree walks - // scheduleUpgrade(); + // this causes an upgrade of the document + this._addNodes(document.childNodes); }, flush() { diff --git a/tests/CustomElements/v1/js/upgrade.js b/tests/CustomElements/v1/js/upgrade.js new file mode 100644 index 0000000..47bfdd4 --- /dev/null +++ b/tests/CustomElements/v1/js/upgrade.js @@ -0,0 +1,124 @@ +/** + * @license + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt + */ + +suite('upgrade', function() { + var work; + var assert = chai.assert; + + setup(function() { + work = document.createElement('div'); + document.body.appendChild(work); + }); + + teardown(function() { + document.body.removeChild(work); + }); + + function registerTestComponent(inName, inValue) { + var C = class extends HTMLElement { + constructor() { + customElements.setCurrentTag('inName'); + super(); + } + }; + C.prototype.value = inValue || 'value'; + customElements.define(inName, C); + } + + function testElements(node, selector, value) { + Array.prototype.forEach.call(node.querySelectorAll(selector), function(n) { + assert.equal(n.value, value); + }); + } + + test('custom element automatically upgrades', function(done) { + work.innerHTML = ''; + var x = work.firstChild; + // must flush after mutations to avoid false passes + customElements.flush(); + assert.isUndefined(x.value); + registerTestComponent('x-auto', 'auto'); + assert.equal(x.value, 'auto'); + done(); + }); + + test('custom element automatically upgrades in subtree', function(done) { + work.innerHTML = '
'; + var target = work.firstChild; + target.innerHTML = ''; + var x = target.firstChild; + // must flush after mutations to avoid false passes + customElements.flush(); + assert.isUndefined(x.value); + registerTestComponent('x-auto-sub', 'auto-sub'); + assert.equal(x.value, 'auto-sub'); + done(); + }); + + test('custom elements automatically upgrade', function(done) { + registerTestComponent('x-auto1', 'auto1'); + registerTestComponent('x-auto2', 'auto2'); + work.innerHTML = '
' + + '
' + + '
'; + customElements.flush(); + testElements(work, 'x-auto1', 'auto1'); + testElements(work, 'x-auto2', 'auto2'); + done(); + }); + + test('custom elements automatically upgrade in subtree', function(done) { + registerTestComponent('x-auto-sub1', 'auto-sub1'); + registerTestComponent('x-auto-sub2', 'auto-sub2'); + work.innerHTML = '
'; + var target = work.firstChild; + target.innerHTML = '
' + + '
' + + '
'; + customElements.flush(); + testElements(target, 'x-auto-sub1', 'auto-sub1'); + testElements(target, 'x-auto-sub2', 'auto-sub2'); + done(); + }); + + test('CustomElements.upgrade upgrades custom element syntax', function() { + registerTestComponent('x-foo31', 'foo'); + work.innerHTML = 'Foo'; + var xfoo = work.firstChild; + customElements.flush(); + assert.equal(xfoo.value, 'foo'); + }); + + test('mutation observer upgrades custom element syntax', function(done) { + registerTestComponent('x-foo32', 'foo'); + work.innerHTML = 'Foo'; + customElements.flush(); + var xfoo = work.firstChild; + assert.equal(xfoo.value, 'foo'); + done(); + }); + + test('document.register upgrades custom element syntax', function() { + work.innerHTML = 'Foo'; + registerTestComponent('x-foo33', 'foo'); + var xfoo = work.firstChild; + assert.equal(xfoo.value, 'foo'); + }); + + test('CustomElements.upgrade upgrades custom element syntax', function() { + registerTestComponent('x-zot', 'zot'); + registerTestComponent('x-zim', 'zim'); + work.innerHTML = ''; + var xzot = work.firstChild, xzim = xzot.firstChild; + customElements.flush(); + assert.equal(xzot.value, 'zot'); + assert.equal(xzim.value, 'zim'); + }); +}); diff --git a/tests/CustomElements/v1/runner.html b/tests/CustomElements/v1/runner.html index c6d3278..08b4007 100644 --- a/tests/CustomElements/v1/runner.html +++ b/tests/CustomElements/v1/runner.html @@ -25,7 +25,7 @@ // 'html/upgrade-dcl.html', // 'html/imports.html', // 'html/shadowdom.html', - // 'js/upgrade.js', + 'js/upgrade.js', // 'js/observe.js', // 'js/documentRegister.js', // 'html/throttled-attached.html'