From 70abd76fb2b7be08c6e79b3933a7c6782ada1903 Mon Sep 17 00:00:00 2001 From: Daniel Troger Date: Mon, 15 Jan 2024 11:05:39 +0100 Subject: [PATCH] Attempt to fix insertExpression throw --- packages/dom-expressions/src/client.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/dom-expressions/src/client.js b/packages/dom-expressions/src/client.js index 79be3f18..30dbd497 100644 --- a/packages/dom-expressions/src/client.js +++ b/packages/dom-expressions/src/client.js @@ -402,7 +402,16 @@ function insertExpression(parent, value, current, marker, unwrapArray) { if (value === current) return current; const t = typeof value, multi = marker !== undefined; - parent = (multi && current[0] && current[0].parentNode) || parent; + const maybeChangedParent = (multi && current[0] && current[0].parentNode); + + // Don't change the parent if that would lead us to run into "Uncaught DOMException: Failed to execute 'replaceChild' on 'Node': The new child element contains the parent." + if(maybeChangedParent && maybeChangedParent !== value){ + // Previously, if maybeChangedParent was truthy, it was always used (the parent was always assumed to be the same as the parent of the first child). + // According to fabiospampinato on discord (https://discord.com/channels/722131463138705510/751355413701591120/1195741968538554438) this is to check if a node should actually be removed from its parent (since it could have moved) + // Probably the whole approach here should be re-thought, but the previous assumption above resulted in bug 1 detailed in https://github.com/solidjs/solid/issues/2030 + // This if statement is the least possible change to make that case work, probably without breaking anything else + parent = maybeChangedParent; + } if (t === "string" || t === "number") { if (sharedConfig.context) return current;