fix(attr): lowercase attribute name before lookup in HTML mode#5248
Closed
algojogacor wants to merge 1 commit into
Closed
fix(attr): lowercase attribute name before lookup in HTML mode#5248algojogacor wants to merge 1 commit into
algojogacor wants to merge 1 commit into
Conversation
- 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
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.
Summary
Fixes #581
When looking up an attribute with
.attr(), the attribute name was not lowercased before looking it up in the element's.attribsobject. This means$("div").attr("CLASS")would not find theclassattribute, even though HTML attribute names are case-insensitive per spec.Root Cause
In
src/api/attributes.ts, bothgetAttr(line 64) andsetAttr(line 99) used the attribute name as-is without lowercasing:getAttr:Object.hasOwn(elem.attribs, name)— direct lookup without case normalizationsetAttr:el.attribs[name] = ...— stored with original caseThis meant:
attr("CLASS", "foo")stored as"CLASS"attr("class")failed because the stored key was"CLASS"not"class"Fix
Added
.toLowerCase()to attribute names in bothgetAttrandsetAttr:getAttr: lowercasesnamebeforehasOwncheck when not in XML mode (XML is case-sensitive)setAttr: lowercasesnamebefore storing inel.attribsThis 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 theclassattribute ✅$("div").attr("ID")returns the value of theidattribute ✅attr("CLASS", "foo")then gettingattr("class")returns"foo"✅