Skip to content

Commit 1c990cd

Browse files
arturovtAndrewKushnir
authored andcommitted
fix(zone.js): patch form-associated custom element callbacks (#50686)
This commit updates the implementation of the `customElements` patch and also patches FACE callbacks (`formAssociatedCallback`, `formDisabledCallback`, `formResetCallback` and `formStateRestoreCallback`). This now allows invoking those callbacks in the same zone where the custom element has been defined. PR Close #50686
1 parent 629a222 commit 1c990cd

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

packages/zone.js/lib/browser/custom-elements.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ export function patchCustomElements(_global: any, api: _ZonePrivate) {
1212
return;
1313
}
1414

15-
const callbacks =
16-
['connectedCallback', 'disconnectedCallback', 'adoptedCallback', 'attributeChangedCallback'];
15+
// https://html.spec.whatwg.org/multipage/custom-elements.html#concept-custom-element-definition-lifecycle-callbacks
16+
const callbacks = [
17+
'connectedCallback', 'disconnectedCallback', 'adoptedCallback', 'attributeChangedCallback',
18+
'formAssociatedCallback', 'formDisabledCallback', 'formResetCallback',
19+
'formStateRestoreCallback'
20+
];
1721

1822
api.patchCallbacks(api, _global.customElements, 'customElements', 'define', callbacks);
1923
}

packages/zone.js/lib/zone.configurations.api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ declare global {
414414
* disconnectedCallback() {}
415415
* attributeChangedCallback(attrName, oldVal, newVal) {}
416416
* adoptedCallback() {}
417+
* formAssociatedCallback(form) {}
418+
* formDisabledCallback(isDisabled) {}
419+
* formResetCallback() {}
420+
* formStateRestoreCallback(state, reason) {}
417421
* }
418422
*
419423
* const zone = Zone.fork({name: 'myZone'});

packages/zone.js/test/browser/custom-element.spec.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ function customElementsSupport() {
1616
}
1717
customElementsSupport.message = 'window.customElements';
1818

19+
function supportsFormAssociatedElements() {
20+
return 'attachInternals' in HTMLElement.prototype;
21+
}
22+
1923
describe('customElements', function() {
2024
const testZone = Zone.current.fork({name: 'test'});
2125
const bridge = {
2226
connectedCallback: () => {},
2327
disconnectedCallback: () => {},
2428
adoptedCallback: () => {},
25-
attributeChangedCallback: () => {}
29+
attributeChangedCallback: () => {},
30+
formAssociatedCallback: () => {}
2631
};
2732

2833
class TestCustomElement extends HTMLElement {
@@ -51,8 +56,17 @@ describe('customElements', function() {
5156
}
5257
}
5358

59+
class TestFormAssociatedCustomElement extends HTMLElement {
60+
static formAssociated = true;
61+
62+
formAssociatedCallback() {
63+
return bridge.formAssociatedCallback();
64+
}
65+
}
66+
5467
testZone.run(() => {
5568
customElements.define('x-test', TestCustomElement);
69+
customElements.define('x-test-form-associated', TestFormAssociatedCustomElement);
5670
});
5771

5872
let elt;
@@ -62,6 +76,7 @@ describe('customElements', function() {
6276
bridge.disconnectedCallback = () => {};
6377
bridge.attributeChangedCallback = () => {};
6478
bridge.adoptedCallback = () => {};
79+
bridge.formAssociatedCallback = () => {};
6580
});
6681

6782
afterEach(() => {
@@ -105,4 +120,20 @@ describe('customElements', function() {
105120
document.body.appendChild(elt);
106121
elt.setAttribute('attr1', 'value1');
107122
});
123+
124+
it('should work with formAssociatedCallback', function(done) {
125+
if (!supportsFormAssociatedElements()) {
126+
return;
127+
}
128+
129+
bridge.formAssociatedCallback = function() {
130+
expect(Zone.current.name).toBe(testZone.name);
131+
done();
132+
};
133+
134+
elt = document.createElement('x-test-form-associated');
135+
const form = document.createElement('form');
136+
form.appendChild(elt);
137+
document.body.appendChild(form);
138+
});
108139
});

0 commit comments

Comments
 (0)