fix(attributes): make removeAttr case-insensitive in HTML mode#5290
fix(attributes): make removeAttr case-insensitive in HTML mode#5290abhu85 wants to merge 1 commit into
Conversation
htmlparser2 stores HTML attribute names in lowercase, but removeAttr
looked them up using the caller-supplied casing, so removeAttr('CLASS')
failed to remove a parsed `class` attribute. Lowercase the name before
lookup when not in xmlMode, matching the DOM's case-insensitive handling
of HTML attribute names. XML mode continues to preserve case.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 798fae029f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| for (const attrName of attrNames) { | ||
| // HTML attribute names are case-insensitive; XML mode preserves case. | ||
| const lookup = this.options.xmlMode ? attrName : attrName.toLowerCase(); |
There was a problem hiding this comment.
Preserve mixed-case SVG attr removals
In default HTML mode Cheerio parses with parse5, which preserves/adjusts SVG foreign-content attribute names such as viewBox and preserveAspectRatio as mixed-case keys on elem.attribs; lowercasing every requested name when xmlMode is false makes $('svg').removeAttr('viewBox') look for viewbox instead and leave the parsed viewBox attribute in place. This affects common inline SVG markup even though the document is not in XML mode.
Useful? React with 👍 / 👎.
Fixes #5257
Problem
htmlparser2stores HTML attribute names in lowercase (per the HTML spec), butremoveAttrlooked them up using the caller-supplied casing. As a result,removeAttr('CLASS')failed to remove a parsedclassattribute:Solution
Lowercase the attribute name before lookup when not in
xmlMode, mirroring how the DOM treats HTML attribute names as case-insensitive. This follows the existingxmlModeconvention already used bygetAttr/setPropin this file.removeAttr('CLASS')now removes aclassattribute.The shared
removeAttributehelper (also used bysetAttrfornull-value deletes) is intentionally left untouched, so programmatic case-sensitive behavior viaattr(name, null)is unaffected.Tests
Added two cases to
.removeAttr:Full suite:
792 passed, no type errors, lint clean.