1
Fork 0
mirror of https://github.com/RGBCube/rgbcube.github.io synced 2025-05-22 09:25:07 +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

@ -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();
}