From d84721866ca04f4d1cdc8c6ef83b85faf8057089 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 2 Apr 2023 12:24:57 +0530 Subject: [PATCH] Comments editor: When copying to clipboard, copy clean HTML rather than the junk Qt produces. Fixes #2012760 [Enhancement Request: Copy text alignment between html longtext columns](https://bugs.launchpad.net/calibre/+bug/2012760) --- src/calibre/gui2/comments_editor.py | 78 ++++++++++++++++------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index 094a65895e..4593957d03 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -225,6 +225,42 @@ def cleanup_qt_markup(root): # }}} +def fix_html(original_html, original_txt): + raw = original_html + raw = xml_to_unicode(raw, strip_encoding_pats=True, resolve_entities=True)[0] + comments_pat = re.compile(r'', re.DOTALL) + raw = comments_pat.sub('', raw) + if not original_txt and ' 1: + ans = '
%s
'%(''.join(elems)) + else: + ans = ''.join(elems) + if not ans.startswith('<'): + ans = '

%s

'%ans + return xml_replace_entities(ans) + class EditorWidget(QTextEdit, LineEditECM): # {{{ data_changed = pyqtSignal() @@ -261,7 +297,6 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ self.em_size = f.horizontalAdvance('m') self.base_url = None self._parent = weakref.ref(parent) - self.comments_pat = re.compile(r'', re.DOTALL) self.shortcut_map = {} def r(name, icon, text, checkable=False, shortcut=None): @@ -736,40 +771,7 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ @property def html(self): - raw = original_html = self.toHtml() - check = self.toPlainText().strip() - raw = xml_to_unicode(raw, strip_encoding_pats=True, resolve_entities=True)[0] - raw = self.comments_pat.sub('', raw) - if not check and ' 1: - ans = '
%s
'%(''.join(elems)) - else: - ans = ''.join(elems) - if not ans.startswith('<'): - ans = '

%s

'%ans - return xml_replace_entities(ans) + return fix_html(self.toHtml(), self.toPlainText().strip()) @html.setter def html(self, val): @@ -822,6 +824,12 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ c = self.textCursor() return c.hasSelection() + def createMimeDataFromSelection(self): + ans = super().createMimeDataFromSelection() + html, txt = ans.html(), ans.text() + ans.setHtml(fix_html(html, txt)) + return ans + def contextMenuEvent(self, ev): menu = QMenu(self) for ac in 'undo redo -- cut copy paste paste_and_match_style -- select_all'.split():