Skip to content

Commit

Permalink
[utils] Skip ! prefixed code in js_to_json
Browse files Browse the repository at this point in the history
  • Loading branch information
dstftw committed Nov 16, 2020
1 parent 6d3bdcf commit fe07e78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 22 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,28 @@ def test_js_to_json_edgecases(self):
self.assertEqual(d['x'], 1)
self.assertEqual(d['y'], 'a')

# Just drop ! prefix for now though this results in a wrong value
on = js_to_json('''{
a: !0,
b: !1,
c: !!0,
d: !!42.42,
e: !!![],
f: !"abc",
g: !"",
!42: 42
}''')
self.assertEqual(json.loads(on), {
'a': 0,
'b': 1,
'c': 0,
'd': 42.42,
'e': [],
'f': "abc",
'g': "",
'42': 42
})

on = js_to_json('["abc", "def",]')
self.assertEqual(json.loads(on), ['abc', 'def'])

Expand Down
5 changes: 3 additions & 2 deletions youtube_dl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4078,7 +4078,7 @@ def fix_kv(m):
v = m.group(0)
if v in ('true', 'false', 'null'):
return v
elif v.startswith('/*') or v.startswith('//') or v == ',':
elif v.startswith('/*') or v.startswith('//') or v.startswith('!') or v == ',':
return ""

if v[0] in ("'", '"'):
Expand All @@ -4103,7 +4103,8 @@ def fix_kv(m):
{comment}|,(?={skip}[\]}}])|
(?:(?<![0-9])[eE]|[a-df-zA-DF-Z_])[.a-zA-Z_0-9]*|
\b(?:0[xX][0-9a-fA-F]+|0+[0-7]+)(?:{skip}:)?|
[0-9]+(?={skip}:)
[0-9]+(?={skip}:)|
!+
'''.format(comment=COMMENT_RE, skip=SKIP_RE), fix_kv, code)


Expand Down

0 comments on commit fe07e78

Please sign in to comment.