aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorac-mmi <79802170+ac-mmi@users.noreply.github.com>2024-09-11 03:48:53 +0530
committerGitHub <noreply@github.com>2024-09-11 00:18:53 +0200
commit3cad5c435aa2333c39baa55a8bceb2b6bf1e2721 (patch)
treed0574da799ad03019ff21a135c870ab16545dc82 /test
parent6d78c0768d9aa6ba213678724c89af69a1958df6 (diff)
downloadjquery-3cad5c435aa2333c39baa55a8bceb2b6bf1e2721.tar.gz
jquery-3cad5c435aa2333c39baa55a8bceb2b6bf1e2721.zip
Manipulation: Make jQuery.cleanData not skip elements during cleanup
When passing a result of `getElementByTagsName` to `jQuery.cleanData`, convert it to an array first. Otherwise, a live NodeList is passed and if any of the event cleanups remove the element itself, a collection is modified during the iteration, making `jQuery.cleanData` skip cleanup for some elements. Fixes gh-5214 Closes gh-5523 Co-authored-by: Michał Gołębiowski-Owczarek <m.goleb@gmail.com> Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/unit/manipulation.js40
1 files changed, 40 insertions, 0 deletions
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index ff1995cb1..8300b4b9c 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -3099,3 +3099,43 @@ testIframe(
} );
}
);
+
+QUnit.test( "should handle node removal in event's remove hook (gh-5214)", function( assert ) {
+
+ assert.expect( 4 );
+
+ jQuery(
+ "<div id='container'>" +
+ " <div class='guarded removeself' data-elt='one'>" +
+ " Guarded 1" +
+ " </div>" +
+ " <div class='guarded' data-elt='two'>" +
+ " Guarded 2" +
+ " </div>" +
+ " <div class='guarded' data-elt='three'>" +
+ " Guarded 3" +
+ " </div>" +
+ "</div>"
+ ).appendTo( "#qunit-fixture" );
+
+ // Define the custom event handler
+ jQuery.event.special.removeondestroy = {
+ remove: function( ) {
+ var $t = jQuery( this );
+ assert.step( $t.data( "elt" ) );
+ if ( $t.is( ".removeself" ) ) {
+ $t.remove();
+ }
+ }
+ };
+
+ // Attach an empty handler to trigger the `remove`
+ // logic for the custom event when the element is removed.
+ jQuery( ".guarded" ).on( "removeondestroy", function( ) { } );
+
+ // Trigger the event's removal logic by emptying the container
+ jQuery( "#container" ).empty();
+
+ assert.verifySteps( [ "one", "two", "three" ], "All elements were processed in order" );
+} );
+