Commit 79d944af authored by Nicolas Delaby's avatar Nicolas Delaby

Add support of deletion/addition for tailed text nodes

parent 0f6d2730
0.8.1.6 (unreleased) 0.8.1.6 (unreleased)
-------------------- --------------------
* Bug fix
* Add support of deletion/addition for tailed text nodes
0.8.1.5 (2011/08/08) 0.8.1.5 (2011/08/08)
-------------------- --------------------
......
...@@ -202,8 +202,7 @@ class ERP5Diff: ...@@ -202,8 +202,7 @@ class ERP5Diff:
for child in element: for child in element:
clone_node = deepcopy(child) clone_node = deepcopy(child)
update_element.append(clone_node) update_element.append(clone_node)
else: update_element.text = element.text
update_element.text = element.text
root.append(update_element) root.append(update_element)
def _xupdateUpdateTextNode(self, element, text, path, nsmap=None): def _xupdateUpdateTextNode(self, element, text, path, nsmap=None):
...@@ -215,6 +214,16 @@ class ERP5Diff: ...@@ -215,6 +214,16 @@ class ERP5Diff:
update_element.text = text update_element.text = text
root.append(update_element) root.append(update_element)
def _xupdateInsertAfterTextNode(self, element, path, nsmap=None):
"""insert new tail text node
"""
root = self._getResultRoot()
insert_element = etree.Element('{%s}insert-after' % self._ns, nsmap=nsmap)
insert_element.attrib['select'] = path
text_element = etree.SubElement(insert_element, '{%s}text' % self._ns, nsmap=nsmap)
text_element.text = element.tail
root.append(insert_element)
def _xupdateRemoveElement(self, path, nsmap=None): def _xupdateRemoveElement(self, path, nsmap=None):
""" """
Remove an element at 'path'. Remove an element at 'path'.
...@@ -607,20 +616,28 @@ class ERP5Diff: ...@@ -607,20 +616,28 @@ class ERP5Diff:
self._testAttributes(old_node, new_node, child_path) self._testAttributes(old_node, new_node, child_path)
if not old_ignore_text and len(old_element): if not old_ignore_text and len(old_element):
# Mixed Content # Mixed Content
if old_node.text and old_node.text.strip() and new_node.text\ if ((old_node.text and old_node.text.strip()) or (new_node.text\
and new_node.text.strip() and old_node.text != new_node.text: and new_node.text.strip())) and old_node.text != new_node.text\
text_path = child_path + '/text()[%i]' % (new_node.getparent().index(new_node)) and (self._checkIgnoreText(new_node) == self._checkIgnoreText(old_node)):
text_path = child_path + '/text()[1]'
self._xupdateUpdateTextNode(new_node, new_node.text, self._xupdateUpdateTextNode(new_node, new_node.text,
text_path, nsmap=new_element.nsmap) text_path, nsmap=new_element.nsmap)
if old_node.tail and old_node.tail.strip() and new_node.tail\ if ((old_node.tail and old_node.tail.strip()) or (new_node.tail\
and new_node.tail.strip() and old_node.tail != new_node.tail: and new_node.tail.strip())) and old_node.tail != new_node.tail:
position = 1 if old_node.tail:
if new_node.getparent().text: # case 1: edit existing text node, usage of position should works to reach
position += 1 # expected text node
position += len([sibling for sibling in old_node.itersiblings(preceding=True) if sibling.tail]) position = 1
text_path = path + '/text()[%i]' % (position) position += len([sibling for sibling in old_node.itersiblings(preceding=True) if sibling.tail])
self._xupdateUpdateTextNode(new_node, new_node.tail, if old_node.getparent().text:
text_path, nsmap=new_element.nsmap) position += 1
text_path = path + '/text()[%i]' % (position)
self._xupdateUpdateTextNode(new_node, new_node.tail,
text_path, nsmap=new_element.nsmap)
else:
# case 2: Means adding a new text node, the position is ambiguous,
# so use insert-after instead
self._xupdateInsertAfterTextNode(new_node, child_path, nsmap=new_element.nsmap)
self._compareChildNodes(old_node, new_node, child_path) self._compareChildNodes(old_node, new_node, child_path)
new_start = new_current + 1 new_start = new_current + 1
if new_current in new_object_left_index_set: if new_current in new_object_left_index_set:
......
...@@ -1120,6 +1120,10 @@ class TestERP5Diff(unittest.TestCase): ...@@ -1120,6 +1120,10 @@ class TestERP5Diff(unittest.TestCase):
<node>AAA<blank/>BBB</node> <node>AAA<blank/>BBB</node>
<node>AAA<blank/>BBB</node> <node>AAA<blank/>BBB</node>
<node>AAA<blank/>BBB<blank/>BBB</node>CCC <node>AAA<blank/>BBB<blank/>BBB</node>CCC
<a_node/>AAA
<b_node/>
<b_node/>
<node/>
</ul> </ul>
""" """
new_xml = """ new_xml = """
...@@ -1128,6 +1132,10 @@ class TestERP5Diff(unittest.TestCase): ...@@ -1128,6 +1132,10 @@ class TestERP5Diff(unittest.TestCase):
<node>C<blank/>BBB</node> <node>C<blank/>BBB</node>
<node>AAA<blank/>D</node> <node>AAA<blank/>D</node>
<node>AAA<blank/>BBB<blank/>E</node>F <node>AAA<blank/>BBB<blank/>E</node>F
<a_node/>
<b_node/>
<b_node/>G
<node>BBB<blank/>H</node>
</ul> </ul>
""" """
expected_result_string = """<xupdate:modifications xmlns:xupdate="http://www.xmldb.org/xupdate" version="1.0"> expected_result_string = """<xupdate:modifications xmlns:xupdate="http://www.xmldb.org/xupdate" version="1.0">
...@@ -1136,8 +1144,14 @@ class TestERP5Diff(unittest.TestCase): ...@@ -1136,8 +1144,14 @@ class TestERP5Diff(unittest.TestCase):
<xupdate:update select="/ul/node[2]/text()[1]">C</xupdate:update> <xupdate:update select="/ul/node[2]/text()[1]">C</xupdate:update>
<xupdate:update select="/ul/node[3]/text()[2]">D</xupdate:update> <xupdate:update select="/ul/node[3]/text()[2]">D</xupdate:update>
<xupdate:update select="/ul/text()[2]">F <xupdate:update select="/ul/text()[2]">F
</xupdate:update> </xupdate:update>
<xupdate:update select="/ul/node[4]/text()[3]">E</xupdate:update> <xupdate:update select="/ul/node[4]/text()[3]">E</xupdate:update>
<xupdate:update select="/ul/text()[3]"/>
<xupdate:insert-after select="/ul/b_node[2]">
<xupdate:text>G
</xupdate:text>
</xupdate:insert-after>
<xupdate:update select="/ul/node[5]">BBB<blank/>H</xupdate:update>
</xupdate:modifications> </xupdate:modifications>
""" """
self._assertERP5DiffWorks(old_xml, new_xml, expected_result_string) self._assertERP5DiffWorks(old_xml, new_xml, expected_result_string)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment