@@ -280,6 +280,33 @@ static zend_result dom_sanity_check_node_list_for_insertion(php_libxml_ref_obj *
280
280
return SUCCESS ;
281
281
}
282
282
283
+ static void dom_pre_insert (xmlNodePtr insertion_point , xmlNodePtr parentNode , xmlNodePtr newchild , xmlNodePtr fragment )
284
+ {
285
+ if (!insertion_point ) {
286
+ /* Place it as last node */
287
+ if (parentNode -> children ) {
288
+ /* There are children */
289
+ newchild -> prev = parentNode -> last ;
290
+ parentNode -> last -> next = newchild ;
291
+ } else {
292
+ /* No children, because they moved out when they became a fragment */
293
+ parentNode -> children = newchild ;
294
+ }
295
+ parentNode -> last = fragment -> last ;
296
+ } else {
297
+ /* Insert fragment before insertion_point */
298
+ fragment -> last -> next = insertion_point ;
299
+ if (insertion_point -> prev ) {
300
+ insertion_point -> prev -> next = newchild ;
301
+ newchild -> prev = insertion_point -> prev ;
302
+ }
303
+ insertion_point -> prev = fragment -> last ;
304
+ if (parentNode -> children == insertion_point ) {
305
+ parentNode -> children = newchild ;
306
+ }
307
+ }
308
+ }
309
+
283
310
void dom_parent_node_append (dom_object * context , zval * nodes , int nodesc )
284
311
{
285
312
xmlNode * parentNode = dom_object_get_node (context );
@@ -331,21 +358,18 @@ void dom_parent_node_prepend(dom_object *context, zval *nodes, int nodesc)
331
358
return ;
332
359
}
333
360
334
- xmlNodePtr newchild , nextsib ;
335
361
xmlNode * fragment = dom_zvals_to_fragment (context -> document , parentNode , nodes , nodesc );
336
362
337
363
if (fragment == NULL ) {
338
364
return ;
339
365
}
340
366
341
- newchild = fragment -> children ;
342
- nextsib = parentNode -> children ;
367
+ xmlNode * newchild = fragment -> children ;
343
368
344
369
if (newchild ) {
345
370
xmlNodePtr last = fragment -> last ;
346
- parentNode -> children = newchild ;
347
- fragment -> last -> next = nextsib ;
348
- nextsib -> prev = last ;
371
+
372
+ dom_pre_insert (parentNode -> children , parentNode , newchild , fragment );
349
373
350
374
dom_fragment_assign_parent_node (parentNode , fragment );
351
375
@@ -355,33 +379,6 @@ void dom_parent_node_prepend(dom_object *context, zval *nodes, int nodesc)
355
379
xmlFree (fragment );
356
380
}
357
381
358
- static void dom_pre_insert (xmlNodePtr insertion_point , xmlNodePtr parentNode , xmlNodePtr newchild , xmlNodePtr fragment )
359
- {
360
- if (!insertion_point ) {
361
- /* Place it as last node */
362
- if (parentNode -> children ) {
363
- /* There are children */
364
- newchild -> prev = parentNode -> last ;
365
- parentNode -> last -> next = newchild ;
366
- } else {
367
- /* No children, because they moved out when they became a fragment */
368
- parentNode -> children = newchild ;
369
- }
370
- parentNode -> last = fragment -> last ;
371
- } else {
372
- /* Insert fragment before insertion_point */
373
- fragment -> last -> next = insertion_point ;
374
- if (insertion_point -> prev ) {
375
- insertion_point -> prev -> next = newchild ;
376
- newchild -> prev = insertion_point -> prev ;
377
- }
378
- insertion_point -> prev = fragment -> last ;
379
- if (parentNode -> children == insertion_point ) {
380
- parentNode -> children = newchild ;
381
- }
382
- }
383
- }
384
-
385
382
void dom_parent_node_after (dom_object * context , zval * nodes , int nodesc )
386
383
{
387
384
/* Spec link: https://dom.spec.whatwg.org/#dom-childnode-after */
0 commit comments