File size: 1,560 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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}\"")
}

#[cfg(test)]
mod tests {
    use crate::util::stringify_js;

    #[test]
    fn surrounds_with_double_quotes() {
        assert_eq!(stringify_js("foo"), r#""foo""#);
    }

    #[test]
    fn escapes_double_quotes() {
        assert_eq!(stringify_js(r#""""#), r#""\"\"""#);
    }

    #[test]
    fn escapes_backslash() {
        assert_eq!(stringify_js(r"\"), r#""\\""#);
        assert_eq!(stringify_js(r"\\"), r#""\\\\""#);
        assert_eq!(stringify_js(r"\n"), r#""\\n""#);
    }

    #[test]
    fn escapes_newlines() {
        assert_eq!(stringify_js("\n"), r#""\n""#);
    }

    #[test]
    fn escapes_mixed() {
        assert_eq!(stringify_js("\n\r\u{0c}"), r#""\n\r\f""#);
    }
}