diff --git a/src/wrappers/Document.js b/src/wrappers/Document.js index 53e09b6..b6ac111 100644 --- a/src/wrappers/Document.js +++ b/src/wrappers/Document.js @@ -110,7 +110,15 @@ if (document.registerElement) { var originalRegisterElement = document.registerElement; Document.prototype.registerElement = function(tagName, object) { - var prototype = object.prototype; + var prototype, extendsOption; + if (object !== undefined) { + prototype = object.prototype; + extendsOption = object.extends; + } + + if (!prototype) + prototype = Object.create(HTMLElement.prototype); + // If we already used the object as a prototype for another custom // element. @@ -171,13 +179,13 @@ }); var p = {prototype: newPrototype}; - if (object.extends) - p.extends = object.extends; + if (extendsOption) + p.extends = extendsOption; function CustomElementConstructor(node) { if (!node) { - if (object.extends) { - return document.createElement(object.extends, tagName); + if (extendsOption) { + return document.createElement(extendsOption, tagName); } else { return document.createElement(tagName); } diff --git a/test/js/Document.js b/test/js/Document.js index 0cc37ba..a5afd72 100644 --- a/test/js/Document.js +++ b/test/js/Document.js @@ -357,7 +357,7 @@ htmlSuite('Document', function() { }; var A = document.registerElement('x-a-span', - {prototype: aPrototype, extends: 'span'}); + {prototype: aPrototype, extends: 'span'}); var a1 = document.createElement('span', 'x-a-span'); assert.equal('span', a1.localName); @@ -554,6 +554,28 @@ htmlSuite('Document', function() { assert.isTrue(div.firstChild.isCustom); }); + test('document.registerElement optional option', function() { + if (!document.registerElement) + return; + + document.registerElement('x-a7'); + var a = document.createElement('x-a7'); + assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(a)), + HTMLElement.prototype); + + document.registerElement('x-a8', {}); + var a2 = document.createElement('x-a8'); + assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(a2)), + HTMLElement.prototype); + + document.registerElement('x-a-span-2', {extends: 'span'}); + var a3 = document.createElement('span', 'x-a-span-2'); + assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(a3)), + HTMLElement.prototype); + a3.localName = 'span'; + assert.equal('', a3.outerHTML); + }); + htmlTest('html/document-write.html'); htmlTest('html/head-then-body.html');