Truly independent web browser
LibRegex: Accept escaped reserved punctuators in v-mode classes
sideshowbarker7d40499c017a
diff --git a/Libraries/LibRegex/Rust/src/parser.rs b/Libraries/LibRegex/Rust/src/parser.rsindex 764f294414e..7d05193aad8 100644--- a/Libraries/LibRegex/Rust/src/parser.rs+++ b/Libraries/LibRegex/Rust/src/parser.rs@@ -1525,56 +1525,9 @@ impl Parser {} else if matches!(c, '(' | ')' | '[' | ']' | '{' | '}' | '/' | '-') {return Err(Error::InvalidCharacterClass);} else if c == '\\' {- let escaped = self.advance().ok_or(Error::LoneTrailingBackslash)?;- current.push(match escaped {- 'n' => '\n',- 'r' => '\r',- 't' => '\t',- 'f' => '\u{0C}',- 'v' => '\u{0B}',- '|' => '|',- '\\' => '\\',- '0' => '\0',- 'b' => '\u{08}',- 'x' => {- let val = self.parse_hex_escape(2)?;- char::from_u32(val).ok_or(Error::InvalidUnicodeEscape)?- }- 'u' => {- let is_braced = self.peek() == Some('{');- let val = self.parse_unicode_escape()?;- // Handle surrogate pairs: \uD800\uDC00- if !is_braced && (0xD800..=0xDBFF).contains(&val) {- let saved = self.pos;- if self.eat('\\') && self.eat('u') && self.peek() != Some('{') {- let low = self.parse_unicode_escape()?;- if (0xDC00..=0xDFFF).contains(&low) {- let combined = 0x10000 + ((val - 0xD800) << 10) + (low - 0xDC00);- char::from_u32(combined).ok_or(Error::InvalidUnicodeEscape)?- } else {- self.pos = saved;- char::from_u32(val).ok_or(Error::InvalidUnicodeEscape)?- }- } else {- self.pos = saved;- char::from_u32(val).ok_or(Error::InvalidUnicodeEscape)?- }- } else {- char::from_u32(val).ok_or(Error::InvalidUnicodeEscape)?- }- }- 'c' => {- let ctrl = self.advance().ok_or(Error::UnexpectedEnd)?;- if ctrl.is_ascii_alphabetic() {- char::from_u32((ctrl as u32) & 0x1F).unwrap()- } else {- return Err(Error::InvalidEscape(ctrl));- }- }- // Only syntax characters and `-` may be escaped in \q{}.- _ if is_syntax_character(escaped) || escaped == '-' => escaped,- _ => return Err(Error::InvalidEscape(escaped)),- });+ // ClassString :: ClassSetCharacter — so \q{} takes the same escapes as a class set operand.+ let value = self.parse_class_set_char_escape()?;+ current.push(char::from_u32(value).ok_or(Error::InvalidUnicodeEscape)?);} else {current.push(c);}@@ -1701,7 +1654,14 @@ impl Parser {}Ok(val)}- _ if is_syntax_character(ch) || ch == '/' || ch == '-' => Ok(ch as u32),+ 'c' => match self.peek() {+ Some(letter) if letter.is_ascii_alphabetic() => {+ self.advance();+ Ok((letter as u32) & 0x1F)+ }+ _ => Err(Error::InvalidEscape(ch)),+ },+ _ if is_syntax_character(ch) || ch == '/' || is_class_set_reserved_punctuator(ch) => Ok(ch as u32),_ => Err(Error::InvalidEscape(ch)),}}@@ -1754,6 +1714,14 @@ fn is_class_set_syntax_character(ch: char) -> bool {matches!(ch, '(' | ')' | '{' | '}' | '/' | '-' | '|')}+/// ClassSetReservedPunctuator :: one of & - ! # % , : ; < = > @ ` ~+fn is_class_set_reserved_punctuator(ch: char) -> bool {+ matches!(+ ch,+ '&' | '-' | '!' | '#' | '%' | ',' | ':' | ';' | '<' | '=' | '>' | '@' | '`' | '~'+ )+}+/// Characters that form ClassSetReservedDoublePunctuator when doubled./// ClassSetReservedDoublePunctuator :: one of && !! ## $$ %% ** ++ ,, .. :: ;; << == >> ?? @@ ^^ `` ~~fn is_class_set_reserved_double_punctuator_char(ch: char) -> bool {diff --git a/Tests/LibJS/Runtime/regexp-unicode-sets-escapes.js b/Tests/LibJS/Runtime/regexp-unicode-sets-escapes.jsnew file mode 100644index 00000000000..86b1a8183f5--- /dev/null+++ b/Tests/LibJS/Runtime/regexp-unicode-sets-escapes.js@@ -0,0 +1,51 @@+describe("escapes in a v-mode class", () => {+ test("every ClassSetReservedPunctuator can be escaped", () => {+ for (const punctuator of "&-!#%,:;<=>@`~") {+ const regex = new RegExp(`[\\${punctuator}]`, "v");+ expect(regex.test(punctuator)).toBeTrue();+ expect(regex.test("a")).toBeFalse();+ }+ });++ test("an escaped punctuator works in ranges and set operations", () => {+ expect(/[\&-\)]/v.test("'")).toBeTrue();+ expect(/[\p{L}--\&]/v.test("a")).toBeTrue();+ expect(/[\p{L}--\&]/v.test("&")).toBeFalse();+ expect(/[\&&&\&]/v.test("&")).toBeTrue();+ expect(/[\!\!]/v.test("!")).toBeTrue();+ });++ test("a control escape", () => {+ expect(/[\cJ]/v.test("\n")).toBeTrue();+ expect(/[\q{\cJ}]/v.test("\n")).toBeTrue();+ });++ test("\\q{} takes the same escapes", () => {+ expect(/[\q{\&}]/v.test("&")).toBeTrue();+ expect(/^[\q{a\!b}]$/v.test("a!b")).toBeTrue();+ expect(/[\q{\/}]/v.test("/")).toBeTrue();+ expect(/[\q{\^}]/v.test("^")).toBeTrue();+ });++ test("other escapes are still syntax errors", () => {+ const cases = [+ ["[\\_]", "invalid escape '\\_'"],+ ['[\\"]', `invalid escape '\\"'`],+ ["[\\']", "invalid escape '\\''"],+ ["[\\c1]", "invalid escape '\\c'"],+ ["[\\q{\\_}]", "invalid escape '\\_'"],+ ["[\\q{\\01}]", "invalid escape '\\0'"],+ ];+ for (const [pattern, message] of cases) {+ expect(() => new RegExp(pattern, "v")).toThrowWithMessage(SyntaxError, message);+ }+ });+});++test("a reserved punctuator still can't be escaped outside a class", () => {+ expect(() => new RegExp("\\&", "v")).toThrowWithMessage(SyntaxError, "invalid escape '\\&'");+});++test("a reserved double punctuator still can't appear unescaped in a class", () => {+ expect(() => new RegExp("[!!]", "v")).toThrowWithMessage(SyntaxError, "invalid character class");+});