1
Fork 0
mirror of https://github.com/RGBCube/rgbcube.github.io synced 2025-05-14 05:54:58 +00:00

Fix CSS escaping

This commit is contained in:
RGBCube 2023-12-20 22:36:45 +03:00
parent 80c8f36435
commit 45dfbddb3d
No known key found for this signature in database
3 changed files with 7 additions and 46 deletions

View file

@ -2,6 +2,7 @@ use embed_file::embed_string as embed;
use maud::{
html,
Markup,
PreEscaped,
};
use crate::{
@ -15,12 +16,12 @@ pub fn create<S: AsRef<str>>(styling: S, faces: [Markup; 6]) -> Markup {
page::create(
html! {
style {
(minify::css(embed!("cube.css")))
(PreEscaped(minify::css(embed!("cube.css"))))
}
},
html! {
style {
(styling.as_ref())
(PreEscaped(styling.as_ref()))
}
div class="scene" {
@ -34,7 +35,7 @@ pub fn create<S: AsRef<str>>(styling: S, faces: [Markup; 6]) -> Markup {
}
script {
(minify::js(embed!("cube.js")))
(PreEscaped(minify::js(embed!("cube.js"))))
}
},
)

View file

@ -1,44 +1,4 @@
const SPECIAL_CHARS: [char; 8] = ['{', '}', ':', ';', ' ', '\n', '!', '>'];
// Taken from https://github.com/amgarrett09/rust-css-minifier/blob/master/src/minify/mod.rs.
pub fn css<S: AsRef<str>>(input: S) -> String {
let mut last_char: Vec<char> = " ".chars().collect();
let mut output = Vec::new();
let mut inside_comment = false;
for c in input.as_ref().chars() {
// We're in a comment if we find '/*'
if !inside_comment && c == '*' && last_char[0] == '/' {
inside_comment = true;
output.pop();
}
// We're no longer in a comment if we find '*/'
if inside_comment && c == '/' && last_char[0] == '*' {
inside_comment = false;
}
// We should NOT add a char to the output if:
// 1) It's a line break, OR
// 2) The char is a space AND the last char scanned was one of our
// special cases OR
// 3) We're inside a comment
// should_add_char is the negation of that
if !(c == '\n' || (c == ' ' && SPECIAL_CHARS.contains(&last_char[0])) || inside_comment) {
// Remove last char (and don't put it back) if it's a space before
// a special character, or if it's a semicolon before an ending brace
if let Some(last) = output.pop() {
if (!SPECIAL_CHARS.contains(&c) || last != ' ') && (c != '}' || last != ';') {
output.push(last);
}
}
output.push(c);
}
last_char[0] = c;
}
output.iter().collect()
// TODO: Actually do something.
return input.as_ref().to_string();
}

View file

@ -16,7 +16,7 @@ pub async fn generate() -> Markup {
minify::css(embed!("404.css")),
array::from_fn(|_| {
(html! {
div class="frame" {}
div class="frame" { "404" }
div class="square black" {}
div class="square magenta" {}
div class="square magenta" {}