|
| 1 | +import { VNodeProps, type ElementVNode, type VNode } from '../client/types'; |
| 2 | +import { |
| 3 | + vnode_getNextSibling, |
| 4 | + vnode_getPreviousSibling, |
| 5 | + vnode_getProp, |
| 6 | + vnode_locate, |
| 7 | +} from '../client/vnode'; |
| 8 | +import type { ISsrNode } from '../ssr/ssr-types'; |
| 9 | +import { QSlotParent } from './utils/markers'; |
| 10 | + |
| 11 | +/// These global variables are used to avoid creating new arrays for each call to `vnode_documentPosition`. |
| 12 | +const aVNodePath: VNode[] = []; |
| 13 | +const bVNodePath: VNode[] = []; |
| 14 | +/** |
| 15 | + * Compare two VNodes and determine their document position relative to each other. |
| 16 | + * |
| 17 | + * @param a VNode to compare |
| 18 | + * @param b VNode to compare |
| 19 | + * @param rootVNode - Root VNode of a container |
| 20 | + * @returns -1 if `a` is before `b`, 0 if `a` is the same as `b`, 1 if `a` is after `b`. |
| 21 | + */ |
| 22 | +export const vnode_documentPosition = ( |
| 23 | + a: VNode, |
| 24 | + b: VNode, |
| 25 | + rootVNode: ElementVNode | null |
| 26 | +): -1 | 0 | 1 => { |
| 27 | + if (a === b) { |
| 28 | + return 0; |
| 29 | + } |
| 30 | + |
| 31 | + let aDepth = -1; |
| 32 | + let bDepth = -1; |
| 33 | + while (a) { |
| 34 | + const vNode = (aVNodePath[++aDepth] = a); |
| 35 | + a = (vNode[VNodeProps.parent] || |
| 36 | + (rootVNode && vnode_getProp(a, QSlotParent, (id) => vnode_locate(rootVNode, id))))!; |
| 37 | + } |
| 38 | + while (b) { |
| 39 | + const vNode = (bVNodePath[++bDepth] = b); |
| 40 | + b = (vNode[VNodeProps.parent] || |
| 41 | + (rootVNode && vnode_getProp(b, QSlotParent, (id) => vnode_locate(rootVNode, id))))!; |
| 42 | + } |
| 43 | + |
| 44 | + while (aDepth >= 0 && bDepth >= 0) { |
| 45 | + a = aVNodePath[aDepth] as VNode; |
| 46 | + b = bVNodePath[bDepth] as VNode; |
| 47 | + if (a === b) { |
| 48 | + // if the nodes are the same, we need to check the next level. |
| 49 | + aDepth--; |
| 50 | + bDepth--; |
| 51 | + } else { |
| 52 | + // We found a difference so we need to scan nodes at this level. |
| 53 | + let cursor: VNode | null = b; |
| 54 | + do { |
| 55 | + cursor = vnode_getNextSibling(cursor); |
| 56 | + if (cursor === a) { |
| 57 | + return 1; |
| 58 | + } |
| 59 | + } while (cursor); |
| 60 | + cursor = b; |
| 61 | + do { |
| 62 | + cursor = vnode_getPreviousSibling(cursor); |
| 63 | + if (cursor === a) { |
| 64 | + return -1; |
| 65 | + } |
| 66 | + } while (cursor); |
| 67 | + if (rootVNode && vnode_getProp(b, QSlotParent, (id) => vnode_locate(rootVNode, id))) { |
| 68 | + // The "b" node is a projection, so we need to set it after "a" node, |
| 69 | + // because the "a" node could be a context provider. |
| 70 | + return -1; |
| 71 | + } |
| 72 | + // The node is not in the list of siblings, that means it must be disconnected. |
| 73 | + return 1; |
| 74 | + } |
| 75 | + } |
| 76 | + return aDepth < bDepth ? -1 : 1; |
| 77 | +}; |
| 78 | + |
| 79 | +/// These global variables are used to avoid creating new arrays for each call to `ssrNodeDocumentPosition`. |
| 80 | +const aSsrNodePath: ISsrNode[] = []; |
| 81 | +const bSsrNodePath: ISsrNode[] = []; |
| 82 | +/** |
| 83 | + * Compare two SSR nodes and determine their document position relative to each other. Compares only |
| 84 | + * position between parent and child. |
| 85 | + * |
| 86 | + * @param a SSR node to compare |
| 87 | + * @param b SSR node to compare |
| 88 | + * @returns -1 if `a` is before `b`, 0 if `a` is the same as `b`, 1 if `a` is after `b`. |
| 89 | + */ |
| 90 | +export const ssrNodeDocumentPosition = (a: ISsrNode, b: ISsrNode): -1 | 0 | 1 => { |
| 91 | + if (a === b) { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + let aDepth = -1; |
| 96 | + let bDepth = -1; |
| 97 | + while (a) { |
| 98 | + const ssrNode = (aSsrNodePath[++aDepth] = a); |
| 99 | + a = ssrNode.currentComponentNode!; |
| 100 | + } |
| 101 | + while (b) { |
| 102 | + const ssrNode = (bSsrNodePath[++bDepth] = b); |
| 103 | + b = ssrNode.currentComponentNode!; |
| 104 | + } |
| 105 | + |
| 106 | + while (aDepth >= 0 && bDepth >= 0) { |
| 107 | + a = aSsrNodePath[aDepth] as ISsrNode; |
| 108 | + b = bSsrNodePath[bDepth] as ISsrNode; |
| 109 | + if (a === b) { |
| 110 | + // if the nodes are the same, we need to check the next level. |
| 111 | + aDepth--; |
| 112 | + bDepth--; |
| 113 | + } else { |
| 114 | + return 1; |
| 115 | + } |
| 116 | + } |
| 117 | + return aDepth < bDepth ? -1 : 1; |
| 118 | +}; |
0 commit comments