Merge branch 'none-position'
This commit is contained in:
commit
408ceefad1
@ -47,7 +47,7 @@ class WebEngineElement(webelem.AbstractWebElement):
|
|||||||
'class_name': str,
|
'class_name': str,
|
||||||
'rects': list,
|
'rects': list,
|
||||||
'attributes': dict,
|
'attributes': dict,
|
||||||
'caret_position': int,
|
'caret_position': (int, type(None)),
|
||||||
}
|
}
|
||||||
assert set(js_dict.keys()).issubset(js_dict_types.keys())
|
assert set(js_dict.keys()).issubset(js_dict_types.keys())
|
||||||
for name, typ in js_dict_types.items():
|
for name, typ in js_dict_types.items():
|
||||||
@ -134,8 +134,11 @@ class WebEngineElement(webelem.AbstractWebElement):
|
|||||||
self._js_call('set_value', value)
|
self._js_call('set_value', value)
|
||||||
|
|
||||||
def caret_position(self):
|
def caret_position(self):
|
||||||
"""Get the text caret position for the current element."""
|
"""Get the text caret position for the current element.
|
||||||
return self._js_dict.get('caret_position', 0)
|
|
||||||
|
If the element is not a text element, None is returned.
|
||||||
|
"""
|
||||||
|
return self._js_dict.get('caret_position', None)
|
||||||
|
|
||||||
def insert_text(self, text):
|
def insert_text(self, text):
|
||||||
if not self.is_editable(strict=True):
|
if not self.is_editable(strict=True):
|
||||||
|
@ -48,14 +48,16 @@ window._qutebrowser.webelem = (function() {
|
|||||||
const id = elements.length;
|
const id = elements.length;
|
||||||
elements[id] = elem;
|
elements[id] = elem;
|
||||||
|
|
||||||
// InvalidStateError will be thrown if elem doesn't have selectionStart
|
// With older Chromium versions (and QtWebKit), InvalidStateError will
|
||||||
let caret_position = 0;
|
// be thrown if elem doesn't have selectionStart.
|
||||||
|
// With newer Chromium versions (>= Qt 5.10), we get null.
|
||||||
|
let caret_position = null;
|
||||||
try {
|
try {
|
||||||
caret_position = elem.selectionStart;
|
caret_position = elem.selectionStart;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof DOMException &&
|
if (err instanceof DOMException &&
|
||||||
err.name === "InvalidStateError") {
|
err.name === "InvalidStateError") {
|
||||||
// nothing to do, caret_position is already 0
|
// nothing to do, caret_position is already null
|
||||||
} else {
|
} else {
|
||||||
// not the droid we're looking for
|
// not the droid we're looking for
|
||||||
throw err;
|
throw err;
|
||||||
|
@ -96,7 +96,7 @@ class ExternalEditor(QObject):
|
|||||||
def on_proc_error(self, _err):
|
def on_proc_error(self, _err):
|
||||||
self._cleanup()
|
self._cleanup()
|
||||||
|
|
||||||
def edit(self, text, caret_position=0):
|
def edit(self, text, caret_position=None):
|
||||||
"""Edit a given text.
|
"""Edit a given text.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -174,11 +174,13 @@ class ExternalEditor(QObject):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
text: the text for which the numbers must be calculated
|
text: the text for which the numbers must be calculated
|
||||||
caret_position: the position of the caret in the text
|
caret_position: the position of the caret in the text, or None
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
A (line, column) tuple of (int, int)
|
A (line, column) tuple of (int, int)
|
||||||
"""
|
"""
|
||||||
|
if caret_position is None:
|
||||||
|
return 1, 1
|
||||||
line = text[:caret_position].count('\n') + 1
|
line = text[:caret_position].count('\n') + 1
|
||||||
column = caret_position - text[:caret_position].rfind('\n')
|
column = caret_position - text[:caret_position].rfind('\n')
|
||||||
return line, column
|
return line, column
|
||||||
|
@ -188,6 +188,7 @@ def test_modify(qtbot, editor, initial_text, edited_text):
|
|||||||
('a\nbb\nccc', 4, (2, 3)),
|
('a\nbb\nccc', 4, (2, 3)),
|
||||||
('a\nbb\nccc', 5, (3, 1)),
|
('a\nbb\nccc', 5, (3, 1)),
|
||||||
('a\nbb\nccc', 8, (3, 4)),
|
('a\nbb\nccc', 8, (3, 4)),
|
||||||
|
('', None, (1, 1)),
|
||||||
])
|
])
|
||||||
def test_calculation(editor, text, caret_position, result):
|
def test_calculation(editor, text, caret_position, result):
|
||||||
"""Test calculation for line and column given text and caret_position."""
|
"""Test calculation for line and column given text and caret_position."""
|
||||||
|
Loading…
Reference in New Issue
Block a user