Bug Tracker

Opened 6 years ago

Closed 5 years ago

#14376 closed bug (migrated)

`.data()` doesn't handle the attributes with digits in the name well

Reported by: zerkms@… Owned by: Rick Waldron
Priority: low Milestone: 1.12/2.2
Component: data Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:

Description

version affected: any (1.10.1) browser: any (chrome) OS: any (windows 7 64bit)

For the following html

<div data-foo-42="bar"></div>

and

var div = $('div');

console.log(div.data());

javascript code I expect to see the object with one property.

Actual result: an empty object

jsfiddle: http://jsfiddle.net/nWCKt/

Change History (15)

comment:1 Changed 6 years ago by ameyms

Seems to be happening because we camelize the key before passing it to dataAttr() function. dataAttr() then tries to reconstruct the hyphenated attribute name and does so incorrectly.

Consider following example:

data-foo-bar --> fooBar --> data-foo-bar (correct)

data-foo-42 --> foo42 --> data-foo42 (incorrect)

comment:2 Changed 6 years ago by dmethvin

Component: unfileddata
Milestone: None1.11/2.1
Priority: undecidedlow
Status: newopen

Ref: http://www.w3.org/TR/2011/WD-html5-20110525/elements.html#embedding-custom-non-visible-data-with-the-data-attributes

It seems like we shouldn't take out the hyphen unless the following char is /[a-z]/. However that's inside jQuery.camelCase() so making that change could affect many other situations outside data attributes.

comment:3 Changed 6 years ago by Rick Waldron

Owner: set to Rick Waldron
Status: openassigned

comment:5 in reply to:  4 Changed 6 years ago by Rick Waldron

Resolution: cantfix
Status: assignedclosed

https://code.google.com/p/chromium/issues/detail?id=315125&thanks=315125&ts=1383668374

...was closed as a duplicate of this: https://code.google.com/p/chromium/issues/detail?id=171175

Based on the spec:

For each name in list, for each U+002D HYPHEN-MINUS character (-) in the name that is followed by a lowercase ASCII letter, remove the U+002D HYPHEN-MINUS character (-) and replace the character that followed it by the same character converted to ASCII uppercase.

the solution would be to _not_ remove hyphens when they are immediately followed by numbers, so that data-foo-42 would produce a key called foo-42. Unfortunately, this is a compatibility breaking change—one that we actually have unit tests to support.

comment:7 Changed 6 years ago by gnarf

Resolution: cantfix
Status: closedreopened

This is one of those weird situations where we "cantfix" but we probably should... Re-opening to discuss the ultimate fate of this one at the next meeting.

comment:8 Changed 6 years ago by dmethvin

Status: reopenedopen

comment:9 Changed 6 years ago by Rick Waldron

comment:10 Changed 6 years ago by dmethvin

Milestone: 1.11/2.11.next/2.next

This is a behavior change so it won't go into 1.11/2.1, but I think we'll need to deal with it.

comment:11 Changed 6 years ago by malsup

This is a breaking change when migrating from 1.9 to 2.x.

comment:12 Changed 6 years ago by dmethvin

@malsup, the two branches seem to work the same with the OP's test case. Is there some other test case?

comment:13 in reply to:  12 Changed 6 years ago by malsup

Replying to dmethvin:

@malsup, the two branches seem to work the same with the OP's test case. Is there some other test case?

@dmethvin See thread: https://github.com/jquery/api.jquery.com/issues/383

comment:14 Changed 5 years ago by dmethvin

Milestone: 1.next/2.next1.12/2.2

comment:15 Changed 5 years ago by m_gol

Resolution: migrated
Status: openclosed
Note: See TracTickets for help on using tickets.