E-book viewer: Show an error if the user tries to search for only punctuation or spaces in the search modes that ignore these. Fixes #2015795 [Searching for punctuation freezes the e-book viewer](https://bugs.launchpad.net/calibre/+bug/2015795)

This commit is contained in:
Kovid Goyal 2023-04-19 18:17:50 +05:30
parent b41e2be8e3
commit cc89ccb265
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 11 additions and 0 deletions

View File

@ -145,6 +145,14 @@ class Search:
self._nsd = word_pats, full_pat
return self._nsd
@property
def is_empty(self):
if not self.text:
return True
if self.mode in ('normal', 'word') and not regex.sub(r'[\s\p{P}]+', '', self.text):
return True
return False
def __str__(self):
from collections import namedtuple
s = ('text', 'mode', 'case_sensitive', 'backwards')

View File

@ -336,6 +336,9 @@ class EbookViewer(MainWindow):
def start_search(self, search_query):
name = self.web_view.current_content_file
if name:
if search_query.is_empty and search_query.text:
return error_dialog(self, _('Empty search expression'), _(
'Cannot search for {!r} as it contains only punctuation and spaces.').format(search_query.text), show=True)
self.web_view.get_current_cfi(self.search_widget.set_anchor_cfi)
self.search_widget.start_search(search_query, name)
self.web_view.setFocus(Qt.FocusReason.OtherFocusReason)