-
Notifications
You must be signed in to change notification settings - Fork 429
/
snapshot.js
61 lines (47 loc) · 1.45 KB
/
snapshot.js
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
import { queryAutofocusableElement } from "../util"
export class Snapshot {
constructor(element) {
this.element = element
}
get activeElement() {
return this.element.ownerDocument.activeElement
}
get children() {
return [...this.element.children]
}
hasAnchor(anchor) {
return this.getElementForAnchor(anchor) != null
}
getElementForAnchor(anchor) {
return anchor ? this.element.querySelector(`[id='${anchor}'], a[name='${anchor}']`) : null
}
get isConnected() {
return this.element.isConnected
}
get firstAutofocusableElement() {
return queryAutofocusableElement(this.element)
}
get permanentElements() {
return queryPermanentElementsAll(this.element)
}
getPermanentElementById(id) {
return getPermanentElementById(this.element, id)
}
getPermanentElementMapForSnapshot(snapshot) {
const permanentElementMap = {}
for (const currentPermanentElement of this.permanentElements) {
const { id } = currentPermanentElement
const newPermanentElement = snapshot.getPermanentElementById(id)
if (newPermanentElement) {
permanentElementMap[id] = [currentPermanentElement, newPermanentElement]
}
}
return permanentElementMap
}
}
export function getPermanentElementById(node, id) {
return node.querySelector(`#${id}[data-turbo-permanent]`)
}
export function queryPermanentElementsAll(node) {
return node.querySelectorAll("[id][data-turbo-permanent]")
}