|
| 1 | +// Copyright (C) 2024 Leo Balter. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | + |
| 4 | +/*--- |
| 5 | +esid: sec-regexp.escape |
| 6 | +description: Escaped lineterminator characters (simple assertions) |
| 7 | +info: | |
| 8 | + EncodeForRegExpEscape ( c ) |
| 9 | +
|
| 10 | + ... |
| 11 | + 3. Let otherPunctuators be the string-concatenation of ",-=<>#&!%:;@~'`" and the code unit 0x0022 (QUOTATION MARK). |
| 12 | + 4. Let toEscape be StringToCodePoints(otherPunctuators). |
| 13 | + 5. If toEscape ..., c is matched by WhiteSpace or LineTerminator, ..., then |
| 14 | + a. If c ≤ 0xFF, then |
| 15 | + i. Let hex be Number::toString(𝔽(c), 16). |
| 16 | + ii. Return the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), "x", and StringPad(hex, 2, "0", START). |
| 17 | + b. Let escaped be the empty String. |
| 18 | + c. Let codeUnits be UTF16EncodeCodePoint(c). |
| 19 | + d. For each code unit cu of codeUnits, do |
| 20 | + i. Set escaped to the string-concatenation of escaped and UnicodeEscape(cu). |
| 21 | + e. Return escaped. |
| 22 | + 6. Return UTF16EncodeCodePoint(c). |
| 23 | +
|
| 24 | + LineTerminator :: |
| 25 | + <LF> |
| 26 | + <CR> |
| 27 | + <LS> |
| 28 | + <PS> |
| 29 | +
|
| 30 | + Exceptions: |
| 31 | +
|
| 32 | + 2. If c is the code point listed in some cell of the “Code Point” column of Table 64, then |
| 33 | + 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. |
| 34 | +
|
| 35 | + ControlEscape, Numeric Value, Code Point, Unicode Name, Symbol |
| 36 | + t 9 U+0009 CHARACTER TABULATION <HT> |
| 37 | + n 10 U+000A LINE FEED (LF) <LF> |
| 38 | + v 11 U+000B LINE TABULATION <VT> |
| 39 | + f 12 U+000C FORM FEED (FF) <FF> |
| 40 | + r 13 U+000D CARRIAGE RETURN (CR) <CR> |
| 41 | +features: [RegExp.escape] |
| 42 | +---*/ |
| 43 | + |
| 44 | +assert.sameValue(RegExp.escape('\u2028'), '\\u2028', 'line terminator \\u2028 is escaped correctly to \\u2028'); |
| 45 | +assert.sameValue(RegExp.escape('\u2029'), '\\u2029', 'line terminator \\u2029 is escaped correctly to \\u2029'); |
| 46 | + |
| 47 | +assert.sameValue(RegExp.escape('\u2028\u2029'), '\\u2028\\u2029', 'line terminators are escaped correctly'); |
| 48 | +assert.sameValue(RegExp.escape('\u2028a\u2029a'), '\\u2028a\\u2029a', 'mixed line terminators are escaped correctly'); |
0 commit comments