Skip to content

Commit 1e05937

Browse files
feat(polyfill): now fires invalid and valid events
1 parent 00cbf52 commit 1e05937

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Element Internals Polyfill
2+
3+
This package is a polyfill for the [`ElementInternals` standard](https://html.spec.whatwg.org/multipage/custom-elements.html#the-elementinternals-interface). The specification is supported by current releases of Google's Chrome.

Diff for: src/element-internals.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class ElementInternals {
2727

2828
checkValidity() {
2929
const validity = validityMap.get(this);
30+
const ref = refMap.get(this);
31+
const validityEvent = new Event(validity.valid, {
32+
bubbles: false,
33+
cancelable: true,
34+
composed: false
35+
});
36+
ref.dispatchEvent(validityEvent)
3037
return validity.valid;
3138
}
3239

@@ -47,7 +54,7 @@ class ElementInternals {
4754
}
4855

4956
reportValidity() {
50-
// TODO: Figure out how to polyfill this
57+
return this.checkValidity();
5158
}
5259

5360
setFormValue(value) {

Diff for: src/maps.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export const validityMap = new WeakMap();
33
export const hiddenInputMap = new WeakMap();
44
export const internalsMap = new WeakMap();
55
export const validationMessageMap = new WeakMap();
6+
export const formsMap = new WeakMap();

Diff for: src/utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hiddenInputMap } from './maps.js';
1+
import { hiddenInputMap, formsMap } from './maps.js';
22

33
const observerConfig = { attributes: true };
44

@@ -60,6 +60,8 @@ export const initForm = (ref, form, internals) => {
6060
ref.formResetCallback();
6161
}
6262
});
63+
64+
formsMap.set(form, { ref, internals });
6365
}
6466
};
6567

Diff for: test/polyfill.test.js

-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ describe('The ElementInternals polyfill', () => {
272272

273273
setTimeout(() => {
274274
expect(element.internals.labels[0]).toBe(label);
275-
expect(element.getAttribute('aria-labelledby')).toBe(`${tagName}_Label`);
276-
expect(label.getAttribute('id', `${tagName}_Label`));
277275
done();
278276
}, 500);
279277
});

0 commit comments

Comments
 (0)