aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUjjwal Verma <ujjwalverma1111@gmail.com>2017-09-03 01:20:30 +0530
committerMaximilian Hils <git@maximilianhils.com>2017-09-03 21:02:29 +0200
commitb05bf588e68ba8be08e27ccdf40f97a59b17250a (patch)
treef10742d63960d09c8e5973f687fea3cd75fcef83
parent781369a3269c05d40fa8d37cfc0eae0401558d8d (diff)
downloadmitmproxy-b05bf588e68ba8be08e27ccdf40f97a59b17250a.tar.gz
mitmproxy-b05bf588e68ba8be08e27ccdf40f97a59b17250a.tar.bz2
mitmproxy-b05bf588e68ba8be08e27ccdf40f97a59b17250a.zip
JS beautifier
-rw-r--r--mitmproxy/contentviews/javascript.py49
-rw-r--r--test/mitmproxy/contentviews/test_js_data/simple-formatted.js68
-rw-r--r--test/mitmproxy/contentviews/test_js_data/simple.js8
3 files changed, 120 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)
diff --git a/test/mitmproxy/contentviews/test_js_data/simple-formatted.js b/test/mitmproxy/contentviews/test_js_data/simple-formatted.js
new file mode 100644
index 00000000..7ffd7b08
--- /dev/null
+++ b/test/mitmproxy/contentviews/test_js_data/simple-formatted.js
@@ -0,0 +1,68 @@
+/* _GlobalPrefix_ */
+this.gbar_=this.gbar_||{};
+(function(_) {
+ var window=this;
+
+/* _Module_:sy25 */
+ try {
+ var Mn=function(){};
+ _.y(Mn,Error);
+ _.Nn=function() {
+ this.b="pending";
+ this.B=[];
+ this.w=this.C=void 0
+ };
+ _.fe(_.Nn);
+ var On=function() {
+ _.qa.call(this,"Multiple attempts to set the state of this Result")
+ };
+ _.y(On,_.qa);
+ _.Nn.prototype.ta=function() {
+ return this.C
+ };
+ _.Pn=function(a,c,d) {
+ "pending"==a.b?a.B.push( {
+ hb:c,scope:d||null
+ }
+ ):c.call(d,a)
+ };
+ _.Nn.prototype.A=function(a) {
+ if("pending"==this.b)this.C=a,this.b="success",Qn(this);
+ else if(!Rn(this))throw new On;
+ };
+ _.Nn.prototype.o=function(a) {
+ if("pending"==this.b)this.w=a,this.b="error",Qn(this);
+ else if(!Rn(this))throw new On;
+ };
+ var Qn=function(a) {
+ var c=a.B;
+ a.B=[];
+ for(var d=0;d<c.length;d++) {
+ var e=c[d];
+ e.hb.call(e.scope,a)
+ }
+
+ };
+ _.Nn.prototype.cancel=function() {
+ return"pending"==this.b?(this.o(new Mn),!0):!1
+ };
+ var Rn=function(a) {
+ return"error"==a.b&&a.w instanceof Mn
+ };
+ _.Nn.prototype.then=function(a,c,d) {
+ var e,f,g=new _.ie(function(a,c) {
+ e=a;
+ f=c
+ }
+ );
+ _.Pn(this,function(a) {
+ Rn(a)?g.cancel():"success"==a.b?e(a.ta()):"error"==a.b&&f(a.w)
+ }
+ );
+ return g.then(a,c,d)
+ };
+
+ }
+ catch(e) {
+ _._DumpException(e)
+ }
diff --git a/test/mitmproxy/contentviews/test_js_data/simple.js b/test/mitmproxy/contentviews/test_js_data/simple.js
new file mode 100644
index 00000000..877dcf8d
--- /dev/null
+++ b/test/mitmproxy/contentviews/test_js_data/simple.js
@@ -0,0 +1,8 @@
+/* _GlobalPrefix_ */
+this.gbar_=this.gbar_||{};(function(_){var window=this;
+/* _Module_:sy25 */
+try{
+var Mn=function(){};_.y(Mn,Error);_.Nn=function(){this.b="pending";this.B=[];this.w=this.C=void 0};_.fe(_.Nn);var On=function(){_.qa.call(this,"Multiple attempts to set the state of this Result")};_.y(On,_.qa);_.Nn.prototype.ta=function(){return this.C};_.Pn=function(a,c,d){"pending"==a.b?a.B.push({hb:c,scope:d||null}):c.call(d,a)};_.Nn.prototype.A=function(a){if("pending"==this.b)this.C=a,this.b="success",Qn(this);else if(!Rn(this))throw new On;};
+_.Nn.prototype.o=function(a){if("pending"==this.b)this.w=a,this.b="error",Qn(this);else if(!Rn(this))throw new On;};var Qn=function(a){var c=a.B;a.B=[];for(var d=0;d<c.length;d++){var e=c[d];e.hb.call(e.scope,a)}};_.Nn.prototype.cancel=function(){return"pending"==this.b?(this.o(new Mn),!0):!1};var Rn=function(a){return"error"==a.b&&a.w instanceof Mn}; _.Nn.prototype.then=function(a,c,d){var e,f,g=new _.ie(function(a,c){e=a;f=c});_.Pn(this,function(a){Rn(a)?g.cancel():"success"==a.b?e(a.ta()):"error"==a.b&&f(a.w)});return g.then(a,c,d)};
+
+}catch(e){_._DumpException(e)} \ No newline at end of file