Skip to content

Commit f111f6b

Browse files
committed
Insert target fix.
1 parent 0270690 commit f111f6b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/HTMLServerComponentsCompiler.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,23 @@ public function process($html)
7373
} else {
7474
throw new \Exception('Component src attribute missing! ' . $domDocument->saveHTML($component));
7575
}
76-
$insertTargetName = 'html-server-components-compiler-target-' . uniqid();
77-
$component->parentNode->insertBefore($domDocument->createInsertTarget($insertTargetName), $component);
78-
$domDocument->insertHTML($componentHTML, $insertTargetName);
76+
77+
$isInBodyTag = false;
78+
$parentNode = $component->parentNode;
79+
while ($parentNode !== null && isset($parentNode->tagName)) {
80+
if ($parentNode->tagName === 'body') {
81+
$isInBodyTag = true;
82+
break;
83+
}
84+
$parentNode = $parentNode->parentNode;
85+
}
86+
if ($isInBodyTag) {
87+
$insertTargetName = 'html-server-components-compiler-target-' . uniqid();
88+
$component->parentNode->insertBefore($domDocument->createInsertTarget($insertTargetName), $component);
89+
$domDocument->insertHTML($componentHTML, $insertTargetName);
90+
} else {
91+
$domDocument->insertHTML($componentHTML);
92+
}
7993

8094
$component->parentNode->removeChild($component);
8195
}

0 commit comments

Comments
 (0)