Browse Source

Add fieldset handling

Any fieldset ancestor that is disabled is now checked for an input's
disabled state.
master
TheoryOfNekomata 1 year ago
parent
commit
1ace316763
1 changed files with 31 additions and 1 deletions
  1. +31
    -1
      src/index.ts

+ 31
- 1
src/index.ts View File

@@ -1018,6 +1018,36 @@ const ATTRIBUTE_DISABLED = 'disabled' as const;
*/ */
const NAME_ATTRIBUTE_VALUE_ISINDEX = 'isindex' as const; const NAME_ATTRIBUTE_VALUE_ISINDEX = 'isindex' as const;


/**
* Determines is an element is a descendant of a disabled <fieldset> element.
* @param el - The element.
* @returns Value determining if element is a descendant of a disabled <fieldset> element.
*/
const isElementDescendantOfDisabledFieldset = (el: HTMLElement) => {
const elementAncestors = [] as HTMLElement[];
let currentParentElement = el.parentElement;
while (currentParentElement !== null) {
if (currentParentElement) {
elementAncestors.push(currentParentElement);
}
currentParentElement = currentParentElement?.parentElement ?? null;
}
return elementAncestors.some((fieldset) => (
fieldset.tagName === 'FIELDSET'
&& Boolean((fieldset as HTMLFieldSetElement)[ATTRIBUTE_DISABLED])
));
};

/**
* Determines if an element is disabled.
* @param el - The element.
* @returns Value determining if element is disabled.
*/
const isElementDisabled = (el: HTMLElement) => (
(ATTRIBUTE_DISABLED in el && Boolean(el[ATTRIBUTE_DISABLED]))
|| isElementDescendantOfDisabledFieldset(el)
);

/** /**
* Determines if an element's value is included when its form is submitted. * Determines if an element's value is included when its form is submitted.
* @param el - The element. * @param el - The element.
@@ -1030,7 +1060,7 @@ export const isElementValueIncludedInFormSubmit = (el: HTMLElement, includeDisab
typeof namedEl[ATTRIBUTE_NAME] === 'string' typeof namedEl[ATTRIBUTE_NAME] === 'string'
&& namedEl[ATTRIBUTE_NAME].length > 0 && namedEl[ATTRIBUTE_NAME].length > 0
&& namedEl[ATTRIBUTE_NAME] !== NAME_ATTRIBUTE_VALUE_ISINDEX && namedEl[ATTRIBUTE_NAME] !== NAME_ATTRIBUTE_VALUE_ISINDEX
&& (includeDisabled || !(ATTRIBUTE_DISABLED in namedEl && Boolean(namedEl[ATTRIBUTE_DISABLED])))
&& (includeDisabled || !(isElementDisabled(el)))
&& isFieldElement(namedEl as unknown as HTMLElement) && isFieldElement(namedEl as unknown as HTMLElement)
); );
}; };


Loading…
Cancel
Save