diff --git a/test/test_utils.py b/test/test_utils.py index 782cb403e6..274eded088 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1273,6 +1273,11 @@ class TestUtil(unittest.TestCase): on = js_to_json('[//comment\n"abc" //comment\n,//comment\n"def",//comment\n]') self.assertEqual(json.loads(on), ['abc', 'def']) + self.assertEqual(json.loads(js_to_json('[1]//comment')), [1]) + for newline in ('\n', '\r\n', '\r', '\u2028', '\u2029'): + with self.subTest(newline=newline): + self.assertEqual(json.loads(js_to_json(f'[1,//comment{newline}2,\n3]')), [1, 2, 3]) + on = js_to_json('{"abc": "def",}') self.assertEqual(json.loads(on), {'abc': 'def'}) diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index 01434d907b..2eb38f6347 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -2769,7 +2769,7 @@ def js_to_json(code, vars={}, *, strict=False): # vars is a dict of var, val pairs to substitute STRING_QUOTES = '\'"`' STRING_RE = '|'.join(rf'{q}(?:\\.|[^\\{q}])*{q}' for q in STRING_QUOTES) - COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n' + COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\r\n\u2028\u2029]*(?:\r\n|[\r\n\u2028\u2029]|$)' SKIP_RE = fr'\s*(?:{COMMENT_RE})?\s*' INTEGER_TABLE = ( (fr'(?s)^(0[xX][0-9a-fA-F]+){SKIP_RE}:?$', 16),