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