Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep Trix dynamic styles in the head #1133

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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