Debian Codegraph @

js/src/tests/test262/built-ins/RegExp/escape/escaped-whitespace.js

raw
1 // |reftest| shell-option(--enable-regexp-escape) skip-if(!RegExp.escape||!xulRuntime.shell) -- RegExp.escape is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2024 Leo Balter, Jordan Harband. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4
5 /*---
6 esid: sec-regexp.escape
7 description: Escaped WhiteSpace characters (simple assertions)
8 info: |
9 EncodeForRegExpEscape ( c )
10
11 ...
12 3. Let otherPunctuators be the string-concatenation of ",-=<>#&!%:;@~'`" and the code unit 0x0022 (QUOTATION MARK).
13 4. Let toEscape be StringToCodePoints(otherPunctuators).
14 5. If toEscape ..., c is matched by WhiteSpace or LineTerminator, ..., then
15 a. If c ≤ 0xFF, then
16 i. Let hex be Number::toString(𝔽(c), 16).
17 ii. Return the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), "x", and StringPad(hex, 2, "0", START).
18 b. Let escaped be the empty String.
19 c. Let codeUnits be UTF16EncodeCodePoint(c).
20 d. For each code unit cu of codeUnits, do
21 i. Set escaped to the string-concatenation of escaped and UnicodeEscape(cu).
22 e. Return escaped.
23 6. Return UTF16EncodeCodePoint(c).
24
25 WhiteSpace ::
26 <TAB> U+0009 CHARACTER TABULATION
27 <VT> U+000B LINE TABULATION
28 <FF> U+000C FORM FEED (FF)
29 <ZWNBSP> U+FEFF ZERO WIDTH NO-BREAK SPACE
30 <USP>
31
32 U+0020 (SPACE) and U+00A0 (NO-BREAK SPACE) code points are part of <USP>
33 Other USP U+202F NARROW NO-BREAK SPACE
34
35 Exceptions:
36
37 2. If c is the code point listed in some cell of the “Code Point” column of Table 64, then
38 a. Return the string-concatenation of 0x005C (REVERSE SOLIDUS) and the string in the “ControlEscape” column of the row whose “Code Point” column contains c.
39
40 ControlEscape, Numeric Value, Code Point, Unicode Name, Symbol
41 t 9 U+0009 CHARACTER TABULATION <HT>
42 n 10 U+000A LINE FEED (LF) <LF>
43 v 11 U+000B LINE TABULATION <VT>
44 f 12 U+000C FORM FEED (FF) <FF>
45 r 13 U+000D CARRIAGE RETURN (CR) <CR>
46 features: [RegExp.escape]
47 ---*/
48
49 const WhiteSpace = '\uFEFF\u0020\u00A0\u202F';
50
51 assert.sameValue(RegExp.escape('\uFEFF'), '\\ufeff', `whitespace \\uFEFF is escaped correctly to \\uFEFF`);
52 assert.sameValue(RegExp.escape('\u0020'), '\\x20', `whitespace \\u0020 is escaped correctly to \\x20`);
53 assert.sameValue(RegExp.escape('\u00A0'), '\\xa0', `whitespace \\u00A0 is escaped correctly to \\xA0`);
54 assert.sameValue(RegExp.escape('\u202F'), '\\u202f', `whitespace \\u202F is escaped correctly to \\u202F`);
55
56 assert.sameValue(RegExp.escape(WhiteSpace), '\\ufeff\\x20\\xa0\\u202f', `whitespaces are escaped correctly`);
57
58 reportCompare(0, 0);