Skip to content

Commit

Permalink
reusing the same function
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Oct 13, 2023
1 parent 1a14d04 commit e858c57
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions packages/runtime-core/src/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ export function createHydrationFunctions(
// on component's rendered output to determine the end of the fragment
// instead, we do a lookahead to find the end anchor node.
nextNode = isFragmentStart
? locateClosingAsyncAnchor(node)
? locateClosingAnchor(node, '[', ']')
: // #4293 #6152 if teleport start look ahead for teleport end.
isComment(node) && node.data === 'teleport start'
? locateClosingTeleportAnchor(node)
? locateClosingAnchor(node, 'teleport start', 'teleport end')
: nextSibling(node)

// #3787
Expand Down Expand Up @@ -527,7 +527,7 @@ export function createHydrationFunctions(

if (isFragment) {
// remove excessive fragment nodes
const end = locateClosingAsyncAnchor(node)
const end = locateClosingAnchor(node, '[', ']')
while (true) {
const next = nextSibling(node)
if (next && next !== end) {
Expand Down Expand Up @@ -555,31 +555,18 @@ export function createHydrationFunctions(
return next
}

const locateClosingAsyncAnchor = (node: Node | null): Node | null => {
let match = 0
while (node) {
node = nextSibling(node)
if (node && isComment(node)) {
if (node.data === '[') match++
if (node.data === ']') {
if (match === 0) {
return nextSibling(node)
} else {
match--
}
}
}
}
return node
}

const locateClosingTeleportAnchor = (node: Node | null): Node | null => {
// looks ahead for a start and closing comment node
const locateClosingAnchor = (
node: Node | null,
startData: string,
endData: string
): Node | null => {
let match = 0
while (node) {
node = nextSibling(node)
if (node && isComment(node)) {
if (node.data === 'teleport start') match++
if (node.data === 'teleport end') {
if (node.data === startData) match++
if (node.data === endData) {
if (match === 0) {
return nextSibling(node)
} else {
Expand Down

0 comments on commit e858c57

Please sign in to comment.