Move hint webelem code to utils.webelem

This commit is contained in:
Florian Bruhin 2014-04-24 16:28:00 +02:00
parent 6fb52e610d
commit b372c23b80
3 changed files with 82 additions and 37 deletions

View File

@ -29,6 +29,7 @@ import qutebrowser.config.config as config
import qutebrowser.utils.message as message
import qutebrowser.utils.url as urlutils
import qutebrowser.utils.modemanager as modemanager
import qutebrowser.utils.webelem as webelem
from qutebrowser.utils.keyparser import KeyParser
@ -86,10 +87,6 @@ class HintManager(QObject):
"""Manage drawing hints over links or other elements.
Class attributes:
SELECTORS: CSS selectors for the different highlighting modes.
FILTERS: A dictionary of filter functions for the modes.
The filter for "links" filters javascript:-links and a-tags
without "href".
HINT_CSS: The CSS template to use for hints.
Attributes:
@ -114,23 +111,6 @@ class HintManager(QObject):
set_cmd_text: Emitted when the commandline text should be set.
"""
SELECTORS = {
"all": ("a, textarea, select, input:not([type=hidden]), button, "
"frame, iframe, [onclick], [onmousedown], [role=link], "
"[role=option], [role=button], img"),
"links": "a",
"images": "img",
"editable": ("input[type=text], input[type=email], input[type=url],"
"input[type=tel], input[type=number], "
"input[type=password], input[type=search], textarea"),
"url": "[src], [href]",
}
FILTERS = {
"links": (lambda e: e.hasAttribute("href") and
urlutils.qurl(e.attribute("href")).scheme() != "javascript"),
}
HINT_CSS = """
color: {config[colors][hints.fg]};
background: {config[colors][hints.bg]};
@ -355,21 +335,11 @@ class HintManager(QObject):
self._target = target
self._baseurl = baseurl
self._frame = frame
elems = frame.findAllElements(self.SELECTORS[mode])
filterfunc = self.FILTERS.get(mode, lambda e: True)
elems = frame.findAllElements(webelem.SELECTORS[mode])
filterfunc = webelem.FILTERS.get(mode, lambda e: True)
visible_elems = []
for e in elems:
if not filterfunc(e):
continue
rect = e.geometry()
if (not rect.isValid()) and rect.x() == 0:
# Most likely an invisible link
continue
framegeom = frame.geometry()
framegeom.translate(frame.scrollPosition())
if not framegeom.contains(rect.topLeft()):
# out of screen
continue
if filterfunc(e) and webelem.is_visible(e, self._frame):
visible_elems.append(e)
if not visible_elems:
message.error("No elements found.")

View File

@ -0,0 +1,74 @@
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Utilities related to QWebElements.
Module attributes:
SELECTORS: CSS selectors for different groups of elements.
FILTERS: A dictionary of filter functions for the modes.
The filter for "links" filters javascript:-links and a-tags
without "href".
"""
import qutebrowser.utils.url as urlutils
SELECTORS = {
'all': ('a, textarea, select, input:not([type=hidden]), button, '
'frame, iframe, [onclick], [onmousedown], [role=link], '
'[role=option], [role=button], img'),
'links': 'a',
'images': 'img',
'editable': ('input[type=text], input[type=email], input[type=url], '
'input[type=tel], input[type=number], '
'input[type=password], input[type=search], textarea'),
'url': '[src], [href]',
}
SELECTORS['editable_focused'] = ', '.join(
[sel.strip() + ':focus' for sel in SELECTORS['editable'].split(',')])
FILTERS = {
'links': (lambda e: e.hasAttribute('href') and
urlutils.qurl(e.attribute('href')).scheme() != 'javascript'),
}
def is_visible(e, frame=None):
"""Check whether the element is currently visible in its frame.
Args:
e: The QWebElement to check.
frame: The QWebFrame in which the element should be visible in.
If None, the element's frame is used.
Return:
True if the element is visible, False otherwise.
"""
if e.isNull():
raise ValueError("Element is a null-element!")
if frame is None:
frame = e.webFrame()
rect = e.geometry()
if (not rect.isValid()) and rect.x() == 0:
# Most likely an invisible link
return False
framegeom = frame.geometry()
framegeom.translate(frame.scrollPosition())
if not framegeom.contains(rect.topLeft()):
# out of screen
return False
return True

View File

@ -29,6 +29,7 @@ import qutebrowser.utils.url as urlutils
import qutebrowser.config.config as config
import qutebrowser.utils.message as message
import qutebrowser.utils.modemanager as modemanager
import qutebrowser.utils.webelem as webelem
from qutebrowser.browser.webpage import BrowserPage
from qutebrowser.browser.hints import HintManager
from qutebrowser.utils.signals import SignalCache
@ -250,8 +251,8 @@ class BrowserTab(QWebView):
modemanager.maybe_leave("insert")
elif config.get('general', 'auto_insert_mode'):
frame = self.page_.currentFrame()
# FIXME better selector (from hintmanager)
elem = frame.findFirstElement('*:focus')
elem = frame.findFirstElement(
webelem.SELECTORS['editable_focused'])
logging.debug("focus element: {}".format(not elem.isNull()))
if elem.isNull():
modemanager.maybe_leave("insert")