Skip to content

A feature-rich command-line audio/video downloader

[jsinterp] Support string concatenation with `+` and `+=` (#15990)

bashonlyd108ca10b926
diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py
index df273b41f0..38240229ed 100644
--- a/test/test_jsinterp.py
+++ b/test/test_jsinterp.py
@@ -109,6 +109,14 @@ class TestJSInterpreter(unittest.TestCase):
self._test('function f(){return -12616 ^ 5041}', -8951)
self._test('function f(){return 21 << 4294967297}', 42)
+ def test_string_concat(self):
+ self._test('function f(){return "a" + "b";}', 'ab')
+ self._test('function f(){let x = "a"; x += "b"; return x;}', 'ab')
+ self._test('function f(){return "a" + 1;}', 'a1')
+ self._test('function f(){let x = "a"; x += 1; return x;}', 'a1')
+ self._test('function f(){return 2 + "b";}', '2b')
+ self._test('function f(){let x = 2; x += "b"; return x;}', '2b')
+
def test_array_access(self):
self._test('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}', [5, 2, 7])
diff --git a/yt_dlp/jsinterp.py b/yt_dlp/jsinterp.py
index 362e5a4380..5104f6354d 100644
--- a/yt_dlp/jsinterp.py
+++ b/yt_dlp/jsinterp.py
@@ -376,6 +376,10 @@ class JSInterpreter:
if not _OPERATORS.get(op):
return right_val
+ # TODO: This is only correct for str+str and str+number; fix for str+array, str+object, etc
+ if op == '+' and (isinstance(left_val, str) or isinstance(right_val, str)):
+ return f'{left_val}{right_val}'
+
try:
return _OPERATORS[op](left_val, right_val)
except Exception as e: