Skip to content

Commit 850e835

Browse files
authored
Fix: Improve feature detection to support Firefox 93 (#46)
* fix: added more feature detection for element internals * fix: override attachInternals on HTMLElement.prototype instead of Element.prototype to support override on Firefox * fix(isElementInternalsSupported): avoid creating multiple elements during feature detection
1 parent 9ba2188 commit 850e835

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

Diff for: src/element-internals.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,38 @@ if (!window.CustomStateSet) {
251251
window.CustomStateSet = CustomStateSet;
252252
}
253253

254-
if (!window.ElementInternals) {
254+
export function isElementInternalsSupported(): boolean {
255+
if (!window.ElementInternals) {
256+
return false;
257+
}
258+
259+
class ElementInternalsFeatureDetection extends HTMLElement {
260+
internals: IElementInternals;
261+
262+
constructor() {
263+
super();
264+
this.internals = this.attachInternals();
265+
}
266+
}
267+
const randomName = `element-internals-feature-detection-${Math.random().toString(36).replace(/[^a-z]+/g, '')}`;
268+
customElements.define(randomName, ElementInternalsFeatureDetection);
269+
const featureDetectionElement = new ElementInternalsFeatureDetection();
270+
return [
271+
"shadowRoot",
272+
"form",
273+
"states",
274+
"willValidate",
275+
"validity",
276+
"validationMessage",
277+
"labels",
278+
"setFormValue",
279+
"setValidity",
280+
"checkValidity",
281+
"reportValidity"
282+
].every(prop => prop in featureDetectionElement.internals);
283+
}
284+
285+
if (!isElementInternalsSupported()) {
255286
window.ElementInternals = ElementInternals;
256287

257288

@@ -278,7 +309,7 @@ if (!window.ElementInternals) {
278309
* Attaches an ElementInternals instance to a custom element. Calling this method
279310
* on a built-in element will throw an error.
280311
*/
281-
Object.defineProperty(Element.prototype, 'attachInternals', {
312+
Object.defineProperty(HTMLElement.prototype, 'attachInternals', {
282313
get() {
283314
return () => {
284315
if (this.tagName.indexOf('-') === -1) {

0 commit comments

Comments
 (0)