Skip to content

Commit

Permalink
Revert #553 (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpuyol authored Jun 3, 2022
1 parent 28b6e31 commit f932cb4
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 77 deletions.
19 changes: 0 additions & 19 deletions src/core/drive/page_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ 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
Expand All @@ -25,7 +23,6 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {

prepareToRender() {
this.mergeHead()
this.updateHtmlElementAttributes()
}

async render() {
Expand Down Expand Up @@ -53,29 +50,13 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
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()
Expand Down
10 changes: 4 additions & 6 deletions src/core/drive/page_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ export class PageSnapshot extends Snapshot<HTMLBodyElement> {
return this.fromDocument(element.ownerDocument)
}

static fromDocument({ head, body, documentElement }: Document) {
return new this(body as HTMLBodyElement, new HeadSnapshot(head), documentElement as HTMLHtmlElement)
static fromDocument({ head, body }: Document) {
return new this(body as HTMLBodyElement, new HeadSnapshot(head))
}

readonly headSnapshot: HeadSnapshot
readonly htmlElement: HTMLHtmlElement

constructor(element: HTMLBodyElement, headSnapshot: HeadSnapshot, htmlElement: HTMLHtmlElement) {
constructor(element: HTMLBodyElement, headSnapshot: HeadSnapshot) {
super(element)
this.headSnapshot = headSnapshot
this.htmlElement = htmlElement
}

clone() {
return new PageSnapshot(this.element.cloneNode(true), this.headSnapshot, this.htmlElement)
return new PageSnapshot(this.element.cloneNode(true), this.headSnapshot)
}

get headElement() {
Expand Down
4 changes: 3 additions & 1 deletion src/core/drive/page_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export class PageView extends View<Element, PageSnapshot, PageViewRenderer, Page

renderPage(snapshot: PageSnapshot, isPreview = false, willRender = true) {
const renderer = new PageRenderer(this.snapshot, snapshot, isPreview, willRender)
if (!renderer.shouldRender) { this.forceReloaded = true }
if (!renderer.shouldRender) {
this.forceReloaded = true
}
return this.render(renderer)
}

Expand Down
15 changes: 0 additions & 15 deletions src/tests/fixtures/html_attributes.html

This file was deleted.

15 changes: 0 additions & 15 deletions src/tests/fixtures/html_attributes_two.html

This file was deleted.

16 changes: 16 additions & 0 deletions src/tests/fixtures/target.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Target</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<script src="/src/tests/fixtures/test.js"></script>
</head>
<body>
<h1>Target</h1>

<div id="target">
Targeted element
</div>
</body>
</html>
4 changes: 2 additions & 2 deletions src/tests/functional/navigation_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export class NavigationTests extends TurboDriveTestCase {
"sets [aria-busy] on the document element"
)
this.assert.equal(
await this.nextAttributeMutationNamed("one", "aria-busy"),
await this.nextAttributeMutationNamed("html", "aria-busy"),
null,
"removes [aria-busy] from the updated document element"
"removes [aria-busy] from the document element"
)
}

Expand Down
8 changes: 5 additions & 3 deletions src/tests/functional/rendering_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ export class RenderingTests extends TurboDriveTestCase {

await this.remote.execute(() => localStorage.setItem("scrolls", "false"))

this.remote.execute(() => addEventListener("scroll", () => {
localStorage.setItem("scrolls", "true")
}))
this.remote.execute(() =>
addEventListener("scroll", () => {
localStorage.setItem("scrolls", "true")
})
)

this.clickSelector("#below-the-fold-visit-control-reload-link")

Expand Down
16 changes: 0 additions & 16 deletions src/tests/functional/visit_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ export class VisitTests extends TurboDriveTestCase {
this.assert.notOk(await this.hasSelector("some-cached-element"))
}

async "test updates HTML element attributes"() {
await this.visitLocation("/src/tests/fixtures/html_attributes.html")
await this.nextBeat

this.assert.equal(await this.getDocumentElementAttribute("class"), "html-attributes")
this.assert.equal(await this.getDocumentElementAttribute("data-attribute"), "attr")
this.assert.equal(await this.getDocumentElementAttribute("data-other-attribute"), null)

await this.clickSelector("#link")
await this.nextBeat

this.assert.equal(await this.getDocumentElementAttribute("class"), "html-attributes-two")
this.assert.equal(await this.getDocumentElementAttribute("data-attribute"), "attr-two")
this.assert.equal(await this.getDocumentElementAttribute("data-other-attribute"), "other-attr")
}

async visitLocation(location: string) {
this.remote.execute((location: string) => window.Turbo.visit(location), [location])
}
Expand Down

0 comments on commit f932cb4

Please sign in to comment.