From 88c78c89c5441a9eab037263f3ea9d05386049cd Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Tue, 5 Nov 2013 10:34:34 -0500 Subject: [PATCH] Don't use native cloneNode --- src/wrappers/Node.js | 3 -- test/js/Node.js | 98 ++++++++++++++++++++++++++++++++++++++++++-- test/js/wrappers.js | 76 +--------------------------------- 3 files changed, 96 insertions(+), 81 deletions(-) diff --git a/src/wrappers/Node.js b/src/wrappers/Node.js index 55d981d..923fac1 100644 --- a/src/wrappers/Node.js +++ b/src/wrappers/Node.js @@ -477,9 +477,6 @@ }, cloneNode: function(deep) { - if (!this.invalidateShadowRenderer()) - return wrap(this.impl.cloneNode(deep)); - var clone = wrap(this.impl.cloneNode(false)); if (deep) { for (var child = this.firstChild; child; child = child.nextSibling) { diff --git a/test/js/Node.js b/test/js/Node.js index 95cdfc6..71db10a 100644 --- a/test/js/Node.js +++ b/test/js/Node.js @@ -7,6 +7,7 @@ suite('Node', function() { var wrap = ShadowDOMPolyfill.wrap; + var unwrap = ShadowDOMPolyfill.unwrap; var DOCUMENT_POSITION_DISCONNECTED = Node.DOCUMENT_POSITION_DISCONNECTED; var DOCUMENT_POSITION_PRECEDING = Node.DOCUMENT_POSITION_PRECEDING; @@ -186,16 +187,107 @@ suite('Node', function() { expectStructure(host2, {}); }); - + test('hasChildNodes without a shadow root', function() { var div = document.createElement('div'); - + assert.isFalse(div.hasChildNodes(), 'should be false with no children'); - + div.innerHTML = ''; assert.isTrue(div.hasChildNodes(), 'should be true with a single child'); div.innerHTML = ''; assert.isTrue(div.hasChildNodes(), 'should be true with multiple children'); }); + + test('parentElement', function() { + var a = document.createElement('a'); + a.textContent = 'text'; + var textNode = a.firstChild; + assert.equal(textNode.parentElement, a); + assert.isNull(a.parentElement); + + var doc = wrap(document); + var body = doc.body; + var documentElement = doc.documentElement; + assert.equal(body.parentElement, documentElement); + assert.isNull(documentElement.parentElement); + }); + + test('contains', function() { + var div = document.createElement('div'); + assert.isTrue(div.contains(div)); + + div.textContent = 'a'; + var textNode = div.firstChild; + assert.isTrue(textNode.contains(textNode)); + assert.isTrue(div.contains(textNode)); + assert.isFalse(textNode.contains(div)); + + var doc = div.ownerDocument; + assert.isTrue(doc.contains(doc)); + assert.isFalse(doc.contains(div)); + assert.isFalse(doc.contains(textNode)); + + assert.isFalse(div.contains(null)); + assert.isFalse(div.contains()); + }); + + test('instanceof', function() { + var div = document.createElement('div'); + assert.instanceOf(div, HTMLElement); + assert.instanceOf(div, Element); + assert.instanceOf(div, EventTarget); + }); + + test('cloneNode(false)', function() { + var doc = wrap(document); + var a = document.createElement('a'); + a.href = 'http://domain.com/'; + a.textContent = 'text'; + var textNode = a.firstChild; + + var aClone = a.cloneNode(false); + + assert.equal(aClone.tagName, 'A'); + assert.equal(aClone.href, 'http://domain.com/'); + expectStructure(aClone, {}); + }); + + test('cloneNode(true)', function() { + var doc = wrap(document); + var a = document.createElement('a'); + a.href = 'http://domain.com/'; + a.textContent = 'text'; + var textNode = a.firstChild; + + var aClone = a.cloneNode(true); + var textNodeClone = aClone.firstChild; + + assert.equal(aClone.tagName, 'A'); + assert.equal(aClone.href, 'http://domain.com/'); + expectStructure(aClone, { + firstChild: textNodeClone, + lastChild: textNodeClone + }); + expectStructure(textNodeClone, { + parentNode: aClone + }); + }); + + test('cloneNode with shadowRoot', function() { + var div = document.createElement('div'); + var a = div.appendChild(document.createElement('a')); + var sr = a.createShadowRoot(); + sr.innerHTML = ''; + div.offsetHeight; + assert.equal(unwrap(div).innerHTML, ''); + + var clone = div.cloneNode(true); + assert.equal(clone.innerHTML, ''); + clone.offsetHeight; + // shadow roots are not cloned. + assert.equal(unwrap(clone).innerHTML, ''); + }); + }); diff --git a/test/js/wrappers.js b/test/js/wrappers.js index 7ba059f..5c6957b 100644 --- a/test/js/wrappers.js +++ b/test/js/wrappers.js @@ -7,6 +7,7 @@ suite('Wrapper creation', function() { var wrap = ShadowDOMPolyfill.wrap; + var unwrap = ShadowDOMPolyfill.unwrap; var knownElements = ShadowDOMPolyfill.knownElements; test('Br element wrapper', function() { @@ -28,79 +29,4 @@ suite('Wrapper creation', function() { }); }); - test('cloneNode(false)', function() { - var doc = wrap(document); - var a = document.createElement('a'); - a.href = 'http://domain.com/'; - a.textContent = 'text'; - var textNode = a.firstChild; - - var aClone = a.cloneNode(false); - - assert.equal(aClone.tagName, 'A'); - assert.equal(aClone.href, 'http://domain.com/'); - expectStructure(aClone, {}); - }); - - test('cloneNode(true)', function() { - var doc = wrap(document); - var a = document.createElement('a'); - a.href = 'http://domain.com/'; - a.textContent = 'text'; - var textNode = a.firstChild; - - var aClone = a.cloneNode(true); - var textNodeClone = aClone.firstChild; - - assert.equal(aClone.tagName, 'A'); - assert.equal(aClone.href, 'http://domain.com/'); - expectStructure(aClone, { - firstChild: textNodeClone, - lastChild: textNodeClone - }); - expectStructure(textNodeClone, { - parentNode: aClone - }); - }); - - test('parentElement', function() { - var a = document.createElement('a'); - a.textContent = 'text'; - var textNode = a.firstChild; - assert.equal(textNode.parentElement, a); - assert.isNull(a.parentElement); - - var doc = wrap(document); - var body = doc.body; - var documentElement = doc.documentElement; - assert.equal(body.parentElement, documentElement); - assert.isNull(documentElement.parentElement); - }); - - test('contains', function() { - var div = document.createElement('div'); - assert.isTrue(div.contains(div)); - - div.textContent = 'a'; - var textNode = div.firstChild; - assert.isTrue(textNode.contains(textNode)); - assert.isTrue(div.contains(textNode)); - assert.isFalse(textNode.contains(div)); - - var doc = div.ownerDocument; - assert.isTrue(doc.contains(doc)); - assert.isFalse(doc.contains(div)); - assert.isFalse(doc.contains(textNode)); - - assert.isFalse(div.contains(null)); - assert.isFalse(div.contains()); - }); - - test('instanceof', function() { - var div = document.createElement('div'); - assert.instanceOf(div, HTMLElement); - assert.instanceOf(div, Element); - assert.instanceOf(div, EventTarget); - }); - });