Skip to content

Fix parent check and viable next sibling search for replaceWith #11888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 20 additions & 2 deletions ext/dom/parentnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,21 +545,39 @@ void dom_child_node_remove(dom_object *context)

void dom_child_replace_with(dom_object *context, zval *nodes, int nodesc)
{
/* Spec link: https://dom.spec.whatwg.org/#dom-childnode-replacewith*/
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
/* Spec link: https://dom.spec.whatwg.org/#dom-childnode-replacewith*/
/* Spec link: https://dom.spec.whatwg.org/#dom-childnode-replacewith */


xmlNodePtr child = dom_object_get_node(context);

/* Spec step 1 */
xmlNodePtr parentNode = child->parent;
/* Spec step 2 */
if (!parentNode) {
return;
}

int stricterror = dom_get_strict_error(context->document);
if (UNEXPECTED(dom_child_removal_preconditions(child, stricterror) != SUCCESS)) {
return;
}

xmlNodePtr insertion_point = child->next;
/* Spec step 3: find first following child not in nodes; otherwise null */
xmlNodePtr viable_next_sibling = child->next;
while (viable_next_sibling) {
if (!dom_is_node_in_list(nodes, nodesc, viable_next_sibling)) {
break;
}
viable_next_sibling = viable_next_sibling->next;
}

/* Spec step 4: convert nodes into fragment */
xmlNodePtr fragment = dom_zvals_to_fragment(context->document, parentNode, nodes, nodesc);
if (UNEXPECTED(fragment == NULL)) {
return;
}

/* Spec step 5: perform the replacement */

xmlNodePtr newchild = fragment->children;
xmlDocPtr doc = parentNode->doc;

Expand All @@ -571,7 +589,7 @@ void dom_child_replace_with(dom_object *context, zval *nodes, int nodesc)
if (newchild) {
xmlNodePtr last = fragment->last;

dom_pre_insert(insertion_point, parentNode, newchild, fragment);
dom_pre_insert(viable_next_sibling, parentNode, newchild, fragment);

dom_fragment_assign_parent_node(parentNode, fragment);
dom_reconcile_ns_list(doc, newchild, last);
Expand Down
28 changes: 28 additions & 0 deletions ext/dom/tests/replaceWith_no_parent.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
replaceWith() without a parent
--EXTENSIONS--
dom
--FILE--
<?php
$doc = new DOMDocument;
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container>
<child/>
</container>
XML);

$container = $doc->documentElement;
$child = $container->firstElementChild;

$test = $doc->createElement('foo');
$test->replaceWith($child);
echo $doc->saveXML();
echo $doc->saveXML($test);
?>
--EXPECT--
<?xml version="1.0"?>
<container>
<child/>
</container>
<foo/>
36 changes: 36 additions & 0 deletions ext/dom/tests/replaceWith_non_viable_next_sibling.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
replaceWith() with a non-variable next sibling
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
replaceWith() with a non-variable next sibling
replaceWith() with a non-viable next sibling

--EXTENSIONS--
dom
--FILE--
<?php
$doc = new DOMDocument;
$doc->loadXML(<<<XML
<?xml version="1.0"?>
<container>
<child>
<alone/>
</child>
</container>
XML);

$container = $doc->documentElement;
$child = $container->firstElementChild;
$alone = $child->firstElementChild;

$child->after($alone);
echo $doc->saveXML();
$child->replaceWith($alone);
echo $doc->saveXML();
?>
--EXPECT--
<?xml version="1.0"?>
<container>
<child>

</child><alone/>
</container>
<?xml version="1.0"?>
<container>
<alone/>
</container>