[jsinterp] Further improve expression parsing (fix fd8242e)
authordirkf <fieldhouse@gmx.net>
Wed, 10 Jul 2024 17:02:11 +0000 (18:02 +0100)
committerdirkf <fieldhouse@gmx.net>
Wed, 10 Jul 2024 23:50:46 +0000 (00:50 +0100)
Passes tests from yt-dlp

youtube_dl/jsinterp.py

index 799497acb35594ed6d1ff2b785477b1058001216..a2074a91e3c447f7661e5ecf5e3e5cb6bad6a797 100644 (file)
@@ -804,16 +804,19 @@ class JSInterpreter(object):
             if op in ('+', '-'):
                 # simplify/adjust consecutive instances of these operators
                 undone = 0
-                while len(separated) > 1 and not separated[-1].strip():
+                separated = [s.strip() for s in separated]
+                while len(separated) > 1 and not separated[-1]:
                     undone += 1
                     separated.pop()
                 if op == '-' and undone % 2 != 0:
                     right_expr = op + right_expr
                 elif op == '+':
-                    while len(separated) > 1 and separated[-1].strip() in self.OP_CHARS:
+                    while len(separated) > 1 and set(separated[-1]) <= self.OP_CHARS:
+                        right_expr = separated.pop() + right_expr
+                    if separated[-1][-1:] in self.OP_CHARS:
                         right_expr = separated.pop() + right_expr
                 # hanging op at end of left => unary + (strip) or - (push right)
-                left_val = separated[-1]
+                left_val = separated[-1] if separated else ''
                 for dm_op in ('*', '%', '/', '**'):
                     bodmas = tuple(self._separate(left_val, dm_op, skip_delims=skip_delim))
                     if len(bodmas) > 1 and not bodmas[-1].strip():