Debian Codegraph @

jsbeautifier/unpackers/urlencode.py

raw
1 #
2 # Trivial bookmarklet/escaped script detector for the javascript beautifier
3 # written by Einar Lielmanis <[email protected]>
4 # rewritten in Python by Stefano Sanfilippo <[email protected]>
5 #
6 # Will always return valid javascript: if `detect()` is false, `code` is
7 # returned, unmodified.
8 #
9 # usage:
10 #
11 # some_string = urlencode.unpack(some_string)
12 #
13
14 """Bookmarklet/escaped script unpacker."""
15
16 # pylint: disable=F0401
17 # pylint: disable=E0611
18 from urllib.parse import unquote_plus
19
20 PRIORITY = 0
21
22
23 def detect(code):
24 """Detects if a scriptlet is urlencoded."""
25 # the fact that script doesn't contain any space, but has %20 instead
26 # should be sufficient check for now.
27 return " " not in code and ("%20" in code or code.count("%") > 3)
28
29
30 def unpack(code):
31 """URL decode `code` source string."""
32 return unquote_plus(code) if detect(code) else code