Debian Codegraph @

src/pyj/select.pyj

raw
1 # vim:fileencoding=utf-8
2 # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
3 from __python__ import bound_methods, hash_literals
4
5
6 if document?.caretPositionFromPoint:
7 caret_position_from_point = document.caretPositionFromPoint.bind(document)
8 else:
9 caret_position_from_point = def (x, y):
10 r = document.caretRangeFromPoint(x, y)
11 if r:
12 return {'offsetNode': r.startContainer, 'offset': r.startOffset}
13 return None
14
15
16 def word_boundary_regex():
17 ans = word_boundary_regex.ans
18 if ans is undefined:
19 ans = word_boundary_regex.ans = RegExp(
20 '[\\s!-#%-\\x2A,-/:;\\x3F@\\x5B-\\x5D_\\x7B}\\u00A1\\u00A7\\u00AB\\u00B6\\u00B7\\u00BB\\u00BF\\u037E\\u0387\\u055A-\\u055F\\u0589\\u058A'
21 '\\u05BE\\u05C0\\u05C3\\u05C6\\u05F3\\u05F4\\u0609\\u060A\\u060C\\u060D\\u061B\\u061E\\u061F\\u066A-\\u066D\\u06D4\\u0700-\\u070D\\u07F7-'
22 '\\u07F9\\u0830-\\u083E\\u085E\\u0964\\u0965\\u0970\\u0AF0\\u0DF4\\u0E4F\\u0E5A\\u0E5B\\u0F04-\\u0F12\\u0F14\\u0F3A-\\u0F3D\\u0F85\\u0FD0-'
23 '\\u0FD4\\u0FD9\\u0FDA\\u104A-\\u104F\\u10FB\\u1360-\\u1368\\u1400\\u166D\\u166E\\u169B\\u169C\\u16EB-\\u16ED\\u1735\\u1736\\u17D4-\\u17D6'
24 '\\u17D8-\\u17DA\\u1800-\\u180A\\u1944\\u1945\\u1A1E\\u1A1F\\u1AA0-\\u1AA6\\u1AA8-\\u1AAD\\u1B5A-\\u1B60\\u1BFC-\\u1BFF\\u1C3B-\\u1C3F'
25 '\\u1C7E\\u1C7F\\u1CC0-\\u1CC7\\u1CD3\\u2010-\\u2027\\u2030-\\u2043\\u2045-\\u2051\\u2053-\\u205E\\u207D\\u207E\\u208D\\u208E\\u2329\\u232A'
26 '\\u2768-\\u2775\\u27C5\\u27C6\\u27E6-\\u27EF\\u2983-\\u2998\\u29D8-\\u29DB\\u29FC\\u29FD\\u2CF9-\\u2CFC\\u2CFE\\u2CFF\\u2D70\\u2E00-\\u2E2E'
27 '\\u2E30-\\u2E3B\\u3001-\\u3003\\u3008-\\u3011\\u3014-\\u301F\\u3030\\u303D\\u30A0\\u30FB\\uA4FE\\uA4FF\\uA60D-\\uA60F\\uA673\\uA67E\\uA6F2-'
28 '\\uA6F7\\uA874-\\uA877\\uA8CE\\uA8CF\\uA8F8-\\uA8FA\\uA92E\\uA92F\\uA95F\\uA9C1-\\uA9CD\\uA9DE\\uA9DF\\uAA5C-\\uAA5F\\uAADE\\uAADF\\uAAF0'
29 '\\uAAF1\\uABEB\\uFD3E\\uFD3F\\uFE10-\\uFE19\\uFE30-\\uFE52\\uFE54-\\uFE61\\uFE63\\uFE68\\uFE6A\\uFE6B\\uFF01-\\uFF03\\uFF05-\\uFF0A\\uFF0C-'
30 '\\uFF0F\\uFF1A\\uFF1B\\uFF1F\\uFF20\\uFF3B-\\uFF3D\\uFF3F\\uFF5B\\uFF5D\\uFF5F-\\uFF65]'
31 )
32 return ans
33
34
35 def expand_offset_to_word(string, offset):
36 start = offset
37 pat = word_boundary_regex()
38 while start >= 1 and not pat.test(string.charAt(start - 1)):
39 start -= 1
40 end, sz = offset, string.length
41 while end < sz and not pat.test(string.charAt(end)):
42 end += 1
43 return {'word': string[start:end], 'start': start, 'end': end}
44
45
46 def word_at_point(x, y):
47 p = caret_position_from_point(x, y)
48 if p and p.offsetNode?.nodeType is Node.TEXT_NODE:
49 word_info = expand_offset_to_word(p.offsetNode.data, p.offset)
50 if word_info.word:
51 r = document.createRange()
52 r.setStart(p.offsetNode, word_info.start)
53 r.setEnd(p.offsetNode, word_info.end)
54 return r
55
56
57 def first_visible_word():
58 width = window.innerWidth
59 height = window.innerHeight
60 xdelta = width // 10
61 ydelta = height // 10
62 for y in range(0, height, ydelta):
63 for x in range(0, width, xdelta):
64 r = word_at_point(x, y)
65 if r?:
66 return r
67
68
69 def empty_range_extents():
70 return {
71 'start': {'x': 0, 'y': 0, 'height': 0, 'width': 0, 'onscreen': False, 'selected_prev': False},
72 'end': {'x': 0, 'y': 0, 'height': 0, 'width': 0, 'onscreen': False, 'selected_prev': False}
73 }
74
75 # Returns a node that we know will produce a reasonable bounding box closest to the start or end
76 # of the DOM tree from the specified node.
77 # Currently: safe_nodes and text nodes.
78 # This a depth first traversal, with the modification that if a node is reached that meets the criteria,
79 # the traversal stops, because higher nodes in the DOM tree always have larger bounds than their children.
80 safe_nodes = {'img': True, 'br': True, 'hr': True}
81
82
83 def get_selection_node_at_boundary(node, start):
84 stack = []
85 stack.push({'node': node, 'visited': False})
86 while stack.length > 0:
87 top = stack[-1]
88 # If the top node is a target type, we know that no nodes below it can be more to the start or end
89 # than it, so return it immediately.
90 name = top.node.nodeName.toLowerCase() if top.node.nodeName else ''
91 if top.node.nodeType is Node.TEXT_NODE or safe_nodes[name]:
92 return top.node
93 # Otherwise, depth-first traversal.
94 else if top.visited:
95 stack.pop()
96 else:
97 top.visited = True
98 if top.node.childNodes:
99 for c in top.node.childNodes if start else reversed(top.node.childNodes):
100 stack.push({'node': c, 'visited': False})
101
102
103 def range_extents(q, in_flow_mode):
104 ans = empty_range_extents()
105 if not q:
106 return ans
107 start = q.cloneRange()
108 end = q.cloneRange()
109
110 def rect_onscreen(r):
111 # we use -1 rather than zero for the top limit because on some
112 # platforms the browser engine returns that for top line selections.
113 # See https://bugs.launchpad.net/calibre/+bug/2024375/ for a test case.
114 if r.right <= window.innerWidth and r.bottom <= window.innerHeight and r.left >= 0 and r.top >= -1:
115 return True
116 return False
117
118 def apply_rect_to_ans(rect, ans):
119 ans.x = Math.round(rect.left)
120 ans.y = Math.round(rect.top)
121 ans.height = Math.round(rect.height)
122 ans.width = Math.round(rect.width)
123 ans.onscreen = rect_onscreen(rect)
124 return ans
125
126 def for_boundary(r, ans, is_start):
127 rect = r.getBoundingClientRect()
128 if rect.height is 0 and rect.width is 0:
129 # this tends to happen when moving the mouse downwards
130 # at the boundary between paragraphs
131 if r.startContainer?.nodeType is Node.ELEMENT_NODE:
132 node = r.startContainer
133 if r.startOffset and node.childNodes.length > r.startOffset:
134 node = node.childNodes[r.startOffset]
135
136 boundary_node = get_selection_node_at_boundary(node, is_start)
137 # If we found a node that will produce a reasonable bounding box at a boundary, use it:
138 if boundary_node:
139 if boundary_node.nodeType is Node.TEXT_NODE:
140 if is_start:
141 r.setStart(boundary_node, 0)
142 r.setEnd(boundary_node, 0)
143 else:
144 r.setStart(boundary_node, boundary_node.length)
145 r.setEnd(boundary_node, boundary_node.length)
146 rect = r.getBoundingClientRect()
147 else:
148 rect = boundary_node.getBoundingClientRect()
149 if not is_start:
150 ans.selected_prev = True
151 # we can't use getBoundingClientRect as the node might be split
152 # among multiple columns
153 else if node.getClientRects:
154 rects = node.getClientRects()
155 if rects.length:
156 erect = rects[0]
157 rect = {'left': erect.left, 'top': erect.top, 'right': erect.left + 2, 'bottom': erect.top + 2, 'width': 2, 'height': 2}
158 apply_rect_to_ans(rect, ans)
159
160 if q.startContainer.nodeType is Node.ELEMENT_NODE:
161 start.collapse(True)
162 for_boundary(start, ans.start, True)
163 elif q.startOffset is 0 and q.startContainer.length is 0:
164 start.collapse(True)
165 for_boundary(start, ans.start, True)
166 elif q.startOffset is q.startContainer.length:
167 start.setStart(q.startContainer, q.startOffset - 1)
168 start.setEnd(q.startContainer, q.startOffset)
169 rect = start.getBoundingClientRect()
170 apply_rect_to_ans(rect, ans.start).selected_prev = True
171 else:
172 start.setStart(q.startContainer, q.startOffset)
173 start.setEnd(q.startContainer, q.startOffset + 1)
174 rect = start.getBoundingClientRect()
175 apply_rect_to_ans(rect, ans.start)
176
177 if q.endContainer.nodeType is Node.ELEMENT_NODE:
178 end.collapse(False)
179 for_boundary(end, ans.end, False)
180 elif q.endOffset is 0 and q.endContainer.length is 0:
181 end.collapse(False)
182 for_boundary(end, ans.end, False)
183 elif q.endOffset is q.endContainer.length:
184 end.setStart(q.endContainer, q.endOffset - 1)
185 end.setEnd(q.endContainer, q.endOffset)
186 rect = end.getBoundingClientRect()
187 apply_rect_to_ans(rect, ans.end).selected_prev = True
188 else:
189 end.setStart(q.endContainer, q.endOffset)
190 end.setEnd(q.endContainer, q.endOffset + 1)
191 rect = end.getBoundingClientRect()
192 apply_rect_to_ans(rect, ans.end)
193
194 if ans.end.height is 2 and ans.start.height > 2:
195 ans.end.height = ans.start.height
196 if ans.start.height is 2 and ans.end.height > 2:
197 ans.start.height = ans.end.height
198 return ans
199
200
201 def selection_extents(in_flow_mode):
202 sel = window.getSelection()
203 if not sel or not sel.rangeCount or sel.isCollapsed:
204 return empty_range_extents()
205 return range_extents(sel.getRangeAt(0), in_flow_mode)
206
207
208 def is_start_closer_to_point(pos):
209 sel = window.getSelection()
210 if not sel.rangeCount:
211 return
212 p = caret_position_from_point(pos.x, pos.y)
213 if p:
214 r = sel.getRangeAt(0)
215 sr = document.createRange()
216 sr.setStart(r.startContainer, r.startOffset)
217 sr.setEnd(p.offsetNode, p.offset)
218 er = document.createRange()
219 er.setStart(p.offsetNode, p.offset)
220 er.setEnd(r.endContainer, r.endOffset)
221 return sr.toString().length < er.toString().length
222 return False
223
224
225 def move_end_of_selection(pos, start):
226 sel = window.getSelection()
227 if not sel.rangeCount:
228 return
229 p = caret_position_from_point(pos.x, pos.y)
230 if not p:
231 return
232 r = sel.getRangeAt(0)
233 if start is None:
234 q = document.createRange()
235 q.setStart(p.offsetNode, p.offset)
236 q.setEnd(p.offsetNode, p.offset)
237 if r.compareBoundaryPoints(window.Range.START_TO_START, q) >= 0:
238 start = True
239 elif r.compareBoundaryPoints(window.Range.END_TO_END, q) <= 0:
240 start = False
241 else:
242 # point is inside the selection
243 start = False
244 new_range = document.createRange()
245 if start:
246 new_range.setStart(p.offsetNode, p.offset)
247 new_range.setEnd(r.endContainer, r.endOffset)
248 other_boundary_changed = r.endContainer is not new_range.endContainer or r.endOffset is not new_range.endOffset
249 else:
250 new_range.setStart(r.startContainer, r.startOffset)
251 new_range.setEnd(p.offsetNode, p.offset)
252 other_boundary_changed = r.startContainer is not new_range.startContainer or r.startOffset is not new_range.startOffset
253 if new_range.collapsed and other_boundary_changed:
254 # we ignore the case when the new range is collapsed and the other end
255 # of the selection is also moved as this is a chromium bug. See https://bugs.launchpad.net/bugs/2054934
256 return
257 sel.removeAllRanges()
258 sel.addRange(new_range)