Skip to content

Commit 441abdf

Browse files
committed
Performance optimizations.
1 parent 92285f3 commit 441abdf

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/HTMLServerComponentsCompiler.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,39 +155,32 @@ public function process($content, array $options = [])
155155
}
156156
$insertHTMLSources = [];
157157
$list = []; // Save the elements into an array because removeChild() messes up the NodeList
158-
foreach ($componentElements as $componentElement) {
159-
$isInOtherComponentTag = false;
158+
foreach ($componentElements as $index => $componentElement) {
160159
$parentNode = $componentElement->parentNode;
160+
$list[$index] = [$componentElement, $parentNode, []]; // The last one will contain the parents tag names
161161
while ($parentNode !== null && isset($parentNode->tagName)) {
162-
if (array_search($parentNode->tagName, $tagNames) !== false) {
163-
$isInOtherComponentTag = true;
162+
$tagName = $parentNode->tagName;
163+
$list[$index][2][] = $tagName;
164+
if ($tagName === 'head' || $tagName === 'body') {
164165
break;
165166
}
166167
$parentNode = $parentNode->parentNode;
167168
}
168-
if (!$isInOtherComponentTag) {
169-
$list[] = $componentElement;
170-
}
171169
}
172-
foreach ($list as $i => $componentElement) {
170+
foreach ($list as $index => $componentData) {
171+
if (!empty(array_intersect($componentData[2], $tagNames))) {
172+
continue;
173+
}
174+
$componentElement = $componentData[0];
173175
$component = $this->makeComponent($componentElement->getAttributes(), $componentElement->innerHTML, $componentElement->tagName);
174176
$componentResultHTML = $getComponentResultHTML($component);
175-
$isInBodyTag = false;
176-
$parentNode = $componentElement->parentNode;
177-
while ($parentNode !== null && isset($parentNode->tagName)) {
178-
if ($parentNode->tagName === 'body') {
179-
$isInBodyTag = true;
180-
break;
181-
}
182-
$parentNode = $parentNode->parentNode;
183-
}
184-
if ($isInBodyTag) {
185-
$insertTargetName = 'html-server-components-compiler-insert-target-' . $i;
186-
$componentElement->parentNode->insertBefore($domDocument->createInsertTarget($insertTargetName), $componentElement);
187-
$componentElement->parentNode->removeChild($componentElement); // must be before insertHTML because a duplicate elements IDs can occur.
177+
if (array_search('body', $componentData[2]) !== false) {
178+
$insertTargetName = 'html-server-components-compiler-insert-target-' . $index;
179+
$componentData[1]->insertBefore($domDocument->createInsertTarget($insertTargetName), $componentElement);
180+
$componentData[1]->removeChild($componentElement); // must be before insertHTML because a duplicate elements IDs can occur.
188181
$insertHTMLSources[] = ['source' => $componentResultHTML, 'target' => $insertTargetName];
189182
} else {
190-
$componentElement->parentNode->removeChild($componentElement);
183+
$componentData[1]->removeChild($componentElement);
191184
$insertHTMLSources[] = ['source' => $componentResultHTML];
192185
}
193186
}

tests/Test.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,16 @@ public function testHeadContent()
222222
$expectedResult = '<!DOCTYPE html>' . "\n" . '<html><head><title>222</title></head><body></body></html>';
223223
$this->assertTrue($result === $expectedResult);
224224
}
225+
226+
/**
227+
*
228+
*/
229+
public function testDifferentComponentLocations()
230+
{
231+
232+
$compiler = new \IvoPetkov\HTMLServerComponentsCompiler();
233+
$result = $compiler->process('<html><head><component src="data:base64,' . base64_encode('<body><script>var a1=1;</script><div>1</div></body>') . '" /></head><body><component src="data:base64,' . base64_encode('<script>var a2=2;</script><div>2</div>') . '" /></body></html>');
234+
$expectedResult = '<!DOCTYPE html>' . "\n" . '<html><head></head><body><script>var a2=2;</script><div>2</div><script>var a1=1;</script><div>1</div></body></html>';
235+
$this->assertTrue($result === $expectedResult);
236+
}
225237
}

0 commit comments

Comments
 (0)