mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
de77bfa3be
commit
887d6b4cb6
@ -488,15 +488,17 @@ class Smarts(NullSmarts):
|
||||
editor.setTextCursor(c)
|
||||
|
||||
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()
|
||||
name = name.lstrip()
|
||||
text = self.get_smart_selection(editor, update=True)
|
||||
c = editor.textCursor()
|
||||
pos = min(c.position(), c.anchor())
|
||||
m = re.match(r'[a-zA-Z0-9:-]+', name)
|
||||
cname = name if m is None else m.group()
|
||||
c.insertText(f'<{name}>{text}</{cname}>')
|
||||
c.setPosition(pos + 2 + len(name))
|
||||
c.insertText(f'{opent}{text}{close}')
|
||||
c.setPosition(pos + len(opent))
|
||||
editor.setTextCursor(c)
|
||||
|
||||
def verify_for_spellcheck(self, cursor, highlighter):
|
||||
|
@ -871,19 +871,8 @@ class TextEdit(PlainTextEdit):
|
||||
'color': ('<span style="color: %s">' % color, '</span>'),
|
||||
'background-color': ('<span style="background-color: %s">' % color, '</span>'),
|
||||
}[formatting]
|
||||
left, right = self.get_range_inside_tag()
|
||||
c = self.textCursor()
|
||||
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)
|
||||
if hasattr(self.smarts, 'surround_with_custom_tag'):
|
||||
self.smarts.surround_with_custom_tag(self, prefix, suffix)
|
||||
|
||||
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False, width=-1, height=-1):
|
||||
if width <= 0:
|
||||
|
Loading…
x
Reference in New Issue
Block a user