jsesc 0.1.0

Escape a string for safe embedding in JavaScript or JSON source. A faithful port of the jsesc npm package's string escaping. Zero deps, no_std.
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! # jsesc — escape a string for JavaScript or JSON
//!
//! Escape a string so it can be safely embedded in JavaScript or JSON source — handling
//! quotes, backslashes, control characters, line/paragraph separators, and (optionally)
//! every non-ASCII character. A faithful Rust port of the string-escaping of the widely-used
//! [`jsesc`](https://www.npmjs.com/package/jsesc) npm package by Mathias Bynens.
//!
//! ```
//! use jsesc::jsesc;
//!
//! assert_eq!(jsesc("café"), "caf\\xE9");
//! assert_eq!(jsesc("foo'bar"), "foo\\'bar");
//! ```
//!
//! Use [`jsesc_with`] / [`Options`] for double/backtick quotes, wrapping, JSON mode, ES6
//! unicode escapes, ASCII-only output, and more:
//!
//! ```
//! use jsesc::{jsesc_with, Options, Quotes};
//!
//! assert_eq!(jsesc_with("hi", &Options::new().json(true)), "\"hi\"");
//! assert_eq!(jsesc_with("ab", &Options::new().escape_everything(true)), "\\x61\\x62");
//! ```
//!
//! **Zero dependencies** and `#![no_std]`.

#![no_std]
#![forbid(unsafe_code)]
#![doc(html_root_url = "https://docs.rs/jsesc/0.1.0")]
#![allow(clippy::struct_excessive_bools, clippy::format_push_string)]

extern crate alloc;

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

// Compile-test the README's examples as part of `cargo test`.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

/// The quote style to escape for (and wrap with).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Quotes {
    /// Single quotes (`'`).
    Single,
    /// Double quotes (`"`).
    Double,
    /// Backticks (`` ` ``).
    Backtick,
}

/// Options for [`jsesc_with`].
#[derive(Debug, Clone, Default)]
pub struct Options {
    quotes: Option<Quotes>,
    wrap: Option<bool>,
    es6: bool,
    escape_everything: bool,
    minimal: bool,
    is_script_context: bool,
    json: bool,
    lowercase_hex: bool,
}

impl Options {
    /// Default options (single quotes, no wrapping, escape non-ASCII).
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Which quote character to escape (and wrap with). Defaults to [`Quotes::Single`]
    /// (or [`Quotes::Double`] when `json` is set).
    #[must_use]
    pub fn quotes(mut self, quotes: Quotes) -> Self {
        self.quotes = Some(quotes);
        self
    }

    /// Wrap the output in the quote character. Defaults to `false` (or `true` when `json`).
    #[must_use]
    pub fn wrap(mut self, wrap: bool) -> Self {
        self.wrap = Some(wrap);
        self
    }

    /// Use ES6 unicode code point escapes (`\u{1D306}`) for astral symbols.
    #[must_use]
    pub fn es6(mut self, es6: bool) -> Self {
        self.es6 = es6;
        self
    }

    /// Escape every character, including printable ASCII.
    #[must_use]
    pub fn escape_everything(mut self, value: bool) -> Self {
        self.escape_everything = value;
        self
    }

    /// Only escape the strictly necessary characters (quotes, backslash, line breaks, and
    /// invisible whitespace), leaving printable non-ASCII as-is.
    #[must_use]
    pub fn minimal(mut self, value: bool) -> Self {
        self.minimal = value;
        self
    }

    /// Escape `</script`, `</style`, and `<!--` so the output is safe inside a `<script>`
    /// or `<style>` element.
    #[must_use]
    pub fn is_script_context(mut self, value: bool) -> Self {
        self.is_script_context = value;
        self
    }

    /// Produce JSON-compatible output (double quotes, wrapped, `\uXXXX` escapes only).
    #[must_use]
    pub fn json(mut self, value: bool) -> Self {
        self.json = value;
        self
    }

    /// Emit hexadecimal digits in lowercase.
    #[must_use]
    pub fn lowercase_hex(mut self, value: bool) -> Self {
        self.lowercase_hex = value;
        self
    }

    fn resolved_quotes(&self) -> Quotes {
        self.quotes.unwrap_or(if self.json {
            Quotes::Double
        } else {
            Quotes::Single
        })
    }

    fn resolved_wrap(&self) -> bool {
        self.wrap.unwrap_or(self.json)
    }
}

/// Escape `string` for embedding in JavaScript source, using the default options.
///
/// ```
/// # use jsesc::jsesc;
/// assert_eq!(jsesc("a\tb"), "a\\tb");
/// ```
#[must_use]
pub fn jsesc(string: &str) -> String {
    jsesc_with(string, &Options::new())
}

/// Escape `string` with the given [`Options`].
#[must_use]
pub fn jsesc_with(string: &str, options: &Options) -> String {
    let quote = match options.resolved_quotes() {
        Quotes::Single => '\'',
        Quotes::Double => '"',
        Quotes::Backtick => '`',
    };

    let units: Vec<u16> = string.encode_utf16().collect();
    let mut result = String::with_capacity(string.len());

    let mut index = 0;
    while index < units.len() {
        let unit = units[index];

        if is_high_surrogate(unit) && index + 1 < units.len() && is_low_surrogate(units[index + 1])
        {
            let low = units[index + 1];
            let code_point =
                (u32::from(unit) - 0xD800) * 0x400 + (u32::from(low) - 0xDC00) + 0x1_0000;
            if options.minimal {
                push_scalar(&mut result, code_point);
            } else if options.es6 {
                result.push_str(&format!(
                    "\\u{{{}}}",
                    hex(code_point, options.lowercase_hex)
                ));
            } else {
                result.push_str(&four_hex_escape(unit, options.lowercase_hex));
                result.push_str(&four_hex_escape(low, options.lowercase_hex));
            }
            index += 2;
            continue;
        }

        if is_surrogate(unit) {
            // Lone surrogate.
            result.push_str(&four_hex_escape(unit, options.lowercase_hex));
            index += 1;
            continue;
        }

        let escape = options.escape_everything || is_quote(unit) || !is_safe(unit);
        if escape {
            let next = units.get(index + 1).copied();
            result.push_str(&escape_unit(unit, next, options, quote));
        } else {
            push_scalar(&mut result, u32::from(unit));
        }
        index += 1;
    }

    if quote == '`' {
        result = result.replace("${", "\\${");
    }
    if options.is_script_context {
        result = escape_script_tags(&result);
        let comment = if options.json {
            "\\u003C!--"
        } else {
            "\\x3C!--"
        };
        result = result.replace("<!--", comment);
    }
    if options.resolved_wrap() {
        let mut wrapped = String::with_capacity(result.len() + 2);
        wrapped.push(quote);
        wrapped.push_str(&result);
        wrapped.push(quote);
        result = wrapped;
    }
    result
}

fn is_high_surrogate(unit: u16) -> bool {
    (0xD800..=0xDBFF).contains(&unit)
}

fn is_low_surrogate(unit: u16) -> bool {
    (0xDC00..=0xDFFF).contains(&unit)
}

fn is_surrogate(unit: u16) -> bool {
    (0xD800..=0xDFFF).contains(&unit)
}

fn is_quote(unit: u16) -> bool {
    matches!(unit, 0x22 | 0x27 | 0x60)
}

/// The characters jsesc leaves untouched in the default (non-`escapeEverything`) mode:
/// `[ !#-&(-[]-_a-~]`.
fn is_safe(unit: u16) -> bool {
    matches!(unit, 0x20 | 0x21 | 0x23..=0x26 | 0x28..=0x5B | 0x5D..=0x5F | 0x61..=0x7E)
}

/// Whitespace that `minimal` mode still escapes (`regexWhitespace` in the reference).
fn is_special_whitespace(unit: u16) -> bool {
    matches!(
        unit,
        0xA0 | 0x1680 | 0x2000..=0x200A | 0x2028 | 0x2029 | 0x202F | 0x205F | 0x3000
    )
}

fn hex(code: u32, lowercase: bool) -> String {
    if lowercase {
        format!("{code:x}")
    } else {
        format!("{code:X}")
    }
}

fn four_hex_escape(unit: u16, lowercase: bool) -> String {
    format!("\\u{:0>4}", hex(u32::from(unit), lowercase))
}

/// Push the scalar value (a non-surrogate code point) as a `char`.
fn push_scalar(result: &mut String, code: u32) {
    if let Some(c) = char::from_u32(code) {
        result.push(c);
    }
}

fn escape_unit(unit: u16, next: Option<u16>, options: &Options, quote: char) -> String {
    // `\0` is escaped as `\0` unless it is followed by a digit (which would form `\01`), or
    // in JSON mode.
    if unit == 0 && !options.json && !matches!(next, Some(0x30..=0x39)) {
        return "\\0".to_string();
    }

    if is_quote(unit) {
        let character = char::from_u32(u32::from(unit)).unwrap_or('"');
        if character == quote || options.escape_everything {
            return format!("\\{character}");
        }
        return character.to_string();
    }

    if let Some(single) = single_escape(unit) {
        return single.to_string();
    }

    if options.minimal && !is_special_whitespace(unit) {
        let mut out = String::new();
        push_scalar(&mut out, u32::from(unit));
        return out;
    }

    let hex = hex(u32::from(unit), options.lowercase_hex);
    if options.json || hex.len() > 2 {
        return format!("\\u{hex:0>4}");
    }
    format!("\\x{hex:0>2}")
}

fn single_escape(unit: u16) -> Option<&'static str> {
    match unit {
        0x5C => Some("\\\\"),
        0x08 => Some("\\b"),
        0x0C => Some("\\f"),
        0x0A => Some("\\n"),
        0x0D => Some("\\r"),
        0x09 => Some("\\t"),
        _ => None,
    }
}

/// Insert a backslash before the `/` of `</script` / `</style` (case-insensitive), matching
/// the reference's `/<\/(script|style)/gi` → `<\/$1`.
fn escape_script_tags(string: &str) -> String {
    let mut out = String::with_capacity(string.len());
    let mut index = 0;
    while index < string.len() {
        let rest = &string[index..];
        if let Some(after) = rest.strip_prefix("</") {
            if starts_with_ignore_ascii_case(after, "script")
                || starts_with_ignore_ascii_case(after, "style")
            {
                out.push_str("<\\/");
                index += 2;
                continue;
            }
        }
        let character = rest.chars().next().expect("non-empty remainder");
        out.push(character);
        index += character.len_utf8();
    }
    out
}

fn starts_with_ignore_ascii_case(haystack: &str, prefix: &str) -> bool {
    haystack.len() >= prefix.len()
        && haystack.as_bytes()[..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults() {
        assert_eq!(jsesc("café"), "caf\\xE9");
        assert_eq!(jsesc("foo'bar"), "foo\\'bar");
        assert_eq!(jsesc("\t\n\\"), "\\t\\n\\\\");
        assert_eq!(jsesc("plain ASCII!"), "plain ASCII!");
    }

    #[test]
    fn quotes_and_wrap() {
        assert_eq!(
            jsesc_with("a\"b", &Options::new().quotes(Quotes::Double)),
            "a\\\"b"
        );
        assert_eq!(
            jsesc_with("x`${y}", &Options::new().quotes(Quotes::Backtick)),
            "x\\`\\${y}"
        );
        assert_eq!(jsesc_with("hi", &Options::new().wrap(true)), "'hi'");
        assert_eq!(jsesc_with("hi", &Options::new().json(true)), "\"hi\"");
    }

    #[test]
    fn astral_and_es6() {
        assert_eq!(jsesc("\u{1D306}"), "\\uD834\\uDF06");
        assert_eq!(
            jsesc_with("\u{1D306}", &Options::new().es6(true)),
            "\\u{1D306}"
        );
        assert_eq!(
            jsesc_with("\u{1D306}", &Options::new().minimal(true)),
            "\u{1D306}"
        );
    }

    #[test]
    fn nulls() {
        assert_eq!(jsesc("\0"), "\\0");
        assert_eq!(jsesc("\0a"), "\\0a");
        assert_eq!(jsesc("\u{0}1"), "\\x001"); // null followed by a digit
    }

    #[test]
    fn escape_everything_and_minimal() {
        assert_eq!(
            jsesc_with("ab", &Options::new().escape_everything(true)),
            "\\x61\\x62"
        );
        assert_eq!(jsesc_with("café", &Options::new().minimal(true)), "café");
        assert_eq!(jsesc_with("a b", &Options::new().minimal(true)), "a b");
        assert_eq!(
            jsesc_with("café", &Options::new().lowercase_hex(true)),
            "caf\\xe9"
        );
    }

    #[test]
    fn script_context() {
        assert_eq!(
            jsesc_with("</script>", &Options::new().is_script_context(true)),
            "<\\/script>"
        );
        assert_eq!(
            jsesc_with("<!--x", &Options::new().is_script_context(true)),
            "\\x3C!--x"
        );
    }

    #[test]
    fn line_separators_and_control() {
        // U+2028 / U+2029 break JS string literals and must be escaped.
        assert_eq!(jsesc("a\u{2028}b"), "a\\u2028b");
        assert_eq!(jsesc("a\u{2029}b"), "a\\u2029b");
        // A C1 control / high BMP code point uses a four-digit escape.
        assert_eq!(jsesc("\u{0100}"), "\\u0100");
    }
}