Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into davidramos-add-instan…
Browse files Browse the repository at this point in the history
…tclick-behavior

* origin/main:
  Keep Trix dynamic styles in the head (hotwired#1133)
  • Loading branch information
afcapel committed Jan 22, 2024
2 parents 1c9b41c + 32b8c30 commit ac310e2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/core/drive/page_renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { activateScriptElement, waitForLoad } from "../../util"
import { Renderer } from "../renderer"
import { ProgressBarID } from "./progress_bar"

export class PageRenderer extends Renderer {
static renderElement(currentElement, newElement) {
Expand Down Expand Up @@ -184,7 +183,13 @@ export class PageRenderer extends Renderer {
}

get unusedHeadStylesheetElements() {
return this.oldHeadStylesheetElements.filter((element) => element.id !== ProgressBarID)
return this.oldHeadStylesheetElements.filter((element) => {
return !(element.hasAttribute("data-turbo-permanent") ||
// Trix dynamically adds styles to the head that we want to keep around which have a
// `data-page-name` attribute. Long term we should moves those styles to Trix's CSS file
// but for now we'll just skip removing them
element.hasAttribute("data-page-name"))
})
}

get oldHeadStylesheetElements() {
Expand Down
1 change: 1 addition & 0 deletions src/core/drive/progress_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class ProgressBar {
createStylesheetElement() {
const element = document.createElement("style")
element.id = ProgressBarID
element.setAttribute("data-turbo-permanent", "")
element.type = "text/css"
element.textContent = ProgressBar.defaultCSS
if (this.cspNonce) {
Expand Down
3 changes: 3 additions & 0 deletions src/tests/functional/drive_stylesheet_merging_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ test.beforeEach(async ({ page }) => {
})

test("navigating removes unused style elements", async ({ page }) => {
assert.ok(await hasSelector(page, 'style[id="turbo-progress-bar"]'))

await page.locator("#go-right").click()
await nextBody(page)

assert.ok(await hasSelector(page, 'style[id="turbo-progress-bar"]'))
assert.ok(await hasSelector(page, 'link[rel=stylesheet][href="/src/tests/fixtures/stylesheets/common.css"]'))
assert.ok(await hasSelector(page, 'link[rel=stylesheet][href="/src/tests/fixtures/stylesheets/right.css"]'))
assert.notOk(await hasSelector(page, 'link[rel=stylesheet][href="/src/tests/fixtures/stylesheets/left.css"]'))
Expand Down

0 comments on commit ac310e2

Please sign in to comment.