Added in support for basic JSON object parsing of data- attributes.

This commit is contained in:
John Resig 2010-09-21 00:51:45 -04:00
parent ae9df1412c
commit e7c2d3b45b
2 changed files with 14 additions and 8 deletions

View file

@ -1,7 +1,8 @@
(function( jQuery ) { (function( jQuery ) {
var windowData = {}, var windowData = {},
rnum = /^-?\d+(?:\.\d+)$/; rnum = /^-?\d+(?:\.\d+)$/,
rbrace = /^{.*}$/;
jQuery.extend({ jQuery.extend({
cache: {}, cache: {},
@ -153,11 +154,14 @@ jQuery.fn.extend({
data = this[0].getAttribute( "data-" + key ); data = this[0].getAttribute( "data-" + key );
if ( typeof data === "string" ) { if ( typeof data === "string" ) {
data = data === "true" ? true : try {
data === "false" ? false : data = data === "true" ? true :
data === "null" ? null : data === "false" ? false :
rnum.test( data ) ? parseFloat( data ) : data === "null" ? null :
data; rnum.test( data ) ? parseFloat( data ) :
rbrace.test( data ) ? jQuery.parseJSON( data ) :
data;
} catch( e ) {}
} else { } else {
data = undefined; data = undefined;

View file

@ -158,7 +158,7 @@ test(".data(String) and .data(String, Object)", function() {
}); });
test("data-* attributes", function() { test("data-* attributes", function() {
expect(22); expect(23);
var div = jQuery("<div>"), var div = jQuery("<div>"),
child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\"></div>"); child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\"></div>");
@ -186,6 +186,7 @@ test("data-* attributes", function() {
.attr("data-point", "5.5") .attr("data-point", "5.5")
.attr("data-pointbad", "5..5") .attr("data-pointbad", "5..5")
.attr("data-pointbad2", "-.") .attr("data-pointbad2", "-.")
.attr("data-badjson", "{123}")
.attr("data-null", "null") .attr("data-null", "null")
.attr("data-string", "test"); .attr("data-string", "test");
@ -195,6 +196,7 @@ test("data-* attributes", function() {
equals( child.data('point'), 5.5, "Primitive number read from attribute"); equals( child.data('point'), 5.5, "Primitive number read from attribute");
equals( child.data('pointbad'), "5..5", "Bad number read from attribute"); equals( child.data('pointbad'), "5..5", "Bad number read from attribute");
equals( child.data('pointbad2'), "-.", "Bad number read from attribute"); equals( child.data('pointbad2'), "-.", "Bad number read from attribute");
equals( child.data('badjson'), "{123}", "Bad number read from attribute");
equals( child.data('null'), null, "Primitive null read from attribute"); equals( child.data('null'), null, "Primitive null read from attribute");
equals( child.data('string'), "test", "Typical string read from attribute"); equals( child.data('string'), "test", "Typical string read from attribute");
@ -213,7 +215,7 @@ test("data-* attributes", function() {
break; break;
case 2: case 2:
equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property"); equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
equals(jQuery(elem).data("bar"), '{"test":"baz"}', "Check bar property"); same(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
break; break;
case 3: case 3:
equals(jQuery(elem).data("number"), true, "Check number property"); equals(jQuery(elem).data("number"), true, "Check number property");