/*
  The MIT License (MIT)
  Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
 Style HTML
---------------
  Written by Nochum Sossonko, (nsossonko@hotmail.com)
  Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
    http://jsbeautifier.org/
  Usage:
    style_html(html_source);
    style_html(html_source, options);
  The options are:
    indent_inner_html (default false)  — indent <head> and <body> sections,
    indent_size (default 4)          — indentation size,
    indent_char (default space)      — character to indent with,
    wrap_line_length (default 250)            -  maximum amount of characters per line (0 = disable)
    brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
            put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
    unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted
    content_unformatted (defaults to pre tag) - list of tags, that its content shouldn't be reformatted
    indent_scripts (default normal)  - "keep"|"separate"|"normal"
    preserve_newlines (default true) - whether existing line breaks before elements should be preserved
                                        Only works before elements, not inside tags or for text.
    max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk
    indent_handlebars (default false) - format and indent {{#foo}} and {{/foo}}
    end_with_newline (false)          - end with a newline
    extra_liners (default [head,body,/html]) -List of tags that should have an extra newline before them.
    e.g.
    style_html(html_source, {
      'indent_inner_html': false,
      'indent_size': 2,
      'indent_char': ' ',
      'wrap_line_length': 78,
      'brace_style': 'expand',
      'preserve_newlines': true,
      'max_preserve_newlines': 5,
      'indent_handlebars': false,
      'extra_liners': ['/html']
    });
*/
!function(){function a(a){return a.replace(/^\s+/g,"")}function b(a){return a.replace(/\s+$/g,"")}function c(a,b){var d,c={};for(d in a)d!==b&&(c[d]=a[d]);if(b in a)for(d in a[b])c[d]=a[b][d];return c}function f(f,g,h,i){function E(){function c(a){var b="",c=function(c){var d=b+c.toLowerCase();b=d.length<=a.length?d:d.substr(d.length-a.length,a.length)},d=function(){return b.indexOf(a)===-1};return{add:c,doesNotMatch:d}}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.newlines=0,this.indent_content=k,this.indent_body_inner_html=l,this.indent_head_inner_html=m,this.Utils={whitespace:"\n\r\t ".split(""),single_token:["area","base","br","col","embed","hr","img","input","keygen","link","menuitem","meta","param","source","track","wbr","!doctype","?xml","?php","basefont","isindex"],extra_liners:C,in_array:function(a,b){for(var c=0;c<b.length;c++)if(a===b[c])return!0;return!1}},this.is_whitespace=function(a){for(var b=0;b<a.length;b++)if(!this.Utils.in_array(a.charAt(b),this.Utils.whitespace))return!1;return!0},this.traverse_whitespace=function(){var a="";if(a=this.input.charAt(this.pos),this.Utils.in_array(a,this.Utils.whitespace)){for(this.newlines=0;this.Utils.in_array(a,this.Utils.whitespace);)t&&"\n"===a&&this.newlines<=u&&(this.newlines+=1),this.pos++,a=this.input.charAt(this.pos);return!0}return!1},this.space_or_wrap=function(a){return this.line_char_count>=this.wrap_line_length?(this.print_newline(!1,a),this.print_indentation(a),!0):(this.line_char_count++,a.push(" "),!1)},this.get_content=function(){for(var a="",b=[],c=0;"<"!==this.input.charAt(this.pos)||2===c;){if(this.pos>=this.input.length)return b.length?b.join(""):["","TK_EOF"];if(c<2&&this.traverse_whitespace())this.space_or_wrap(b);else{if(a=this.input.charAt(this.pos),v){if("{"===a?c+=1:c<2&&(c=0),"}"===a&&c>0&&0===c--)break;var d=this.input.substr(this.pos,3);if("{{#"===d||"{{/"===d)break;if("{{!"===d)return[this.get_tag(),"TK_TAG_HANDLEBARS_COMMENT"];if("{{"===this.input.substr(this.pos,2)&&"{{else}}"===this.get_tag(!0))break}this.pos++,this.line_char_count++,b.push(a)}}return b.length?b.join(""):""},this.get_contents_to=function(a){if(this.pos===this.input.length)return["","TK_EOF"];var b="",c=new RegExp("</"+a+"\\s*>","igm");c.lastIndex=this.pos;var d=c.exec(this.input),e=d?d.index:this.input.length;return this.pos<e&&(b=this.input.substring(this.pos,e),this.pos=e),b},this.record_tag=function(a){this.tags[a+"count"]?(this.tags[a+"count"]++,this.tags[a+this.tags[a+"count"]]=this.indent_level):(this.tags[a+"count"]=1,this.tags[a+this.tags[a+"count"]]=this.indent_level),this.tags[a+this.tags[a+"count"]+"parent"]=this.tags.parent,this.tags.parent=a+this.tags[a+"count"]},this.retrieve_tag=function(a){if(this.tags[a+"count"]){for(var b=this.tags.parent;b&&a+this.tags[a+"count"]!==b;)b=this.tags[b+"parent"];b&&(this.indent_level=this.tags[a+this.tags[a+"count"]],this.tags.parent=this.tags[b+"parent"]),delete this.tags[a+this.tags[a+"count"]+"parent"],delete this.tags[a+this.tags[a+"count"]],1===this.tags[a+"count"]?delete this.tags[a+"count"]:this.tags[a+"count"]--}},this.indent_to_tag=function(a){if(this.tags[a+"count"]){for(var b=this.tags.parent;b&&a+this.tags[a+"count"]!==b;)b=this.tags[b+"parent"];b&&(this.indent_level=this.tags[a+this.tags[a+"count"]])}},this.get_tag=function(a){var h,i,j,n,b="",c=[],d="",e=!1,f=!0,g=!1,k=this.pos,l=this.line_char_count,m=!1;a=void 0!==a&&a;do{if(this.pos>=this.input.length)return a&&(this.pos=k,this.line_char_count=l),c.length?c.join(""):["","TK_EOF"];if(b=this.input.charAt(this.pos),this.pos++,this.Utils.in_array(b,this.Utils.whitespace))e=!0;else{if("'"!==b&&'"'!==b||(b+=this.get_unformatted(b),e=!0),"="===b&&(e=!1),n=this.input.substr(this.pos-1),!z||!g||m||">"!==b&&"/"!==b||n.match(/^\/?\s*>/)&&(e=!1,m=!0,this.print_newline(!1,c),this.print_indentation(c)),c.length&&"="!==c[c.length-1]&&">"!==b&&e){var o=this.space_or_wrap(c),p=o&&"/"!==b&&!y;if(e=!1,y&&"/"!==b){var q=!1;if(z&&f){var t=null!==n.match(/^\S*(="([^"]|\\")*")?\s*\/?\s*>/);q=!t}f&&!q||(this.print_newline(!1,c),this.print_indentation(c),p=!0)}if(p){g=!0;var u=x;A&&(u=c.indexOf(" ")+1);for(var w=0;w<u;w++)c.push(" ")}if(f)for(var B=0;B<c.length;B++)if(" "===c[B]){f=!1;break}}if(v&&"<"===j&&b+this.input.charAt(this.pos)==="{{"&&(b+=this.get_unformatted("}}"),c.length&&" "!==c[c.length-1]&&"<"!==c[c.length-1]&&(b=" "+b),e=!0),"<"!==b||j||(h=this.pos-1,j="<"),v&&!j&&c.length>=2&&"{"===c[c.length-1]&&"{"===c[c.length-2]&&(h="#"===b||"/"===b||"!"===b?this.pos-3:this.pos-2,j="{"),this.line_char_count++,c.push(b),c[1]&&("!"===c[1]||"?"===c[1]||"%"===c[1])){c=[this.get_comment(h)];break}if(v&&c[1]&&"{"===c[1]&&c[2]&&"!"===c[2]){c=[this.get_comment(h)];break}if(v&&"{"===j&&c.length>2&&"}"===c[c.length-2]&&"}"===c[c.length-1])break}}while(">"!==b);var D,E,C=c.join("");D=C.indexOf(" ")!==-1?C.indexOf(" "):C.indexOf("\n")!==-1?C.indexOf("\n"):"{"===C.charAt(0)?C.indexOf("}"):C.indexOf(">"),E="<"!==C.charAt(0)&&v?"#"===C.charAt(2)?3:2:1;var F=C.substring(E,D).toLowerCase();return"/"===C.charAt(C.length-2)||this.Utils.in_array(F,this.Utils.single_token)?a||(this.tag_type="SINGLE"):v&&"{"===C.charAt(0)&&"else"===F?a||(this.indent_to_tag("if"),this.tag_type="HANDLEBARS_ELSE",this.indent_content=!0,this.traverse_whitespace()):this.is_unformatted(F,r)||this.is_unformatted(F,s)?(d=this.get_unformatted("</"+F+">",C),c.push(d),i=this.pos-1,this.tag_type="SINGLE"):"script"===F&&(C.search("type")===-1||C.search("type")>-1&&C.search(/\b(text|application|dojo)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json|method|aspect)/)>-1)?a||(this.record_tag(F),this.tag_type="SCRIPT"):"style"===F&&(C.search("type")===-1||C.search("type")>-1&&C.search("text/css")>-1)?a||(this.record_tag(F),this.tag_type="STYLE"):"!"===F.charAt(0)?a||(this.tag_type="SINGLE",this.traverse_whitespace()):a||("/"===F.charAt(0)?(this.retrieve_tag(F.substring(1)),this.tag_type="END"):(this.record_tag(F),"html"!==F.toLowerCase()&&(this.indent_content=!0),this.tag_type="START"),this.traverse_whitespace()&&this.space_or_wrap(c),this.Utils.in_array(F,this.Utils.extra_liners)&&(this.print_newline(!1,this.output),this.output.length&&"\n"!==this.output[this.output.length-2]&&this.print_newline(!0,this.output))),a&&(this.pos=k,this.line_char_count=l),c.join("")},this.get_comment=function(a){var b="",c=">",d=!1;this.pos=a;var e=this.input.charAt(this.pos);for(this.pos++;this.pos<=this.input.length&&(b+=e,b.charAt(b.length-1)!==c.charAt(c.length-1)||b.indexOf(c)===-1);)!d&&b.length<10&&(0===b.indexOf("<![if")?(c="<![endif]>",d=!0):0===b.indexOf("<![cdata[")?(c="]]>",d=!0):0===b.indexOf("<![")?(c="]>",d=!0):0===b.indexOf("<!--")?(c="-->",d=!0):0===b.indexOf("{{!--")?(c="--}}",d=!0):0===b.indexOf("{{!")?5===b.length&&b.indexOf("{{!--")===-1&&(c="}}",d=!0):0===b.indexOf("<?")?(c="?>",d=!0):0===b.indexOf("<%")&&(c="%>",d=!0)),e=this.input.charAt(this.pos),this.pos++;return b},this.get_unformatted=function(a,b){if(b&&b.toLowerCase().indexOf(a)!==-1)return"";var d="",e="",f=!0,g=c(a);do{if(this.pos>=this.input.length)return e;if(d=this.input.charAt(this.pos),this.pos++,this.Utils.in_array(d,this.Utils.whitespace)){if(!f){this.line_char_count--;continue}if("\n"===d||"\r"===d){e+="\n",this.line_char_count=0;continue}}e+=d,g.add(d),this.line_char_count++,f=!0,v&&"{"===d&&e.length&&"{"===e.charAt(e.length-2)&&(e+=this.get_unformatted("}}"))}while(g.doesNotMatch());return e},this.get_token=function(){var a;if("TK_TAG_SCRIPT"===this.last_token||"TK_TAG_STYLE"===this.last_token){var b=this.last_token.substr(7);return a=this.get_contents_to(b),"string"!=typeof a?a:[a,"TK_"+b]}if("CONTENT"===this.current_mode)return a=this.get_content(),"string"!=typeof a?a:[a,"TK_CONTENT"];if("TAG"===this.current_mode){if(a=this.get_tag(),"string"!=typeof a)return a;var c="TK_TAG_"+this.tag_type;return[a,c]}},this.get_full_indent=function(a){return a=this.indent_level+a||0,a<1?"":Array(a+1).join(this.indent_string)},this.is_unformatted=function(a,b){if(!this.Utils.in_array(a,b))return!1;if("a"!==a.toLowerCase()||!this.Utils.in_array("a",b))return!0;var c=this.get_tag(!0),d=(c||"").match(/^\s*<\s*\/?([a-z]*)\s*[^>]*>\s*$/);return!(d&&!this.Utils.in_array(d,b))},this.printer=function(c,d,e,f,g){this.input=c||"",this.input=this.input.replace(/\r\n|[\r\u2028\u2029]/g,"\n"),this.output=[],this.indent_character=d,this.indent_string="",this.indent_size=e,this.brace_style=g,this.indent_level=0,this.wrap_line_length=f,this.line_char_count=0;for(var h=0;h<this.indent_size;h++)this.indent_string+=this.indent_character;this.print_newline=function(a,c){this.line_char_count=0,c&&c.length&&(a||"\n"!==c[c.length-1])&&("\n"!==c[c.length-1]&&(c[c.length-1]=b(c[c.length-1])),c.push("\n"))},this.print_indentation=function(a){for(var b=0;b<this.indent_level;b++)a.push(this.indent_string),this.line_char_count+=this.indent_string.length},this.print_token=function(b){this.is_whitespace(b)&&!this.output.length||((b||""!==b)&&this.output.length&&"\n"===this.output[this.output.length-1]&&(this.print_indentation(this.output),b=a(b)),this.print_token_raw(b))},this.print_token_raw=function(a){this.newlines>0&&(a=b(a)),a&&""!==a&&(a.length>1&&"\n"===a.charAt(a.length-1)?(this.output.push(a.slice(0,-1)),this.print_newline(!1,this.output)):this.output.push(a));for(var c=0;c<this.newlines;c++)this.print_newline(c>0,this.output);this.newlines=0},this.indent=function(){this.indent_level++},this.unindent=function(){this.indent_level>0&&this.indent_level--}},this}var j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D;for(g=g||{},g=c(g,"html"),void 0!==g.wrap_line_length&&0!==parseInt(g.wrap_line_length,10)||void 0===g.max_char||0===parseInt(g.max_char,10)||(g.wrap_line_length=g.max_char),k=void 0!==g.indent_inner_html&&g.indent_inner_html,l=void 0===g.indent_body_inner_html||g.indent_body_inner_html,m=void 0===g.indent_head_inner_html||g.indent_head_inner_html,n=void 0===g.indent_size?4:parseInt(g.indent_size,10),o=void 0===g.indent_char?" ":g.indent_char,q=void 0===g.brace_style?"collapse":g.brace_style,p=0===parseInt(g.wrap_line_length,10)?32786:parseInt(g.wrap_line_length||250,10),r=g.unformatted||["a","abbr","area","audio","b","bdi","bdo","br","button","canvas","cite","code","data","datalist","del","dfn","em","embed","i","iframe","img","input","ins","kbd","keygen","label","map","mark","math","meter","noscript","object","output","progress","q","ruby","s","samp","select","small","span","strong","sub","sup","svg","template","textarea","time","u","var","video","wbr","text","acronym","address","big","dt","ins","strike","tt"],s=g.content_unformatted||["pre"],t=void 0===g.preserve_newlines||g.preserve_newlines,u=t?isNaN(parseInt(g.max_preserve_newlines,10))?32786:parseInt(g.max_preserve_newlines,10):0,v=void 0!==g.indent_handlebars&&g.indent_handlebars,w=void 0===g.wrap_attributes?"auto":g.wrap_attributes,x=isNaN(parseInt(g.wrap_attributes_indent_size,10))?n:parseInt(g.wrap_attributes_indent_size,10),y="force"===w.substr(0,"force".length),z="force-expand-multiline"===w,A="force-aligned"===w,B=void 0!==g.end_with_newline&&g.end_with_newline,C="object"==typeof g.extra_liners&&g.extra_liners?g.extra_liners.concat():"string"==typeof g.extra_liners?g.extra_liners.split(","):"head,body,/html".split(","),D=g.eol?g.eol:"auto",g.indent_with_tabs&&(o="\t",n=1),"auto"===D&&(D="\n",f&&d.test(f||"")&&(D=f.match(d)[0])),D=D.replace(/\\r/,"\r").replace(/\\n/,"\n"),f=f.replace(e,"\n"),j=new E,j.printer(f,o,n,p,q);;){var F=j.get_token();if(j.token_text=F[0],j.token_type=F[1],"TK_EOF"===j.token_type)break;switch(j.token_type){case"TK_TAG_START":j.print_newline(!1,j.output),j.print_token(j.token_text),j.indent_content&&(!j.indent_body_inner_html&&j.token_text.match(/<body(?:.*)>/)||!j.indent_head_inner_html&&j.token_text.match(/<head(?:.*)>/)||j.indent(),j.indent_content=!1),j.current_mode="CONTENT";break;case"TK_TAG_STYLE":case"TK_TAG_SCRIPT":j.print_newline(!1,j.output),j.print_token(j.token_text),j.current_mode="CONTENT";break;case"TK_TAG_END":if("TK_CONTENT"===j.last_token&&""===j.last_text){var G=(j.token_text.match(/\w+/)||[])[0],H=null;j.output.length&&(H=j.output[j.output.length-1].match(/(?:<|{{#)\s*(\w+)/)),(null===H||H[1]!==G&&!j.Utils.in_array(H[1],r))&&j.print_newline(!1,j.output)}j.print_token(j.token_text),j.current_mode="CONTENT";break;case"TK_TAG_SINGLE":var I=j.token_text.match(/^\s*<([a-z-]+)/i);I&&j.Utils.in_array(I[1],r)||j.print_newline(!1,j.output),j.print_token(j.token_text),j.current_mode="CONTENT";break;case"TK_TAG_HANDLEBARS_ELSE":for(var J=!1,K=j.output.length-1;K>=0&&"\n"!==j.output[K];K--)if(j.output[K].match(/{{#if/)){J=!0;break}J||j.print_newline(!1,j.output),j.print_token(j.token_text),j.indent_content&&(j.indent(),j.indent_content=!1),j.current_mode="CONTENT";break;case"TK_TAG_HANDLEBARS_COMMENT":j.print_token(j.token_text),j.current_mode="TAG";break;case"TK_CONTENT":j.print_token(j.token_text),j.current_mode="TAG";break;case"TK_STYLE":case"TK_SCRIPT":if(""!==j.token_text){j.print_newline(!1,j.output);var M,L=j.token_text,N=1;"TK_SCRIPT"===j.token_type?M="function"==typeof h&&h:"TK_STYLE"===j.token_type&&(M="function"==typeof i&&i),"keep"===g.indent_scripts?N=0:"separate"===g.indent_scripts&&(N=-j.indent_level);var O=j.get_full_indent(N);if(M){var P=function(){this.eol="\n"};P.prototype=g;var Q=new P;L=M(L.replace(/^\s*/,O),Q)}else{var R=L.match(/^\s*/)[0],S=R.match(/[^\n\r]*$/)[0].split(j.indent_string).length-1,T=j.get_full_indent(N-S);L=L.replace(/^\s*/,O).replace(/\r\n|\r|\n/g,"\n"+T).replace(/\s+$/,"")}L&&(j.print_token_raw(L),j.print_newline(!0,j.output))}j.current_mode="TAG";break;default:""!==j.token_text&&j.print_token(j.token_text)}j.last_token=j.token_type,j.last_text=j.token_text}var U=j.output.join("").replace(/[\r\n\t ]+$/,"");return B&&(U+="\n"),"\n"!==D&&(U=U.replace(/[\n]/g,D)),U}var d=/\r\n|[\n\r\u2028\u2029]/,e=new RegExp(d.source,"g");if("function"==typeof define&&define.amd)define(["require","./beautify","./beautify-css"],function(a){var b=a("./beautify"),c=a("./beautify-css");return{html_beautify:function(a,d){return f(a,d,b.js_beautify,c.css_beautify)}}});else if("undefined"!=typeof exports){var g=require("./beautify.js"),h=require("./beautify-css.js");exports.html_beautify=function(a,b){return f(a,b,g.js_beautify,h.css_beautify)}}else"undefined"!=typeof window?window.html_beautify=function(a,b){return f(a,b,window.js_beautify,window.css_beautify)}:"undefined"!=typeof global&&(global.html_beautify=function(a,b){return f(a,b,global.js_beautify,global.css_beautify)})}();