[xml] Text node merging is inconsistent when using addPrevSibling vs. addNextSibling



Given the following document,

<root>a<b>c</b></root>

reparenting the 'c' text node using addPrevSibling(b_node, c_node) cause the document to end up with two text nodes 'a' and 'c', in other words root will have 3 nodes 'a', 'c' text nodes and the empty b node. On the other hand if addNextSibling(a_node, c_node) was used, the text nodes will be merged correctly and the root node will have 2 nodes 'ac' text node and the empty b node. Below is a test program that outputs the contents of the nodes in both cases.

This was tested with version 2.7.8 on ubuntu as well as master (commit 8fc913fcc9d2c3fd5c76694cb4e631ca1242de37)

#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

int main() {
  /*
   * this initialize the library and check potential ABI mismatches
   * between the version it was compiled for and the actual shared
   * library used.
   */
  LIBXML_TEST_VERSION;

  xmlDocPtr doc1, doc2; /* the resulting document tree */

  const char doc_content [] = "<root>a<b>c</b></root>";
  doc1 = xmlReadMemory(doc_content, sizeof(doc_content), NULL, NULL, 1);
  doc2 = xmlReadMemory(doc_content, sizeof(doc_content), NULL, NULL, 1);
  if (!doc1 || !doc2) {
    fprintf(stderr, "Failed to parse %s\n", doc_content);
    return 1;
  }

  xmlNodePtr b_node = doc1->children->last;
  xmlNodePtr c_node = b_node->last;
  xmlAddPrevSibling(b_node, c_node);
  b_node = doc2->children->last;
  c_node = b_node->last;
  xmlAddNextSibling(doc2->children->children, c_node);

  for (xmlNodePtr node = doc1->children->children; node; node = node->next)
    printf("contents: %s\n", node->content);

  for (xmlNodePtr node = doc2->children->children; node; node = node->next)
    printf("contents: %s\n", node->content);

  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]