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

Commit 3bf48ce

Browse files
committed
Add a CustomElements.instanceOf(obj, actor) method to support custom elements implemented on systems without Object.__proto__ (e.g. IE10).
1 parent 18efac1 commit 3bf48ce

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/CustomElements.js

+26
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,32 @@ if (useNative) {
407407
scope.upgrade = upgradeElement;
408408
}
409409

410+
// Create a custom 'instanceof'. This is necessary when CustomElements
411+
// are implemented via a mixin strategy, as for example on IE10.
412+
var isInstance;
413+
if (!Object.__proto__ && !useNative) {
414+
isInstance = function(obj, ctor) {
415+
var p = obj;
416+
while (p) {
417+
// NOTE: this is not technically correct since we're not checking if
418+
// an object is an instance of a constructor; however, this should
419+
// be good enough for the mixin strategy.
420+
if (p === ctor.prototype) {
421+
return true;
422+
}
423+
p = p.__proto__;
424+
}
425+
return false;
426+
}
427+
} else {
428+
isInstance = function(obj, base) {
429+
return obj instanceof base;
430+
}
431+
}
432+
433+
// exports
434+
scope.instanceof = isInstance;
435+
410436
// bc
411437
document.register = document.registerElement;
412438

test/js/customElements.js

+30
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,36 @@
413413
CustomElements.takeRecords();
414414
assert.deepEqual(['a', 'b', 'c', 'd', 'e'], log);
415415
});
416+
417+
test('instanceof', function() {
418+
var p = Object.create(HTMLElement.prototype);
419+
var PCtor = document.registerElement('x-instance', {prototype: p});
420+
var x = document.createElement('x-instance');
421+
assert.isTrue(CustomElements.instanceof(x, PCtor), 'instanceof failed for x-instance');
422+
423+
var p2 = Object.create(PCtor.prototype);
424+
var P2Ctor = document.registerElement('x-instance2', {prototype: p2});
425+
var x2 = document.createElement('x-instance2');
426+
assert.isTrue(CustomElements.instanceof(x2, P2Ctor), 'instanceof failed for x-instance2');
427+
assert.isTrue(CustomElements.instanceof(x2, PCtor), 'instanceof failed for x-instance2');
428+
});
429+
430+
431+
test('instanceof typeExtension', function() {
432+
var p = Object.create(HTMLButtonElement.prototype);
433+
var PCtor = document.registerElement('x-button-instance', {prototype: p, extends: 'button'});
434+
var x = document.createElement('button', 'x-button-instance');
435+
assert.isTrue(CustomElements.instanceof(x, PCtor), 'instanceof failed for x-button-instance');
436+
assert.isTrue(CustomElements.instanceof(x, HTMLButtonElement), 'instanceof failed for x-button-instance');
437+
438+
var p2 = Object.create(PCtor.prototype);
439+
var P2Ctor = document.registerElement('x-button-instance2', {prototype: p2, extends: 'button'});
440+
var x2 = document.createElement('button','x-button-instance2');
441+
assert.isTrue(CustomElements.instanceof(x2, P2Ctor), 'instanceof failed for x-button-instance2');
442+
assert.isTrue(CustomElements.instanceof(x2, PCtor), 'instanceof failed for x-button-instance2');
443+
assert.isTrue(CustomElements.instanceof(x2, HTMLButtonElement), 'instanceof failed for x-button-instance2');
444+
});
445+
416446
});
417447

418448
htmlSuite('customElements (html)', function() {

0 commit comments

Comments
 (0)