From 71cccbf65ee17cd990fa741aaef8e8caedb041da Mon Sep 17 00:00:00 2001 From: sigmundch Date: Fri, 1 Nov 2013 14:10:37 -0700 Subject: [PATCH] Preserve 'extends' when the ShadowDOM polyfill patches document.register Before this change this worked well in combination with the custom elements polyfill, however it failed with the native document.register. --- src/wrappers/Document.js | 15 +++++++++++---- test/js/Document.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/wrappers/Document.js b/src/wrappers/Document.js index e132592..c12830a 100644 --- a/src/wrappers/Document.js +++ b/src/wrappers/Document.js @@ -150,12 +150,19 @@ }; }); - var nativeConstructor = originalRegister.call(unwrap(this), tagName, - {prototype: newPrototype}); + var p = {prototype: newPrototype}; + if (object.extends) + p.extends = object.extends; + var nativeConstructor = originalRegister.call(unwrap(this), tagName, p); function GeneratedWrapper(node) { - if (!node) - return document.createElement(tagName); + if (!node) { + if (object.extends) { + return document.createElement(object.extends, tagName); + } else { + return document.createElement(tagName); + } + } this.impl = node; } GeneratedWrapper.prototype = prototype; diff --git a/test/js/Document.js b/test/js/Document.js index 30e5769..48228bf 100644 --- a/test/js/Document.js +++ b/test/js/Document.js @@ -310,6 +310,35 @@ htmlSuite('Document', function() { assert.equal(b2.getName(), 'b'); }); + test('document.register type extension', function() { + if (!document.register) + return; + + var aPrototype = Object.create(HTMLSpanElement.prototype); + aPrototype.getName = function() { + return 'a'; + }; + + var A = document.register('x-a-span', + {prototype: aPrototype, extends: 'span'}); + + var a1 = document.createElement('span', 'x-a-span'); + assert.equal('span', a1.localName); + assert.equal('', a1.outerHTML); + assert.equal(Object.getPrototypeOf(a1), aPrototype); + assert.instanceOf(a1, A); + assert.instanceOf(a1, HTMLSpanElement); + assert.equal(a1.getName(), 'a'); + + var a2 = new A(); + assert.equal('span', a2.localName); + assert.equal('', a2.outerHTML); + assert.equal(Object.getPrototypeOf(a2), aPrototype); + assert.instanceOf(a2, A); + assert.instanceOf(a2, HTMLSpanElement); + assert.equal(a2.getName(), 'a'); + }); + test('document.register deeper', function() { if (!document.register) return;