readerhighlight: ignore cases where no text is selected (#8399)

It's possible for the user to have selected nothing, and trying to
operate on the nil highlight can cause confusion or crashes. This
restores the behaviour before commit 7a0e3d5e68 ("readerhighlight:
remove selected_word and use selected_text everywhere"), which missed
this case.

In addition, add some debug guards to ReaderHighlight methods which
cannot handle selected_text being nil (or at least, shouldn't be called
with selected_text being nil).

Fixes: 7a0e3d5e68 ("readerhighlight: remove selected_word and use selected_text everywhere")
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
reviewable/pr8425/r1
Aleksa Sarai 3 years ago committed by GitHub
parent 7baeebc2d4
commit 3fd931bb2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,6 +9,7 @@ local Notification = require("ui/widget/notification")
local TimeVal = require("ui/timeval") local TimeVal = require("ui/timeval")
local Translator = require("ui/translator") local Translator = require("ui/translator")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local dbg = require("dbg")
local logger = require("logger") local logger = require("logger")
local util = require("util") local util = require("util")
local ffiUtil = require("ffi/util") local ffiUtil = require("ffi/util")
@ -771,6 +772,11 @@ function ReaderHighlight:onShowHighlightMenu()
} }
UIManager:show(self.highlight_dialog) UIManager:show(self.highlight_dialog)
end end
dbg:guard(ReaderHighlight, "onShowHighlightMenu",
function(self)
assert(self.selected_text ~= nil,
"onShowHighlightMenu must not be called with nil self.selected_text!")
end)
function ReaderHighlight:_resetHoldTimer(clear) function ReaderHighlight:_resetHoldTimer(clear)
if clear then if clear then
@ -1093,6 +1099,11 @@ function ReaderHighlight:lookup(selected_text, selected_link)
end end
end end
end end
dbg:guard(ReaderHighlight, "lookup",
function(self, selected_text, selected_link)
assert(selected_text ~= nil,
"lookup must not be called with nil selected_text!")
end)
function ReaderHighlight:viewSelectionHTML(debug_view, no_css_files_buttons) function ReaderHighlight:viewSelectionHTML(debug_view, no_css_files_buttons)
if self.ui.document.info.has_pages then if self.ui.document.info.has_pages then
@ -1251,6 +1262,11 @@ function ReaderHighlight:translate(selected_text)
end end
end end
end end
dbg:guard(ReaderHighlight, "translate",
function(self, selected_text)
assert(selected_text ~= nil,
"translate must not be called with nil selected_text!")
end)
function ReaderHighlight:onTranslateText(text) function ReaderHighlight:onTranslateText(text)
Translator:showTranslation(text) Translator:showTranslation(text)
@ -1272,29 +1288,31 @@ function ReaderHighlight:onHoldRelease()
end end
end end
if self.is_word_selection then if self.selected_text then
self:lookup(self.selected_text, self.selected_link) if self.is_word_selection then
else self:lookup(self.selected_text, self.selected_link)
local default_highlight_action = G_reader_settings:readSetting("default_highlight_action", "ask") else
if long_final_hold or default_highlight_action == "ask" then local default_highlight_action = G_reader_settings:readSetting("default_highlight_action", "ask")
-- bypass default action and show popup if long final hold if long_final_hold or default_highlight_action == "ask" then
self:onShowHighlightMenu() -- bypass default action and show popup if long final hold
elseif default_highlight_action == "highlight" then self:onShowHighlightMenu()
self:saveHighlight() elseif default_highlight_action == "highlight" then
self:onClose() self:saveHighlight()
elseif default_highlight_action == "translate" then self:onClose()
self:translate(self.selected_text) elseif default_highlight_action == "translate" then
self:onClose() self:translate(self.selected_text)
elseif default_highlight_action == "wikipedia" then self:onClose()
self:lookupWikipedia() elseif default_highlight_action == "wikipedia" then
self:onClose() self:lookupWikipedia()
elseif default_highlight_action == "dictionary" then self:onClose()
self:onHighlightDictLookup() elseif default_highlight_action == "dictionary" then
self:onClose() self:onHighlightDictLookup()
elseif default_highlight_action == "search" then self:onClose()
self:onHighlightSearch() elseif default_highlight_action == "search" then
-- No self:onClose() to not remove the selected text self:onHighlightSearch()
-- which will have been the first search result -- No self:onClose() to not remove the selected text
-- which will have been the first search result
end
end end
end end
return true return true

Loading…
Cancel
Save