From ffa7ea75ee5f49bd6c2061f3c0a83df3d48a595a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 14 Sep 2022 11:21:05 +0530 Subject: [PATCH] Make lifting of props more robust, ignoring styles on empty children and also removing empty anchor tags before lifting --- src/calibre/gui2/comments_editor.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index f1617be838..9c106688c4 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -55,10 +55,12 @@ def lift_styles(tag, style_map): has_text = bool(tag.text) child_styles = [] for child in tag.iterchildren('*'): - if child.tail: + if child.tail and child.tail.strip(): has_text = True style = style_map[child] child_styles.append(style) + if not child.text and len(child) == 0: + continue if common_props is None: common_props = style.copy() else: @@ -75,7 +77,7 @@ def lift_styles(tag, style_map): if lifted_props: for style in child_styles: for k in lifted_props: - del style[k] + style.pop(k, None) def filter_qt_styles(style): @@ -183,6 +185,7 @@ def cleanup_qt_markup(root): style_map = defaultdict(dict) for tag in root.xpath('//*[@style]'): style_map[tag] = parse_style(tag.get('style')) + convert_anchors_to_ids(root) block_tags = root.xpath('//body/*') for tag in block_tags: lift_styles(tag, style_map) @@ -218,7 +221,6 @@ def cleanup_qt_markup(root): lift(span) merge_contiguous_links(root) - convert_anchors_to_ids(root) # }}}