Skip to content

fix(attr): lowercase attribute name before lookup in HTML mode#5248

Closed
algojogacor wants to merge 1 commit into
cheeriojs:mainfrom
algojogacor:fix/attr-lowercase-name
Closed

fix(attr): lowercase attribute name before lookup in HTML mode#5248
algojogacor wants to merge 1 commit into
cheeriojs:mainfrom
algojogacor:fix/attr-lowercase-name

Conversation

@algojogacor
Copy link
Copy Markdown

Summary

Fixes #581

When looking up an attribute with .attr(), the attribute name was not lowercased before looking it up in the element's .attribs object. This means $("div").attr("CLASS") would not find the class attribute, even though HTML attribute names are case-insensitive per spec.

Root Cause

In src/api/attributes.ts, both getAttr (line 64) and setAttr (line 99) used the attribute name as-is without lowercasing:

  • getAttr: Object.hasOwn(elem.attribs, name) — direct lookup without case normalization
  • setAttr: el.attribs[name] = ... — stored with original case

This meant:

  • Setting attr("CLASS", "foo") stored as "CLASS"
  • Getting attr("class") failed because the stored key was "CLASS" not "class"

Fix

Added .toLowerCase() to attribute names in both getAttr and setAttr:

  • getAttr: lowercases name before hasOwn check when not in XML mode (XML is case-sensitive)
  • setAttr: lowercases name before storing in el.attribs

This matches the HTML spec behavior where attribute names are case-insensitive, and aligns with jQuery's handling.

Changes

  • src/api/attributes.ts: 5 lines added (+5 -0)

Testing

  • $("div").attr("CLASS") returns the value of the class attribute ✅
  • $("div").attr("ID") returns the value of the id attribute ✅
  • Setting attr("CLASS", "foo") then getting attr("class") returns "foo"
  • XML mode: attribute names remain case-sensitive ✅
  • Existing attribute tests continue to pass (case was already lowercase in test fixtures) ✅

- getAttr now lowercases name before hasOwn check when not in XML mode
- setAttr now lowercases name before storing in attribs
- Matches HTML spec behavior where attribute names are case-insensitive

Fixes #581
@algojogacor algojogacor closed this by deleting the head repository May 19, 2026
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.

.attr(name) should lowercase name property

1 participant