From 0e4dc0b04154d989a81845c4fb77b4eff7bf5c14 Mon Sep 17 00:00:00 2001 From: doe1080 <98906116+doe1080@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:14:19 +0900 Subject: [PATCH] [utils] `js_to_json`: Fix line comment removal (#17845) Authored by: doe1080 --- test/test_utils.py | 5 +++++ yt_dlp/utils/_utils.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) 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),