-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
16 lines (16 loc) · 11 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Remove space in HTML</title>
</head>
<body>
<h1 style="color: #8BC34A; margin: 40px 0 30px ;">Remove space in HTML</h1>
<textarea name="" cols="90" rows="30" id="source" style="border: 1px solid #ccc; margin-right:10px; padding:5px;" placeholder="html goes here"></textarea>
<textarea name="" cols="90" rows="30" id="result" style="border: 1px solid #ccc; padding:5px;"></textarea>
<div><button id="btn" style="font-size: 16px;background-color:#ccc;width: 150px;height: 40px;border: 0px;color:#fff;background-color: #8BC34A;margin-left: 505px;margin-top: 10px;margin-bottom: 20px;">Remove Space</button></div>
<script>
function js_beautify(t,e,i,n){function s(){for(;g.length&&(" "===g[g.length-1]||g[g.length-1]===R);)g.pop()}function r(t){if(t=void 0===t?!0:t,s(),g.length){"\n"===g[g.length-1]&&t||g.push("\n");for(var e=0;n>e;e++)g.push(R)}}function h(){var t=g.length?g[g.length-1]:" ";" "!==t&&"\n"!==t&&t!==R&&g.push(" ")}function _(){g.push(K)}function a(){n++}function o(){n&&n--}function c(){g.length&&g[g.length-1]===R&&g.pop()}function u(t){A.push(N),N=t}function l(){m="DO_BLOCK"===N,N=A.pop()}function T(t,e){for(var i=0;i<e.length;i++)if(e[i]===t)return!0;return!1}function f(){var t=0,e="";do{if(D>=p.length)return["","TK_EOF"];e=p.charAt(D),D+=1,"\n"===e&&(t+=1)}while(T(e,v));if(t>1)for(var i=0;2>i;i++)r(0===i);var n=1===t;if(T(e,C)){if(D<p.length)for(;T(p.charAt(D),C)&&(e+=p.charAt(D),D+=1,D!==p.length););if(D!==p.length&&e.match(/^[0-9]+[Ee]$/)&&"-"===p.charAt(D)){D+=1;var s=f(D);return e+="-"+s[0],[e,"TK_WORD"]}return"in"===e?[e,"TK_OPERATOR"]:[e,"TK_WORD"]}if("("===e||"["===e)return[e,"TK_START_EXPR"];if(")"===e||"]"===e)return[e,"TK_END_EXPR"];if("{"===e)return[e,"TK_START_BLOCK"];if("}"===e)return[e,"TK_END_BLOCK"];if(";"===e)return[e,"TK_END_COMMAND"];if("/"===e){var h="";if("*"===p.charAt(D)){if(D+=1,D<p.length)for(;("*"!==p.charAt(D)||!p.charAt(D+1)||"/"!==p.charAt(D+1))&&D<p.length&&(h+=p.charAt(D),D+=1,D<p.length););return D+=2,["/*"+h+"*/","TK_BLOCK_COMMENT"]}if("/"===p.charAt(D)){for(h=e;"\r"!==p.charAt(D)&&"\n"!==p.charAt(D)&&(h+=p.charAt(D),D+=1,D<p.length););return D+=1,n&&r(),[h,"TK_COMMENT"]}}if("'"===e||'"'===e||"/"===e&&("TK_WORD"===E&&"return"===O||"TK_START_EXPR"===E||"TK_END_BLOCK"===E||"TK_OPERATOR"===E||"TK_EOF"===E||"TK_END_COMMAND"===E)){var _=e,a=!1;if(e="",D<p.length)for(;(a||p.charAt(D)!==_)&&(e+=p.charAt(D),a=a?!1:"\\"===p.charAt(D),D+=1,D<p.length););return D+=1,"TK_END_COMMAND"===E&&r(),[_+e+_,"TK_STRING"]}if(T(e,k)){for(;D<p.length&&T(e+p.charAt(D),k)&&(e+=p.charAt(D),D+=1,D<p.length););return[e,"TK_OPERATOR"]}return[e,"TK_UNKNOWN"]}var p,g,K,E,O,d,N,A,R,v,C,k,D,y,S,L,b,m,P,x;for(i=i||" ",e=e||4,R="";e--;)R+=i;for(p=t,d="",E="TK_START_EXPR",O="",g=[],m=!1,P=!1,x=!1,v="\n\r ".split(""),C="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$".split(""),k="+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |=".split(" "),y="continue,try,throw,return,var,if,switch,case,default,for,while,break,function".split(","),N="BLOCK",A=[N],n=n||0,D=0,S=!1;;){var w=f(D);if(K=w[0],b=w[1],"TK_EOF"===b)break;switch(b){case"TK_START_EXPR":P=!1,u("EXPRESSION"),"TK_END_EXPR"===E||"TK_START_EXPR"===E||("TK_WORD"!==E&&"TK_OPERATOR"!==E?h():T(d,y)&&"function"!==d&&h()),_();break;case"TK_END_EXPR":_(),l();break;case"TK_START_BLOCK":u("do"===d?"DO_BLOCK":"BLOCK"),"TK_OPERATOR"!==E&&"TK_START_EXPR"!==E&&("TK_START_BLOCK"===E?r():h()),_(),a();break;case"TK_END_BLOCK":"TK_START_BLOCK"===E?(s(),o()):(o(),r()),_(),l();break;case"TK_WORD":if(m){h(),_(),h();break}if("case"===K||"default"===K){":"===O?c():(o(),r(),a()),_(),S=!0;break}L="NONE","TK_END_BLOCK"===E?T(K.toLowerCase(),["else","catch","finally"])?(L="SPACE",h()):L="NEWLINE":"TK_END_COMMAND"!==E||"BLOCK"!==N&&"DO_BLOCK"!==N?"TK_END_COMMAND"===E&&"EXPRESSION"===N?L="SPACE":"TK_WORD"===E?L="SPACE":"TK_START_BLOCK"===E?L="NEWLINE":"TK_END_EXPR"===E&&(h(),L="NEWLINE"):L="NEWLINE","TK_END_BLOCK"!==E&&T(K.toLowerCase(),["else","catch","finally"])?r():T(K,y)||"NEWLINE"===L?"else"===O?h():("TK_START_EXPR"!==E&&"="!==O||"function"!==K)&&("TK_WORD"!==E||"return"!==O&&"throw"!==O?"TK_END_EXPR"!==E?"TK_START_EXPR"===E&&"var"===K||":"===O||("if"===K&&"TK_WORD"===E&&"else"===d?h():r()):T(K,y)&&")"!==O&&r():h()):"SPACE"===L&&h(),_(),d=K,"var"===K&&(P=!0,x=!1);break;case"TK_END_COMMAND":_(),P=!1;break;case"TK_STRING":"TK_START_BLOCK"===E||"TK_END_BLOCK"===E?r():"TK_WORD"===E&&h(),_();break;case"TK_OPERATOR":var B=!0,I=!0;if(P&&","!==K&&(x=!0,":"===K&&(P=!1)),":"===K&&S){_(),r();break}if(S=!1,","===K){P?x?(_(),r(),x=!1):(_(),h()):"TK_END_BLOCK"===E?(_(),r()):"BLOCK"===N?(_(),r()):(_(),h());break}"--"===K||"++"===K?";"===O?(B=!0,I=!1):(B=!1,I=!1):"!"===K&&"TK_START_EXPR"===E?(B=!1,I=!1):"TK_OPERATOR"===E?(B=!1,I=!1):"TK_END_EXPR"===E?(B=!0,I=!0):"."===K?(B=!1,I=!1):":"===K&&(B=O.match(/^\d+$/)?!0:!1),B&&h(),_(),I&&h();break;case"TK_BLOCK_COMMENT":r(),_(),r();break;case"TK_COMMENT":h(),_(),r();break;case"TK_UNKNOWN":_()}E=b,O=K}return g.join("")}function style_html(t,e,i){function n(){return this.pos=0,this.token="",this.current_mode="CONTENT",this.tags={parent:"parent1",parentcount:1,parent1:""},this.tag_type="",this.token_text=this.last_token=this.last_text=this.token_type="",this.Utils={whitespace:"\n\r ".split(""),single_token:"br,input,link,meta,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed".split(","),extra_liners:"head,body,/html".split(","),in_array:function(t,e){for(var i=0;i<e.length;i++)if(t===e[i])return!0;return!1}},this.get_content=function(){for(var t="",e=[],i=!1;"<"!==this.input.charAt(this.pos);){if(this.pos>=this.input.length)return e.length?e.join(""):["","TK_EOF"];if(t=this.input.charAt(this.pos),this.pos++,this.line_char_count++,this.Utils.in_array(t,this.Utils.whitespace))e.length&&(i=!0),this.line_char_count--;else{if(i){if(this.line_char_count<this.max_char)e.push(" "),this.line_char_count++;else{e.push("\n");for(var n=0;n<this.indent_level;n++)e.push(this.indent_string);this.line_char_count=0}i=!1}e.push(t)}}return e.length?e.join(""):""},this.get_script=function(){var t="",e=[],i=RegExp("<\/script>","igm");i.lastIndex=this.pos;for(var n=i.exec(this.input),s=n?n.index:this.input.length;this.pos<s;){if(this.pos>=this.input.length)return e.length?e.join(""):["","TK_EOF"];t=this.input.charAt(this.pos),this.pos++,e.push(t)}return e.length?e.join(""):""},this.record_tag=function(t){this.tags[t+"count"]?(this.tags[t+"count"]++,this.tags[t+this.tags[t+"count"]]=this.indent_level):(this.tags[t+"count"]=1,this.tags[t+this.tags[t+"count"]]=this.indent_level),this.tags[t+this.tags[t+"count"]+"parent"]=this.tags.parent,this.tags.parent=t+this.tags[t+"count"]},this.retrieve_tag=function(t){if(this.tags[t+"count"]){for(var e=this.tags.parent;e&&t+this.tags[t+"count"]!==e;)e=this.tags[e+"parent"];e&&(this.indent_level=this.tags[t+this.tags[t+"count"]],this.tags.parent=this.tags[e+"parent"]),delete this.tags[t+this.tags[t+"count"]+"parent"],delete this.tags[t+this.tags[t+"count"]],1==this.tags[t+"count"]?delete this.tags[t+"count"]:this.tags[t+"count"]--}},this.get_tag=function(){var t="",e=[],i=!1;do{if(this.pos>=this.input.length)return e.length?e.join(""):["","TK_EOF"];t=this.input.charAt(this.pos),this.pos++,this.line_char_count++,this.Utils.in_array(t,this.Utils.whitespace)?(i=!0,this.line_char_count--):(("'"===t||'"'===t)&&(e[1]&&"!"===e[1]||(t+=this.get_unformatted(t),i=!0)),"="===t&&(i=!1),e.length&&"="!==e[e.length-1]&&">"!==t&&i&&(this.line_char_count<this.max_char?(e.push(" "),this.line_char_count++):(this.print_newline(!1,e),this.line_char_count=0),i=!1),e.push(t))}while(">"!==t);var n,s=e.join("");n=s.indexOf(-1!=s.indexOf(" ")?" ":">");var r=s.substring(1,n).toLowerCase();if("/"===s.charAt(s.length-2)||this.Utils.in_array(r,this.Utils.single_token))this.tag_type="SINGLE";else if("script"===r)this.record_tag(r),this.tag_type="SCRIPT";else if("style"===r)this.record_tag(r),this.tag_type="STYLE";else if("!"===r.charAt(0))if(-1!=r.indexOf("[if")){if(-1!=s.indexOf("!IE")){var h=this.get_unformatted("-->",s);e.push(h)}this.tag_type="START"}else if(-1!=r.indexOf("[endif"))this.tag_type="END",this.unindent();else if(-1!=r.indexOf("[cdata[")){var h=this.get_unformatted("]]>",s);e.push(h),this.tag_type="SINGLE"}else{var h=this.get_unformatted("-->",s);e.push(h),this.tag_type="SINGLE"}else"/"===r.charAt(0)?(this.retrieve_tag(r.substring(1)),this.tag_type="END"):(this.record_tag(r),this.tag_type="START"),this.Utils.in_array(r,this.Utils.extra_liners)&&this.print_newline(!0,this.output);return e.join("")},this.get_unformatted=function(t,e){if(e&&-1!=e.indexOf(t))return"";var i="",n="",s=!0;do{if(i=this.input.charAt(this.pos),this.pos++,this.Utils.in_array(i,this.Utils.whitespace)){if(!s){this.line_char_count--;continue}if("\n"===i||"\r"===i){n+="\n";for(var r=0;r<this.indent_level;r++)n+=this.indent_string;s=!1,this.line_char_count=0;continue}}n+=i,this.line_char_count++,s=!0}while(-1==n.indexOf(t));return n},this.get_token=function(){var t;if("TK_TAG_SCRIPT"===this.last_token){var e=this.get_script();return"string"!=typeof e?e:(t=js_beautify(e,this.indent_size,this.indent_character,this.indent_level),[t,"TK_CONTENT"])}if("CONTENT"===this.current_mode)return t=this.get_content(),"string"!=typeof t?t:[t,"TK_CONTENT"];if("TAG"===this.current_mode){if(t=this.get_tag(),"string"!=typeof t)return t;var i="TK_TAG_"+this.tag_type;return[t,i]}},this.printer=function(t,e,i,n){this.input=t||"",this.output=[],this.indent_character=e||" ",this.indent_string="",this.indent_size=i||2,this.indent_level=0,this.max_char=n||7e3,this.line_char_count=0;for(var s=0;s<this.indent_size;s++)this.indent_string+=this.indent_character;this.print_newline=function(t,e){if(this.line_char_count=0,e&&e.length){if(!t)for(;this.Utils.in_array(e[e.length-1],this.Utils.whitespace);)e.pop();e.push("\n");for(var i=0;i<this.indent_level;i++)e.push(this.indent_string)}},this.print_token=function(t){this.output.push(t)},this.indent=function(){this.indent_level++},this.unindent=function(){this.indent_level>0&&this.indent_level--}},this}var n,s;s=new n,s.printer(t,i,e);for(var r=!0;;){var h=s.get_token();if(s.token_text=h[0],s.token_type=h[1],"TK_EOF"===s.token_type)break;switch(s.token_type){case"TK_TAG_START":case"TK_TAG_SCRIPT":case"TK_TAG_STYLE":s.print_newline(!1,s.output),s.print_token(s.token_text),s.indent(),s.current_mode="CONTENT";break;case"TK_TAG_END":r&&s.print_newline(!0,s.output),s.print_token(s.token_text),s.current_mode="CONTENT",r=!0;break;case"TK_TAG_SINGLE":s.print_newline(!1,s.output),s.print_token(s.token_text),s.current_mode="CONTENT";break;case"TK_CONTENT":""!==s.token_text&&(r=!1,s.print_token(s.token_text)),s.current_mode="TAG"}s.last_token=s.token_type,s.last_text=s.token_text}return s.output.join("")}var source=document.getElementById("source"),result=document.getElementById("result"),btn=document.getElementById("btn");btn.onclick=function(){var t=source.value;t=style_html(t,1," ",80),t=t.replace(/>(?! )\s+/gm,function(t){return t.replace(/\s+/g,"")}),result.value=t};
</script>
</body>
</html>