Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/zone.js/lib/browser/custom-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export function patchCustomElements(_global: any, api: _ZonePrivate) {
return;
}

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

api.patchCallbacks(api, _global.customElements, 'customElements', 'define', callbacks);
}
4 changes: 4 additions & 0 deletions packages/zone.js/lib/zone.configurations.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ declare global {
* disconnectedCallback() {}
* attributeChangedCallback(attrName, oldVal, newVal) {}
* adoptedCallback() {}
* formAssociatedCallback(form) {}
* formDisabledCallback(isDisabled) {}
* formResetCallback() {}
* formStateRestoreCallback(state, reason) {}
* }
*
* const zone = Zone.fork({name: 'myZone'});
Expand Down
33 changes: 32 additions & 1 deletion packages/zone.js/test/browser/custom-element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ function customElementsSupport() {
}
customElementsSupport.message = 'window.customElements';

function supportsFormAssociatedElements() {
return 'attachInternals' in HTMLElement.prototype;
}

describe('customElements', function() {
const testZone = Zone.current.fork({name: 'test'});
const bridge = {
connectedCallback: () => {},
disconnectedCallback: () => {},
adoptedCallback: () => {},
attributeChangedCallback: () => {}
attributeChangedCallback: () => {},
formAssociatedCallback: () => {}
};

class TestCustomElement extends HTMLElement {
Expand Down Expand Up @@ -51,8 +56,17 @@ describe('customElements', function() {
}
}

class TestFormAssociatedCustomElement extends HTMLElement {
static formAssociated = true;

formAssociatedCallback() {
return bridge.formAssociatedCallback();
}
}

testZone.run(() => {
customElements.define('x-test', TestCustomElement);
customElements.define('x-test-form-associated', TestFormAssociatedCustomElement);
});

let elt;
Expand All @@ -62,6 +76,7 @@ describe('customElements', function() {
bridge.disconnectedCallback = () => {};
bridge.attributeChangedCallback = () => {};
bridge.adoptedCallback = () => {};
bridge.formAssociatedCallback = () => {};
});

afterEach(() => {
Expand Down Expand Up @@ -105,4 +120,20 @@ describe('customElements', function() {
document.body.appendChild(elt);
elt.setAttribute('attr1', 'value1');
});

it('should work with formAssociatedCallback', function(done) {
if (!supportsFormAssociatedElements()) {
return;
}

bridge.formAssociatedCallback = function() {
expect(Zone.current.name).toBe(testZone.name);
done();
};

elt = document.createElement('x-test-form-associated');
const form = document.createElement('form');
form.appendChild(elt);
document.body.appendChild(form);
});
});