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

Commit d3c785a

Browse files
committed
feat: support document.registerElement
Closes #18
1 parent ad711b8 commit d3c785a

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

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

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* check that document.registerElement(name, { prototype: proto });
3+
* is properly patched
4+
*/
5+
6+
'use strict';
7+
8+
describe('document.registerElement', function () {
9+
if (!'registerElement' in document) {
10+
console.log('WARNING: skipping document.registerElement test (missing this API)');
11+
return;
12+
}
13+
14+
var flag, hasParent;
15+
16+
// register a custom element for each callback
17+
var callbacks = [
18+
'created',
19+
'attached',
20+
'detached',
21+
'attributeChanged'
22+
];
23+
24+
function flagAndCheckZone() {
25+
flag = true;
26+
hasParent = !!window.zone.parent;
27+
}
28+
29+
var customElements = callbacks.map(function (callbackName) {
30+
var fullCallbackName = callbackName + 'Callback';
31+
var proto = Object.create(HTMLElement.prototype);
32+
proto[fullCallbackName] = flagAndCheckZone;
33+
return document.registerElement('x-' + callbackName.toLowerCase(), {
34+
prototype: proto
35+
});
36+
});
37+
38+
39+
beforeEach(function () {
40+
flag = false;
41+
hasParent = false;
42+
});
43+
44+
45+
it('should work with createdCallback', function () {
46+
runs(function () {
47+
var elt = document.createElement('x-created');
48+
});
49+
50+
waitsFor(function() {
51+
return flag;
52+
}, 'createdCallback to fire', 100);
53+
54+
runs(function() {
55+
expect(hasParent).toBe(true);
56+
});
57+
});
58+
59+
60+
it('should work with attachedCallback', function () {
61+
runs(function () {
62+
var elt = document.createElement('x-attached');
63+
document.body.appendChild(elt);
64+
document.body.removeChild(elt);
65+
});
66+
67+
waitsFor(function() {
68+
return flag;
69+
}, 'attachedCallback to fire', 100);
70+
71+
runs(function() {
72+
expect(hasParent).toBe(true);
73+
});
74+
});
75+
76+
77+
it('should work with detachedCallback', function () {
78+
runs(function () {
79+
var elt = document.createElement('x-detached');
80+
document.body.appendChild(elt);
81+
document.body.removeChild(elt);
82+
});
83+
84+
waitsFor(function() {
85+
return flag;
86+
}, 'detachedCallback to fire', 100);
87+
88+
runs(function() {
89+
expect(hasParent).toBe(true);
90+
});
91+
});
92+
93+
94+
it('should work with attributeChanged', function () {
95+
runs(function () {
96+
var elt = document.createElement('x-attributechanged');
97+
elt.id = 'bar';
98+
});
99+
100+
waitsFor(function() {
101+
return flag;
102+
}, 'attributeChanged to fire', 100);
103+
104+
runs(function() {
105+
expect(hasParent).toBe(true);
106+
});
107+
});
108+
109+
});

Diff for: zone.js

+22
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ Zone.patch = function patch () {
366366
}
367367
Zone.patchMutationObserverClass('MutationObserver');
368368
Zone.patchMutationObserverClass('WebKitMutationObserver');
369+
Zone.patchRegisterElement();
369370
};
370371

371372
//
@@ -510,6 +511,27 @@ Zone.patchMutationObserverClass = function (className) {
510511
}
511512
};
512513

514+
Zone.patchRegisterElement = function () {
515+
if (!'registerElement' in document) {
516+
return;
517+
}
518+
var _registerElement = document.registerElement;
519+
var callbacks = [
520+
'createdCallback',
521+
'attachedCallback',
522+
'detachedCallback',
523+
'attributeChangedCallback'
524+
];
525+
document.registerElement = function (name, opts) {
526+
callbacks.forEach(function (callback) {
527+
if (opts.prototype[callback]) {
528+
opts.prototype[callback] = zone.bind(opts.prototype[callback]);
529+
}
530+
});
531+
return _registerElement.apply(document, [name, opts]);
532+
};
533+
}
534+
513535
Zone.eventNames = 'copy cut paste abort blur focus canplay canplaythrough change click contextmenu dblclick drag dragend dragenter dragleave dragover dragstart drop durationchange emptied ended input invalid keydown keypress keyup load loadeddata loadedmetadata loadstart mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup pause play playing progress ratechange reset scroll seeked seeking select show stalled submit suspend timeupdate volumechange waiting mozfullscreenchange mozfullscreenerror mozpointerlockchange mozpointerlockerror error'.split(' ');
514536
Zone.onEventNames = Zone.eventNames.map(function (property) {
515537
return 'on' + property;

0 commit comments

Comments
 (0)