Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/engine/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,17 @@ impl RegExp {
} else {
let mut s = Vec::with_capacity(src.len());
let mut buf = [0; 2];
let mut escaped = false;
for c in src.code_points() {
// Drop the pattern's own `\` so the code point is not escaped twice.
if escaped
&& matches!(
c,
CodePoint::Unicode('/' | '\n' | '\r' | '\u{2028}' | '\u{2029}')
)
{
s.pop();
}
match c {
CodePoint::Unicode('/') => s.extend_from_slice(utf16!(r"\/")),
CodePoint::Unicode('\n') => s.extend_from_slice(utf16!(r"\n")),
Expand All @@ -808,6 +818,7 @@ impl RegExp {
CodePoint::Unicode(c) => s.extend_from_slice(c.encode_utf16(&mut buf)),
CodePoint::UnpairedSurrogate(surr) => s.push(surr),
}
escaped = !escaped && c == CodePoint::Unicode('\\');
}

JsValue::new(js_string!(&s[..]))
Expand Down
49 changes: 49 additions & 0 deletions core/engine/src/builtins/regexp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,55 @@ fn to_string() {
TestAction::assert_eq(r"/,\;/.toString()", js_string!(r"/,\;/")),
]);
}
#[test]
fn source_escapes_slash() {
run_test_actions([
TestAction::assert_eq(r#"new RegExp("/").source"#, js_string!(r"\/")),
TestAction::assert_eq(r"/a\/b/.source", js_string!(r"a\/b")),
TestAction::assert_eq(r#"new RegExp("\\/").source"#, js_string!(r"\/")),
// The backslash is escaped, the `/` is not.
TestAction::assert_eq(r#"new RegExp("\\\\/").source"#, js_string!(r"\\\/")),
]);
}

#[test]
fn source_escapes_line_terminators() {
run_test_actions([
TestAction::assert_eq(r#"new RegExp("\n").source"#, js_string!(r"\n")),
TestAction::assert_eq(r#"new RegExp("\\\n").source"#, js_string!(r"\n")),
TestAction::assert_eq(r#"new RegExp("\\\r").source"#, js_string!(r"\r")),
TestAction::assert_eq(r#"new RegExp("\\\u2028").source"#, js_string!(r"\u2028")),
TestAction::assert_eq(r#"new RegExp("\\\u2029").source"#, js_string!(r"\u2029")),
// The backslash is escaped, the line terminator is not.
TestAction::assert_eq(r#"new RegExp("\\\\\n").source"#, js_string!(r"\\\n")),
]);
}

#[test]
fn source_round_trips() {
run_test_actions([
TestAction::run(indoc! {r#"
function roundTrips(re, inputs) {
var copies = [
new RegExp(re.source, re.flags),
eval("/" + re.source + "/" + re.flags),
];
return copies.every(function (copy) {
return inputs.every(function (input) {
return copy.test(input) === re.test(input);
});
});
}
"#}),
TestAction::assert(r#"roundTrips(/a\/b/, ["a/b", "a\\/b"])"#),
TestAction::assert(r#"roundTrips(new RegExp("/"), ["/", "\\/"])"#),
TestAction::assert(r#"roundTrips(new RegExp("\\\\/"), ["/", "\\/", "\\"])"#),
TestAction::assert(r#"roundTrips(new RegExp("[/]"),["/", "\\"])"#),
TestAction::assert(r#"roundTrips(new RegExp("\\\n"), ["\n", "\\n"])"#),
TestAction::assert(r#"roundTrips(new RegExp("\\\u2028"), ["\u2028", "\\u2028"])"#),
]);
}

#[test]
fn search() {
const ERROR: &str = "RegExp.prototype[Symbol.search] method called on incompatible value";
Expand Down
Loading