mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2024-12-22 06:00:00 +00:00
[utils] js_to_json
: Implement template strings (#6623)
Authored by: Grub4K
This commit is contained in:
parent
f68434cc74
commit
0898c5c8cc
2 changed files with 16 additions and 2 deletions
|
@ -1190,6 +1190,13 @@ def test_js_to_json_malformed(self):
|
||||||
self.assertEqual(js_to_json('42a1'), '42"a1"')
|
self.assertEqual(js_to_json('42a1'), '42"a1"')
|
||||||
self.assertEqual(js_to_json('42a-1'), '42"a"-1')
|
self.assertEqual(js_to_json('42a-1'), '42"a"-1')
|
||||||
|
|
||||||
|
def test_js_to_json_template_literal(self):
|
||||||
|
self.assertEqual(js_to_json('`Hello ${name}`', {'name': '"world"'}), '"Hello world"')
|
||||||
|
self.assertEqual(js_to_json('`${name}${name}`', {'name': '"X"'}), '"XX"')
|
||||||
|
self.assertEqual(js_to_json('`${name}${name}`', {'name': '5'}), '"55"')
|
||||||
|
self.assertEqual(js_to_json('`${name}"${name}"`', {'name': '5'}), '"5\\"5\\""')
|
||||||
|
self.assertEqual(js_to_json('`${name}`', {}), '"name"')
|
||||||
|
|
||||||
def test_extract_attributes(self):
|
def test_extract_attributes(self):
|
||||||
self.assertEqual(extract_attributes('<e x="y">'), {'x': 'y'})
|
self.assertEqual(extract_attributes('<e x="y">'), {'x': 'y'})
|
||||||
self.assertEqual(extract_attributes("<e x='y'>"), {'x': 'y'})
|
self.assertEqual(extract_attributes("<e x='y'>"), {'x': 'y'})
|
||||||
|
|
|
@ -3366,7 +3366,7 @@ def strip_jsonp(code):
|
||||||
|
|
||||||
def js_to_json(code, vars={}, *, strict=False):
|
def js_to_json(code, vars={}, *, strict=False):
|
||||||
# vars is a dict of var, val pairs to substitute
|
# vars is a dict of var, val pairs to substitute
|
||||||
STRING_QUOTES = '\'"'
|
STRING_QUOTES = '\'"`'
|
||||||
STRING_RE = '|'.join(rf'{q}(?:\\.|[^\\{q}])*{q}' for q in STRING_QUOTES)
|
STRING_RE = '|'.join(rf'{q}(?:\\.|[^\\{q}])*{q}' for q in STRING_QUOTES)
|
||||||
COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n'
|
COMMENT_RE = r'/\*(?:(?!\*/).)*?\*/|//[^\n]*\n'
|
||||||
SKIP_RE = fr'\s*(?:{COMMENT_RE})?\s*'
|
SKIP_RE = fr'\s*(?:{COMMENT_RE})?\s*'
|
||||||
|
@ -3384,6 +3384,12 @@ def process_escape(match):
|
||||||
else '' if escape == '\n'
|
else '' if escape == '\n'
|
||||||
else escape)
|
else escape)
|
||||||
|
|
||||||
|
def template_substitute(match):
|
||||||
|
evaluated = js_to_json(match.group(1), vars, strict=strict)
|
||||||
|
if evaluated[0] == '"':
|
||||||
|
return json.loads(evaluated)
|
||||||
|
return evaluated
|
||||||
|
|
||||||
def fix_kv(m):
|
def fix_kv(m):
|
||||||
v = m.group(0)
|
v = m.group(0)
|
||||||
if v in ('true', 'false', 'null'):
|
if v in ('true', 'false', 'null'):
|
||||||
|
@ -3394,7 +3400,8 @@ def fix_kv(m):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
if v[0] in STRING_QUOTES:
|
if v[0] in STRING_QUOTES:
|
||||||
escaped = re.sub(r'(?s)(")|\\(.)', process_escape, v[1:-1])
|
v = re.sub(r'(?s)\${([^}]+)}', template_substitute, v[1:-1]) if v[0] == '`' else v[1:-1]
|
||||||
|
escaped = re.sub(r'(?s)(")|\\(.)', process_escape, v)
|
||||||
return f'"{escaped}"'
|
return f'"{escaped}"'
|
||||||
|
|
||||||
for regex, base in INTEGER_TABLE:
|
for regex, base in INTEGER_TABLE:
|
||||||
|
|
Loading…
Reference in a new issue