Skip to content

Removing an element after its children are moved to be siblings fails on client side #11983

Open

Description

@pleku

Reproducing

// Open following element hierarchy
Element container = ElementFactory.createDiv();
Element wrapper = ElementFactory.createDiv();
Element first = ElementFactory.createDiv();
Element second = ElementFactory.createDiv();
container.add(wrapper);
wrapper.add(first, second);

Element button = ElementFactore.createButton();
button.setText("Click me");
add(container, button);

// Then later on click the button, executing the following code
button.addListener("click", event -> {
    container.appendChild(wrapper.getChildren().toArray(Element[]::new));
    wrapper.removeFromParent();
});

Expected

The children of the wrapper are moved one layer up and wrapper itself is removed.

Actual

Client side error with

(NotFoundError) : Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.


Workaround is to switch the order of operations

wrapper.removeFromParent();
container.appendChild(wrapper.getChildren().toArray(Element[]::new));

Activity

  1. added 2 commits that reference this issue on Oct 15, 2021
    e45da0d
    e51dd7b
  2. added a commit that references this issue on Oct 20, 2021
    2203895
  3. added 4 commits that reference this issue on Oct 20, 2021
    c9fd7b1
    7ff8c7f
    1b399e6
    ed16b62
  4. 3 remaining items

  5. denis-anisimov commented on Oct 22, 2021

    @denis-anisimov
    Contributor

    The issue is caused by the fact that the children are not detached.
    As a result the clean up code which is executed for detached nodes is not executed.
    Such clean up code unregisters a StateNode on the client side from a StateTree and when the Element on the server side is reattached back the StateNode is completely recreated and rebound.
    In the described scenario the moved StateNode child keeps the DomNode whose parent is a removed node.

    I've tried to fix that via removing DomNode on setParent call (so when the StateNode is moved it should be rebound with a new DOM Node which emulate reataching).
    But it looks like there is some scenario which relies on the behavior of preserving DOM node when a new parent is set.
    It might be that this is a case of virtual child but it's absolutely unclear what's exactly broken.
    The broken scenario is : https://github.com/vaadin/flow-components/blob/master/vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/grid/it/GridHeaderFooterRowIT.java#L295
    (some test in Grid).

  6. removed their assignment
    on Oct 22, 2021
  7. vaadin-bot commented on Nov 8, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 14.7.4.

  8. vaadin-bot commented on Nov 8, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 21.0.4.

  9. vaadin-bot commented on Nov 22, 2021

    @vaadin-bot
    Collaborator

    This ticket/PR has been released with platform 21.0.7.

  10. moved this to 🔖 Normal Priority (P2) in Vaadin Flow bugs & maintenanceon Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions