]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Support for hex escapes in JavaScript string literals
authorPrzemyslaw Wegrzyn <pwegrzyn@codepainters.com>
Thu, 12 May 2022 22:15:48 +0000 (00:15 +0200)
committerAarni Koskela <akx@iki.fi>
Mon, 31 Oct 2022 10:41:49 +0000 (12:41 +0200)
babel/messages/jslexer.py
tests/messages/test_jslexer.py

index c1c25575cd3136998a31743513eee93a4cb49939..1264b2dbc29fd1693c88fba3bc9b76f99129535e 100644 (file)
@@ -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)
index 35204eee064d0ef5c16bab909329165da63bf209..bd6322e9e9e39dcfcbb3b16b0ffbbacdcdf4ba6a 100644 (file)
@@ -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():