summaryrefslogtreecommitdiff
path: root/qutebrowser/browser/webkit/webkitelem.py
blob: 1a41ea060caf974a39067450a2be514425af258f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:

# Copyright 2014-2021 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 <https://www.gnu.org/licenses/>.

# FIXME:qt6 (lint)
# pylint: disable=no-name-in-module

"""QtWebKit specific part of the web element API."""

from typing import cast, TYPE_CHECKING, Iterator, List, Optional, Set
from qutebrowser.qt import webkitwidgets

from qutebrowser.config import config
from qutebrowser.utils import log, utils, javascript, usertypes
from qutebrowser.browser import webelem
from qutebrowser.qt import webkit, core

if TYPE_CHECKING:
    from qutebrowser.browser.webkit import webkittab


class IsNullError(webelem.Error):

    """Gets raised by WebKitElement if an element is null."""


class WebKitElement(webelem.AbstractWebElement):

    """A wrapper around a QWebElement."""

    _tab: 'webkittab.WebKitTab'

    def __init__(self, elem: webkit.QWebElement, tab: 'webkittab.WebKitTab') -> None:
        super().__init__(tab)
        if isinstance(elem, self.__class__):
            raise TypeError("Trying to wrap a wrapper!")
        if elem.isNull():
            raise IsNullError('{} is a null element!'.format(elem))
        self._elem = elem

    def __str__(self) -> str:
        self._check_vanished()
        return self._elem.toPlainText()

    def __eq__(self, other: object) -> bool:
        if not isinstance(other, WebKitElement):
            return NotImplemented
        return self._elem == other._elem

    def __getitem__(self, key: str) -> str:
        self._check_vanished()
        if key not in self:
            raise KeyError(key)
        return self._elem.attribute(key)

    def __setitem__(self, key: str, val: str) -> None:
        self._check_vanished()
        self._elem.setAttribute(key, val)

    def __delitem__(self, key: str) -> None:
        self._check_vanished()
        if key not in self:
            raise KeyError(key)
        self._elem.removeAttribute(key)

    def __contains__(self, key: object) -> bool:
        assert isinstance(key, str)
        self._check_vanished()
        return self._elem.hasAttribute(key)

    def __iter__(self) -> Iterator[str]:
        self._check_vanished()
        yield from self._elem.attributeNames()

    def __len__(self) -> int:
        self._check_vanished()
        return len(self._elem.attributeNames())

    def _check_vanished(self) -> None:
        """Raise an exception if the element vanished (is null)."""
        if self._elem.isNull():
            raise IsNullError('Element {} vanished!'.format(self._elem))

    def has_frame(self) -> bool:
        self._check_vanished()
        return self._elem.webFrame() is not None

    def geometry(self) -> core.QRect:
        self._check_vanished()
        return self._elem.geometry()

    def classes(self) -> Set[str]:
        self._check_vanished()
        return set(self._elem.classes())

    def tag_name(self) -> str:
        """Get the tag name for the current element."""
        self._check_vanished()
        return self._elem.tagName().lower()

    def outer_xml(self) -> str:
        """Get the full HTML representation of this element."""
        self._check_vanished()
        return self._elem.toOuterXml()

    def is_content_editable_prop(self) -> bool:
        self._check_vanished()
        val = self._elem.evaluateJavaScript('this.isContentEditable || false')
        assert isinstance(val, bool)
        return val

    def value(self) -> webelem.JsValueType:
        self._check_vanished()
        val = self._elem.evaluateJavaScript('this.value')
        assert isinstance(val, (int, float, str, type(None))), val
        return val

    def set_value(self, value: webelem.JsValueType) -> None:
        self._check_vanished()
        if self._tab.is_deleted():
            raise webelem.OrphanedError("Tab containing element vanished")
        if self.is_content_editable():
            log.webelem.debug("Filling {!r} via set_text.".format(self))
            assert isinstance(value, str)
            self._elem.setPlainText(value)
        else:
            log.webelem.debug("Filling {!r} via javascript.".format(self))
            value = javascript.to_js(value)
            self._elem.evaluateJavaScript("this.value={}".format(value))

    def dispatch_event(self, event: str,
                       bubbles: bool = False,
                       cancelable: bool = False,
                       composed: bool = False) -> None:
        self._check_vanished()
        log.webelem.debug("Firing event on {!r} via javascript.".format(self))
        self._elem.evaluateJavaScript(
            "this.dispatchEvent(new Event({}, "
            "{{'bubbles': {}, 'cancelable': {}, 'composed': {}}}))"
            .format(javascript.to_js(event),
                    javascript.to_js(bubbles),
                    javascript.to_js(cancelable),
                    javascript.to_js(composed)))

    def caret_position(self) -> int:
        """Get the text caret position for the current element."""
        self._check_vanished()
        pos = self._elem.evaluateJavaScript('this.selectionStart')
        if pos is None:
            return 0
        return int(pos)

    def insert_text(self, text: str) -> None:
        self._check_vanished()
        if not self.is_editable(strict=True):
            raise webelem.Error("Element is not editable!")
        log.webelem.debug("Inserting text into element {!r}".format(self))
        self._elem.evaluateJavaScript("""
            var text = {};
            var event = document.createEvent("TextEvent");
            event.initTextEvent("textInput", true, true, null, text);
            this.dispatchEvent(event);
        """.format(javascript.to_js(text)))

    def _parent(self) -> Optional['WebKitElement']:
        """Get the parent element of this element."""
        self._check_vanished()
        elem = cast(Optional[webkit.QWebElement], self._elem.parent())
        if elem is None or elem.isNull():
            return None

        return WebKitElement(elem, tab=self._tab)

    def _rect_on_view_js(self) -> Optional[core.QRect]:
        """Javascript implementation for rect_on_view."""
        # FIXME:qtwebengine maybe we can reuse this?
        rects = self._elem.evaluateJavaScript("this.getClientRects()")
        if rects is None:  # pragma: no cover
            # On e.g. Void Linux with musl libc, the stack size is too small
            # for jsc, and running JS will fail. If that happens, fall back to
            # the Python implementation.
            # https://github.com/qutebrowser/qutebrowser/issues/1641
            return None

        text = utils.compact_text(self._elem.toOuterXml(), 500)
        log.webelem.vdebug(  # type: ignore[attr-defined]
            "Client rectangles of element '{}': {}".format(text, rects))

        for i in range(int(rects.get("length", 0))):
            rect = rects[str(i)]
            width = rect.get("width", 0)
            height = rect.get("height", 0)
            if width > 1 and height > 1:
                # fix coordinates according to zoom level
                zoom = self._elem.webFrame().zoomFactor()
                if not config.val.zoom.text_only:
                    rect["left"] *= zoom
                    rect["top"] *= zoom
                    width *= zoom
                    height *= zoom
                rect = core.QRect(int(rect["left"]), int(rect["top"]),
                             int(width), int(height))

                frame = cast(Optional[webkitwidgets.QWebFrame], self._elem.webFrame())
                while frame is not None:
                    # Translate to parent frames' position (scroll position
                    # is taken care of inside getClientRects)
                    rect.translate(frame.geometry().topLeft())
                    frame = frame.parentFrame()

                return rect

        return None

    def _rect_on_view_python(self, elem_geometry: Optional[core.QRect]) -> core.QRect:
        """Python implementation for rect_on_view."""
        if elem_geometry is None:
            geometry = self._elem.geometry()
        else:
            geometry = elem_geometry
        rect = core.QRect(geometry)

        frame = cast(Optional[webkitwidgets.QWebFrame], self._elem.webFrame())
        while frame is not None:
            rect.translate(frame.geometry().topLeft())
            rect.translate(frame.scrollPosition() * -1)
            frame = cast(Optional[webkitwidgets.QWebFrame], frame.parentFrame())

        return rect

    def rect_on_view(self, *, elem_geometry: core.QRect = None,
                     no_js: bool = False) -> core.QRect:
        """Get the geometry of the element relative to the webview.

        Uses the getClientRects() JavaScript method to obtain the collection of
        rectangles containing the element and returns the first rectangle which
        is large enough (larger than 1px times 1px). If all rectangles returned
        by getClientRects() are too small, falls back to elem.rect_on_view().

        Skipping of small rectangles is due to <a> elements containing other
        elements with "display:block" style, see
        https://github.com/qutebrowser/qutebrowser/issues/1298

        Args:
            elem_geometry: The geometry of the element, or None.
                           Calling QWebElement::geometry is rather expensive so
                           we want to avoid doing it twice.
            no_js: Fall back to the Python implementation
        """
        self._check_vanished()

        # First try getting the element rect via JS, as that's usually more
        # accurate
        if elem_geometry is None and not no_js:
            rect = self._rect_on_view_js()
            if rect is not None:
                return rect

        # No suitable rects found via JS, try via the QWebElement API
        return self._rect_on_view_python(elem_geometry)

    def _is_hidden_css(self) -> bool:
        """Check if the given element is hidden via CSS."""
        attr_values = {
            attr: self._elem.styleProperty(attr, webkit.QWebElement.StyleResolveStrategy.ComputedStyle)
            for attr in ['visibility', 'display', 'opacity']
        }
        invisible = attr_values['visibility'] == 'hidden'
        none_display = attr_values['display'] == 'none'
        zero_opacity = attr_values['opacity'] == '0'

        is_framework = ('ace_text-input' in self.classes() or
                        'custom-control-input' in self.classes())
        return invisible or none_display or (zero_opacity and not is_framework)

    def _is_visible(self, mainframe: webkitwidgets.QWebFrame) -> bool:
        """Check if the given element is visible in the given frame.

        This is not public API because it can't be implemented easily here with
        QtWebEngine, and is only used via find_css(..., only_visible=True) via
        the tab API.
        """
        self._check_vanished()
        if self._is_hidden_css():
            return False

        elem_geometry = self._elem.geometry()
        if not elem_geometry.isValid() and elem_geometry.x() == 0:
            # Most likely an invisible link
            return False
        # First check if the element is visible on screen
        elem_rect = self.rect_on_view(elem_geometry=elem_geometry)
        mainframe_geometry = mainframe.geometry()
        if elem_rect.isValid():
            visible_on_screen = mainframe_geometry.intersects(elem_rect)
        else:
            # We got an invalid rectangle (width/height 0/0 probably), but this
            # can still be a valid link.
            visible_on_screen = mainframe_geometry.contains(
                elem_rect.topLeft())
        # Then check if it's visible in its frame if it's not in the main
        # frame.
        elem_frame = self._elem.webFrame()
        framegeom = core.QRect(elem_frame.geometry())
        if not framegeom.isValid():
            visible_in_frame = False
        elif elem_frame.parentFrame() is not None:
            framegeom.moveTo(0, 0)
            framegeom.translate(elem_frame.scrollPosition())
            if elem_geometry.isValid():
                visible_in_frame = framegeom.intersects(elem_geometry)
            else:
                # We got an invalid rectangle (width/height 0/0 probably), but
                # this can still be a valid link.
                visible_in_frame = framegeom.contains(elem_geometry.topLeft())
        else:
            visible_in_frame = visible_on_screen
        return all([visible_on_screen, visible_in_frame])

    def remove_blank_target(self) -> None:
        elem: Optional[WebKitElement] = self
        for _ in range(5):
            if elem is None:
                break
            if elem.is_link():
                if elem.get('target', None) == '_blank':
                    elem['target'] = '_top'
                break
            elem = elem._parent()  # pylint: disable=protected-access

    def delete(self) -> None:
        self._elem.evaluateJavaScript('this.remove();')

    def _move_text_cursor(self) -> None:
        if self.is_text_input() and self.is_editable():
            self._tab.caret.move_to_end_of_document()

    def _requires_user_interaction(self) -> bool:
        return False

    def _click_editable(self, click_target: usertypes.ClickTarget) -> None:
        ok = self._elem.evaluateJavaScript('this.focus(); true;')
        if ok:
            self._move_text_cursor()
        else:
            log.webelem.debug("Failed to focus via JS, falling back to event")
            self._click_fake_event(click_target)

    def _click_js(self, click_target: usertypes.ClickTarget) -> None:
        settings = webkit.QWebSettings.globalSettings()
        attribute = webkit.QWebSettings.WebAttribute.JavascriptCanOpenWindows
        could_open_windows = settings.testAttribute(attribute)
        settings.setAttribute(attribute, True)
        ok = self._elem.evaluateJavaScript('this.click(); true;')
        settings.setAttribute(attribute, could_open_windows)
        if not ok:
            log.webelem.debug("Failed to click via JS, falling back to event")
            self._click_fake_event(click_target)

    def _click_fake_event(self, click_target: usertypes.ClickTarget,
                          button: core.Qt.MouseButton = core.Qt.MouseButton.LeftButton) -> None:
        self._tab.data.override_target = click_target
        super()._click_fake_event(click_target)


def get_child_frames(startframe: webkitwidgets.QWebFrame) -> List[webkitwidgets.QWebFrame]:
    """Get all children recursively of a given QWebFrame.

    Loosely based on https://blog.nextgenetics.net/?e=64

    Args:
        startframe: The QWebFrame to start with.

    Return:
        A list of children QWebFrame, or an empty list.
    """
    results = []
    frames = [startframe]
    while frames:
        new_frames: List[webkitwidgets.QWebFrame] = []
        for frame in frames:
            results.append(frame)
            new_frames += frame.childFrames()
        frames = new_frames
    return results