From 69ad2e29843c85b2dc36145c94705571245a029b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 1 Jul 2019 13:49:35 +0530 Subject: [PATCH] Get rid of remaining mainFrame references --- src/calibre/gui2/comments_editor.py | 72 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/src/calibre/gui2/comments_editor.py b/src/calibre/gui2/comments_editor.py index 1617a58531..3116922ed3 100644 --- a/src/calibre/gui2/comments_editor.py +++ b/src/calibre/gui2/comments_editor.py @@ -3,7 +3,6 @@ # License: GPLv3 Copyright: 2010, Kovid Goyal from __future__ import absolute_import, division, print_function, unicode_literals -import json import os import re import weakref @@ -325,43 +324,35 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ @property def html(self): - ans = '' + 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 - ans = xml_replace_entities(ans) + root = html.fromstring(raw) except Exception: - import traceback - traceback.print_exc() + root = parse(raw, maybe_xhtml=False, sanitize_names=True) + if root.xpath('//meta[@name="calibre-dont-sanitize"]'): + # Bypass cleanup if special meta tag exists + return original_html - return ans + elems = [] + for body in root.xpath('//body'): + if body.text: + elems.append(body.text) + elems += [html.tostring(x, encoding='unicode') for x in body if + x.tag not in ('script', 'style')] + + if len(elems) > 1: + ans = '
%s
'%(u''.join(elems)) + else: + ans = ''.join(elems) + if not ans.startswith('<'): + ans = '

%s

'%ans + return xml_replace_entities(ans) @html.setter def html(self, val): @@ -389,9 +380,14 @@ class EditorWidget(QTextEdit, LineEditECM): # {{{ if not allow_undo or self.readonly: self.html = val return - mf = self.page().mainFrame() - mf.evaluateJavaScript('document.execCommand("selectAll", false, null)') - mf.evaluateJavaScript('document.execCommand("insertHTML", false, %s)' % json.dumps(unicode_type(val))) + c = self.textCursor() + c.beginEditBlock() + c.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) + c.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) + c.removeSelectedText() + c.insertHTML(val) + c.endEditBlock() + self.setTextCursor(c) def text(self): return self.textCursor().selectedText()