Skip to content

fix(attributes): make removeAttr case-insensitive for HTML documents#5260

Open
algojogacor wants to merge 1 commit into
cheeriojs:mainfrom
algojogacor:fix/removeattr-case-insensitive
Open

fix(attributes): make removeAttr case-insensitive for HTML documents#5260
algojogacor wants to merge 1 commit into
cheeriojs:mainfrom
algojogacor:fix/removeattr-case-insensitive

Conversation

@algojogacor
Copy link
Copy Markdown

This is an independent contribution made by an individual developer. This work is not associated with any hackathon, competition, or coordinated PR campaign.

Summary

Fixes #5257

.removeAttr() fails to remove attributes when called with an uppercase name (e.g., .removeAttr('CLASS')) because htmlparser2 stores HTML attribute names in lowercase per the HTML spec, but the removeAttribute helper does a case-sensitive lookup.

Root Cause

The removeAttribute function at src/api/attributes.ts:840 performs Object.hasOwn(elem.attribs, name) without normalizing the attribute name. htmlparser2 lowercases all attribute names when parsing HTML documents, so elem.attribs['class'] exists but elem.attribs['CLASS'] does not. The same issue affects the delete on the following line.

Fix

Normalize the attribute name to lowercase with toLowerCase() before checking existence and deleting. This matches htmlparser2's storage convention for HTML documents.

Changes

  • src/api/attributes.ts: normalize attribute name in removeAttribute (+3 -1 lines)

Testing

  • Existing test suite: 127/127 passing (vitest) ✅
  • .removeAttr('class') on element with lowercase class attribute: still works ✅
  • .removeAttr('CLASS') on element with lowercase class attribute: now correctly removes it ✅
  • .removeAttr('id class') space-separated: both attributes removed ✅

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 cheeriojs#5257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Bug: .removeAttr() should be case-insensitive for HTML documents

2 participants