Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit d7629dd

Browse files
committed
Merge pull request #407 from arv/register-element-optional
document.registerElement option argument should be optional
2 parents 0658c23 + 4a630bc commit d7629dd

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/wrappers/Document.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@
110110
if (document.registerElement) {
111111
var originalRegisterElement = document.registerElement;
112112
Document.prototype.registerElement = function(tagName, object) {
113-
var prototype = object.prototype;
113+
var prototype, extendsOption;
114+
if (object !== undefined) {
115+
prototype = object.prototype;
116+
extendsOption = object.extends;
117+
}
118+
119+
if (!prototype)
120+
prototype = Object.create(HTMLElement.prototype);
121+
114122

115123
// If we already used the object as a prototype for another custom
116124
// element.
@@ -171,13 +179,13 @@
171179
});
172180

173181
var p = {prototype: newPrototype};
174-
if (object.extends)
175-
p.extends = object.extends;
182+
if (extendsOption)
183+
p.extends = extendsOption;
176184

177185
function CustomElementConstructor(node) {
178186
if (!node) {
179-
if (object.extends) {
180-
return document.createElement(object.extends, tagName);
187+
if (extendsOption) {
188+
return document.createElement(extendsOption, tagName);
181189
} else {
182190
return document.createElement(tagName);
183191
}

test/js/Document.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ htmlSuite('Document', function() {
357357
};
358358

359359
var A = document.registerElement('x-a-span',
360-
{prototype: aPrototype, extends: 'span'});
360+
{prototype: aPrototype, extends: 'span'});
361361

362362
var a1 = document.createElement('span', 'x-a-span');
363363
assert.equal('span', a1.localName);
@@ -554,6 +554,28 @@ htmlSuite('Document', function() {
554554
assert.isTrue(div.firstChild.isCustom);
555555
});
556556

557+
test('document.registerElement optional option', function() {
558+
if (!document.registerElement)
559+
return;
560+
561+
document.registerElement('x-a7');
562+
var a = document.createElement('x-a7');
563+
assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(a)),
564+
HTMLElement.prototype);
565+
566+
document.registerElement('x-a8', {});
567+
var a2 = document.createElement('x-a8');
568+
assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(a2)),
569+
HTMLElement.prototype);
570+
571+
document.registerElement('x-a-span-2', {extends: 'span'});
572+
var a3 = document.createElement('span', 'x-a-span-2');
573+
assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(a3)),
574+
HTMLElement.prototype);
575+
a3.localName = 'span';
576+
assert.equal('<span is="x-a-span-2"></span>', a3.outerHTML);
577+
});
578+
557579
htmlTest('html/document-write.html');
558580

559581
htmlTest('html/head-then-body.html');

0 commit comments

Comments
 (0)