Skip to content
Merged
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
29 changes: 29 additions & 0 deletions integration-tests/fragment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,35 @@ describe("<Fragment>", () => {
);
});

test("flattens nested <Fragment> components recursively", () => {
function App() {
return createElement("main", {
"data-testid": "container",
children: [
"before",
createElement(Fragment, {
children: [
createElement("span", { children: "outer" }),
createElement(Fragment, {
children: createElement("b", { children: "inner" }),
}),
],
}),
"after",
],
});
}

cleanup = attachComponent({
htmlElement: document.body,
component: createElement(App),
});

expect(screen.getByTestId("container").innerHTML).toBe(
"before<span>outer</span><b>inner</b>after",
);
});

test("supports updating children in <Fragment> components correctly", async () => {
const user = userEvent.setup();
function App() {
Expand Down
49 changes: 12 additions & 37 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,43 +157,18 @@ function insertNode({
let lastInsertedNode: HTMLElement | Text | null = null;

(velesElement as ExecutedVelesElement).childComponents.forEach((childComponentofPhantom) => {
if ("executedVelesNode" in childComponentofPhantom) {
if (lastInsertedNode) {
lastInsertedNode.after(childComponentofPhantom.html);
} else {
if (adjacentNode) {
adjacentNode.after(childComponentofPhantom.html);
} else {
parentVelesElement.html.prepend(childComponentofPhantom.html);
}
}
childComponentofPhantom.parentVelesElement = parentVelesElement;
lastInsertedNode = childComponentofPhantom.html;
} else if ("executedVelesStringElement" in childComponentofPhantom) {
if (lastInsertedNode) {
lastInsertedNode.after(childComponentofPhantom.html);
} else {
if (adjacentNode) {
adjacentNode.after(childComponentofPhantom.html);
} else {
parentVelesElement.html.prepend(childComponentofPhantom.html);
}
}
childComponentofPhantom.parentVelesElement = parentVelesElement;
lastInsertedNode = childComponentofPhantom.html;
} else {
const executedNode = getExecutedComponentVelesNode(childComponentofPhantom);
if (lastInsertedNode) {
lastInsertedNode.after(executedNode.html);
} else {
if (adjacentNode) {
adjacentNode.after(executedNode.html);
} else {
parentVelesElement.html.prepend(executedNode.html);
}
}
executedNode.parentVelesElement = parentVelesElement;
lastInsertedNode = executedNode.html;
const executedNode =
"executedVelesComponent" in childComponentofPhantom
? getExecutedComponentVelesNode(childComponentofPhantom)
: childComponentofPhantom;
const lastInsertedChildNode = insertNode({
velesElement: executedNode,
adjacentNode: lastInsertedNode ?? adjacentNode,
parentVelesElement,
});

if (lastInsertedChildNode) {
lastInsertedNode = lastInsertedChildNode;
}
});
velesElement.parentVelesElement = parentVelesElement;
Expand Down
Loading