Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Brute force property removal when removeData([a,b,c]). Fixes #12786

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information...
commit 812c6087ada4a6383fd2e033d04648ec0f6691ea 1 parent 2eff7fe
Rick Waldron rwaldron authored

Showing 2 changed files with 78 additions and 0 deletions. Show diff stats Hide diff stats

  1. +8 0 src/data.js
  2. +70 0 test/unit/data.js
8 src/data.js
@@ -136,6 +136,14 @@ function internalRemoveData( elem, name, pvt /* For internal use only */ ){
136 136 name = name.split(" ");
137 137 }
138 138 }
  139 + } else {
  140 + // If "name" is an array of keys...
  141 + // When data is initially created, via ("key", "val") signature,
  142 + // keys will be converted to camelCase.
  143 + // Since there is no way to tell _how_ a key was added, remove
  144 + // both plain key and camelCase key. #12786
  145 + // This will only penalize the array argument path.
  146 + name = name.concat( jQuery.map( name, jQuery.camelCase ) );
139 147 }
140 148
141 149 for ( i = 0, l = name.length; i < l; i++ ) {
70 test/unit/data.js
@@ -621,6 +621,76 @@ test("jQuery.data supports interoperable removal of hyphenated/camelCase propert
621 621 });
622 622 });
623 623
  624 +test( "jQuery.fn.removeData supports interoperable removal of hyphenated properties via array (#12786)", function() {
  625 + expect( 10 );
  626 +
  627 + var div, plain, datas, keys;
  628 +
  629 + div = jQuery("<div/>", { id: "test12786" }).appendTo("#qunit-fixture"),
  630 + datas = {
  631 + "non-empty": "a string",
  632 + "empty-string": "",
  633 + "one-value": 1,
  634 + "zero-value": 0,
  635 + "an-array": [],
  636 + "an-object": {},
  637 + "bool-true": true,
  638 + "bool-false": false,
  639 + // JSHint enforces double quotes,
  640 + // but JSON strings need double quotes to parse
  641 + // so we need escaped double quotes here
  642 + "some-json": "{ \"foo\": \"bar\" }"
  643 + };
  644 +
  645 + keys = jQuery.map( datas, function( _, key ) {
  646 + return key;
  647 + });
  648 +
  649 + // "keys" is an array that looks like this:
  650 + // [
  651 + // "non-empty", "empty-string", "one-value", "zero-value",
  652 + // "an-array", "an-object", "bool-true", "bool-false", "some-json"
  653 + // ]
  654 +
  655 + div.data( datas );
  656 + deepEqual( div.data(), datas, "div.data() returns an object whose values match those of datas (div)" );
  657 +
  658 + div.removeData( keys.hyphen );
  659 + ok( jQuery.isEmptyObject( div.data() ), "After removal by array of hyphenated keys, div.data() returns an object with no properties (div)" );
  660 +
  661 + div.data( "a-a", 1 );
  662 + div.data( "b-b", 2 );
  663 +
  664 + deepEqual( div.data( "a-a" ), 1, "div.data('a-a') returns value that matches the manually set value (div)" );
  665 + deepEqual( div.data( "b-b" ), 2, "div.data('b-b') returns value that matches the manually set value (div)" );
  666 +
  667 + div.removeData([ "a-a", "b-b" ]);
  668 +
  669 + ok( jQuery.isEmptyObject( div.data() ), "After removal by array of hyphenated keys, div.data() returns an object with no properties (div)" );
  670 +
  671 + plain = jQuery({});
  672 +
  673 + plain.data( datas );
  674 + deepEqual( plain.data(), datas, "plain.data() returns an object whose values match those of datas (plain)" );
  675 +
  676 + plain.removeData( keys );
  677 +
  678 + ok( jQuery.isEmptyObject( plain.data() ), "After removal by array of hyphenated keys, plain.data() returns an object with no properties (plain)" );
  679 +
  680 +
  681 + plain = jQuery({});
  682 +
  683 + plain.data( "a-a", 1 );
  684 + plain.data( "b-b", 2 );
  685 +
  686 + deepEqual( plain.data( "a-a" ), 1, "plain.data('a-a') returns value that matches the manually set value (plain)" );
  687 + deepEqual( plain.data( "b-b" ), 2, "plain.data('b-b') returns value that matches the manually set value (plain)" );
  688 +
  689 + plain.removeData([ "a-a", "b-b" ]);
  690 +
  691 + ok( jQuery.isEmptyObject( plain.data() ), "After removal by array of hyphenated keys, plain.data() returns an object with no properties (plain)" );
  692 +});
  693 +
624 694 // Test originally by Moschel
625 695 test("Triggering the removeData should not throw exceptions. (#10080)", function() {
626 696 expect(1);

0 comments on commit 812c608

Please sign in to comment.
Something went wrong with that request. Please try again.