Skip to content

Commit

Permalink
fix: only inits form association if element is form associated
Browse files Browse the repository at this point in the history
  • Loading branch information
enjoythelive1 committed Oct 11, 2023
1 parent 17dbe63 commit 8b32cdc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/mutation-observers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export function observerCallback(mutationList: MutationRecord[]) {
const formElements = formElementsMap.get(node as unknown as HTMLFormElement);
const walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT, {
acceptNode(node: ICustomElement): number {
return internalsMap.has(node) && !formElements && !formElements.has(node) ?
NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
return (
internalsMap.has(node) && node.constructor['formAssociated'] && !formElements && !formElements.has(node)
) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});

Expand Down
11 changes: 11 additions & 0 deletions test/ElementInternals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ describe('The ElementInternals polyfill', () => {
constructor() {
super();
this.internals = this.attachInternals();
this.internals.role = 'generic';
}
}

Expand All @@ -199,6 +200,16 @@ describe('The ElementInternals polyfill', () => {
it('will throw from setFormValue', async () => {
expect(() => element.internals.setFormValue('foo')).to.throw();
});

describe('inside a form', () => {
let form;

afterEach(async () => await fixtureCleanup(form));

it('will not throw', async () => {
form = await fixture(html`<form><not-associated></not-associated></form>`);
})
})
});

describe('inside a custom element with a form', () => {
Expand Down

0 comments on commit 8b32cdc

Please sign in to comment.