Commit 10d08679 authored by Nicolas Delaby's avatar Nicolas Delaby

Improve Handling of mixed content

parent b0dbe4fd
0.8.1.5 (unreleased)
--------------------
* Fix rst syntax
* Improve Handling of mixed content
0.8.1.4 (2011/08/05)
--------------------
......
......@@ -206,6 +206,15 @@ class ERP5Diff:
update_element.text = element.text
root.append(update_element)
def _xupdateUpdateTextNode(self, element, text, path, nsmap=None):
"""Update only text attribute
"""
root = self._getResultRoot()
update_element = etree.Element('{%s}update' % self._ns, nsmap=nsmap)
update_element.attrib['select'] = path
update_element.text = text
root.append(update_element)
def _xupdateRemoveElement(self, path, nsmap=None):
"""
Remove an element at 'path'.
......@@ -571,7 +580,7 @@ class ERP5Diff:
# This means that the semantics of this element is quite different.
self._p("One of them has only text and the other does not, so just update all the contents.")
self._xupdateUpdateElement(new_element, path, nsmap=new_element.nsmap)
elif not old_ignore_text:
elif not old_ignore_text and not len(old_element):
# The contents are only text.
self._p("Both have only text.")
old_text = self._aggregateText(old_element)
......@@ -596,6 +605,22 @@ class ERP5Diff:
new_node = new_list[new_current]
if self._testElements(old_node, new_node):
self._testAttributes(old_node, new_node, child_path)
if not old_ignore_text and len(old_element):
# Mixed Content
if old_node.text and old_node.text.strip() and 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))
self._xupdateUpdateTextNode(new_node, new_node.text,
text_path, nsmap=new_element.nsmap)
if old_node.tail and old_node.tail.strip() and new_node.tail\
and new_node.tail.strip() and old_node.tail != new_node.tail:
position = 1
if new_node.getparent().text:
position += 1
position += len([sibling for sibling in old_node.itersiblings(preceding=True) if sibling.tail])
text_path = path + '/text()[%i]' % (position)
self._xupdateUpdateTextNode(new_node, new_node.tail,
text_path, nsmap=new_element.nsmap)
self._compareChildNodes(old_node, new_node, child_path)
new_start = new_current + 1
if new_current in new_object_left_index_set:
......
......@@ -1117,16 +1117,27 @@ class TestERP5Diff(unittest.TestCase):
old_xml = """
<ul>
<node/>blablabla
<node>AAA<blank/>BBB</node>
<node>AAA<blank/>BBB</node>
<node>AAA<blank/>BBB<blank/>BBB</node>CCC
</ul>
"""
new_xml = """
<ul>
<node/>yayaya
<node>C<blank/>BBB</node>
<node>AAA<blank/>D</node>
<node>AAA<blank/>BBB<blank/>E</node>F
</ul>
"""
expected_result_string = """<xupdate:modifications xmlns:xupdate="http://www.xmldb.org/xupdate" version="1.0">
<xupdate:update select="/ul"><node/>yayaya
<xupdate:update select="/ul/text()[1]">yayaya
</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/text()[2]">F
</xupdate:update>
<xupdate:update select="/ul/node[4]/text()[3]">E</xupdate:update>
</xupdate:modifications>
"""
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