Download next.js/turbopack/crates/turbopack-css/src/util.rs from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-css/src/util.rs
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/turbopack/crates/turbopack-css/src/util.rs
-
curl -L -o util.rs https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/turbopack/crates/turbopack-css/src/util.rs
1.56 kB
| pub fn stringify_js(str: &str) -> String { | |
| let mut escaped = String::with_capacity(str.len()); | |
| for char in str.chars() { | |
| match char { | |
| // Escape the following characters required for strings by the CSS scanner: | |
| // https://www.w3.org/TR/CSS21/grammar.html#scanner | |
| // https://github.com/vercel/turbo/pull/2598#discussion_r1022625909 | |
| // | |
| // Note that the form feed character is not representable as \f in Rust strings, so | |
| // the unicode representation \u{0c} is used. | |
| '\\' => escaped.push_str(r"\\"), | |
| '\n' => escaped.push_str(r"\n"), | |
| '\r' => escaped.push_str(r"\r"), | |
| '"' => escaped.push_str(r#"\""#), | |
| '\u{0c}' => escaped.push_str(r"\f"), | |
| _ => escaped.push(char), | |
| } | |
| } | |
| format!("\"{escaped}\"") | |
| } | |
| mod tests { | |
| use crate::util::stringify_js; | |
| fn surrounds_with_double_quotes() { | |
| assert_eq!(stringify_js("foo"), r#""foo""#); | |
| } | |
| fn escapes_double_quotes() { | |
| assert_eq!(stringify_js(r#""""#), r#""\"\"""#); | |
| } | |
| fn escapes_backslash() { | |
| assert_eq!(stringify_js(r"\"), r#""\\""#); | |
| assert_eq!(stringify_js(r"\\"), r#""\\\\""#); | |
| assert_eq!(stringify_js(r"\n"), r#""\\n""#); | |
| } | |
| fn escapes_newlines() { | |
| assert_eq!(stringify_js("\n"), r#""\n""#); | |
| } | |
| fn escapes_mixed() { | |
| assert_eq!(stringify_js("\n\r\u{0c}"), r#""\n\r\f""#); | |
| } | |
| } | |