Skip to content

fix(attributes): lowercase attribute name before lookup in getAttr and setAttr#5256

Open
chu0119 wants to merge 1 commit into
cheeriojs:mainfrom
chu0119:fix/attr-case-insensitive-lookup
Open

fix(attributes): lowercase attribute name before lookup in getAttr and setAttr#5256
chu0119 wants to merge 1 commit into
cheeriojs:mainfrom
chu0119:fix/attr-case-insensitive-lookup

Conversation

@chu0119
Copy link
Copy Markdown

@chu0119 chu0119 commented May 16, 2026

Summary

Fixed .attr() to handle case-insensitive attribute name lookup, matching HTML specification and jQuery behavior.

Why

HTML attribute names are case-insensitive per the spec. htmlparser2 stores all attribute names in lowercase within the internal attribs object. When a user passes an uppercase attribute name (e.g., $el.attr('CLASS')), the lookup fails because the stored key is class.

What changed

  • getAttr: Lowercase the attribute name before looking it up in elem.attribs, and in the option/checkbox value fallback comparisons
  • setAttr: Lowercase the attribute name before storing, maintaining consistency with htmlparser2's lowercase storage
  • Added test case for case-insensitive attribute lookup

Test plan

  • All 128 existing tests pass
  • New test valid attr should be case-insensitive for HTML documents added

…tAttr

HTML attribute names are case-insensitive, but cheerio's internal
attribs object stores them in lowercase (from htmlparser2). When
a user passes an uppercase attribute name like `CLASS`, the lookup
fails because the key is stored as `class`.

This change lowercases the attribute name in getAttr before looking
up in elem.attribs, and in setAttr before storing, matching jQuery's
behavior for HTML documents.
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.

1 participant