Skip to content

Commit

Permalink
Merge pull request #14 from Shoobx/htmlformatter
Browse files Browse the repository at this point in the history
Make a HTML snippet differ instead of RML
  • Loading branch information
regebro committed Sep 9, 2018
2 parents 94c2eca + c0ff692 commit 24d4ba8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
11 changes: 11 additions & 0 deletions tests/test_data/example.expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html xmlns:diff="http://namespaces.shoobx.com/diff">
<head>
<title><diff:insert>HTML </diff:insert>Example<diff:delete> HTML</diff:delete></title>
</head>
<body>
<h1>My <i diff:insert-formatting="">First</i> Heading</h1>
<p>My <diff:delete>first </diff:delete>paragraph<diff:insert> has changed</diff:insert>.</p>
</body>
</html>

14 changes: 14 additions & 0 deletions tests/test_data/example.left.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Example HTML</title>
</head>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

14 changes: 14 additions & 0 deletions tests/test_data/example.right.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>HTML Example</title>
</head>
<body>

<h1>My <i>First</i> Heading</h1>

<p>My paragraph has changed.</p>

</body>
</html>

5 changes: 5 additions & 0 deletions xmldiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def set_trees(self, left, right):
self.clear()

# Make sure we were passed two lxml elements:
if isinstance(left, etree._ElementTree):
left = left.getroot()
if isinstance(right, etree._ElementTree):
right = right.getroot()

if not (etree.iselement(left) and etree.iselement(right)):
raise TypeError("The 'left' and 'right' parameters must be "
"lxml Elements.")
Expand Down
11 changes: 8 additions & 3 deletions xmldiff/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ def format(self, diff, orig_tree):
# and also because we don't want to modify the original tree.

result = deepcopy(orig_tree)
if isinstance(result, etree._ElementTree):
root = result.getroot()
else:
root = result

etree.register_namespace(DIFF_PREFIX, DIFF_NS)

deferred = []
Expand All @@ -320,12 +325,12 @@ def format(self, diff, orig_tree):
# We need to do text updates last
deferred.append(action)
continue
self.handle_action(action, result)
self.handle_action(action, root)

for action in reversed(deferred):
self.handle_action(action, result)
self.handle_action(action, root)

self.finalize(result)
self.finalize(root)

etree.cleanup_namespaces(result, top_nsmap={DIFF_PREFIX: DIFF_NS})
return etree.tounicode(result, pretty_print=self.pretty_print)
Expand Down
4 changes: 0 additions & 4 deletions xmldiff/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

def diff_trees(left, right, F=0.5, uniqueattrs=None, formatter=None):
"""Takes two lxml root elements or element trees"""
if isinstance(left, etree._ElementTree):
left = left.getroot()
if isinstance(right, etree._ElementTree):
right = right.getroot()
if formatter is not None:
formatter.prepare(left, right)
differ = diff.Differ(F=F, uniqueattrs=uniqueattrs)
Expand Down

0 comments on commit 24d4ba8

Please sign in to comment.