Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 8bce00e

Browse files
committed
fix(register-element): add check for callback being own property of opts
Using other component libraries, like Polymer, alongside Angular2+zones would cause errors because the descriptor was undefined after calling getOwnPropertyDescriptor on objects which had inherited callbacks that were not their own properties. Fixes #52
1 parent a442a32 commit 8bce00e

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

Diff for: lib/patch/register-element.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ function apply() {
1616
];
1717

1818
document.registerElement = function (name, opts) {
19-
callbacks.forEach(function (callback) {
20-
if (opts.prototype[callback]) {
21-
var descriptor = Object.getOwnPropertyDescriptor(opts.prototype, callback);
22-
if (descriptor.value) {
23-
descriptor.value = global.zone.bind(descriptor.value || opts.prototype[callback]);
24-
_redefineProperty(opts.prototype, callback, descriptor);
19+
if (opts && opts.prototype) {
20+
callbacks.forEach(function (callback) {
21+
if (opts.prototype.hasOwnProperty(callback)) {
22+
var descriptor = Object.getOwnPropertyDescriptor(opts.prototype, callback);
23+
if (descriptor.value) {
24+
descriptor.value = global.zone.bind(descriptor.value);
25+
_redefineProperty(opts.prototype, callback, descriptor);
26+
} else {
27+
opts.prototype[callback] = global.zone.bind(opts.prototype[callback]);
28+
}
29+
} else if (opts.prototype[callback]) {
30+
opts.prototype[callback] = global.zone.bind(opts.prototype[callback]);
2531
}
26-
}
27-
});
32+
});
33+
}
34+
2835
return _registerElement.apply(document, [name, opts]);
2936
};
3037
}

Diff for: test/patch/registerElement.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,33 @@ describe('document.registerElement', ifEnvSupports(registerElement, function ()
129129
var elt = document.createElement('x-props-desc');
130130
});
131131

132+
133+
it('should check bind callback if not own property', function (done) {
134+
testZone.run(function() {
135+
var originalProto = {
136+
createdCallback: checkZone
137+
};
138+
139+
var secondaryProto = Object.create(originalProto);
140+
expect(secondaryProto.createdCallback).toBe(originalProto.createdCallback);
141+
142+
document.registerElement('x-inherited-callback', { prototype: secondaryProto });
143+
expect(secondaryProto.createdCallback).not.toBe(originalProto.createdCallback);
144+
145+
function checkZone() {
146+
expect(window.zone).toBeDirectChildOf(testZone);
147+
done();
148+
}
149+
150+
var elt = document.createElement('x-inherited-callback');
151+
});
152+
});
153+
154+
155+
it('should not throw if no options passed to registerElement', function () {
156+
expect(function() {
157+
document.registerElement('x-no-opts');
158+
}).not.toThrow();
159+
});
160+
132161
}));

0 commit comments

Comments
 (0)