mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use caret position from point rahter than range from point as the former is standards compliant
This commit is contained in:
parent
6d0cc73d93
commit
41717d5eca
@ -3,17 +3,13 @@
|
|||||||
from __python__ import bound_methods, hash_literals
|
from __python__ import bound_methods, hash_literals
|
||||||
|
|
||||||
|
|
||||||
def range_from_point(x, y):
|
def caret_position_from_point(x, y):
|
||||||
r = None
|
|
||||||
if document.caretPositionFromPoint:
|
if document.caretPositionFromPoint:
|
||||||
p = document.caretPositionFromPoint(x, y)
|
return document.caretPositionFromPoint(x, y)
|
||||||
if p:
|
r = document.caretRangeFromPoint(x, y)
|
||||||
r = document.createRange()
|
if r:
|
||||||
r.setStart(p.offsetNode, p.offset)
|
return {'offsetNode': r.startContainer, 'offset': r.startOffset}
|
||||||
r.collapse(True)
|
return None
|
||||||
elif document.caretRangeFromPoint:
|
|
||||||
r = document.caretRangeFromPoint(x, y)
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
def word_boundary_regex():
|
def word_boundary_regex():
|
||||||
@ -35,12 +31,13 @@ def expand_offset_to_word(string, offset):
|
|||||||
|
|
||||||
|
|
||||||
def word_at_point(x, y):
|
def word_at_point(x, y):
|
||||||
r = range_from_point(x, y)
|
p = caret_position_from_point(x, y)
|
||||||
if r and r.startContainer.nodeType is 3:
|
if p and p.offsetNode?.nodeType is Node.TEXT_NODE:
|
||||||
word_info = expand_offset_to_word(r.startContainer.data, r.startOffset)
|
word_info = expand_offset_to_word(p.offsetNode.data, p.offset)
|
||||||
if word_info.word:
|
if word_info.word:
|
||||||
r.setStart(r.startContainer, word_info.start)
|
r = document.createRange()
|
||||||
r.setEnd(r.startContainer, word_info.end)
|
r.setStart(p.offsetNode, word_info.start)
|
||||||
|
r.setEnd(p.offsetNode, word_info.end)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
@ -105,10 +102,10 @@ def move_end_of_selection(pos, start):
|
|||||||
sel = window.getSelection()
|
sel = window.getSelection()
|
||||||
if not sel.rangeCount:
|
if not sel.rangeCount:
|
||||||
return
|
return
|
||||||
newr = range_from_point(pos.x, pos.y)
|
p = caret_position_from_point(pos.x, pos.y)
|
||||||
if newr:
|
if p:
|
||||||
r = sel.getRangeAt(0)
|
r = sel.getRangeAt(0)
|
||||||
if start:
|
if start:
|
||||||
r.setStart(newr.startContainer, newr.startOffset)
|
r.setStart(p.offsetNode, p.offset)
|
||||||
else:
|
else:
|
||||||
r.setEnd(newr.endContainer, newr.endOffset)
|
r.setEnd(p.offsetNode, p.offset)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user