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

handle moving nodes #382

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/dom-expressions/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what might break by this removal but this for sure breaks the parent comparison in cleanChildren

Copy link
Contributor

Choose a reason for hiding this comment

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

Ryan made a comment about this code in a related issue here #307 (comment)
I will have to study it again to refresh my mind, I'm not sure I actually ever understood that part

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've tried to track this down and, oh well 😅, no wonder even Ryan can't remember this. This check existed since the beginning.

Copy link
Owner

Choose a reason for hiding this comment

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

Yeah someone made a codesandbox that was sorting nodes around randomly on an interval and it would occasionally break. I added that way back. But I think its need predates the "modern" way we handle fragments I changed to back in Feb 2019.

In any case I'm moving forward with this not there right now in 2.0 stuff.


if (t === "string" || t === "number") {
if (hydrating) return current;
Expand Down Expand Up @@ -570,7 +569,17 @@ function appendNodes(parent, array, marker = null) {
for (let i = 0, len = array.length; i < len; i++) parent.insertBefore(array[i], marker);
}

const moved = new Set();
let scheduled = false;

function cleanChildren(parent, current, marker, replacement) {
if (!scheduled && moved.size) {
queueMicrotask(() => {
moved.clear();
scheduled = false;
})
scheduled = true;
}
if (marker === undefined) return (parent.textContent = "");
const node = replacement || document.createTextNode("");
if (current.length) {
Expand All @@ -579,9 +588,12 @@ function cleanChildren(parent, current, marker, replacement) {
const el = current[i];
if (node !== el) {
const isParent = el.parentNode === parent;
if (!inserted && !i)
isParent ? parent.replaceChild(node, el) : parent.insertBefore(node, marker);
else isParent && el.remove();
if (!inserted && !i) {
node.parentNode && moved.add(node);
isParent && !moved.has(el)
? parent.replaceChild(node, el)
: parent.insertBefore(node, marker);
} else isParent && el.remove();
} else inserted = true;
}
} else parent.insertBefore(node, marker);
Expand Down
50 changes: 49 additions & 1 deletion packages/dom-expressions/test/dom/insert.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @jest-environment jsdom
*/
import * as r from '../../src/client';
import * as S from "s-js";
import S from "s-js";

describe("r.insert", () => {
// <div><!-- insert --></div>
Expand Down Expand Up @@ -355,6 +355,54 @@ describe("r.insert with Markers", () => {
expect(parent.innerHTML).toBe('hello ');
});

it("can move a node within a parent with marker", () => {
// <div><!-- -->bar<!-- foo --></div>
// <div><!-- foo -->bar<!-- --></div>

const parent = document.createElement("div");
parent.textContent = "bar";
const marker = parent.firstChild;

const node = document.createElement("div");
node.textContent = "foo";

let current = r.insert(parent, null, marker);
let current2 = r.insert(parent, node, null);
expect(parent.outerHTML).toBe("<div>bar<div>foo</div></div>");

current = r.insert(parent, node, marker, current);
current2 = r.insert(parent, null, null, current2);
expect(parent.outerHTML).toBe("<div><div>foo</div>bar</div>");
});

it("can move a node within parents with markers", () => {
// <div><!-- -->bar</div><div><!-- foo -->baz</div>
// <div><!-- foo -->bar</div><div><!-- -->baz</div>

const bar = document.createElement("div");
bar.textContent = "bar";
const barMarker = bar.firstChild;

const baz = document.createElement("div");
baz.textContent = "baz";
const bazMarker = baz.firstChild;

const node = document.createElement("div");
node.textContent = "foo";

let current = r.insert(bar, null, barMarker);
let current2 = r.insert(baz, node, bazMarker);

expect(bar.outerHTML).toBe("<div>bar</div>");
expect(baz.outerHTML).toBe("<div><div>foo</div>baz</div>");

current = r.insert(bar, node, barMarker, current);
current2 = r.insert(baz, null, bazMarker, current2);

expect(bar.outerHTML).toBe("<div><div>foo</div>bar</div>");
expect(baz.outerHTML).toBe("<div>baz</div>");
});

function insert(val) {
const parent = container.cloneNode(true);
r.insert(parent, val, parent.childNodes[1]);
Expand Down
Loading