mirror of
				https://github.com/RGBCube/rgbcube.github.io
				synced 2025-10-31 06:52:45 +00:00 
			
		
		
		
	Fix CSS escaping
This commit is contained in:
		
							parent
							
								
									80c8f36435
								
							
						
					
					
						commit
						45dfbddb3d
					
				
					 3 changed files with 7 additions and 46 deletions
				
			
		|  | @ -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")))) | ||||
|             } | ||||
|         }, | ||||
|     ) | ||||
|  |  | |||
|  | @ -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(); | ||||
| } | ||||
|  |  | |||
|  | @ -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" {} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 RGBCube
						RGBCube