[utils] Handle single-line comments in js_to_json
authorMichal Čihař <michal@cihar.com>
Tue, 31 Jan 2017 06:54:53 +0000 (07:54 +0100)
committerSergey M․ <dstftw@gmail.com>
Thu, 2 Feb 2017 20:04:33 +0000 (03:04 +0700)
test/test_utils.py
youtube_dl/utils.py

index a74d59f3430c86d1577f0607b0191be27edd3fb8..954bb7d8b8192ac821268f98a05027ca005cd217 100644 (file)
@@ -791,6 +791,9 @@ class TestUtil(unittest.TestCase):
         on = js_to_json('{ 0: /* " \n */ ",]" , }')
         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>"]')
         self.assertEqual(json.loads(on), ['<p>x</p>'])
 
index cf46711b9f5556c01c8a2eba5300dbe5df14ae94..6c462625b47261ca666c486e804c85412a6e8269 100644 (file)
@@ -2107,7 +2107,7 @@ def js_to_json(code):
         v = m.group(0)
         if v in ('true', 'false', 'null'):
             return v
-        elif v.startswith('/*') or v == ',':
+        elif v.startswith('/*') or v.startswith('//') or v == ',':
             return ""
 
         if v[0] in ("'", '"'):
@@ -2134,7 +2134,7 @@ def js_to_json(code):
     return re.sub(r'''(?sx)
         "(?:[^"\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^"\\]*"|
         '(?:[^'\\]*(?:\\\\|\\['"nurtbfx/\n]))*[^'\\]*'|
-        /\*.*?\*/|,(?=\s*[\]}])|
+        /\*.*?\*/|//[^\n]*|,(?=\s*[\]}])|
         [a-zA-Z_][.a-zA-Z_0-9]*|
         \b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:\s*:)?|
         [0-9]+(?=\s*:)