fix(attributes): make removeAttr case-insensitive for HTML documents#5260
Open
algojogacor wants to merge 1 commit into
Open
fix(attributes): make removeAttr case-insensitive for HTML documents#5260algojogacor wants to merge 1 commit into
algojogacor wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 theremoveAttributehelper does a case-sensitive lookup.Root Cause
The
removeAttributefunction atsrc/api/attributes.ts:840performsObject.hasOwn(elem.attribs, name)without normalizing the attribute name. htmlparser2 lowercases all attribute names when parsing HTML documents, soelem.attribs['class']exists butelem.attribs['CLASS']does not. The same issue affects thedeleteon 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 inremoveAttribute(+3 -1lines)Testing
.removeAttr('class')on element with lowercaseclassattribute: still works ✅.removeAttr('CLASS')on element with lowercaseclassattribute: now correctly removes it ✅.removeAttr('id class')space-separated: both attributes removed ✅