Skip to content

Commit 3b86acf

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

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
@@ -23,6 +23,7 @@ module.exports = function( grunt ) {
2323
"src/migrate.js",
2424
"src/core.js",
2525
"src/css.js",
26+
"src/data.js",
2627
"src/effects.js",
2728
"src/event.js",
2829
"src/traversing.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
@@ -37,6 +37,7 @@
3737
<script src="migrate.js"></script>
3838
<script src="core.js"></script>
3939
<script src="css.js"></script>
40+
<script src="data.js"></script>
4041
<script src="event.js"></script>
4142
<script src="traversing.js"></script>
4243
<script src="deferred.js"></script>

test/testinit.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ TestManager = {
2323
"migrate",
2424
"core",
2525
"css",
26+
"data",
2627
"effects",
2728
"event",
2829
"traversing",

warnings.md

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

5757
**Solution**: Replace any use of `.size()` with `.length`.
5858

59+
### JQMIGRATE: jQuery.data() always sets/gets camelCased names
60+
61+
**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.
62+
63+
**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.
64+
5965
### JQMIGRATE: jQuery.swap() is undocumented and deprecated
6066

6167
**Cause**: The `jQuery.swap()` method temporarily exchanges a set of CSS properties. It was never documented as part of jQuery's public API and should not be used because it can cause performance problems due to forced layout. This method has been removed in jQuery 3.0.

0 commit comments

Comments
 (0)