summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrzemyslaw Wegrzyn <pwegrzyn@codepainters.com>2022-05-13 00:15:48 +0200
committerAarni Koskela <akx@iki.fi>2022-10-31 12:41:49 +0200
commitc7d04e8cb7bd60ff305d8bc6df2ce8a3f92c0223 (patch)
tree9592d2554ea7d0db7ac3363b4e20cae0a6af8eea
parent8f5757cc85402a46c1db722f08c18cfa7f119858 (diff)
downloadbabel-c7d04e8cb7bd60ff305d8bc6df2ce8a3f92c0223.tar.gz
Support for hex escapes in JavaScript string literals
-rw-r--r--babel/messages/jslexer.py13
-rw-r--r--tests/messages/test_jslexer.py2
2 files changed, 15 insertions, 0 deletions
diff --git a/babel/messages/jslexer.py b/babel/messages/jslexer.py
index c1c2557..1264b2d 100644
--- a/babel/messages/jslexer.py
+++ b/babel/messages/jslexer.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
"""
babel.messages.jslexer
~~~~~~~~~~~~~~~~~~~~~~
@@ -27,6 +28,7 @@ regex_re = re.compile(r'/(?:[^/\\]*(?:\\.[^/\\]*)*)/[a-zA-Z]*', re.DOTALL)
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')
@@ -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)
diff --git a/tests/messages/test_jslexer.py b/tests/messages/test_jslexer.py
index 35204ee..bd6322e 100644
--- a/tests/messages/test_jslexer.py
+++ b/tests/messages/test_jslexer.py
@@ -4,6 +4,8 @@ from babel.messages import jslexer
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():