/*

 Style HTML
---------------

  Written by Nochum Sossonko, (nsossonko@hotmail.com)

  Based on code initially developed by: Einar Lielmanis, <elfz@laacz.lv>
    http://jsbeautifier.org/


  You are free to use this in any way you want, in case you find this useful or working for you.

  Usage:
    style_html(html_source);

    style_html(html_source, options);

  The options are:
    indent_size (default 4)          — indentation size,
    indent_char (default space)      — character to indent with,
    max_char (default 70)            -  maximum amount of characters per line,
    brace_style (default "collapse") - "collapse" | "expand" | "end-expand"
            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.
    unformatted (default ['a'])      - list of tags, that shouldn't be reformatted
    indent_scripts (default normal)  - "keep"|"separate"|"normal"

    e.g.

    style_html(html_source, {
      'indent_size': 2,
      'indent_char': ' ',
      'max_char': 78,
      'brace_style': 'expand',
      'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u']
    });
*///inside_using("qybase");
//qyboot.forbidLoadDefaultBase();
//using("wwliu_qiyun/qybase");
function style_html(e,t){function u(){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(e,t){for(var n=0;n<t.length;n++)if(e===t[n])return!0;return!1}},this.get_content=function(){var e="",t=[],n=!1;while(this.input.charAt(this.pos)!=="<"){if(this.pos>=this.input.length)return t.length?t.join(""):["","TK_EOF"];e=this.input.charAt(this.pos),this.pos++,this.line_char_count++;if(this.Utils.in_array(e,this.Utils.whitespace)){t.length&&(n=!0),this.line_char_count--;continue}if(n){if(this.line_char_count>=this.max_char){t.push("\n");for(var r=0;r<this.indent_level;r++)t.push(this.indent_string);this.line_char_count=0}else t.push(" "),this.line_char_count++;n=!1}t.push(e)}return t.length?t.join(""):""},this.get_contents_to=function(e){if(this.pos==this.input.length)return["","TK_EOF"];var t="",n="",r=new RegExp("</"+e+"\\s*>","igm");r.lastIndex=this.pos;var i=r.exec(this.input),s=i?i.index:this.input.length;return this.pos<s&&(n=this.input.substring(this.pos,s),this.pos=s),n},this.record_tag=function(e){this.tags[e+"count"]?(this.tags[e+"count"]++,this.tags[e+this.tags[e+"count"]]=this.indent_level):(this.tags[e+"count"]=1,this.tags[e+this.tags[e+"count"]]=this.indent_level),this.tags[e+this.tags[e+"count"]+"parent"]=this.tags.parent,this.tags.parent=e+this.tags[e+"count"]},this.retrieve_tag=function(e){if(this.tags[e+"count"]){var t=this.tags.parent;while(t){if(e+this.tags[e+"count"]===t)break;t=this.tags[t+"parent"]}t&&(this.indent_level=this.tags[e+this.tags[e+"count"]],this.tags.parent=this.tags[t+"parent"]),delete this.tags[e+this.tags[e+"count"]+"parent"],delete this.tags[e+this.tags[e+"count"]],this.tags[e+"count"]==1?delete this.tags[e+"count"]:this.tags[e+"count"]--}},this.get_tag=function(){var e="",t=[],n=!1;do{if(this.pos>=this.input.length)return t.length?t.join(""):["","TK_EOF"];e=this.input.charAt(this.pos),this.pos++,this.line_char_count++;if(this.Utils.in_array(e,this.Utils.whitespace)){n=!0,this.line_char_count--;continue}if(e==="'"||e==='"')if(!t[1]||t[1]!=="!")e+=this.get_unformatted(e),n=!0;e==="="&&(n=!1),t.length&&t[t.length-1]!=="="&&e!==">"&&n&&(this.line_char_count>=this.max_char?(this.print_newline(!1,t),this.line_char_count=0):(t.push(" "),this.line_char_count++),n=!1),t.push(e)}while(e!==">");var r=t.join(""),i;r.indexOf(" ")!=-1?i=r.indexOf(" "):i=r.indexOf(">");var s=r.substring(1,i).toLowerCase();if(r.charAt(r.length-2)==="/"||this.Utils.in_array(s,this.Utils.single_token))this.tag_type="SINGLE";else if(s==="script")this.record_tag(s),this.tag_type="SCRIPT";else if(s==="style")this.record_tag(s),this.tag_type="STYLE";else if(this.Utils.in_array(s,unformatted)){var o=this.get_unformatted("</"+s+">",r);t.push(o),this.tag_type="SINGLE"}else if(s.charAt(0)==="!")if(s.indexOf("[if")!=-1){if(r.indexOf("!IE")!=-1){var o=this.get_unformatted("-->",r);t.push(o)}this.tag_type="START"}else if(s.indexOf("[endif")!=-1)this.tag_type="END",this.unindent();else if(s.indexOf("[cdata[")!=-1){var o=this.get_unformatted("]]>",r);t.push(o),this.tag_type="SINGLE"}else{var o=this.get_unformatted("-->",r);t.push(o),this.tag_type="SINGLE"}else s.charAt(0)==="/"?(this.retrieve_tag(s.substring(1)),this.tag_type="END"):(this.record_tag(s),this.tag_type="START"),this.Utils.in_array(s,this.Utils.extra_liners)&&this.print_newline(!0,this.output);return t.join("")},this.get_unformatted=function(e,t){if(t&&t.indexOf(e)!=-1)return"";var n="",r="",i=!0;do{if(this.pos>=this.input.length)return r;n=this.input.charAt(this.pos),this.pos++;if(this.Utils.in_array(n,this.Utils.whitespace)){if(!i){this.line_char_count--;continue}if(n==="\n"||n==="\r"){r+="\n",this.line_char_count=0;continue}}r+=n,this.line_char_count++,i=!0}while(r.indexOf(e)==-1);return r},this.get_token=function(){var e;if(this.last_token==="TK_TAG_SCRIPT"||this.last_token==="TK_TAG_STYLE"){var t=this.last_token.substr(7);return e=this.get_contents_to(t),typeof e!="string"?e:[e,"TK_"+t]}if(this.current_mode==="CONTENT")return e=this.get_content(),typeof e!="string"?e:[e,"TK_CONTENT"];if(this.current_mode==="TAG"){e=this.get_tag();if(typeof e!="string")return e;var n="TK_TAG_"+this.tag_type;return[e,n]}},this.get_full_indent=function(e){return e=this.indent_level+e||0,e<1?"":Array(e+1).join(this.indent_string)},this.printer=function(e,t,n,r,i){this.input=e||"",this.output=[],this.indent_character=t,this.indent_string="",this.indent_size=n,this.brace_style=i,this.indent_level=0,this.max_char=r,this.line_char_count=0;for(var s=0;s<this.indent_size;s++)this.indent_string+=this.indent_character;this.print_newline=function(e,t){this.line_char_count=0;if(!t||!t.length)return;if(!e)while(this.Utils.in_array(t[t.length-1],this.Utils.whitespace))t.pop();t.push("\n");for(var n=0;n<this.indent_level;n++)t.push(this.indent_string)},this.print_token=function(e){this.output.push(e)},this.indent=function(){this.indent_level++},this.unindent=function(){this.indent_level>0&&this.indent_level--}},this}var n,r,i,s,o;t=t||{},r=t.indent_size||4,i=t.indent_char||" ",o=t.brace_style||"collapse",s=t.max_char||"70",unformatted=t.unformatted||["a"],n=new u,n.printer(e,i,r,s,o);for(;;){var a=n.get_token();n.token_text=a[0],n.token_type=a[1];if(n.token_type==="TK_EOF")break;switch(n.token_type){case"TK_TAG_START":n.print_newline(!1,n.output),n.print_token(n.token_text),n.indent(),n.current_mode="CONTENT";break;case"TK_TAG_STYLE":case"TK_TAG_SCRIPT":n.print_newline(!1,n.output),n.print_token(n.token_text),n.current_mode="CONTENT";break;case"TK_TAG_END":if(n.last_token==="TK_CONTENT"&&n.last_text===""){var f=n.token_text.match(/\w+/)[0],l=n.output[n.output.length-1].match(/<\s*(\w+)/);(l===null||l[1]!==f)&&n.print_newline(!0,n.output)}n.print_token(n.token_text),n.current_mode="CONTENT";break;case"TK_TAG_SINGLE":n.print_newline(!1,n.output),n.print_token(n.token_text),n.current_mode="CONTENT";break;case"TK_CONTENT":n.token_text!==""&&n.print_token(n.token_text),n.current_mode="TAG";break;case"TK_STYLE":case"TK_SCRIPT":if(n.token_text!==""){n.output.push("\n");var c=n.token_text;if(n.token_type=="TK_SCRIPT")var h=typeof js_beautify=="function"&&js_beautify;else if(n.token_type=="TK_STYLE")var h=typeof css_beautify=="function"&&css_beautify;if(t.indent_scripts=="keep")var p=0;else if(t.indent_scripts=="separate")var p=-n.indent_level;else var p=1;var d=n.get_full_indent(p);if(h)c=h(c.replace(/^\s*/,d),t);else{var v=c.match(/^\s*/)[0],m=v.match(/[^\n\r]*$/)[0].split(n.indent_string).length-1,g=n.get_full_indent(p-m);c=c.replace(/^\s*/,d).replace(/\r\n|\r|\n/g,"\n"+g).replace(/\s*$/,"")}c&&(n.print_token(c),n.print_newline(!0,n.output))}n.current_mode="TAG"}n.last_token=n.token_type,n.last_text=n.token_text}return n.output.join("")}window.html_beautify=style_html;