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

Commit

Permalink
Preserve 'extends' when the ShadowDOM polyfill patches document.register
Browse files Browse the repository at this point in the history
Before this change this worked well in combination with the custom elements polyfill, however it failed with the native document.register.
  • Loading branch information
sigmundch committed Nov 2, 2013
1 parent 589410b commit 71cccbf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/wrappers/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions test/js/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<span is="x-a-span"></span>', 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('<span is="x-a-span"></span>', 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;
Expand Down

0 comments on commit 71cccbf

Please sign in to comment.