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

Commit

Permalink
document.registerElement option argument should be optional
Browse files Browse the repository at this point in the history
Fixes #406
  • Loading branch information
arv committed Mar 24, 2014
1 parent 0658c23 commit 4a630bc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/wrappers/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 23 additions & 1 deletion test/js/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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('<span is="x-a-span-2"></span>', a3.outerHTML);
});

htmlTest('html/document-write.html');

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

0 comments on commit 4a630bc

Please sign in to comment.