From c843e685884ccc1a5186693d6450a34232b4377d Mon Sep 17 00:00:00 2001 From: felix Date: Tue, 13 Jul 2021 09:18:20 +0200 Subject: [PATCH] [utils] Improve `js_to_json` comment regex Capture the newline character as part of a single-line comment From #497, Authored by: fstirlitz --- test/test_utils.py | 3 +++ yt_dlp/utils.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_utils.py b/test/test_utils.py index 0067e1ec9..4bfe250ac 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1054,6 +1054,9 @@ class TestUtil(unittest.TestCase): on = js_to_json('{ "040": "040" }') self.assertEqual(json.loads(on), {'040': '040'}) + on = js_to_json('[1,//{},\n2]') + self.assertEqual(json.loads(on), [1, 2]) + def test_js_to_json_malformed(self): self.assertEqual(js_to_json('42a1'), '42"a1"') self.assertEqual(js_to_json('42a-1'), '42"a"-1') diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index 8f9cb46f6..795c5632f 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -4365,7 +4365,7 @@ def strip_jsonp(code): def js_to_json(code, vars={}): # vars is a dict of var, val pairs to substitute - COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*' + COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n' SKIP_RE = r'\s*(?:{comment})?\s*'.format(comment=COMMENT_RE) INTEGER_TABLE = ( (r'(?s)^(0[xX][0-9a-fA-F]+){skip}:?$'.format(skip=SKIP_RE), 16),