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

Commit

Permalink
Add a CustomElements.instanceOf(obj, actor) method to support custom …
Browse files Browse the repository at this point in the history
…elements implemented on systems without Object.__proto__ (e.g. IE10).
  • Loading branch information
sorvell committed Feb 19, 2014
1 parent 18efac1 commit 3bf48ce
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,32 @@ if (useNative) {
scope.upgrade = upgradeElement;
}

// Create a custom 'instanceof'. This is necessary when CustomElements
// are implemented via a mixin strategy, as for example on IE10.
var isInstance;
if (!Object.__proto__ && !useNative) {
isInstance = function(obj, ctor) {
var p = obj;
while (p) {
// NOTE: this is not technically correct since we're not checking if
// an object is an instance of a constructor; however, this should
// be good enough for the mixin strategy.
if (p === ctor.prototype) {
return true;
}
p = p.__proto__;
}
return false;
}
} else {
isInstance = function(obj, base) {
return obj instanceof base;
}
}

// exports
scope.instanceof = isInstance;

// bc
document.register = document.registerElement;

Expand Down
30 changes: 30 additions & 0 deletions test/js/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,36 @@
CustomElements.takeRecords();
assert.deepEqual(['a', 'b', 'c', 'd', 'e'], log);
});

test('instanceof', function() {
var p = Object.create(HTMLElement.prototype);
var PCtor = document.registerElement('x-instance', {prototype: p});
var x = document.createElement('x-instance');
assert.isTrue(CustomElements.instanceof(x, PCtor), 'instanceof failed for x-instance');

var p2 = Object.create(PCtor.prototype);
var P2Ctor = document.registerElement('x-instance2', {prototype: p2});
var x2 = document.createElement('x-instance2');
assert.isTrue(CustomElements.instanceof(x2, P2Ctor), 'instanceof failed for x-instance2');
assert.isTrue(CustomElements.instanceof(x2, PCtor), 'instanceof failed for x-instance2');
});


test('instanceof typeExtension', function() {
var p = Object.create(HTMLButtonElement.prototype);
var PCtor = document.registerElement('x-button-instance', {prototype: p, extends: 'button'});
var x = document.createElement('button', 'x-button-instance');
assert.isTrue(CustomElements.instanceof(x, PCtor), 'instanceof failed for x-button-instance');
assert.isTrue(CustomElements.instanceof(x, HTMLButtonElement), 'instanceof failed for x-button-instance');

var p2 = Object.create(PCtor.prototype);
var P2Ctor = document.registerElement('x-button-instance2', {prototype: p2, extends: 'button'});
var x2 = document.createElement('button','x-button-instance2');
assert.isTrue(CustomElements.instanceof(x2, P2Ctor), 'instanceof failed for x-button-instance2');
assert.isTrue(CustomElements.instanceof(x2, PCtor), 'instanceof failed for x-button-instance2');
assert.isTrue(CustomElements.instanceof(x2, HTMLButtonElement), 'instanceof failed for x-button-instance2');
});

});

htmlSuite('customElements (html)', function() {
Expand Down

0 comments on commit 3bf48ce

Please sign in to comment.