Skip to content

Commit fc0bdd8

Browse files
committed
Data: Restore preference for kebab-case names and warn about it
Fixes #87 Closes #161
1 parent dbc215d commit fc0bdd8

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

Gruntfile.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = function( grunt ) {
2424
"src/core.js",
2525
"src/ajax.js",
2626
"src/css.js",
27+
"src/data.js",
2728
"src/effects.js",
2829
"src/event.js",
2930
"src/offset.js",

src/data.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var oldData = jQuery.data;
2+
3+
jQuery.data = function( elem, name, value ) {
4+
var curData;
5+
6+
// If the name is transformed, look for the un-transformed name in the data object
7+
if ( name && name !== jQuery.camelCase( name ) ) {
8+
curData = jQuery.hasData( elem ) && oldData.call( this, elem );
9+
if ( curData && name in curData ) {
10+
migrateWarn( "jQuery.data() always sets/gets camelCased names: " + name );
11+
if ( arguments.length > 2 ) {
12+
curData[ name ] = value;
13+
}
14+
return curData[ name ];
15+
}
16+
}
17+
18+
return oldData.apply( this, arguments );
19+
};

test/data.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module( "data" );
2+
3+
test( "jQuery.data() camelCased names", function( assert ) {
4+
5+
var sames = [
6+
"datum",
7+
"ropeAdope",
8+
"Олег\u0007Michał",
9+
"already-Big",
10+
"number-2",
11+
"unidash-"
12+
],
13+
diffs = [
14+
"dat-data",
15+
"hangy-dasher-",
16+
"-dashy-hanger"
17+
];
18+
19+
assert.expect( 16 );
20+
21+
var curData,
22+
div = document.createElement( "div" );
23+
24+
// = .hasData + noWarning
25+
expectNoWarning( "No existing data object", function() {
26+
sames.concat( diffs ).forEach( function( name ) {
27+
jQuery.data( div, name );
28+
} );
29+
assert.equal( jQuery.hasData( div ), false, "data probes did not fill a data object" );
30+
} );
31+
32+
// = sames.length + diffs.length + noWarning
33+
expectNoWarning( "Data set/get without warning via API", function() {
34+
sames.concat( diffs ).forEach( function( name, index ) {
35+
jQuery.data( div, name, index );
36+
assert.equal( jQuery.data( div, name ), index, name + "=" + index );
37+
} );
38+
} );
39+
40+
// Camelized values set for all names above, get the data object
41+
curData = jQuery.data( div );
42+
43+
// = diffs.length + warning
44+
expectWarning( "Dashed name conflicts", diffs.length, function() {
45+
diffs.forEach( function( name, index ) {
46+
curData[ name ] = index;
47+
assert.equal( jQuery.data( div, name ), curData[ name ],
48+
name + " respects data object" );
49+
} );
50+
} );
51+
52+
} );

test/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<script src="core.js"></script>
2929
<script src="ajax.js"></script>
3030
<script src="css.js"></script>
31+
<script src="data.js"></script>
3132
<script src="event.js"></script>
3233
<script src="offset.js"></script>
3334
<script src="traversing.js"></script>

test/testinit.js

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ TestManager = {
2424
"core",
2525
"ajax",
2626
"css",
27+
"data",
2728
"effects",
2829
"event",
2930
"offset",

warnings.md

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ This is _not_ a warning, but a console log message the plugin shows when it firs
6464

6565
**Solution**: Replace any use of `.size()` with `.length`.
6666

67+
### JQMIGRATE: jQuery.data() always sets/gets camelCased names
68+
69+
**Cause:** The page is attempting to set or get a jQuery data item using kebab case, e.g. `my-data`, when a `my-data` item has been set directly on the jQuery data object. jQuery 3.0 always exclusively uses camel case, e.g., `myData`, when it accesses data items via the `.data()` API and does not find kebab case data in that object.
70+
71+
**Solution:** Either 1) Always use the `.data()` API to set or get data items, 2) Always use camelCase names when also setting properties directly on jQuery's data object, or 3) Always set properties directly on the data object without using the API call to set or get data by name. Never mix direct access to the data object and API calls with kebab case names.
72+
6773
### JQMIGRATE: jQuery.fn.offset() requires a valid DOM element
6874
### JQMIGRATE: jQuery.fn.offset() requires an element connected to a document
6975

0 commit comments

Comments
 (0)