Skip to content
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

Support for hex escapes in JavaScript strings #877

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions babel/messages/jslexer.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
babel.messages.jslexer
~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -27,6 +28,7 @@
line_re = re.compile(r'(\r\n|\n|\r)')
line_join_re = re.compile(r'\\' + line_re.pattern)
uni_escape_re = re.compile(r'[a-fA-F0-9]{1,4}')
hex_escape_re = re.compile(r'[a-fA-F0-9]{1,2}')

Token = namedtuple('Token', 'type value lineno')

Expand Down Expand Up @@ -127,6 +129,17 @@ def unquote_string(string):
else:
add(next_char)

# hex escapes. conversion from 2-digits hex to char is infallible
elif next_char in 'xX':
escaped = hex_escape_re.match(string, escape_pos + 2)
if escaped is not None:
escaped_value = escaped.group()
add(chr(int(escaped_value, 16)))
pos = escape_pos + 2 + len(escaped_value)
continue
else:
add(next_char)

# bogus escape. Just remove the backslash.
else:
add(next_char)
Expand Down
2 changes: 2 additions & 0 deletions tests/messages/test_jslexer.py
Expand Up @@ -4,6 +4,8 @@
def test_unquote():
assert jslexer.unquote_string('""') == ''
assert jslexer.unquote_string(r'"h\u00ebllo"') == u"hëllo"
assert jslexer.unquote_string(r'"h\xebllo"') == u"hëllo"
assert jslexer.unquote_string(r'"\xebb"') == u"ëb"


def test_dollar_in_identifier():
Expand Down