From e8cf41a051a62bf1f19beab1a5c1d643f121e28e Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Wed, 31 Oct 2012 13:55:51 -0400 Subject: [PATCH] Fix #10544. Remove deprecated .data() event namespaced triggering. Data events were horribly slow, never documented, and caused strange interpretation of data items with dots in them. --- src/data.js | 28 ++++------------------ src/event.js | 10 +------- test/unit/data.js | 59 +---------------------------------------------- 3 files changed, 7 insertions(+), 90 deletions(-) diff --git a/src/data.js b/src/data.js index eb682feab..af5f7c16f 100644 --- a/src/data.js +++ b/src/data.js @@ -232,7 +232,7 @@ jQuery.extend({ jQuery.fn.extend({ data: function( key, value ) { - var parts, part, attr, name, l, + var attrs, name, elem = this[0], i = 0, data = null; @@ -243,9 +243,9 @@ jQuery.fn.extend({ data = jQuery.data( elem ); if ( elem.nodeType === 1 && !jQuery._data( elem, "parsedAttrs" ) ) { - attr = elem.attributes; - for ( l = attr.length; i < l; i++ ) { - name = attr[i].name; + attrs = elem.attributes; + for ( ; i < attrs.length; i++ ) { + name = attrs[i].name; if ( !name.indexOf( "data-" ) ) { name = jQuery.camelCase( name.substring(5) ); @@ -267,33 +267,15 @@ jQuery.fn.extend({ }); } - parts = key.split( ".", 2 ); - parts[1] = parts[1] ? "." + parts[1] : ""; - part = parts[1] + "!"; - return jQuery.access( this, function( value ) { if ( value === undefined ) { - data = this.triggerHandler( "getData" + part, [ parts[0] ] ); - // Try to fetch any internally stored data first - if ( data === undefined && elem ) { - data = jQuery.data( elem, key ); - data = dataAttr( elem, key, data ); - } - - return data === undefined && parts[1] ? - this.data( parts[0] ) : - data; + return elem ? dataAttr( elem, key, jQuery.data( elem, key ) ) : null; } - parts[1] = value; this.each(function() { - var self = jQuery( this ); - - self.triggerHandler( "setData" + part, parts ); jQuery.data( this, key, value ); - self.triggerHandler( "changeData" + part, parts ); }); }, null, value, arguments.length > 1, null, false ); }, diff --git a/src/event.js b/src/event.js index 4b0040528..d52102adb 100644 --- a/src/event.js +++ b/src/event.js @@ -195,14 +195,6 @@ jQuery.event = { } }, - // Events that are safe to short-circuit if no handlers are attached. - // Native DOM events should not be added, they may have inline handlers. - customEvent: { - "getData": true, - "setData": true, - "changeData": true - }, - trigger: function( event, data, elem, onlyHandlers ) { // Don't do events on text and comment nodes if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) { @@ -232,7 +224,7 @@ jQuery.event = { namespaces.sort(); } - if ( (!elem || jQuery.event.customEvent[ type ]) && !jQuery.event.global[ type ] ) { + if ( !elem && !jQuery.event.global[ type ] ) { // No jQuery handlers for this event type, and it can't have inline handlers return; } diff --git a/test/unit/data.js b/test/unit/data.js index 378750108..f861ee4a2 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -175,15 +175,10 @@ test(".data()", function() { }); test(".data(String) and .data(String, Object)", function() { - expect(29); + expect( 12 ); var parent = jQuery("
"), div = parent.children(); - parent - .bind("getData", function(){ ok( false, "getData bubbled." ); }) - .bind("setData", function(){ ok( false, "setData bubbled." ); }) - .bind("changeData", function(){ ok( false, "changeData bubbled." ); }); - ok( div.data("test") === undefined, "Check for no data exists" ); div.data("test", "success"); @@ -199,58 +194,6 @@ test(".data(String) and .data(String, Object)", function() { ok( div.data("notexist") === undefined, "Check for no data exists" ); - div.data("test", "overwritten"); - var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null}; - - - function logChangeData(e,key,value) { - var dataKey = key; - if ( e.namespace ) { - dataKey = dataKey + "." + e.namespace; - } - changes[key] += value; - changes.value = jQuery.data(e.target, dataKey); - } - - div - .bind("setData",function(e,key,value){ hits[key] += value; }) - .bind("setData.foo",function(e,key,value){ hits[key] += value; }) - .bind("changeData",logChangeData) - .bind("changeData.foo",logChangeData) - .bind("getData",function(e,key){ gets[key] += 1; }) - .bind("getData.foo",function(e,key){ gets[key] += 3; }); - - div.data("test.foo", 2); - equal( div.data("test"), "overwritten", "Check for original data" ); - equal( div.data("test.foo"), 2, "Check for namespaced data" ); - equal( div.data("test.bar"), "overwritten", "Check for unmatched namespace" ); - equal( hits.test, 2, "Check triggered setter functions" ); - equal( gets.test, 5, "Check triggered getter functions" ); - equal( changes.test, 2, "Check sets raise changeData"); - equal( changes.value, 2, "Check changeData after data has been set" ); - - hits.test = 0; - gets.test = 0; - changes.test = 0; - changes.value = null; - - div.data("test", 1); - equal( div.data("test"), 1, "Check for original data" ); - equal( div.data("test.foo"), 2, "Check for namespaced data" ); - equal( div.data("test.bar"), 1, "Check for unmatched namespace" ); - equal( hits.test, 1, "Check triggered setter functions" ); - equal( gets.test, 5, "Check triggered getter functions" ); - equal( changes.test, 1, "Check sets raise changeData" ); - equal( changes.value, 1, "Check changeData after data has been set" ); - - div - .bind("getData",function(e,key){ return key + "root"; }) - .bind("getData.foo",function(e,key){ return key + "foo"; }); - - equal( div.data("test"), "testroot", "Check for original data" ); - equal( div.data("test.foo"), "testfoo", "Check for namespaced data" ); - equal( div.data("test.bar"), "testroot", "Check for unmatched namespace" ); - // #3748 var $elem = jQuery({exists:true}); equal( $elem.data("nothing"), undefined, "Non-existent data returns undefined"); -- 2.39.5