Refactor search method of AbstractBrowserTab into a field

This commit is contained in:
Jay Kamat 2017-05-09 00:14:59 -07:00
parent 02f79c2990
commit a3d41c0467
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
4 changed files with 18 additions and 27 deletions

View File

@ -163,6 +163,8 @@ class AbstractSearch(QObject):
super().__init__(parent)
self._widget = None
self.text = None
# Implementing classes will set this.
self.search_displayed = False
def search(self, text, *, ignore_case=False, reverse=False,
result_cb=None):
@ -196,10 +198,6 @@ class AbstractSearch(QObject):
"""
raise NotImplementedError
def searching(self):
"""Return True if we are currently searching or not."""
raise NotImplementedError
class AbstractZoom(QObject):

View File

@ -127,11 +127,11 @@ class WebEngineSearch(browsertab.AbstractSearch):
def __init__(self, parent=None):
super().__init__(parent)
self._flags = QWebEnginePage.FindFlags(0)
self._searching = False
self.search_displayed = False
def _find(self, text, flags, callback, caller):
"""Call findText on the widget."""
self._searching = True
self.search_displayed = True
def wrapped_callback(found):
"""Wrap the callback to do debug logging."""
@ -163,12 +163,9 @@ class WebEngineSearch(browsertab.AbstractSearch):
self._find(text, flags, result_cb, 'search')
def clear(self):
self._searching = False
self.search_displayed = False
self._widget.findText('')
def searching(self):
return self._searching
def prev_result(self, *, result_cb=None):
# The int() here makes sure we get a copy of the flags.
flags = QWebEnginePage.FindFlags(int(self._flags))
@ -256,10 +253,10 @@ class WebEngineCaret(browsertab.AbstractCaret):
def _follow_selected_cb(self, js_elem, tab=False):
"""Callback for javascript which clicks the selected element.
Args:
js_elems: The elements serialized from javascript.
tab: Open in a new tab or not.
"""
Args:
js_elems: The elements serialized from javascript.
tab: Open in a new tab or not.
"""
if js_elem is None:
return
assert isinstance(js_elem, dict), js_elem
@ -276,7 +273,7 @@ class WebEngineCaret(browsertab.AbstractCaret):
elem.click(click_type)
def follow_selected(self, *, tab=False):
if self._tab.search.searching():
if self._tab.search.search_displayed:
# We are currently in search mode.
# let's click the link via a fake-click
# https://bugreports.qt.io/browse/QTBUG-60673

View File

@ -103,7 +103,7 @@ class WebKitSearch(browsertab.AbstractSearch):
def __init__(self, parent=None):
super().__init__(parent)
self._flags = QWebPage.FindFlags(0)
self._searching = True
self.search_displayed = True
def _call_cb(self, callback, found, text, flags, caller):
"""Call the given callback if it's non-None.
@ -133,14 +133,14 @@ class WebKitSearch(browsertab.AbstractSearch):
QTimer.singleShot(0, functools.partial(callback, found))
def clear(self):
self._searching = False
self.search_displayed = False
# We first clear the marked text, then the highlights
self._widget.findText('')
self._widget.findText('', QWebPage.HighlightAllOccurrences)
def search(self, text, *, ignore_case=False, reverse=False,
result_cb=None):
self._searching = True
self.search_displayed = True
flags = QWebPage.FindWrapsAroundDocument
if ignore_case == 'smart':
if not text.islower():
@ -158,12 +158,12 @@ class WebKitSearch(browsertab.AbstractSearch):
self._call_cb(result_cb, found, text, flags, 'search')
def next_result(self, *, result_cb=None):
self._searching = True
self.search_displayed = True
found = self._widget.findText(self.text, self._flags)
self._call_cb(result_cb, found, self.text, self._flags, 'next_result')
def prev_result(self, *, result_cb=None):
self._searching = True
self.search_displayed = True
# The int() here makes sure we get a copy of the flags.
flags = QWebPage.FindFlags(int(self._flags))
if flags & QWebPage.FindBackward:
@ -173,9 +173,6 @@ class WebKitSearch(browsertab.AbstractSearch):
found = self._widget.findText(self.text, flags)
self._call_cb(result_cb, found, self.text, flags, 'prev_result')
def searching(self):
return self._searching
class WebKitCaret(browsertab.AbstractCaret):

View File

@ -1,13 +1,12 @@
/* Select all elements marked with toselect */
var toSelect = document.getElementsByClassName("toselect");
var s = window.getSelection();
if(s.rangeCount > 0) s.removeAllRanges();
for(var i = 0; i < toSelect.length; i++) {
var range = document.createRange();
range.selectNode(toSelect[i]);
s.addRange(range);
var range = document.createRange();
range.selectNode(toSelect[i]);
s.addRange(range);
}