@@ -485,35 +485,45 @@ void dom_parent_node_before(dom_object *context, zval *nodes, int nodesc)
485
485
xmlFree (fragment );
486
486
}
487
487
488
- void dom_child_node_remove ( dom_object * context )
488
+ static zend_result dom_child_removal_preconditions ( const xmlNodePtr child , int stricterror )
489
489
{
490
- xmlNode * child = dom_object_get_node (context );
491
- xmlNodePtr children ;
492
- int stricterror ;
493
-
494
- stricterror = dom_get_strict_error (context -> document );
495
-
496
490
if (dom_node_is_read_only (child ) == SUCCESS ||
497
491
(child -> parent != NULL && dom_node_is_read_only (child -> parent ) == SUCCESS )) {
498
492
php_dom_throw_error (NO_MODIFICATION_ALLOWED_ERR , stricterror );
499
- return ;
493
+ return FAILURE ;
500
494
}
501
495
502
496
if (!child -> parent ) {
503
497
php_dom_throw_error (NOT_FOUND_ERR , stricterror );
504
- return ;
498
+ return FAILURE ;
505
499
}
506
500
507
501
if (dom_node_children_valid (child -> parent ) == FAILURE ) {
508
- return ;
502
+ return FAILURE ;
509
503
}
510
504
511
- children = child -> parent -> children ;
505
+ xmlNodePtr children = child -> parent -> children ;
512
506
if (!children ) {
513
507
php_dom_throw_error (NOT_FOUND_ERR , stricterror );
508
+ return FAILURE ;
509
+ }
510
+
511
+ return SUCCESS ;
512
+ }
513
+
514
+ void dom_child_node_remove (dom_object * context )
515
+ {
516
+ xmlNode * child = dom_object_get_node (context );
517
+ xmlNodePtr children ;
518
+ int stricterror ;
519
+
520
+ stricterror = dom_get_strict_error (context -> document );
521
+
522
+ if (UNEXPECTED (dom_child_removal_preconditions (child , stricterror ) != SUCCESS )) {
514
523
return ;
515
524
}
516
525
526
+ children = child -> parent -> children ;
517
527
while (children ) {
518
528
if (children == child ) {
519
529
xmlUnlinkNode (child );
@@ -525,4 +535,41 @@ void dom_child_node_remove(dom_object *context)
525
535
php_dom_throw_error (NOT_FOUND_ERR , stricterror );
526
536
}
527
537
538
+ void dom_child_replace_with (dom_object * context , zval * nodes , int nodesc )
539
+ {
540
+ xmlNodePtr child = dom_object_get_node (context );
541
+ xmlNodePtr parentNode = child -> parent ;
542
+
543
+ int stricterror = dom_get_strict_error (context -> document );
544
+ if (UNEXPECTED (dom_child_removal_preconditions (child , stricterror ) != SUCCESS )) {
545
+ return ;
546
+ }
547
+
548
+ xmlNodePtr insertion_point = child -> next ;
549
+
550
+ xmlNodePtr fragment = dom_zvals_to_fragment (context -> document , parentNode , nodes , nodesc );
551
+ if (UNEXPECTED (fragment == NULL )) {
552
+ return ;
553
+ }
554
+
555
+ xmlNodePtr newchild = fragment -> children ;
556
+ xmlDocPtr doc = parentNode -> doc ;
557
+
558
+ if (newchild ) {
559
+ xmlNodePtr last = fragment -> last ;
560
+
561
+ /* Unlink and free it unless it became a part of the fragment. */
562
+ if (child -> parent != fragment ) {
563
+ xmlUnlinkNode (child );
564
+ }
565
+
566
+ dom_pre_insert (insertion_point , parentNode , newchild , fragment );
567
+
568
+ dom_fragment_assign_parent_node (parentNode , fragment );
569
+ dom_reconcile_ns_list (doc , newchild , last );
570
+ }
571
+
572
+ xmlFree (fragment );
573
+ }
574
+
528
575
#endif
0 commit comments