Skip to content

Commit

Permalink
Merge branch 'PHP-8.4'
Browse files Browse the repository at this point in the history
* PHP-8.4:
  Add missing cache invalidation for innerHTML (#16652)
  • Loading branch information
nielsdos committed Oct 30, 2024
2 parents df2436d + d5e6dd8 commit 1a5ef4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ext/dom/inner_outer_html_mixin.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ zend_result dom_element_inner_html_write(dom_object *obj, zval *newval)
}
}

ZEND_ASSERT(obj->document != NULL);
php_libxml_invalidate_node_list_cache(obj->document);

/* 5. Replace all with fragment within context. */
dom_remove_all_children(context_node);
return php_dom_pre_insert(obj->document, fragment, context_node, NULL) ? SUCCESS : FAILURE;
Expand Down Expand Up @@ -451,6 +454,9 @@ zend_result dom_element_outer_html_write(dom_object *obj, zval *newval)
return FAILURE;
}

ZEND_ASSERT(obj->document != NULL);
php_libxml_invalidate_node_list_cache(obj->document);

/* 7. Replace this with fragment within this's parent. */
if (!php_dom_pre_insert(obj->document, fragment, this->parent, this)) {
xmlFreeNode(fragment);
Expand Down
27 changes: 27 additions & 0 deletions ext/dom/tests/modern/common/innerHTML_cache_invalidation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
$innerHTML cache invalidation
--EXTENSIONS--
dom
--FILE--
<?php

$dom = Dom\XMLDocument::createFromString('<root><a/><a/><a/></root>');
$els = $dom->getElementsByTagName('a');
var_dump($els[0]->tagName);

$dom->documentElement->innerHTML = '<b/>';

echo $dom->saveXML(), "\n";
var_dump($els);
var_dump($els[0]?->tagName);

?>
--EXPECT--
string(1) "a"
<?xml version="1.0" encoding="UTF-8"?>
<root><b/></root>
object(Dom\HTMLCollection)#2 (1) {
["length"]=>
int(0)
}
NULL

0 comments on commit 1a5ef4b

Please sign in to comment.