# Bug OP-19669 - Sibling morph breaks after OPCE-* replaceWith in dialog preview

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-19669

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | Medium |
| Module | Frontend components and Primer design system |
| Target versions | 17.9.0 |
| Observed in versions | 17.7.0 |
| Sprint |  |
| Backlog bucket |  |
| Bug found in version (deprecated) | 17.7.x |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers | Alexander Coles |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

*Filed as a "hypothetical bug" — found with AI tooling. Since confirmed real, but not reachable from the UI today.*

## Context

Found while adding test coverage in <mention class="mention" data-id="OP-19666" data-type="work_package" data-text="#OP-19666">#OP-19666</mention> for a code path that had no prior tests. Pre-existing, not introduced by that work — the buggy `replaceWith` call is unchanged on `dev`.

## The bug

In `DialogPreviewController`'s `beforeNodeMorphed` callback (`frontend/src/stimulus/controllers/dynamic/work-packages/dialog/preview.controller.ts`, shared by the date-picker and progress preview controllers):

``` js
if (oldNode.tagName?.startsWith('OPCE-')) {
  if (schedulingChanged) {
    oldNode.replaceWith(newNode);
  }
  return false;
}
```

`replaceWith` mutates both trees idiomorph is walking, breaking the walk two ways:

1.  **Stale insertion point.** `morphChildren` tracks its position via `insertionPoint = bestMatch.nextSibling`, evaluated after `morphNode` returns — but `bestMatch` is the now-detached `oldNode`, so `nextSibling` is `null`. Remaining new children get appended at the end of the parent, and the trailing "remove remaining old nodes" loop is guarded on the same `insertionPoint`, so stale old siblings survive. The result is duplication, not just staleness.

2.  **Skipped new sibling.** `morphChildren` iterates the live `newParent.childNodes` `NodeList` with an index-based iterator. Moving `newNode` out shifts every later entry down one index, so the new sibling immediately after the OPCE node is never visited.

Net effect: when `schedulingChanged` is true and an `OPCE-*` node is replaced, DOM siblings that follow it in the same parent are left stale, skipped, or duplicated.

Verified with a standalone spec calling `Idiomorph.morph()` directly:

``` text
BEFORE replaceWith, nextSibling: <div data-marker="old">old</div>
AFTER replaceWith, oldNode.nextSibling: null
AFTER replaceWith, oldNode.parentNode: null
FINAL HTML: <opce-test-marker data-marker="new"></opce-test-marker><div data-marker="old">old</div>
```

## Can a user hit this?

No. The only `OPCE-*` element in any dialog served by `DialogPreviewController` is `opce-wp-date-picker-instance` (`app/components/work_packages/date_picker/form_component.html.erb`), emitted by `angular_component_tag` as a single empty element and rendered as the sole child of its own `body.with_row` wrapper. With no siblings in that parent there is nothing for the broken walk to skip. The Angular component adds no siblings either: it renders its `<input>` inside the custom element host, and flatpickr's `inline: true` puts the calendar there too. The progress dialog has no `OPCE-*` elements and never sets `schedule_manually`.

So this is a latent trap, not a live defect: adding a second element to that row, or flattening the `with_row` wrapper, would silently break the morph with no error and no failing test.

## Why it went unnoticed

No existing spec mounted a sibling alongside the OPCE node in the `schedulingChanged`-replace case, so the gap was invisible until <mention class="mention" data-id="OP-19666" data-type="work_package" data-text="#OP-19666">#OP-19666</mention> added coverage.

## Hints for QA

**No UI reproduction exists and none is expected** — see "Can a user hit this?" above. There is nothing to test manually, so this can be closed directly after the fix is merged. Coverage is a Vitest regression case in `date-picker/preview.controller.spec.ts` that fails on `dev` and passes with the fix.
