@@ -26,7 +26,7 @@ import { ValidityState, reconcileValidity, setValid } from './ValidityState';
26
26
import { deferUpgrade , observerCallback , observerConfig } from './mutation-observers' ;
27
27
import { IElementInternals , ICustomElement , LabelsList } from './types' ;
28
28
import { CustomStateSet } from './CustomStateSet' ;
29
- import { HTMLFormControlsCollection } from './HTMLFormControlsCollection ' ;
29
+ import { patchFormPrototype } from './patch-form-prototype ' ;
30
30
31
31
export class ElementInternals implements IElementInternals {
32
32
ariaAtomic : string ;
@@ -348,24 +348,14 @@ if (!isElementInternalsSupported()) {
348
348
return shadowRoot ;
349
349
}
350
350
351
- function checkValidityOverride ( ...args ) : boolean {
352
- let returnValue = checkValidity . apply ( this , args ) ;
353
- return overrideFormMethod ( this , returnValue , 'checkValidity' ) ;
354
- }
355
-
356
- function reportValidityOverride ( ...args ) : boolean {
357
- let returnValue = reportValidity . apply ( this , args ) ;
358
- return overrideFormMethod ( this , returnValue , 'reportValidity' ) ;
359
- }
360
-
361
351
/**
362
352
* Attaches an ElementInternals instance to a custom element. Calling this method
363
353
* on a built-in element will throw an error.
364
354
*/
365
355
HTMLElement . prototype . attachInternals = function ( ) : IElementInternals {
366
356
if ( ! this . tagName ) {
367
357
/** This happens in the LitSSR environment. Here we can generally ignore internals for now */
368
- return { } as unknown as IElementInternals ;
358
+ return { } as object as IElementInternals ;
369
359
} else if ( this . tagName . indexOf ( '-' ) === - 1 ) {
370
360
throw new Error ( `Failed to execute 'attachInternals' on 'HTMLElement': Unable to attach ElementInternals to non-custom elements.` ) ;
371
361
}
@@ -381,35 +371,13 @@ if (!isElementInternalsSupported()) {
381
371
const documentObserver = new MutationObserver ( observerCallback ) ;
382
372
documentObserver . observe ( document . documentElement , observerConfig ) ;
383
373
384
- const checkValidity = HTMLFormElement . prototype . checkValidity ;
385
- HTMLFormElement . prototype . checkValidity = checkValidityOverride ;
386
-
387
- const reportValidity = HTMLFormElement . prototype . reportValidity ;
388
- HTMLFormElement . prototype . reportValidity = reportValidityOverride ;
389
-
390
- const { get } = Object . getOwnPropertyDescriptor ( HTMLFormElement . prototype , 'elements' ) ;
391
- Object . defineProperty ( HTMLFormElement . prototype , 'elements' , {
392
- get ( ...args ) {
393
- const elements = get . call ( this , ...args ) ;
394
- const polyfilledElements = Array . from ( formElementsMap . get ( this ) || [ ] ) ;
395
-
396
- // If there are no polyfilled elements, return the native elements collection
397
- if ( polyfilledElements . length === 0 ) {
398
- return elements ;
399
- }
400
-
401
- // Merge the native elements with the polyfilled elements
402
- // and order them by their position in the DOM
403
- const orderedElements = Array . from ( elements ) . concat ( polyfilledElements ) . sort ( ( a : Element , b : Element ) => {
404
- if ( a . compareDocumentPosition ) {
405
- return a . compareDocumentPosition ( b ) & 2 ? 1 : - 1 ;
406
- }
407
- return 0 ;
408
- } ) ;
409
-
410
- return new HTMLFormControlsCollection ( orderedElements ) ;
411
- } ,
412
- } ) ;
374
+ /**
375
+ * Keeps the polyfill from throwing in environments where HTMLFormElement
376
+ * is undefined like in a server environment
377
+ */
378
+ if ( HTMLFormElement ) {
379
+ patchFormPrototype ( ) ;
380
+ }
413
381
414
382
if ( ! window . CustomStateSet ) {
415
383
window . CustomStateSet = CustomStateSet ;
0 commit comments