-
Notifications
You must be signed in to change notification settings - Fork 903
Implement ES2025 RegExp.escape static method #2183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anivar
wants to merge
2
commits into
mozilla:master
Choose a base branch
from
anivar:es2025-regexp-escape
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
tests/src/test/java/org/mozilla/javascript/tests/es2025/RegExpEscapeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| package org.mozilla.javascript.tests.es2025; | ||
|
|
||
| import org.junit.Test; | ||
| import org.mozilla.javascript.testutils.Utils; | ||
|
|
||
| public class RegExpEscapeTest { | ||
|
|
||
| // Basic functionality | ||
| @Test | ||
| public void testRegExpEscapeExists() { | ||
| String script = "typeof RegExp.escape === 'function'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Syntax characters | ||
| @Test | ||
| public void testEscapeSyntaxCharacters() { | ||
| String script = | ||
| "RegExp.escape('.') === '\\\\.' && " | ||
| + "RegExp.escape('*') === '\\\\*' && " | ||
| + "RegExp.escape('+') === '\\\\+' && " | ||
| + "RegExp.escape('?') === '\\\\?' && " | ||
| + "RegExp.escape('^') === '\\\\^' && " | ||
| + "RegExp.escape('$') === '\\\\$' && " | ||
| + "RegExp.escape('|') === '\\\\|' && " | ||
| + "RegExp.escape('(') === '\\\\(' && " | ||
| + "RegExp.escape(')') === '\\\\)' && " | ||
| + "RegExp.escape('[') === '\\\\[' && " | ||
| + "RegExp.escape(']') === '\\\\]' && " | ||
| + "RegExp.escape('{') === '\\\\{' && " | ||
| + "RegExp.escape('}') === '\\\\}' && " | ||
| + "RegExp.escape('\\\\') === '\\\\\\\\' && " | ||
| + "RegExp.escape('/') === '\\\\/'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Control characters | ||
| @Test | ||
| public void testEscapeControlCharacters() { | ||
| String script = | ||
| "RegExp.escape('\\t') === '\\\\t' && " | ||
| + "RegExp.escape('\\n') === '\\\\n' && " | ||
| + "RegExp.escape('\\v') === '\\\\v' && " | ||
| + "RegExp.escape('\\f') === '\\\\f' && " | ||
| + "RegExp.escape('\\r') === '\\\\r'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Other punctuators (should use hex escapes) | ||
| @Test | ||
| public void testEscapeOtherPunctuators() { | ||
| String script = | ||
| "RegExp.escape(',') === '\\\\x2c' && " | ||
| + "RegExp.escape('-') === '\\\\x2d' && " | ||
| + "RegExp.escape('=') === '\\\\x3d' && " | ||
| + "RegExp.escape('<') === '\\\\x3c' && " | ||
| + "RegExp.escape('>') === '\\\\x3e' && " | ||
| + "RegExp.escape('#') === '\\\\x23' && " | ||
| + "RegExp.escape('&') === '\\\\x26' && " | ||
| + "RegExp.escape('!') === '\\\\x21' && " | ||
| + "RegExp.escape('%') === '\\\\x25' && " | ||
| + "RegExp.escape(':') === '\\\\x3a' && " | ||
| + "RegExp.escape(';') === '\\\\x3b' && " | ||
| + "RegExp.escape('@') === '\\\\x40' && " | ||
| + "RegExp.escape('~') === '\\\\x7e' && " | ||
| + "RegExp.escape(\"'\") === '\\\\x27' && " | ||
| + "RegExp.escape('`') === '\\\\x60' && " | ||
| + "RegExp.escape('\"') === '\\\\x22'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // WhiteSpace characters | ||
| @Test | ||
| public void testEscapeWhiteSpace() { | ||
| String script = | ||
| "RegExp.escape('\\u0020') === '\\\\x20' && " | ||
| + "RegExp.escape('\\u00A0') === '\\\\xa0' && " | ||
| + "RegExp.escape('\\uFEFF') === '\\\\ufeff' && " | ||
| + "RegExp.escape('\\u202F') === '\\\\u202f'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Line terminators | ||
| @Test | ||
| public void testEscapeLineTerminators() { | ||
| String script = | ||
| "RegExp.escape('\\u2028') === '\\\\u2028' && " | ||
| + "RegExp.escape('\\u2029') === '\\\\u2029'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Initial digits/letters are escaped, non-initial are not | ||
| @Test | ||
| public void testNotEscaped() { | ||
| String script = | ||
| "RegExp.escape('abc') === '\\\\x61bc' && " | ||
| + "RegExp.escape('123') === '\\\\x3123' && " | ||
| + "RegExp.escape('_') === '_' && " | ||
| + "RegExp.escape('ABC') === '\\\\x41BC' && " | ||
| + "RegExp.escape('_abc') === '_abc'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Empty string | ||
| @Test | ||
| public void testEmptyString() { | ||
| String script = "RegExp.escape('') === ''"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Mixed string (spaces are escaped, initial H is escaped) | ||
| @Test | ||
| public void testMixedString() { | ||
| String script = | ||
| "RegExp.escape('Hello. How are you?') === '\\\\x48ello\\\\.\\\\x20How\\\\x20are\\\\x20you\\\\?'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Complex example | ||
| @Test | ||
| public void testComplexExample() { | ||
| String script = "RegExp.escape('(*.*)') === '\\\\(\\\\*\\\\.\\\\*\\\\)'"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // TypeError for non-string inputs | ||
| @Test | ||
| public void testTypeErrorNumber() { | ||
| String script = "try { RegExp.escape(123); false; } catch(e) { e instanceof TypeError; }"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeErrorObject() { | ||
| String script = "try { RegExp.escape({}); false; } catch(e) { e instanceof TypeError; }"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeErrorNull() { | ||
| String script = "try { RegExp.escape(null); false; } catch(e) { e instanceof TypeError; }"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTypeErrorUndefined() { | ||
| String script = | ||
| "try { RegExp.escape(undefined); false; } catch(e) { e instanceof TypeError; }"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
|
|
||
| // Practical use case: user input escaping | ||
| @Test | ||
| public void testPracticalUseCase() { | ||
| String script = | ||
| "var userInput = 'example.com';" | ||
| + "var pattern = new RegExp(RegExp.escape(userInput));" | ||
| + "pattern.test('example.com') === true && " | ||
| + "pattern.test('exampleXcom') === false"; | ||
| Utils.assertWithAllModes(true, script); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure that we already have implementations of these checks either elsewhere in the codebase, or in the Java Character class. Can you please check first to see if we can reuse those or make them more generic rather than encode this here? I won't be surprised if we need a few new ones but checks like isWhiteSpace and isDecimalDigit are certainly not unique to this method. Thanks!