@@ -895,21 +895,23 @@ static void dom_node_insert_before_legacy(zval *return_value, zval *ref, dom_obj
895
895
RETURN_FALSE ;
896
896
}
897
897
898
- if (child -> doc == NULL && parentp -> doc != NULL ) {
899
- dom_set_document_ref_pointers (child , intern -> document );
900
- }
901
-
902
- php_libxml_invalidate_node_list_cache (intern -> document );
903
-
898
+ xmlNodePtr refp = NULL ;
904
899
if (ref != NULL ) {
905
- xmlNodePtr refp ;
906
900
dom_object * refpobj ;
907
901
DOM_GET_OBJ (refp , ref , xmlNodePtr , refpobj );
908
902
if (refp -> parent != parentp ) {
909
903
php_dom_throw_error (NOT_FOUND_ERR , stricterror );
910
904
RETURN_FALSE ;
911
905
}
906
+ }
907
+
908
+ if (child -> doc == NULL && parentp -> doc != NULL ) {
909
+ dom_set_document_ref_pointers (child , intern -> document );
910
+ }
911
+
912
+ php_libxml_invalidate_node_list_cache (intern -> document );
912
913
914
+ if (ref != NULL ) {
913
915
if (child -> parent != NULL ) {
914
916
xmlUnlinkNode (child );
915
917
}
@@ -1196,6 +1198,13 @@ static void dom_node_replace_child(INTERNAL_FUNCTION_PARAMETERS, bool modern)
1196
1198
RETURN_FALSE ;
1197
1199
}
1198
1200
1201
+ /* This is already disallowed by libxml, but we should check it here to avoid
1202
+ * breaking assumptions and assertions. */
1203
+ if ((oldchild -> type == XML_ATTRIBUTE_NODE ) != (newchild -> type == XML_ATTRIBUTE_NODE )) {
1204
+ php_dom_throw_error (HIERARCHY_REQUEST_ERR , stricterror );
1205
+ RETURN_FALSE ;
1206
+ }
1207
+
1199
1208
if (oldchild -> parent != nodep ) {
1200
1209
php_dom_throw_error (NOT_FOUND_ERR , stricterror );
1201
1210
RETURN_FALSE ;
0 commit comments