Edit book: Fix various formatting operations not inserting the tags in the correct place in the presence of non-BMP characters. Fixes #1999349 [Inconsistent tag insertion in the editor, depending on tool used, when the selected text is bounded by "<" or ">"](https://bugs.launchpad.net/calibre/+bug/1999349)

This commit is contained in:
Kovid Goyal 2022-12-12 19:48:12 +05:30
parent de77bfa3be
commit 887d6b4cb6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 9 additions and 18 deletions

View File

@ -488,15 +488,17 @@ class Smarts(NullSmarts):
editor.setTextCursor(c) editor.setTextCursor(c)
def insert_tag(self, editor, name): def insert_tag(self, editor, name):
m = re.match(r'[a-zA-Z0-9:-]+', name)
cname = name if m is None else m.group()
self.surround_with_custom_tag(editor, f'<{name}>', f'</{cname}>')
def surround_with_custom_tag(self, editor, opent, close):
editor.highlighter.join() editor.highlighter.join()
name = name.lstrip()
text = self.get_smart_selection(editor, update=True) text = self.get_smart_selection(editor, update=True)
c = editor.textCursor() c = editor.textCursor()
pos = min(c.position(), c.anchor()) pos = min(c.position(), c.anchor())
m = re.match(r'[a-zA-Z0-9:-]+', name) c.insertText(f'{opent}{text}{close}')
cname = name if m is None else m.group() c.setPosition(pos + len(opent))
c.insertText(f'<{name}>{text}</{cname}>')
c.setPosition(pos + 2 + len(name))
editor.setTextCursor(c) editor.setTextCursor(c)
def verify_for_spellcheck(self, cursor, highlighter): def verify_for_spellcheck(self, cursor, highlighter):

View File

@ -871,19 +871,8 @@ class TextEdit(PlainTextEdit):
'color': ('<span style="color: %s">' % color, '</span>'), 'color': ('<span style="color: %s">' % color, '</span>'),
'background-color': ('<span style="background-color: %s">' % color, '</span>'), 'background-color': ('<span style="background-color: %s">' % color, '</span>'),
}[formatting] }[formatting]
left, right = self.get_range_inside_tag() if hasattr(self.smarts, 'surround_with_custom_tag'):
c = self.textCursor() self.smarts.surround_with_custom_tag(self, prefix, suffix)
c.setPosition(left)
c.setPosition(right, QTextCursor.MoveMode.KeepAnchor)
prev_text = str(c.selectedText()).rstrip('\0')
c.insertText(prefix + prev_text + suffix)
if prev_text:
right = c.position()
c.setPosition(left)
c.setPosition(right, QTextCursor.MoveMode.KeepAnchor)
else:
c.setPosition(c.position() - len(suffix))
self.setTextCursor(c)
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False, width=-1, height=-1): def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False, width=-1, height=-1):
if width <= 0: if width <= 0: