| 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. 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 lineterminator 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 |
LineTerminator :: |
| 26 |
<LF> |
| 27 |
<CR> |
| 28 |
<LS> |
| 29 |
<PS> |
| 30 |
|
| 31 |
Exceptions: |
| 32 |
|
| 33 |
2. If c is the code point listed in some cell of the “Code Point” column of Table 64, then |
| 34 |
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. |
| 35 |
|
| 36 |
ControlEscape, Numeric Value, Code Point, Unicode Name, Symbol |
| 37 |
t 9 U+0009 CHARACTER TABULATION <HT> |
| 38 |
n 10 U+000A LINE FEED (LF) <LF> |
| 39 |
v 11 U+000B LINE TABULATION <VT> |
| 40 |
f 12 U+000C FORM FEED (FF) <FF> |
| 41 |
r 13 U+000D CARRIAGE RETURN (CR) <CR> |
| 42 |
features: [RegExp.escape] |
| 43 |
---*/ |
| 44 |
|
| 45 |
assert.sameValue(RegExp.escape('\u2028'), '\\u2028', 'line terminator \\u2028 is escaped correctly to \\u2028'); |
| 46 |
assert.sameValue(RegExp.escape('\u2029'), '\\u2029', 'line terminator \\u2029 is escaped correctly to \\u2029'); |
| 47 |
|
| 48 |
assert.sameValue(RegExp.escape('\u2028\u2029'), '\\u2028\\u2029', 'line terminators are escaped correctly'); |
| 49 |
assert.sameValue(RegExp.escape('\u2028a\u2029a'), '\\u2028a\\u2029a', 'mixed line terminators are escaped correctly'); |
| 50 |
|
| 51 |
reportCompare(0, 0); |