-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdom.ts
324 lines (294 loc) · 11.7 KB
/
dom.ts
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
export function getSelection(root: DocumentOrShadowRoot): Selection | null {
let target
// Browsers differ on whether shadow roots have a getSelection
// method. If it exists, use that, otherwise, call it on the
// document.
if ((root as any).nodeType == 11) { // Shadow root
target = (root as any).getSelection ? root as Document : (root as ShadowRoot).ownerDocument
} else {
target = root as Document
}
return target.getSelection()
}
export function contains(dom: Node, node: Node | null) {
return node ? dom == node || dom.contains(node.nodeType != 1 ? node.parentNode : node) : false
}
export function deepActiveElement(doc: Document) {
let elt = doc.activeElement
while (elt && elt.shadowRoot) elt = elt.shadowRoot.activeElement
return elt
}
export function hasSelection(dom: HTMLElement, selection: SelectionRange): boolean {
if (!selection.anchorNode) return false
try {
// Firefox will raise 'permission denied' errors when accessing
// properties of `sel.anchorNode` when it's in a generated CSS
// element.
return contains(dom, selection.anchorNode)
} catch(_) {
return false
}
}
export function clientRectsFor(dom: Node) {
if (dom.nodeType == 3)
return textRange(dom as Text, 0, dom.nodeValue!.length).getClientRects()
else if (dom.nodeType == 1)
return (dom as HTMLElement).getClientRects()
else
return [] as any as DOMRectList
}
// Scans forward and backward through DOM positions equivalent to the
// given one to see if the two are in the same place (i.e. after a
// text node vs at the end of that text node)
export function isEquivalentPosition(node: Node, off: number, targetNode: Node | null, targetOff: number): boolean {
return targetNode ? (scanFor(node, off, targetNode, targetOff, -1) ||
scanFor(node, off, targetNode, targetOff, 1)) : false
}
export function domIndex(node: Node): number {
for (var index = 0;; index++) {
node = node.previousSibling!
if (!node) return index
}
}
function scanFor(node: Node, off: number, targetNode: Node, targetOff: number, dir: -1 | 1): boolean {
for (;;) {
if (node == targetNode && off == targetOff) return true
if (off == (dir < 0 ? 0 : maxOffset(node))) {
if (node.nodeName == "DIV") return false
let parent = node.parentNode
if (!parent || parent.nodeType != 1) return false
off = domIndex(node) + (dir < 0 ? 0 : 1)
node = parent
} else if (node.nodeType == 1) {
node = node.childNodes[off + (dir < 0 ? -1 : 0)]
if (node.nodeType == 1 && (node as HTMLElement).contentEditable == "false") return false
off = dir < 0 ? maxOffset(node) : 0
} else {
return false
}
}
}
export function maxOffset(node: Node): number {
return node.nodeType == 3 ? node.nodeValue!.length : node.childNodes.length
}
/// Basic rectangle type.
export interface Rect {
readonly left: number
readonly right: number
readonly top: number
readonly bottom: number
}
export function flattenRect(rect: Rect, left: boolean) {
let x = left ? rect.left : rect.right
return {left: x, right: x, top: rect.top, bottom: rect.bottom}
}
function windowRect(win: Window): Rect {
return {left: 0, right: win.innerWidth,
top: 0, bottom: win.innerHeight}
}
export type ScrollStrategy = "nearest" | "start" | "end" | "center"
export function scrollRectIntoView(dom: HTMLElement, rect: Rect, side: -1 | 1,
x: ScrollStrategy, y: ScrollStrategy,
xMargin: number, yMargin: number, ltr: boolean) {
let doc = dom.ownerDocument!, win = doc.defaultView || window
for (let cur: any = dom, stop = false; cur && !stop;) {
if (cur.nodeType == 1) { // Element
let bounding: Rect, top = cur == doc.body
let scaleX = 1, scaleY = 1
if (top) {
bounding = windowRect(win)
} else {
if (/^(fixed|sticky)$/.test(getComputedStyle(cur).position)) stop = true
if (cur.scrollHeight <= cur.clientHeight && cur.scrollWidth <= cur.clientWidth) {
cur = cur.assignedSlot || cur.parentNode
continue
}
let rect = cur.getBoundingClientRect()
scaleX = rect.width / cur.offsetWidth
scaleY = rect.height / cur.offsetHeight
// Make sure scrollbar width isn't included in the rectangle
bounding = {left: rect.left, right: rect.left + cur.clientWidth * scaleX,
top: rect.top, bottom: rect.top + cur.clientHeight * scaleY}
}
let moveX = 0, moveY = 0
if (y == "nearest") {
if (rect.top < bounding.top) {
moveY = -(bounding.top - rect.top + yMargin)
if (side > 0 && rect.bottom > bounding.bottom + moveY)
moveY = rect.bottom - bounding.bottom + moveY + yMargin
} else if (rect.bottom > bounding.bottom) {
moveY = rect.bottom - bounding.bottom + yMargin
if (side < 0 && (rect.top - moveY) < bounding.top)
moveY = -(bounding.top + moveY - rect.top + yMargin)
}
} else {
let rectHeight = rect.bottom - rect.top, boundingHeight = bounding.bottom - bounding.top
let targetTop =
y == "center" && rectHeight <= boundingHeight ? rect.top + rectHeight / 2 - boundingHeight / 2 :
y == "start" || y == "center" && side < 0 ? rect.top - yMargin :
rect.bottom - boundingHeight + yMargin
moveY = targetTop - bounding.top
}
if (x == "nearest") {
if (rect.left < bounding.left) {
moveX = -(bounding.left - rect.left + xMargin)
if (side > 0 && rect.right > bounding.right + moveX)
moveX = rect.right - bounding.right + moveX + xMargin
} else if (rect.right > bounding.right) {
moveX = rect.right - bounding.right + xMargin
if (side < 0 && rect.left < bounding.left + moveX)
moveX = -(bounding.left + moveX - rect.left + xMargin)
}
} else {
let targetLeft =
x == "center" ? rect.left + (rect.right - rect.left) / 2 - (bounding.right - bounding.left) / 2 :
(x == "start") == ltr ? rect.left - xMargin :
rect.right - (bounding.right - bounding.left) + xMargin
moveX = targetLeft - bounding.left
}
if (moveX || moveY) {
if (top) {
win.scrollBy(moveX, moveY)
} else {
let movedX = 0, movedY = 0
if (moveY) {
let start = cur.scrollTop
cur.scrollTop += moveY / scaleY
movedY = (cur.scrollTop - start) * scaleY
}
if (moveX) {
let start = cur.scrollLeft
cur.scrollLeft += moveX / scaleX
movedX = (cur.scrollLeft - start) * scaleX
}
rect = {left: rect.left - movedX, top: rect.top - movedY,
right: rect.right - movedX, bottom: rect.bottom - movedY} as ClientRect
if (movedX && Math.abs(movedX - moveX) < 1) x = "nearest"
if (movedY && Math.abs(movedY - moveY) < 1) y = "nearest"
}
}
if (top) break
cur = cur.assignedSlot || cur.parentNode
} else if (cur.nodeType == 11) { // A shadow root
cur = cur.host
} else {
break
}
}
}
export function scrollableParent(dom: HTMLElement) {
let doc = dom.ownerDocument
for (let cur = dom.parentNode as HTMLElement | null; cur;) {
if (cur == doc.body) {
break
} else if (cur.nodeType == 1) {
if (cur.scrollHeight > cur.clientHeight || cur.scrollWidth > cur.clientWidth) return cur
cur = cur.assignedSlot || cur.parentNode as HTMLElement | null
} else if (cur.nodeType == 11) {
cur = (cur as any).host
} else {
break
}
}
return null
}
export interface SelectionRange {
focusNode: Node | null, focusOffset: number,
anchorNode: Node | null, anchorOffset: number
}
export class DOMSelectionState implements SelectionRange {
anchorNode: Node | null = null
anchorOffset: number = 0
focusNode: Node | null = null
focusOffset: number = 0
eq(domSel: SelectionRange): boolean {
return this.anchorNode == domSel.anchorNode && this.anchorOffset == domSel.anchorOffset &&
this.focusNode == domSel.focusNode && this.focusOffset == domSel.focusOffset
}
setRange(range: SelectionRange) {
let {anchorNode, focusNode} = range
// Clip offsets to node size to avoid crashes when Safari reports bogus offsets (#1152)
this.set(anchorNode, Math.min(range.anchorOffset, anchorNode ? maxOffset(anchorNode) : 0),
focusNode, Math.min(range.focusOffset, focusNode ? maxOffset(focusNode) : 0))
}
set(anchorNode: Node | null, anchorOffset: number, focusNode: Node | null, focusOffset: number) {
this.anchorNode = anchorNode; this.anchorOffset = anchorOffset
this.focusNode = focusNode; this.focusOffset = focusOffset
}
}
let preventScrollSupported: null | false | {preventScroll: boolean} = null
// Feature-detects support for .focus({preventScroll: true}), and uses
// a fallback kludge when not supported.
export function focusPreventScroll(dom: HTMLElement) {
if ((dom as any).setActive) return (dom as any).setActive() // in IE
if (preventScrollSupported) return dom.focus(preventScrollSupported)
let stack = []
for (let cur: Node | null = dom; cur; cur = cur.parentNode) {
stack.push(cur, (cur as any).scrollTop, (cur as any).scrollLeft)
if (cur == cur.ownerDocument) break
}
dom.focus(preventScrollSupported == null ? {
get preventScroll() {
preventScrollSupported = {preventScroll: true}
return true
}
} : undefined)
if (!preventScrollSupported) {
preventScrollSupported = false
for (let i = 0; i < stack.length;) {
let elt = stack[i++] as HTMLElement, top = stack[i++] as number, left = stack[i++] as number
if (elt.scrollTop != top) elt.scrollTop = top
if (elt.scrollLeft != left) elt.scrollLeft = left
}
}
}
let scratchRange: Range | null
export function textRange(node: Text, from: number, to = from) {
let range = scratchRange || (scratchRange = document.createRange())
range.setEnd(node, to)
range.setStart(node, from)
return range
}
export function dispatchKey(elt: HTMLElement, name: string, code: number): boolean {
let options = {key: name, code: name, keyCode: code, which: code, cancelable: true}
let down = new KeyboardEvent("keydown", options)
;(down as any).synthetic = true
elt.dispatchEvent(down)
let up = new KeyboardEvent("keyup", options)
;(up as any).synthetic = true
elt.dispatchEvent(up)
return down.defaultPrevented || up.defaultPrevented
}
export function getRoot(node: Node | null | undefined): DocumentOrShadowRoot | null {
while (node) {
if (node && (node.nodeType == 9 || node.nodeType == 11 && (node as ShadowRoot).host))
return node as unknown as DocumentOrShadowRoot
node = (node as HTMLElement).assignedSlot || node.parentNode
}
return null
}
export function clearAttributes(node: HTMLElement) {
while (node.attributes.length) node.removeAttributeNode(node.attributes[0])
}
export function atElementStart(doc: HTMLElement, selection: SelectionRange) {
let node = selection.focusNode, offset = selection.focusOffset
if (!node || selection.anchorNode != node || selection.anchorOffset != offset) return false
// Safari can report bogus offsets (#1152)
offset = Math.min(offset, maxOffset(node))
for (;;) {
if (offset) {
if (node.nodeType != 1) return false
let prev: Node = node.childNodes[offset - 1]
if ((prev as HTMLElement).contentEditable == "false") offset--
else { node = prev; offset = maxOffset(node) }
} else if (node == doc) {
return true
} else {
offset = domIndex(node)
node = node.parentNode!
}
}
}
export function isScrolledToBottom(elt: HTMLElement) {
return elt.scrollTop > Math.max(1, elt.scrollHeight - elt.clientHeight - 4)
}