From 9d76c0b163675505d1a901e5fe5249a2c55609bc Mon Sep 17 00:00:00 2001 From: Michał Gołębiowski-Owczarek Date: Mon, 2 Mar 2020 23:02:42 +0100 Subject: Data:Event:Manipulation: Prevent collisions with Object.prototype Make sure events & data keys matching Object.prototype properties work. A separate fix for such events on cloned elements was added as well. Fixes gh-3256 Closes gh-4603 --- test/unit/data.js | 15 +++++++++++++++ test/unit/event.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'test/unit') diff --git a/test/unit/data.js b/test/unit/data.js index 154ea5eeb..cba519e9e 100644 --- a/test/unit/data.js +++ b/test/unit/data.js @@ -998,3 +998,18 @@ QUnit.test( ".data(prop) does not create expando", function( assert ) { } } } ); + +QUnit.test( "keys matching Object.prototype properties (gh-3256)", function( assert ) { + assert.expect( 2 ); + + var div = jQuery( "
" ); + + assert.strictEqual( div.data( "hasOwnProperty" ), undefined, + "hasOwnProperty not matched (before forced data creation)" ); + + // Force the creation of a data object for this element. + div.data( { foo: "bar" } ); + + assert.strictEqual( div.data( "hasOwnProperty" ), undefined, + "hasOwnProperty not matched (after forced data creation)" ); +} ); diff --git a/test/unit/event.js b/test/unit/event.js index cecad3f34..938c66876 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -1796,6 +1796,49 @@ QUnit.test( "jQuery.off using dispatched jQuery.Event", function( assert ) { .remove(); } ); +QUnit.test( "events with type matching an Object.prototype property (gh-3256)", function( assert ) { + assert.expect( 1 ); + + var elem = jQuery( "
" ), + eventFired = false; + + elem.appendTo( "#qunit-fixture" ); + + try { + elem + .one( "hasOwnProperty", function() { + eventFired = true; + } ) + .trigger( "hasOwnProperty" ); + } finally { + assert.strictEqual( eventFired, true, "trigger fired without crashing" ); + } +} ); + +QUnit.test( "events with type matching an Object.prototype property, cloned element (gh-3256)", + function( assert ) { + assert.expect( 1 ); + + var elem = jQuery( "
" ), + eventFired = false; + + elem.appendTo( "#qunit-fixture" ); + + try { + // Make sure the original element has some event data. + elem.on( "click", function() {} ); + + elem + .clone( true ) + .one( "hasOwnProperty", function() { + eventFired = true; + } ) + .trigger( "hasOwnProperty" ); + } finally { + assert.strictEqual( eventFired, true, "trigger fired without crashing" ); + } +} ); + // selector-native does not support scope-fixing in delegation QUnit[ QUnit.jQuerySelectors ? "test" : "skip" ]( "delegated event with delegateTarget-relative selector", function( assert ) { assert.expect( 3 ); -- cgit v1.2.3