/** * @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('HTMLContentElement', function() { var unwrap = ShadowDOMPolyfill.unwrap; test('select', function() { var el = document.createElement('content'); assert.equal(el.select, null); el.select = '.xxx'; assert.equal(el.select, '.xxx'); assert.isTrue(el.hasAttribute('select')); assert.equal(el.getAttribute('select'), '.xxx'); el.select = '.xxx'; assert.equal(el.select, '.xxx'); assert.isTrue(el.hasAttribute('select')); assert.equal(el.getAttribute('select'), '.xxx'); }); test('getDistributedNodes', function() { var host = document.createElement('div'); host.innerHTML = 'ab'; var a = host.firstChild; var b = host.lastChild; var sr = host.createShadowRoot(); sr.innerHTML = ''; var content = sr.firstChild; assertArrayEqual(content.getDistributedNodes(), [a, b]); content.select = 'a'; assertArrayEqual(content.getDistributedNodes(), [a]); content.select = 'b'; assertArrayEqual(content.getDistributedNodes(), [b]); }); test('getDistributedNodes add document fragment with content', function() { var host = document.createElement('div'); host.innerHTML = ' '; var root = host.createShadowRoot(); var df = document.createDocumentFragment(); df.appendChild(document.createTextNode(' ')); var content = df.appendChild(document.createElement('content')); df.appendChild(document.createTextNode(' ')); root.appendChild(df); assert.equal(content.getDistributedNodes().length, 7); assertArrayEqual(content.getDistributedNodes(), host.childNodes); }); test('getDistributedNodes add content deep inside tree', function() { var host = document.createElement('div'); host.innerHTML = ' '; var root = host.createShadowRoot(); var b = document.createElement('b'); var content = b.appendChild(document.createElement('content')); content.select = '*'; root.appendChild(b); assert.equal(content.getDistributedNodes().length, 3); assertArrayEqual(content.getDistributedNodes(), host.children); }); test('getDistributedNodes add content deeper inside tree', function() { var foo = document.createElement('div'); var fooRoot = foo.createShadowRoot(); fooRoot.innerHTML = '
' + '
item1
item2
item3
' + '
'; var bar = fooRoot.firstChild; var barRoot = bar.createShadowRoot(); barRoot.innerHTML = '
'; var zot = barRoot.firstChild; var zotRoot = zot.createShadowRoot(); zotRoot.innerHTML = ''; var content = zotRoot.firstChild; assert.equal(content.getDistributedNodes().length, 3); assertArrayEqual(content.getDistributedNodes(), fooRoot.firstChild.children); }); test('Adding tree with content again', function() { var host = document.createElement('div'); host.innerHTML = '

Content

'; var t = document.createElement('template'); t.innerHTML = '
[]
'; var sr = host.createShadowRoot(); sr.appendChild(t.content.cloneNode(true)); host.offsetHeight; assert.equal(unwrap(host).innerHTML, '
[

Content

]
'); }); test('adding a new content element to a shadow tree', function() { var host = document.createElement('div'); host.innerHTML = ''; var a = host.firstChild; var b = host.lastChild; var sr = host.createShadowRoot(); sr.innerHTML = ''; var c = sr.firstChild; host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); var content = document.createElement('content'); content.select = 'b'; c.appendChild(content); host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); c.removeChild(content); host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); }); test('adding a new content element to a shadow tree 2', function() { var host = document.createElement('div'); host.innerHTML = ''; var a = host.firstChild; var b = host.lastChild; var sr = host.createShadowRoot(); sr.innerHTML = ''; var c = sr.firstChild; host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); var d = document.createElement('d'); var content = d.appendChild(document.createElement('content')); content.select = 'b'; c.appendChild(d); host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); c.removeChild(d); host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); }); test('restricting select further', function() { var host = document.createElement('div'); host.innerHTML = ''; var a = host.firstChild; var b = host.lastChild; var sr = host.createShadowRoot(); sr.innerHTML = ''; var c = sr.firstChild; var content = c.firstChild; host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); content.select = 'b'; host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); }); test('Mutating content fallback', function() { var host = document.createElement('div'); host.innerHTML = ''; var a = host.firstChild; var sr = host.createShadowRoot(); sr.innerHTML = ''; var content = sr.firstChild; host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); content.textContent = 'fallback'; host.offsetHeight; assert.equal(unwrap(host).innerHTML, 'fallback'); var b = content.appendChild(document.createElement('b')); host.offsetHeight; assert.equal(unwrap(host).innerHTML, 'fallback'); content.removeChild(b); host.offsetHeight; assert.equal(unwrap(host).innerHTML, 'fallback'); }); test('Mutating content fallback 2', function() { var host = document.createElement('div'); host.innerHTML = ''; var a = host.firstChild; var sr = host.createShadowRoot(); sr.innerHTML = ''; var b = sr.firstChild; var content = b.firstChild; host.offsetHeight; assert.equal(unwrap(host).innerHTML, ''); content.textContent = 'fallback'; host.offsetHeight; assert.equal(unwrap(host).innerHTML, 'fallback'); var c = content.appendChild(document.createElement('c')); host.offsetHeight; assert.equal(unwrap(host).innerHTML, 'fallback'); content.removeChild(c); host.offsetHeight; assert.equal(unwrap(host).innerHTML, 'fallback'); }); test('getDistributedNodes outside shadow dom', function() { var content = document.createElement('content'); assert.deepEqual(content.getDistributedNodes(), []); }); test('instanceof', function() { assert.instanceOf(document.createElement('content'), HTMLContentElement); }); test('constructor', function() { assert.equal(HTMLContentElement, document.createElement('content').constructor); }); });