Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#4087 - insertAfter, insertBefore, etc do not work when destination is original element #1033

Closed
wants to merge 2 commits into from

Conversation

PaulBRamos
Copy link
Contributor

No description provided.

@@ -272,7 +272,8 @@ jQuery.fn.extend({

// Make sure that the elements are removed from the DOM before they are inserted
// this can help fix replacing a parent with child elements
if ( !isFunc && typeof value !== "string" ) {
// #4087 - don't detach if original and target are the same (element is removed otherwise)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this entire special-case block and corresponding expense of .index can be eliminated if the jQuery( this ).remove() on L296 moves after the .before/.append. Did you try anything like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't, but I can definitely give it a shot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery.remove() not only remove node from DOM, but also remove all data from the element,
so with this change, if we have, for example, events attached to node that replaces itself, all events from that node will be lost.

@PaulBRamos how you feel about this suggestion?

@gibson042
Copy link
Member

This approach has promise, but also a fair amount of risk and potential pitfalls. I have to wonder about alternatives like creating temporary proxy nodes (that wouldn't get detached when their analogs were pulled into a fragment), or—potentially more performant—a callback for domManip/buildFragment that lets the caller see the set of preexisting nodes about to be swept into a fragment and perform some prep work before that happens. In this case, the callback would look something like:

function( fragmentChildren ) {
    self.each(function() {
        jQuery._data( this, "sub", isDisconnected( this ) || !jQuery.inArray( this, fragmentChildren ) ?
            this :
            this.parentNode.insertBefore( this.ownerDocument.createTextNode(""), this )
        );
    });
}

@PaulBRamos
Copy link
Contributor Author

I definitely see your concerns. I had considered the method you mentioned above, but went with this solution to minimize the footprint of the change. I'll explore this alternative if you think it's more promising.

@dmethvin
Copy link
Member

@PaulBRamos Thanks for looking at this ticket! As you can tell it's been sitting around for a while but your solution looks like it could be pretty elegant. It's definitely worth doing some experiments to see if there is a better/smaller way.

@gibson042
Copy link
Member

@PaulBRamos I'd certainly like to see how it goes. Now that I've pulled the rug out from under these changes with 22f58bd anyway, how about giving it a shot with a new branch from master?

@PaulBRamos
Copy link
Contributor Author

@gibson042 That's exactly what I'm doing. :)

@dmethvin
Copy link
Member

Where does this stand now? It seemed like the technique was showing some promise.

@PaulBRamos
Copy link
Contributor Author

I'm about to do a pull request for a newer version of this fix.

@PaulBRamos
Copy link
Contributor Author

new pull request for this issue: #1047

@PaulBRamos PaulBRamos closed this Nov 28, 2012
@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants