Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SECURITY: Generate more category CSS on client
This commit moves the generation of category background CSS from the server side to the client side. This simplifies the server side code because it does not need to check which categories are visible to the current user.
- Loading branch information
Showing
8 changed files
with
190 additions
and
209 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...sets/javascripts/discourse/app/instance-initializers/category-background-css-generator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { getURLWithCDN } from "discourse-common/lib/get-url"; | ||
|
||
export default { | ||
after: "register-hashtag-types", | ||
|
||
initialize(owner) { | ||
this.session = owner.lookup("service:session"); | ||
this.site = owner.lookup("service:site"); | ||
|
||
if (!this.site.categories?.length) { | ||
return; | ||
} | ||
|
||
const css = []; | ||
const darkCss = []; | ||
|
||
this.site.categories.forEach((category) => { | ||
const lightUrl = category.uploaded_background?.url; | ||
const darkUrl = | ||
this.session.defaultColorSchemeIsDark || this.session.darkModeAvailable | ||
? category.uploaded_background_dark?.url | ||
: null; | ||
const defaultUrl = | ||
darkUrl && this.session.defaultColorSchemeIsDark ? darkUrl : lightUrl; | ||
|
||
if (defaultUrl) { | ||
const url = getURLWithCDN(defaultUrl); | ||
css.push( | ||
`body.category-${category.fullSlug} { background-image: url(${url}); }` | ||
); | ||
} | ||
|
||
if (darkUrl && defaultUrl !== darkUrl) { | ||
const url = getURLWithCDN(darkUrl); | ||
darkCss.push( | ||
`body.category-${category.fullSlug} { background-image: url(${url}); }` | ||
); | ||
} | ||
}); | ||
|
||
if (darkCss.length > 0) { | ||
css.push("@media (prefers-color-scheme: dark) {", ...darkCss, "}"); | ||
} | ||
|
||
const cssTag = document.createElement("style"); | ||
cssTag.type = "text/css"; | ||
cssTag.id = "category-background-css-generator"; | ||
cssTag.innerHTML = css.join("\n"); | ||
document.head.appendChild(cssTag); | ||
}, | ||
|
||
teardown() { | ||
document.querySelector("#category-background-css-generator")?.remove(); | ||
}, | ||
}; |
132 changes: 132 additions & 0 deletions
132
app/assets/javascripts/discourse/tests/acceptance/category-background-css-generator-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { visit } from "@ember/test-helpers"; | ||
import { test } from "qunit"; | ||
import Session from "discourse/models/session"; | ||
import { acceptance } from "discourse/tests/helpers/qunit-helpers"; | ||
|
||
const SITE_DATA = { | ||
categories: [ | ||
{ | ||
id: 1, | ||
color: "ff0000", | ||
text_color: "ffffff", | ||
name: "category1", | ||
slug: "foo", | ||
uploaded_background: { | ||
id: 54, | ||
url: "/uploads/default/original/1X/c5c84b16ebf745ab848d1498267c541facbf1ff0.png", | ||
width: 1024, | ||
height: 768, | ||
}, | ||
}, | ||
{ | ||
id: 2, | ||
color: "333", | ||
text_color: "ffffff", | ||
name: "category2", | ||
slug: "bar", | ||
uploaded_background_dark: { | ||
id: 25, | ||
url: "/uploads/default/original/1X/f9fdb0ad108f2aed178c40f351bbb2c7cb2571e3.png", | ||
width: 1024, | ||
height: 768, | ||
}, | ||
}, | ||
{ | ||
id: 4, | ||
color: "2B81AF", | ||
text_color: "ffffff", | ||
parent_category_id: 1, | ||
name: "category3", | ||
slug: "baz", | ||
uploaded_background: { | ||
id: 11, | ||
url: "/uploads/default/original/1X/684c104edc18a7e9cef1fa31f41215f3eec5d92b.png", | ||
width: 1024, | ||
height: 768, | ||
}, | ||
uploaded_background_dark: { | ||
id: 19, | ||
url: "/uploads/default/original/1X/89b1a2641e91604c32b21db496be11dba7a253e6.png", | ||
width: 1024, | ||
height: 768, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
acceptance("Category Background CSS Generator", function (needs) { | ||
needs.user(); | ||
needs.site(SITE_DATA); | ||
|
||
test("CSS classes are generated", async function (assert) { | ||
await visit("/"); | ||
|
||
assert.equal( | ||
document.querySelector("#category-background-css-generator").innerHTML, | ||
"body.category-foo { background-image: url(/uploads/default/original/1X/c5c84b16ebf745ab848d1498267c541facbf1ff0.png); }\n" + | ||
"body.category-foo-baz { background-image: url(/uploads/default/original/1X/684c104edc18a7e9cef1fa31f41215f3eec5d92b.png); }" | ||
); | ||
}); | ||
}); | ||
|
||
acceptance("Category Background CSS Generator (dark)", function (needs) { | ||
needs.user(); | ||
needs.site(SITE_DATA); | ||
|
||
needs.hooks.beforeEach(function () { | ||
const session = Session.current(); | ||
session.set("darkModeAvailable", true); | ||
session.set("defaultColorSchemeIsDark", false); | ||
}); | ||
|
||
needs.hooks.afterEach(function () { | ||
const session = Session.current(); | ||
session.set("darkModeAvailable", null); | ||
session.set("defaultColorSchemeIsDark", null); | ||
}); | ||
|
||
test("CSS classes are generated", async function (assert) { | ||
await visit("/"); | ||
|
||
assert.equal( | ||
document.querySelector("#category-background-css-generator").innerHTML, | ||
"body.category-foo { background-image: url(/uploads/default/original/1X/c5c84b16ebf745ab848d1498267c541facbf1ff0.png); }\n" + | ||
"body.category-foo-baz { background-image: url(/uploads/default/original/1X/684c104edc18a7e9cef1fa31f41215f3eec5d92b.png); }\n" + | ||
"@media (prefers-color-scheme: dark) {\n" + | ||
"body.category-bar { background-image: url(/uploads/default/original/1X/f9fdb0ad108f2aed178c40f351bbb2c7cb2571e3.png); }\n" + | ||
"body.category-foo-baz { background-image: url(/uploads/default/original/1X/89b1a2641e91604c32b21db496be11dba7a253e6.png); }\n" + | ||
"}" | ||
); | ||
}); | ||
}); | ||
|
||
acceptance( | ||
"Category Background CSS Generator (dark is default)", | ||
function (needs) { | ||
needs.user(); | ||
needs.site(SITE_DATA); | ||
|
||
needs.hooks.beforeEach(function () { | ||
const session = Session.current(); | ||
session.set("darkModeAvailable", true); | ||
session.set("defaultColorSchemeIsDark", true); | ||
}); | ||
|
||
needs.hooks.afterEach(function () { | ||
const session = Session.current(); | ||
session.set("darkModeAvailable", null); | ||
session.set("defaultColorSchemeIsDark", null); | ||
}); | ||
|
||
test("CSS classes are generated", async function (assert) { | ||
await visit("/"); | ||
|
||
assert.equal( | ||
document.querySelector("#category-background-css-generator").innerHTML, | ||
"body.category-foo { background-image: url(/uploads/default/original/1X/c5c84b16ebf745ab848d1498267c541facbf1ff0.png); }\n" + | ||
"body.category-bar { background-image: url(/uploads/default/original/1X/f9fdb0ad108f2aed178c40f351bbb2c7cb2571e3.png); }\n" + | ||
"body.category-foo-baz { background-image: url(/uploads/default/original/1X/89b1a2641e91604c32b21db496be11dba7a253e6.png); }" | ||
); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.