Repository navigation
Decode JavaScript Unicode code point escapes during extraction #1394
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
Merged
akx
merged 1 commit into
python-babel:master
from
theman6660:fix/js-unicode-codepoint-escapes
Oct 26, 2026
+49
−0
Merged
Changes from all commits
Commits
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
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,40 @@ | ||
| from io import BytesIO | ||
|
|
||
| import pytest | ||
|
|
||
| from babel.messages.extract import extract | ||
| from babel.messages.jslexer import unquote_string | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('quote', ["'", '"', '`']) | ||
| @pytest.mark.parametrize(('body', 'expected'), [ | ||
| (r'a\u{0}b', 'a\0b'), | ||
| (r'\u{41}', 'A'), | ||
| (r'\u{e9}', 'é'), | ||
| (r'\u{1F600}', '😀'), | ||
| (r'\u{10FFFF}', '\U0010ffff'), | ||
| (r'\u{000000000041}', 'A'), | ||
| ]) | ||
| def test_unicode_codepoint_escapes(quote, body, expected): | ||
| assert unquote_string(quote + body + quote) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('quote', ["'", '"', '`']) | ||
| def test_unicode_codepoint_extraction_uses_runtime_message(quote): | ||
| source = f'gettext({quote}Hello \\u{{1F600}}{quote});\ngettext("next");' | ||
| messages = list(extract('javascript', BytesIO(source.encode()), options={})) | ||
| assert messages == [(1, 'Hello 😀', [], None), (2, 'next', [], None)] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(('body', 'expected'), [ | ||
| (r'\u{1f600}\u{1f680}', '😀🚀'), | ||
| (r'\u00e9\u{1f600}\x21', 'é😀!'), | ||
| (r'\\u{1F600}', r'\u{1F600}'), | ||
| (r'\U{41}', 'U{41}'), | ||
| (r'\u{}', 'u{}'), | ||
| (r'\u{xyz}', 'u{xyz}'), | ||
| (r'\u{110000}', 'u{110000}'), | ||
| (r'\u{41', 'u{41'), | ||
| ]) | ||
| def test_codepoint_escapes_preserve_existing_escape_behavior(body, expected): | ||
| assert unquote_string('"' + body + '"') == expected |
Oops, something went wrong.
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.
Why is this check needed?
EDIT: ah, right, that's basically the maximum for JavaScript. Sure, doesn't hurt here...