Skip to content

Commit 9cd2788

Browse files
committed
fix(attributes): make removeAttr case-insensitive for HTML documents
htmlparser2 stores HTML attribute names in lowercase per the HTML spec. removeAttribute was doing a case-sensitive Object.hasOwn check and delete, which caused removeAttr('CLASS') to fail when the attribute was stored as 'class'. Normalize the attribute name to lowercase before checking and deleting to match htmlparser2's storage convention. Fixes #5257
1 parent 5aa508d commit 9cd2788

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

src/api/attributes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,9 +838,12 @@ export function val<T extends AnyNode>(
838838
* @param name - Name of the attribute to remove.
839839
*/
840840
function removeAttribute(elem: Element, name: string) {
841-
if (!(elem.attribs && Object.hasOwn(elem.attribs, name))) return;
841+
// Normalize attribute name to lowercase; htmlparser2 stores HTML attribute
842+
// names in lowercase per the HTML spec (case-insensitive attributes).
843+
const normalizedName = name.toLowerCase();
844+
if (!(elem.attribs && Object.hasOwn(elem.attribs, normalizedName))) return;
842845

843-
delete elem.attribs[name];
846+
delete elem.attribs[normalizedName];
844847
}
845848

846849
/**

0 commit comments

Comments
 (0)