-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathpage_renderer.ts
146 lines (118 loc) · 3.65 KB
/
page_renderer.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
import { Renderer } from "../renderer"
import { PageSnapshot } from "./page_snapshot"
import { ReloadReason } from "../native/browser_adapter"
import { activateScriptElement, waitForLoad } from "../../util"
export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
static renderElement(currentElement: HTMLBodyElement, newElement: HTMLBodyElement) {
if (document.body && newElement instanceof HTMLBodyElement) {
document.body.replaceWith(newElement)
} else {
document.documentElement.appendChild(newElement)
}
}
get shouldRender() {
return this.newSnapshot.isVisitable && this.trackedElementsAreIdentical
}
get reloadReason(): ReloadReason {
if (!this.newSnapshot.isVisitable) {
return {
reason: "turbo_visit_control_is_reload",
}
}
if (!this.trackedElementsAreIdentical) {
return {
reason: "tracked_element_mismatch",
}
}
}
async prepareToRender() {
await this.mergeHead()
}
async render() {
if (this.willRender) {
this.replaceBody()
}
}
finishRendering() {
super.finishRendering()
if (!this.isPreview) {
this.focusFirstAutofocusableElement()
}
}
get currentHeadSnapshot() {
return this.currentSnapshot.headSnapshot
}
get newHeadSnapshot() {
return this.newSnapshot.headSnapshot
}
get newElement() {
return this.newSnapshot.element
}
async mergeHead() {
const newStylesheetElements = this.copyNewHeadStylesheetElements()
this.copyNewHeadScriptElements()
this.removeCurrentHeadProvisionalElements()
this.copyNewHeadProvisionalElements()
await newStylesheetElements
}
replaceBody() {
this.preservingPermanentElements(() => {
this.activateNewBody()
this.assignNewBody()
})
}
get trackedElementsAreIdentical() {
return this.currentHeadSnapshot.trackedElementSignature == this.newHeadSnapshot.trackedElementSignature
}
async copyNewHeadStylesheetElements() {
const loadingElements = []
for (const element of this.newHeadStylesheetElements) {
loadingElements.push(waitForLoad(element as HTMLLinkElement))
document.head.appendChild(element)
}
await Promise.all(loadingElements)
}
copyNewHeadScriptElements() {
for (const element of this.newHeadScriptElements) {
document.head.appendChild(activateScriptElement(element))
}
}
removeCurrentHeadProvisionalElements() {
for (const element of this.currentHeadProvisionalElements) {
document.head.removeChild(element)
}
}
copyNewHeadProvisionalElements() {
for (const element of this.newHeadProvisionalElements) {
document.head.appendChild(element)
}
}
activateNewBody() {
document.adoptNode(this.newElement)
this.activateNewBodyScriptElements()
}
activateNewBodyScriptElements() {
for (const inertScriptElement of this.newBodyScriptElements) {
const activatedScriptElement = activateScriptElement(inertScriptElement)
inertScriptElement.replaceWith(activatedScriptElement)
}
}
assignNewBody() {
this.renderElement(this.currentElement, this.newElement)
}
get newHeadStylesheetElements() {
return this.newHeadSnapshot.getStylesheetElementsNotInSnapshot(this.currentHeadSnapshot)
}
get newHeadScriptElements() {
return this.newHeadSnapshot.getScriptElementsNotInSnapshot(this.currentHeadSnapshot)
}
get currentHeadProvisionalElements() {
return this.currentHeadSnapshot.provisionalElements
}
get newHeadProvisionalElements() {
return this.newHeadSnapshot.provisionalElements
}
get newBodyScriptElements() {
return this.newElement.querySelectorAll("script")
}
}