Skip to content

Instantly share code, notes, and snippets.

@ryuone
Created May 6, 2011 15:26
Show Gist options
  • Save ryuone/959152 to your computer and use it in GitHub Desktop.
Save ryuone/959152 to your computer and use it in GitHub Desktop.
jQuery data.js diff from 1.5.2 to 1.6
diff --git a/src/data.js b/src/data.js
index 2d53a71..c2fd558 100644
--- a/src/data.js
+++ b/src/data.js
@@ -1,6 +1,7 @@
(function( jQuery ) {
-var rbrace = /^(?:\{.*\}|\[.*\])$/;
+var rbrace = /^(?:\{.*\}|\[.*\])$/,
+ rmultiDash = /([a-z])([A-Z])/g;
jQuery.extend({
cache: {},
@@ -223,12 +224,13 @@ jQuery.fn.extend({
data = jQuery.data( this[0] );
if ( this[0].nodeType === 1 ) {
- var attr = this[0].attributes, name;
+ var attr = this[0].attributes, name;
for ( var i = 0, l = attr.length; i < l; i++ ) {
name = attr[i].name;
if ( name.indexOf( "data-" ) === 0 ) {
- name = name.substr( 5 );
+ name = jQuery.camelCase( name.substring(5) );
+
dataAttr( this[0], name, data[ name ] );
}
}
@@ -282,7 +284,9 @@ function dataAttr( elem, key, data ) {
// If nothing was found internally, try to fetch any
// data from the HTML5 data-* attribute
if ( data === undefined && elem.nodeType === 1 ) {
- data = elem.getAttribute( "data-" + key );
+ name = "data-" + key.replace( rmultiDash, "$1-$2" ).toLowerCase();
+
+ data = elem.getAttribute( name );
if ( typeof data === "string" ) {
try {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment