-
Notifications
You must be signed in to change notification settings - Fork 435
/
page_snapshot.js
81 lines (62 loc) · 2.04 KB
/
page_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { parseHTMLDocument } from "../../util"
import { Snapshot } from "../snapshot"
import { expandURL } from "../url"
import { HeadSnapshot } from "./head_snapshot"
export class PageSnapshot extends Snapshot {
static fromHTMLString(html = "") {
return this.fromDocument(parseHTMLDocument(html))
}
static fromElement(element) {
return this.fromDocument(element.ownerDocument)
}
static fromDocument({ head, body }) {
return new this(body, new HeadSnapshot(head))
}
constructor(element, headSnapshot) {
super(element)
this.headSnapshot = headSnapshot
}
clone() {
const clonedElement = this.element.cloneNode(true)
const selectElements = this.element.querySelectorAll("select")
const clonedSelectElements = clonedElement.querySelectorAll("select")
for (const [index, source] of selectElements.entries()) {
const clone = clonedSelectElements[index]
for (const option of clone.selectedOptions) option.selected = false
for (const option of source.selectedOptions) clone.options[option.index].selected = true
}
for (const clonedPasswordInput of clonedElement.querySelectorAll('input[type="password"]')) {
clonedPasswordInput.value = ""
}
return new PageSnapshot(clonedElement, this.headSnapshot)
}
get html() {
return `${this.headElement.outerHTML}\n\n${this.element.outerHTML}`
}
get headElement() {
return this.headSnapshot.element
}
get rootLocation() {
const root = this.getSetting("root") ?? "/"
return expandURL(root)
}
get cacheControlValue() {
return this.getSetting("cache-control")
}
get isPreviewable() {
return this.cacheControlValue != "no-preview"
}
get isCacheable() {
return this.cacheControlValue != "no-cache"
}
get isVisitable() {
return this.getSetting("visit-control") != "reload"
}
get prefersViewTransitions() {
return this.headSnapshot.getMetaValue("view-transition") === "same-origin"
}
// Private
getSetting(name) {
return this.headSnapshot.getMetaValue(`turbo-${name}`)
}
}