diff --git a/src/calibre/ebooks/epub/from_html.py b/src/calibre/ebooks/epub/from_html.py
index e0224cea88..722b6cd4e3 100644
--- a/src/calibre/ebooks/epub/from_html.py
+++ b/src/calibre/ebooks/epub/from_html.py
@@ -153,11 +153,27 @@ class HTMLProcessor(Processor, Rationalizer):
Perform various markup transforms to get the output to render correctly
in the quirky ADE.
'''
- # Replace
that are children of
with
+ # Replace
that are children of as ADE doesn't handle them
if hasattr(self.body, 'xpath'):
for br in self.body.xpath('./br'):
+ if br.getparent() is None:
+ continue
+ try:
+ sibling = br.itersiblings().next()
+ except:
+ sibling = None
br.tag = 'p'
br.text = u'\u00a0'
+ if (br.tail and br.tail.strip()) or sibling is None or \
+ getattr(sibling, 'tag', '') != 'br':
+ br.set('style', br.get('style', '')+'; margin: 0pt; border:0pt; height:0pt')
+ else:
+ sibling.getparent().remove(sibling)
+ if sibling.tail:
+ if not br.tail:
+ br.tail = ''
+ br.tail += sibling.tail
+
if self.opts.profile.remove_object_tags:
for tag in self.root.xpath('//embed'):