Index: tree.c =================================================================== RCS file: /sources/public/XML/tree.c,v retrieving revision 1.130 diff -p -r1.130 tree.c *** tree.c 2000/10/13 17:01:10 1.130 --- tree.c 2000/10/21 09:08:32 *************** xmlNewChild(xmlNodePtr parent, xmlNsPtr *** 1779,1784 **** --- 1779,1785 ---- * Add a new element @elem as the next siblings of @cur * If the new element was already inserted in a document it is * first unlinked from its existing context. + * As a result of text merging @elem may be freed. * * Returns the new element or NULL in case of error. */ *************** xmlAddNextSibling(xmlNodePtr cur, xmlNod *** 1798,1803 **** --- 1799,1832 ---- } xmlUnlinkNode(elem); + + if (elem->type == XML_TEXT_NODE) { + if (cur->type == XML_TEXT_NODE) { + #ifndef XML_USE_BUFFER_CONTENT + xmlNodeAddContent(cur, elem->content); + #else + xmlNodeAddContent(cur, xmlBufferContent(elem->content)); + #endif + xmlFreeNode(elem); + return(cur); + } + if ((cur->next != NULL) && (cur->type == XML_TEXT_NODE)) { + #ifndef XML_USE_BUFFER_CONTENT + xmlChar *tmp; + + tmp = xmlStrdup(elem->content); + tmp = xmlStrcat(tmp, cur->next->content); + xmlNodeSetContent(cur->next, tmp); + xmlFree(tmp); + #else + xmlBufferAddHead(cur->next, xmlBufferContent(elem->content), + xmlBufferLength(elem->content)); + #endif + xmlFreeNode(elem); + return(cur->next); + } + } + elem->doc = cur->doc; elem->parent = cur->parent; elem->prev = cur; *************** xmlAddNextSibling(xmlNodePtr cur, xmlNod *** 1816,1821 **** --- 1845,1851 ---- * @elem: the new node * * Add a new element @elem as the previous siblings of @cur + * merging adjacent TEXT nodes (@elem may be freed) * If the new element was already inserted in a document it is * first unlinked from its existing context. * *************** xmlAddPrevSibling(xmlNodePtr cur, xmlNod *** 1837,1842 **** --- 1867,1900 ---- } xmlUnlinkNode(elem); + + if (elem->type == XML_TEXT_NODE) { + if (cur->type == XML_TEXT_NODE) { + #ifndef XML_USE_BUFFER_CONTENT + xmlChar *tmp; + + tmp = xmlStrdup(elem->content); + tmp = xmlStrcat(tmp, cur->content); + xmlNodeSetContent(cur, tmp); + xmlFree(tmp); + #else + xmlBufferAddHead(cur->content, xmlBufferContent(elem->content), + xmlBufferLength(elem->content)); + #endif + xmlFreeNode(elem); + return(cur); + } + if ((cur->prev != NULL) && (cur->prev->type == XML_TEXT_NODE)) { + #ifndef XML_USE_BUFFER_CONTENT + xmlNodeAddContent(cur->prev, elem->content); + #else + xmlNodeAddContent(cur->prev, xmlBufferContent(elem->content)); + #endif + xmlFreeNode(elem); + return(cur->prev); + } + } + elem->doc = cur->doc; elem->parent = cur->parent; elem->next = cur; *************** xmlAddPrevSibling(xmlNodePtr cur, xmlNod *** 1855,1860 **** --- 1913,1919 ---- * @elem: the new node * * Add a new element @elem to the list of siblings of @cur + * merging adjacent TEXT nodes (@elem may be freed) * If the new element was already inserted in a document it is * first unlinked from its existing context. * *************** xmlAddSibling(xmlNodePtr cur, xmlNodePtr *** 1892,1897 **** --- 1951,1967 ---- } xmlUnlinkNode(elem); + + if ((cur->type == XML_TEXT_NODE) && (elem->type == XML_TEXT_NODE)) { + #ifndef XML_USE_BUFFER_CONTENT + xmlNodeAddContent(cur, elem->content); + #else + xmlNodeAddContent(cur, xmlBufferContent(elem->content)); + #endif + xmlFreeNode(elem); + return(cur); + } + if (elem->doc == NULL) elem->doc = cur->doc; /* the parent may not be linked to a doc ! */ *************** xmlAddSibling(xmlNodePtr cur, xmlNodePtr *** 1912,1917 **** --- 1982,1988 ---- * @cur: the first node in the list * * Add a list of node at the end of the child list of the parent + * merging adjacent TEXT nodes (@cur may be freed) * * Returns the last child or NULL in case of error. */ *************** xmlAddChildList(xmlNodePtr parent, xmlNo *** 1946,1951 **** --- 2017,2043 ---- if (parent->children == NULL) { parent->children = cur; } else { + /* + * If cur and parent->last both are TEXT nodes, then merge them. + */ + if ((cur->type == XML_TEXT_NODE) && + (parent->last->type == XML_TEXT_NODE)) { + #ifndef XML_USE_BUFFER_CONTENT + xmlNodeAddContent(parent->last, cur->content); + #else + xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); + #endif + /* + * if it's the only child, nothing more to be done. + */ + if (cur->next == NULL) { + xmlFreeNode(cur); + return(parent->last); + } + prev = cur; + cur = cur->next; + xmlFreeNode(prev); + } prev = parent->last; prev->next = cur; cur->prev = prev; *************** xmlAddChildList(xmlNodePtr parent, xmlNo *** 1967,1973 **** * @parent: the parent node * @cur: the child node * ! * Add a new child element, to @parent, at the end of the child list. * Returns the child or NULL in case of error. */ xmlNodePtr --- 2059,2066 ---- * @parent: the parent node * @cur: the child node * ! * Add a new child element, to @parent, at the end of the child list ! * merging adjacent TEXT nodes (in which case @cur is freed) * Returns the child or NULL in case of error. */ xmlNodePtr *************** xmlAddChild(xmlNodePtr parent, xmlNodePt *** 1993,1998 **** --- 2086,2119 ---- #ifdef DEBUG_TREE fprintf(stderr, "Elements moved to a different document\n"); #endif + } + + /* + * If cur is a TEXT node, merge its content with adjacent TEXT nodes + * or with parent->content if parent->content != NULL. + * cur is then freed. + */ + if (cur->type == XML_TEXT_NODE) { + if (((parent->type == XML_ELEMENT_NODE) || + (parent->type == XML_TEXT_NODE)) && + (parent->content != NULL)) { + #ifndef XML_USE_BUFFER_CONTENT + xmlNodeAddContent(parent, cur->content); + #else + xmlNodeAddContent(parent, xmlBufferContent(cur->content)); + #endif + xmlFreeNode(cur); + return(parent); + } + if ((parent->last != NULL) && (parent->last->type == XML_TEXT_NODE)) { + #ifndef XML_USE_BUFFER_CONTENT + xmlNodeAddContent(parent->last, cur->content); + #else + xmlNodeAddContent(parent->last, xmlBufferContent(cur->content)); + #endif + xmlFreeNode(cur); + return(parent->last); + } } /*