From 5db83941caddd5fef5a9448fbd379a283aba01ad Mon Sep 17 00:00:00 2001 From: bashonly Date: Thu, 27 Mar 2025 14:06:36 -0500 Subject: [PATCH] [jsinterp] Fix assignment to array elements with nested brackets Authored by: bashonly --- test/test_jsinterp.py | 1 + yt_dlp/jsinterp.py | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py index 7b01bc39cd..0bae2b5661 100644 --- a/test/test_jsinterp.py +++ b/test/test_jsinterp.py @@ -118,6 +118,7 @@ class TestJSInterpreter(unittest.TestCase): self._test('function f(){var x = 20; x = 30 + 1; return x;}', 31) self._test('function f(){var x = 20; x += 30 + 1; return x;}', 51) self._test('function f(){var x = 20; x -= 30 + 1; return x;}', -11) + self._test('function f(){var x = 2; var y = ["a", "b"]; y[x%y["length"]]="z"; return y}', ["z", "b"]) @unittest.skip('Not implemented') def test_comments(self): diff --git a/yt_dlp/jsinterp.py b/yt_dlp/jsinterp.py index f2f98135be..b59fb2c615 100644 --- a/yt_dlp/jsinterp.py +++ b/yt_dlp/jsinterp.py @@ -188,6 +188,7 @@ _COMP_OPERATORS = {'===', '!==', '==', '!=', '<=', '>=', '<', '>'} _NAME_RE = r'[a-zA-Z_$][\w$]*' _MATCHING_PARENS = dict(zip(*zip('()', '{}', '[]'))) _QUOTES = '\'"/' +_NESTED_BRACKETS = r'[^[\]]+(?:\[[^[\]]+(?:\[[^\]]+\])?\])?' class JS_Undefined: @@ -606,7 +607,7 @@ class JSInterpreter: m = re.match(fr'''(?x) (?P - (?P{_NAME_RE})(?:\[(?P[^\]]+?)\])?\s* + (?P{_NAME_RE})(?:\[(?P{_NESTED_BRACKETS})\])?\s* (?P{"|".join(map(re.escape, set(_OPERATORS) - _COMP_OPERATORS))})? =(?!=)(?P.*)$ )|(?P @@ -614,11 +615,8 @@ class JSInterpreter: )|(?P (?P{_NAME_RE})(?: (?P\?)?\.(?P[^(]+)| - \[(?P[^[\]]+ - (?:\[[^[\]]+ - (?:\[[^\]]+\])?\] - )?) - \])\s* + \[(?P{_NESTED_BRACKETS})\] + )\s* )|(?P (?P{_NAME_RE})\[(?P.+)\]$ )|(?P