diff options
Diffstat (limited to 'mitmproxy/contentviews/javascript.py')
-rw-r--r-- | mitmproxy/contentviews/javascript.py | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/mitmproxy/contentviews/javascript.py b/mitmproxy/contentviews/javascript.py index c2fab875..1d671fe6 100644 --- a/mitmproxy/contentviews/javascript.py +++ b/mitmproxy/contentviews/javascript.py @@ -1,6 +1,47 @@ -import jsbeautifier +import io +import re -from . import base +from mitmproxy.utils import strutils +from mitmproxy.contentviews import base + +DELIMITERS = '{};\n' +SPECIAL_AREAS = ( + r"(?<=[^\w\s)])\s*/(?:[^\n/]|(?<!\\)(?:\\\\)*\\/)+?/(?=[gimsuy]{0,6}\s*(?:[;,).\n]|$))", + r"'" + strutils.MULTILINE_CONTENT_LINE_CONTINUATION + strutils.NO_ESCAPE + "'", + r'"' + strutils.MULTILINE_CONTENT_LINE_CONTINUATION + strutils.NO_ESCAPE + '"', + r'`' + strutils.MULTILINE_CONTENT + strutils.NO_ESCAPE + '`', + r"/\*" + strutils.MULTILINE_CONTENT + "\*/", + r"//" + strutils.SINGLELINE_CONTENT + "$", + r"for\(" + strutils.SINGLELINE_CONTENT + "\)", +) + + +def beautify(data): + data = strutils.escape_special_areas( + data, + SPECIAL_AREAS, + DELIMITERS + ) + + data = re.sub(r"\s*{\s*(?!};)", " {\n", data) + data = re.sub(r"\s*;\s*", ";\n", data) + data = re.sub(r"(?<!{)\s*}(;)?\s*", r"\n}\1\n", data) + + beautified = io.StringIO() + indent_level = 0 + + for line in data.splitlines(True): + if line.endswith("{\n"): + beautified.write(" " * 2 * indent_level + line) + indent_level += 1 + elif line.startswith("}"): + indent_level -= 1 + beautified.write(" " * 2 * indent_level + line) + else: + beautified.write(" " * 2 * indent_level + line) + + data = strutils.unescape_special_areas(beautified.getvalue()) + return data class ViewJavaScript(base.View): @@ -13,8 +54,6 @@ class ViewJavaScript(base.View): ] def __call__(self, data, **metadata): - opts = jsbeautifier.default_options() - opts.indent_size = 2 data = data.decode("utf-8", "replace") - res = jsbeautifier.beautify(data, opts) + res = beautify(data) return "JavaScript", base.format_text(res) |