| 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 other punctuators characters |
| 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 contains c, (...), 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 |
codePoints |
| 26 |
0x002c , |
| 27 |
0x002d - |
| 28 |
0x003d = |
| 29 |
0x003c < |
| 30 |
0x003e > |
| 31 |
0x0023 # |
| 32 |
0x0026 & |
| 33 |
0x0021 ! |
| 34 |
0x0025 % |
| 35 |
0x003a : |
| 36 |
0x003b ; |
| 37 |
0x0040 @ |
| 38 |
0x007e ~ |
| 39 |
0x0027 ' |
| 40 |
0x0060 ` |
| 41 |
0x0022 " |
| 42 |
features: [RegExp.escape] |
| 43 |
---*/ |
| 44 |
|
| 45 |
const otherPunctuators = ",-=<>#&!%:;@~'`\""; |
| 46 |
|
| 47 |
// Return the string-concatenation of the code unit 0x005C (REVERSE SOLIDUS), "x", and StringPad(hex, 2, "0", START). |
| 48 |
for (const c of otherPunctuators) { |
| 49 |
const expected = `\\x${c.codePointAt(0).toString(16)}`; |
| 50 |
assert.sameValue(RegExp.escape(c), expected, `${c} is escaped correctly`); |
| 51 |
} |
| 52 |
|
| 53 |
const otherPunctuatorsExpected = "\\x2c\\x2d\\x3d\\x3c\\x3e\\x23\\x26\\x21\\x25\\x3a\\x3b\\x40\\x7e\\x27\\x60\\x22"; |
| 54 |
|
| 55 |
assert.sameValue( |
| 56 |
RegExp.escape(otherPunctuators), |
| 57 |
otherPunctuatorsExpected, |
| 58 |
'all other punctuators are escaped correctly' |
| 59 |
); |
| 60 |
|
| 61 |
reportCompare(0, 0); |