-
Notifications
You must be signed in to change notification settings - Fork 412
/
page_renderer.ts
153 lines (125 loc) · 3.82 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
147
148
149
150
151
152
153
import { Renderer } from "../renderer"
import { PageSnapshot } from "./page_snapshot"
import { ReloadReason } from "../native/browser_adapter"
const INTERNAL_ATTRIBUTES = ["aria-busy", "data-turbo-preview"]
export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
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",
}
}
}
prepareToRender() {
this.mergeHead()
this.updateHtmlElementAttributes()
}
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
}
get newHtmlElement() {
return this.newSnapshot.htmlElement
}
mergeHead() {
this.copyNewHeadStylesheetElements()
this.copyNewHeadScriptElements()
this.removeCurrentHeadProvisionalElements()
this.copyNewHeadProvisionalElements()
}
updateHtmlElementAttributes() {
for (const attr of document.documentElement.attributes) {
if (!this.newElement.hasAttribute(attr.nodeName) && !INTERNAL_ATTRIBUTES.includes(attr.nodeName)) {
document.documentElement.removeAttribute(attr.nodeName)
}
}
for (const attr of this.newHtmlElement.attributes) {
document.documentElement.setAttribute(attr.nodeName, attr.nodeValue!)
}
}
replaceBody() {
this.preservingPermanentElements(() => {
this.activateNewBody()
this.assignNewBody()
})
}
get trackedElementsAreIdentical() {
return this.currentHeadSnapshot.trackedElementSignature == this.newHeadSnapshot.trackedElementSignature
}
copyNewHeadStylesheetElements() {
for (const element of this.newHeadStylesheetElements) {
document.head.appendChild(element)
}
}
copyNewHeadScriptElements() {
for (const element of this.newHeadScriptElements) {
document.head.appendChild(this.createScriptElement(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 = this.createScriptElement(inertScriptElement)
inertScriptElement.replaceWith(activatedScriptElement)
}
}
assignNewBody() {
if (document.body && this.newElement instanceof HTMLBodyElement) {
document.body.replaceWith(this.newElement)
} else {
document.documentElement.appendChild(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")
}
}