chore: make it easier to debug markers (#1805)

This commit is contained in:
Marvin Hagemeister
2023-09-21 03:07:31 +02:00
committed by GitHub
parent d906076dee
commit 738a51ad52
+60 -35
View File
@@ -11,16 +11,33 @@ import { assetHashingHook } from "../utils.ts";
function createRootFragment( function createRootFragment(
parent: Element, parent: Element,
replaceNode: Node | Node[], startMarker: Text | Comment,
endMarker: Text, // We need an end marker for islands because multiple
// islands can share the same parent node. Since
// islands are root-level render calls any calls to
// `.appendChild` would lead to a wrong result.
endMarker: Text | Comment,
) { ) {
replaceNode = ([] as Node[]).concat(replaceNode);
// @ts-ignore this is fine // @ts-ignore this is fine
return parent.__k = { return parent.__k = {
nodeType: 1, nodeType: 1,
parentNode: parent, parentNode: parent,
firstChild: replaceNode[0], get firstChild() {
childNodes: replaceNode, const child = startMarker.nextSibling;
if (child === endMarker) return null;
return child;
},
get childNodes() {
const children: ChildNode[] = [];
let child = startMarker.nextSibling;
while (child !== null && child !== endMarker) {
children.push(child);
child = child.nextSibling;
}
return children;
},
insertBefore(node: Node, child: Node | null) { insertBefore(node: Node, child: Node | null) {
parent.insertBefore(node, child ?? endMarker); parent.insertBefore(node, child ?? endMarker);
}, },
@@ -97,8 +114,38 @@ interface Marker {
// string representing the actual intended comment value which makes // string representing the actual intended comment value which makes
// a bunch of stuff easier. // a bunch of stuff easier.
text: string; text: string;
startNode: Comment | null; startNode: Text | Comment | null;
endNode: Comment | null; endNode: Text | Comment | null;
}
const HIDE_MARKERS = true;
/**
* Replace comment markers with empty text nodes to hide them
* in DevTools. This is done to avoid user confusion.
*/
function hideMarker(marker: Marker) {
const { startNode, endNode } = marker;
const parent = endNode!.parentNode!;
if (
!HIDE_MARKERS && startNode !== null &&
startNode.nodeType === Node.COMMENT_NODE
) {
const text = new Text("");
marker.startNode = text;
parent.insertBefore(text, startNode);
startNode.remove();
}
if (
!HIDE_MARKERS && endNode !== null && endNode.nodeType === Node.COMMENT_NODE
) {
const text = new Text("");
marker.endNode = text;
parent.insertBefore(text, endNode);
endNode.remove();
}
} }
/** /**
@@ -189,25 +236,14 @@ function _walkInner(
islandParent.props.children = vnode; islandParent.props.children = vnode;
} }
// Remove markers hideMarker(marker);
marker.startNode?.remove(); sib = marker.endNode.nextSibling;
sib = sib.nextSibling;
marker.endNode.remove();
continue; continue;
} else if (marker.kind === MarkerKind.Island) { } else if (marker.kind === MarkerKind.Island) {
// We're ready to revive this island if it has // We're ready to revive this island if it has
// no roots of its own. Otherwise we'll treat it // no roots of its own. Otherwise we'll treat it
// as a standard component // as a standard component
if (markerStack.length === 0) { if (markerStack.length === 0) {
const children: Node[] = [];
let child: Node | null = marker.startNode;
while (
(child = child!.nextSibling) !== null && child !== marker.endNode
) {
children.push(child);
}
const vnode = vnodeStack[vnodeStack.length - 1]; const vnode = vnodeStack[vnodeStack.length - 1];
if (vnode.props.children == null) { if (vnode.props.children == null) {
@@ -244,23 +280,15 @@ function _walkInner(
const parentNode = sib.parentNode! as HTMLElement; const parentNode = sib.parentNode! as HTMLElement;
// We need an end marker for islands because multiple hideMarker(marker);
// islands can share the same parent node. Since
// islands are root-level render calls any calls to
// `.appendChild` would lead to a wrong result.
const endMarker = new Text("");
parentNode.insertBefore(
endMarker,
marker.endNode,
);
const _render = () => const _render = () =>
render( render(
vnode, vnode,
createRootFragment( createRootFragment(
parentNode, parentNode,
children, marker.startNode!,
endMarker, marker.endNode!,
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
) as any as HTMLElement, ) as any as HTMLElement,
); );
@@ -273,10 +301,7 @@ function _walkInner(
? scheduler!.postTask(_render) ? scheduler!.postTask(_render)
: setTimeout(_render, 0); : setTimeout(_render, 0);
// Remove markers sib = marker.endNode.nextSibling;
marker.startNode?.remove();
sib = sib.nextSibling;
marker.endNode.remove();
continue; continue;
} else if (parent?.kind === MarkerKind.Slot) { } else if (parent?.kind === MarkerKind.Slot) {
// Treat the island as a standard component when it // Treat the island as a standard component when it