Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,31 @@ private function handleComponent( DOMElement $node, array $data ): bool {
}
$rendered = $this->app->renderComponentToDOM( $componentName, $componentData );
// TODO use adoptNode() instead of importNode() in PHP 8.3+ (see php-src commit ed6df1f0ad)
$node->replaceWith( $node->ownerDocument->importNode( $rendered, true ) );
$importNode = $node->ownerDocument->importNode( $rendered, true );
// TODO An issue in PHP 8.1.21's libxml integration causes a double-free if we replace a node
// directly with itself. T398821
if ( $node != $importNode ) {
$node->replaceWith( $importNode );
} else {
// TODO To work around the double-free, we detach all the children of the parent node and
// re-attach them in the correct sequence, replacing the target node with our newly-imported
// node. Once `mwcli` has moved off this outdated version of PHP (T388411) we should be able
// to remove this workaround. T398821
$parent = $node->parentNode;
$children = [];
foreach ( iterator_to_array( $parent->childNodes ) as $child ) {
if ( $child !== $node ) {
$children[] = $child;
} else {
$children[] = $importNode;
}
$child->remove();
}

foreach ( $children as $child ) {
$parent->appendChild( $child );
}
Comment on lines +163 to +165
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$parent->append( ...$children ); also seems to work FWIW (tested on PHP 8.4.10 and 8.1.18).

}
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/php/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ public function testNestedComponentObjectProp(): void {
$this->assertSame( '<div><p>obj = { a: A, b: B }</p></div>', $result );
}

public function testComponentSubstitutionPreservesOrder(): void {
$app = new App( [] );
$app->registerComponentTemplate( 'root', '<div><x-a></x-a><div><p>Following Text</p></div>' );
$app->registerComponentTemplate( 'x-a', '<p>obj = { a: 1, b: 2 }</p>' );

$result = $app->renderComponent( 'root', [] );

$this->assertSame( '<div><p>obj = { a: 1, b: 2 }</p><div><p>Following Text</p></div></div>', $result );
}

public function testComponentPropKebabCase(): void {
$app = new App( [] );
$app->registerComponentTemplate(
Expand Down