0
0
Fork 0

[utils] Handle single-line comments in js_to_json

main
Michal Čihař 2017-01-31 07:54:53 +01:00 committed by Sergey M․
parent a22b2fd19b
commit b3ee552e4b
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D
2 changed files with 5 additions and 2 deletions

View File

@ -791,6 +791,9 @@ class TestUtil(unittest.TestCase):
on = js_to_json('{ 0: /* " \n */ ",]" , }') on = js_to_json('{ 0: /* " \n */ ",]" , }')
self.assertEqual(json.loads(on), {'0': ',]'}) self.assertEqual(json.loads(on), {'0': ',]'})
on = js_to_json('{ 0: // comment\n1 }')
self.assertEqual(json.loads(on), {'0': 1})
on = js_to_json(r'["<p>x<\/p>"]') on = js_to_json(r'["<p>x<\/p>"]')
self.assertEqual(json.loads(on), ['<p>x</p>']) self.assertEqual(json.loads(on), ['<p>x</p>'])

View File

@ -2107,7 +2107,7 @@ def js_to_json(code):
v = m.group(0) v = m.group(0)
if v in ('true', 'false', 'null'): if v in ('true', 'false', 'null'):
return v return v
elif v.startswith('/*') or v == ',': elif v.startswith('/*') or v.startswith('//') or v == ',':
return "" return ""
if v[0] in ("'", '"'): if v[0] in ("'", '"'):
@ -2134,7 +2134,7 @@ def js_to_json(code):
return re.sub(r'''(?sx) return re.sub(r'''(?sx)
"(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"| "(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"|
'(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^'\\]*'| '(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^'\\]*'|
/\*.*?\*/|,(?=\s*[\]}])| /\*.*?\*/|//[^\n]*|,(?=\s*[\]}])|
[a-zA-Z_][.a-zA-Z_0-9]*| [a-zA-Z_][.a-zA-Z_0-9]*|
\b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:\s*:)?| \b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:\s*:)?|
[0-9]+(?=\s*:) [0-9]+(?=\s*:)