Skip to content

Commit 7eb1085

Browse files
committed
data attribute namespace normalization to comply with spec and jquery 1.6 changes
1 parent 2cff5c3 commit 7eb1085

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

js/jquery.mobile.core.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//hash segment before &ui-page= is used to make Ajax request
2121
subPageUrlKey: "ui-page",
2222

23-
//anchor links with a data-rel, or pages with a data-role, that match these selectors will be untrackable in history
23+
//anchor links with a data-rel, or pages with a data-role, that match these selectors will be untrackable in history
2424
//(no change in URL, not bookmarkable)
2525
nonHistorySelectors: "dialog",
2626

@@ -110,32 +110,41 @@
110110
setTimeout(function() {
111111
$.event.special.scrollstart.enabled = true;
112112
}, 150 );
113+
},
114+
115+
nsNormalize: function(prop){
116+
if(!prop) return;
117+
118+
// NOTE the spec specifies that attributes will be converted to lower case
119+
// ascii so the regex can remain simple
120+
return ($.mobile.ns + prop).replace(/-([a-z])/g, function(s, capture){
121+
return capture.toUpperCase();
122+
});
113123
}
114124
});
115125

116126
//mobile version of data and removeData and hasData methods
117127
//ensures all data is set and retrieved using jQuery Mobile's data namespace
118128
$.fn.jqmData = function( prop, value ){
119-
return this.data( prop ? $.mobile.ns + prop : prop, value );
129+
return this.data( prop ? $.mobile.nsNormalize(prop) : prop, value );
120130
};
121131

122132
$.jqmData = function( elem, prop, value ){
123-
return $.data( elem, prop && $.mobile.ns + prop, value );
133+
return $.data( elem, $.mobile.nsNormalize(prop), value );
124134
};
125135

126136
$.fn.jqmRemoveData = function( prop ){
127-
return this.removeData( $.mobile.ns + prop );
137+
return this.removeData( $.mobile.nsNormalize(prop) );
128138
};
129139

130140
$.jqmRemoveData = function( elem, prop ){
131-
return $.removeData( elem, prop && $.mobile.ns + prop );
141+
return $.removeData( elem, $.mobile.nsNormalize(prop) );
132142
};
133143

134144
$.jqmHasData = function( elem, prop ){
135-
return $.hasData( elem, prop && $.mobile.ns + prop );
145+
return $.hasData( elem, $.mobile.nsNormalize(prop) );
136146
};
137147

138-
139148
// Monkey-patching Sizzle to filter the :jqmData selector
140149
var oldFind = $.find;
141150

tests/unit/core/core.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@
4141

4242
same( $("body").jqmData("foo"), true, "getting data returns the right value" );
4343

44-
same( $("body").data($.mobile.ns + "foo"), true, "data was set using namespace" );
44+
same( $("body").data($.mobile.nsNormalize("foo")), true, "data was set using namespace" );
4545

4646
same( $("body").jqmData("foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
4747

48-
same( $("body").jqmData(), { "nstest-foo": true}, "passing no arguments returns a hash with all set properties" );
48+
same( $("body").jqmData(), { "nstestFoo": true}, "passing no arguments returns a hash with all set properties" );
4949

50-
same( $("body").jqmData(undefined), { "nstest-foo": true}, "passing a single undefined argument returns a hash with all set properties" );
50+
same( $("body").jqmData(undefined), { "nstestFoo": true}, "passing a single undefined argument returns a hash with all set properties" );
5151

52-
same( $("body").jqmData(undefined, undefined), {"nstest-foo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
52+
same( $("body").jqmData(undefined, undefined), {"nstestFoo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
5353

5454
same( $("body").jqmRemoveData("foo"), $("body"), "jqmRemoveData returns the element" );
5555

@@ -63,15 +63,15 @@
6363

6464
same( $.jqmData(document.body, "foo"), true, "getting data returns the right value" );
6565

66-
same( $.data(document.body, $.mobile.ns + "foo"), true, "data was set using namespace" );
66+
same( $.data(document.body, $.mobile.nsNormalize("foo")), true, "data was set using namespace" );
6767

6868
same( $.jqmData(document.body, "foo", undefined), true, "getting data still returns the value if there's an undefined second arg" );
6969

70-
same( $.jqmData(document.body), { "nstest-foo": true}, "passing no arguments returns a hash with all set properties" );
70+
same( $.jqmData(document.body), { "nstestFoo": true}, "passing no arguments returns a hash with all set properties" );
7171

72-
same( $.jqmData(document.body, undefined), { "nstest-foo": true}, "passing a single undefined argument returns a hash with all set properties" );
72+
same( $.jqmData(document.body, undefined), { "nstestFoo": true}, "passing a single undefined argument returns a hash with all set properties" );
7373

74-
same( $.jqmData(document.body, undefined, undefined), {"nstest-foo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
74+
same( $.jqmData(document.body, undefined, undefined), {"nstestFoo": true}, "passing 2 undefined arguments returns a hash with all set properties" );
7575

7676
same( $.jqmRemoveData(document.body, "foo"), undefined, "jqmRemoveData returns the undefined value" );
7777

0 commit comments

Comments
 (0)