function js_beautify(r,o){function F(){for(;h.length&&(h[h.length-1]===" "||h[h.length-1]===u);)h.pop()}function j(a){if(!(w&&J(d.mode))){a=typeof a==="undefined"?true:a;d.if_line=false;F();if(h.length){if(h[h.length-1]!=="\n"||!a){s=true;h.push("\n")}for(a=0;a<x;a+=1)h.push(u)}}}function n(){var a=" ";if(h.length)a=h[h.length-1];a!==" "&&a!=="\n"&&a!==u&&h.push(" ")}function m(){s=false;h.push(f)}function A(){x+=1}function B(){if(x)x-=1}function K(){h.length&&h[h.length-1]===u&&h.pop()}function Q(){var a= f.split("\n");h.push(a[0]);for(var g=1;g<a.length;g++){j();h.push(" ");h.push(a[g].replace(/^\s+/,""))}}function y(a){d&&C.push(d);d={mode:a,var_line:false,var_line_tainted:false,if_line:false,in_case:false,indentation_baseline:-1}}function L(a){return a==="[EXPRESSION]"||a==="[INDENTED-EXPRESSION]"||a==="(EXPRESSION)"}function J(a){return a==="[EXPRESSION]"||a==="[INDENTED-EXPRESSION]"}function M(){D=d.mode==="DO_BLOCK";if(C.length>0)d=C.pop()}function p(a,g){for(var k=0;k<g.length;k+=1)if(g[k]=== a)return true;return false}function R(){for(var a=0,g=0,k=h.length-1;k>=0;k--)switch(h[k]){case ":":a===0&&g++;break;case "?":if(a===0)if(g===0)return true;else g--;break;case "{":if(a===0)return false;a--;break;case "(":case "[":a--;break;case ")":case "]":case "}":a++;break}}function N(){v=0;if(b>=c.length)return["","TK_EOF"];var a=c.charAt(b);b+=1;var g=w&&J(d.mode);z=false;if(g){for(g=0;p(a,G);){if(a==="\n"){F();h.push("\n");s=true;g=0}else g+=a==="\t"?4:1;if(b>=c.length)return["","TK_EOF"];a= c.charAt(b);b+=1}if(d.indentation_baseline===-1)d.indentation_baseline=g;if(s){for(var k=0;k<x+1;k+=1)h.push(u);if(d.indentation_baseline!==-1)for(k=0;k<g-d.indentation_baseline;k++)h.push(" ")}}else{for(;p(a,G);){if(a==="\n")v+=1;if(b>=c.length)return["","TK_EOF"];a=c.charAt(b);b+=1}if(O)if(v>1)for(k=0;k<v;k+=1){j(k===0);s=true}z=v>0}if(p(a,E)){if(b<c.length)for(;p(c.charAt(b),E);){a+=c.charAt(b);b+=1;if(b===c.length)break}if(b!==c.length&&a.match(/^[0-9]+[Ee]$/)&&(c.charAt(b)==="-"||c.charAt(b)=== "+")){g=c.charAt(b);b+=1;k=N(b);a+=g+k[0];return[a,"TK_WORD"]}if(a==="in")return[a,"TK_OPERATOR"];if(z&&e!=="TK_OPERATOR"&&!d.if_line&&(O||l!=="var"))j();return[a,"TK_WORD"]}if(a==="("||a==="[")return[a,"TK_START_EXPR"];if(a===")"||a==="]")return[a,"TK_END_EXPR"];if(a==="{")return[a,"TK_START_BLOCK"];if(a==="}")return[a,"TK_END_BLOCK"];if(a===";")return[a,"TK_SEMICOLON"];if(a==="/"){g="";if(c.charAt(b)==="*"){b+=1;if(b<c.length)for(;!(c.charAt(b)==="*"&&c.charAt(b+1)&&c.charAt(b+1)==="/")&&b<c.length;){g+= c.charAt(b);b+=1;if(b>=c.length)break}b+=2;return["/*"+g+"*/","TK_BLOCK_COMMENT"]}if(c.charAt(b)==="/"){for(g=a;c.charAt(b)!=="\r"&&c.charAt(b)!=="\n";){g+=c.charAt(b);b+=1;if(b>=c.length)break}b+=1;z&&j();return[g,"TK_COMMENT"]}}if(a==="'"||a==='"'||a==="/"&&(e==="TK_WORD"&&p(l,["return","do"])||e==="TK_START_EXPR"||e==="TK_START_BLOCK"||e==="TK_END_BLOCK"||e==="TK_OPERATOR"||e==="TK_EOF"||e==="TK_SEMICOLON")){g=a;k=false;a=a;if(b<c.length)if(g==="/")for(var H=false;k||H||c.charAt(b)!==g;){a+=c.charAt(b); if(k)k=false;else{k=c.charAt(b)==="\\";if(c.charAt(b)==="[")H=true;else if(c.charAt(b)==="]")H=false}b+=1;if(b>=c.length)return[a,"TK_STRING"]}else for(;k||c.charAt(b)!==g;){a+=c.charAt(b);k=k?false:c.charAt(b)==="\\";b+=1;if(b>=c.length)return[a,"TK_STRING"]}b+=1;a+=g;if(g==="/")for(;b<c.length&&p(c.charAt(b),E);){a+=c.charAt(b);b+=1}return[a,"TK_STRING"]}if(a==="#"){g="#";if(b<c.length&&p(c.charAt(b),P)){do{a=c.charAt(b);g+=a;b+=1}while(b<c.length&&a!=="#"&&a!=="=");return a==="#"?[g,"TK_WORD"]: [g,"TK_OPERATOR"]}}if(a==="<"&&c.substring(b-1,b+3)==="<!--"){b+=3;return["<!--","TK_COMMENT"]}if(a==="-"&&c.substring(b-1,b+2)==="--\>"){b+=2;z&&j();return["--\>","TK_COMMENT"]}if(p(a,I)){for(;b<c.length&&p(a+c.charAt(b),I);){a+=c.charAt(b);b+=1;if(b>=c.length)break}return[a,"TK_OPERATOR"]}return[a,"TK_UNKNOWN"]}var c,h,f,e,l,i,d,C,u,G,E,I,b,P,q,D,x,z,s,v;o=o||{};q=o.indent_size||4;i=o.indent_char||" ";var O=typeof o.preserve_newlines==="undefined"?true:o.preserve_newlines,t=o.indent_level||0,S= o.space_after_anon_function==="undefined"?false:o.space_after_anon_function,w=typeof o.keep_array_indentation==="undefined"?true:o.keep_array_indentation;s=false;for(u="";q>0;){u+=i;q-=1}x=t;c=r;r="";e="TK_START_EXPR";i=l="";h=[];D=false;G="\n\r\t ".split("");E="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$".split("");P="0123456789".split("");I="+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::".split(" ");o="continue,try,throw,return,var,if,switch,case,default,for,while,break,function".split(","); C=[];y("BLOCK");for(b=0;;){q=N(b);f=q[0];q=q[1];if(q==="TK_EOF")break;switch(q){case "TK_START_EXPR":if(f==="["){if(e==="TK_WORD"||l===")"){if(r==="return"||r==="throw")n();y("(EXPRESSION)");m();break}if(d.mode==="[EXPRESSION]"||d.mode==="[INDENTED-EXPRESSION]")if(i==="]"&&l===","){if(d.mode==="[EXPRESSION]"){w||A();d.mode="[INDENTED-EXPRESSION]"}w||j()}else if(l==="["){if(d.mode==="[EXPRESSION]"){w||A();d.mode="[INDENTED-EXPRESSION]"}w||j()}y("[EXPRESSION]")}else y("(EXPRESSION)");if(l===";"||e=== "TK_START_BLOCK")j();else if(!(e==="TK_END_EXPR"||e==="TK_START_EXPR"||e==="TK_END_BLOCK"))if(e!=="TK_WORD"&&e!=="TK_OPERATOR")n();else if(r==="function")S&&n();else p(r,o)&&n();m();break;case "TK_END_EXPR":if(f==="]")if(d.mode==="[INDENTED-EXPRESSION]"){B();l==="]"&&j()}M();m();break;case "TK_START_BLOCK":r==="do"?y("DO_BLOCK"):y("BLOCK");if(e!=="TK_OPERATOR"&&e!=="TK_START_EXPR")e==="TK_START_BLOCK"?j():n();m();A();break;case "TK_END_BLOCK":if(e==="TK_START_BLOCK"){s?K():F();B()}else{B();j()}m(); M();break;case "TK_WORD":if(D){n();m();n();D=false;break}if(f==="function")if((s||l==";")&&l!=="{"){v=s?v:0;for(i=0;i<2-v;i++)j(false)}if(f==="case"||f==="default"){if(l===":")K();else{B();j();A()}m();d.in_case=true;break}i="NONE";if(e==="TK_END_BLOCK")if(p(f.toLowerCase(),["else","catch","finally"])){i="SPACE";n()}else i="NEWLINE";else if(e==="TK_SEMICOLON"&&(d.mode==="BLOCK"||d.mode==="DO_BLOCK"))i="NEWLINE";else if(e==="TK_SEMICOLON"&&L(d.mode))i="SPACE";else if(e==="TK_STRING")i="NEWLINE";else if(e=== "TK_WORD")i="SPACE";else if(e==="TK_START_BLOCK")i="NEWLINE";else if(e==="TK_END_EXPR"){n();i="NEWLINE"}if(e!=="TK_END_BLOCK"&&p(f.toLowerCase(),["else","catch","finally"]))j();else if(p(f,o)||i==="NEWLINE")if(l==="else")n();else{if(!((e==="TK_START_EXPR"||l==="="||l===",")&&f==="function"))if(l==="return"||l==="throw")n();else if(e!=="TK_END_EXPR"){if((e!=="TK_START_EXPR"||f!=="var")&&l!==":")f==="if"&&r==="else"&&l!=="{"?n():j()}else p(f,o)&&l!==")"&&j()}else i==="SPACE"&&n();m();r=f;if(f==="var"){d.var_line= true;d.var_line_tainted=false}if(f==="if"||f==="else")d.if_line=true;break;case "TK_SEMICOLON":m();d.var_line=false;break;case "TK_STRING":if(e==="TK_START_BLOCK"||e==="TK_END_BLOCK"||e==="TK_SEMICOLON")j();else e==="TK_WORD"&&n();m();break;case "TK_OPERATOR":t=i=true;if(d.var_line&&f===","&&L(d.mode))d.var_line_tainted=false;if(d.var_line)if(f===",")if(d.var_line_tainted){m();j();h.push(u);d.var_line_tainted=false;break}else d.var_line_tainted=false;else{d.var_line_tainted=true;if(f===":")d.var_line= false}if(l==="return"||l==="throw"){n();m();break}if(f===":"&&d.in_case){m();j();d.in_case=false;break}if(f==="::"){m();break}if(f===","){if(d.var_line)if(d.var_line_tainted){m();j();d.var_line_tainted=false}else{m();n()}else if(e==="TK_END_BLOCK"){m();j()}else if(d.mode==="BLOCK"){m();j()}else{m();n()}break}else if(f==="--"||f==="++"){if(l===";"){d.mode==="BLOCK"&&j();i=true}else{l==="{"&&j();i=false}t=false}else if((f==="!"||f==="+"||f==="-")&&(l==="return"||l==="case")){i=true;t=false}else if((f=== "!"||f==="+"||f==="-")&&e==="TK_START_EXPR")t=i=false;else if(e==="TK_OPERATOR")t=i=false;else if(e==="TK_END_EXPR")t=i=true;else if(f===".")t=i=false;else if(f===":")i=R()?true:false;i&&n();m();t&&n();break;case "TK_BLOCK_COMMENT":j();f.substring(0,3)=="/**"?Q():m();j();break;case "TK_COMMENT":z?j():n();m();j();break;case "TK_UNKNOWN":m();break}i=l;e=q;l=f}return h.join("").replace(/\n+$/,"")};
