!function(e){"object"==typeof exports&&"object"==typeof module?e(require("../../lib/codemirror"),require("../fold/xml-fold")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","../fold/xml-fold"],e):e(CodeMirror)}(function(e){function t(t){if(t.getOption("disableInput"))return e.Pass;for(var n=t.listSelections(),o=[],l=t.getOption("autoCloseTags"),c=0;c<n.length;c++){if(!n[c].empty())return e.Pass;var d=n[c].head,f=t.getTokenAt(d),g=e.innerMode(t.getMode(),f.state),u=g.state;if("xml"!=g.mode.name||!u.tagName)return e.Pass;var m="html"==g.mode.configuration,h="object"==typeof l&&l.dontCloseTags||m&&i,p="object"==typeof l&&l.indentTags||m&&s,v=u.tagName;f.end>d.ch&&(v=v.slice(0,v.length-f.end+d.ch));var y=v.toLowerCase();if(!v||"string"==f.type&&(f.end!=d.ch||!/[\"\']/.test(f.string.charAt(f.string.length-1))||1==f.string.length)||"tag"==f.type&&"closeTag"==u.type||f.string.indexOf("/")==f.string.length-1||h&&a(h,y)>-1||r(t,v,d,u,!0))return e.Pass;var b="object"==typeof l&&l.emptyTags;if(b&&a(b,v)>-1)o[c]={text:"/>",newPos:e.Pos(d.line,d.ch+2)};else{var x=p&&a(p,y)>-1;o[c]={indent:x,text:">"+(x?"\n\n":"")+"</"+v+">",newPos:x?e.Pos(d.line+1,0):e.Pos(d.line,d.ch+1)}}}for(var P="object"==typeof l&&l.dontIndentOnAutoClose,c=n.length-1;c>=0;c--){var T=o[c];t.replaceRange(T.text,n[c].head,n[c].anchor,"+insert");var C=t.listSelections().slice(0);C[c]={head:T.newPos,anchor:T.newPos},t.setSelections(C),!P&&T.indent&&(t.indentLine(T.newPos.line,null,!0),t.indentLine(T.newPos.line+1,null,!0))}}function n(t,n){for(var o=t.listSelections(),a=[],i=n?"/":"</",s=t.getOption("autoCloseTags"),l="object"==typeof s&&s.dontIndentOnSlash,c=0;c<o.length;c++){if(!o[c].empty())return e.Pass;var d=o[c].head,f=t.getTokenAt(d),g=e.innerMode(t.getMode(),f.state),u=g.state;if(n&&("string"==f.type||"<"!=f.string.charAt(0)||f.start!=d.ch-1))return e.Pass;var m;if("xml"!=g.mode.name)if("htmlmixed"==t.getMode().name&&"javascript"==g.mode.name)m=i+"script";else{if("htmlmixed"!=t.getMode().name||"css"!=g.mode.name)return e.Pass;m=i+"style"}else{if(!u.context||!u.context.tagName||r(t,u.context.tagName,d,u))return e.Pass;m=i+u.context.tagName}">"!=t.getLine(d.line).charAt(f.end)&&(m+=">"),a[c]=m}if(t.replaceSelections(a),o=t.listSelections(),!l)for(var c=0;c<o.length;c++)(c==o.length-1||o[c].head.line<o[c+1].head.line)&&t.indentLine(o[c].head.line)}function o(t){return t.getOption("disableInput")?e.Pass:n(t,!0)}function a(e,t){if(e.indexOf)return e.indexOf(t);for(var n=0,o=e.length;n<o;++n)if(e[n]==t)return n;return-1}function r(t,n,o,a,r){if(!e.scanForClosingTag)return!1;var i=Math.min(t.lastLine()+1,o.line+500),s=e.scanForClosingTag(t,o,null,i);if(!s||s.tag!=n)return!1;for(var l=a.context,c=r?1:0;l&&l.tagName==n;l=l.prev)++c;o=s.to;for(var d=1;d<c;d++){var f=e.scanForClosingTag(t,o,null,i);if(!f||f.tag!=n)return!1;o=f.to}return!0}e.defineOption("autoCloseTags",!1,function(n,a,r){if(r!=e.Init&&r&&n.removeKeyMap("autoCloseTags"),a){var i={name:"autoCloseTags"};("object"!=typeof a||a.whenClosing)&&(i["'/'"]=function(e){return o(e)}),("object"!=typeof a||a.whenOpening)&&(i["'>'"]=function(e){return t(e)}),n.addKeyMap(i)}});var i=["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"],s=["applet","blockquote","body","button","div","dl","fieldset","form","frameset","h1","h2","h3","h4","h5","h6","head","html","iframe","layer","legend","object","ol","p","select","table","ul"];e.commands.closeTag=function(e){return n(e)}});