(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 r(b){if(b.getOption("disableInput"))return e.Pass;for(var h=b.listSelections(),a=[],d=0;d<h.length;d++){if(!h[d].empty())return e.Pass;var f=h[d].head,c=b.getTokenAt(f),k=e.innerMode(b.getMode(),c.state),m=k.state;if("xml"!=k.mode.name||!m.tagName)return e.Pass;
var g=b.getOption("autoCloseTags"),l="html"==k.mode.configuration,k="object"==typeof g&&g.dontCloseTags||l&&t,l="object"==typeof g&&g.indentTags||l&&u,g=m.tagName;c.end>f.ch&&(g=g.slice(0,g.length-c.end+f.ch));var n=g.toLowerCase();if(!g||"string"==c.type&&(c.end!=f.ch||!/[\"\']/.test(c.string.charAt(c.string.length-1))||1==c.string.length)||"tag"==c.type&&"closeTag"==m.type||c.string.indexOf("/")==c.string.length-1||k&&-1<p(k,n)||q(b,g,f,m,!0))return e.Pass;c=l&&-1<p(l,n);a[d]={indent:c,text:"\x3e"+
(c?"\n\n":"")+"\x3c/"+g+"\x3e",newPos:c?e.Pos(f.line+1,0):e.Pos(f.line,f.ch+1)}}for(d=h.length-1;0<=d;d--)f=a[d],b.replaceRange(f.text,h[d].head,h[d].anchor,"+insert"),c=b.listSelections().slice(0),c[d]={head:f.newPos,anchor:f.newPos},b.setSelections(c),f.indent&&(b.indentLine(f.newPos.line,null,!0),b.indentLine(f.newPos.line+1,null,!0))}function n(b,h){for(var a=b.listSelections(),d=[],f=h?"/":"\x3c/",c=0;c<a.length;c++){if(!a[c].empty())return e.Pass;var k=a[c].head,m=b.getTokenAt(k),g=e.innerMode(b.getMode(),
m.state),l=g.state;if(h&&("string"==m.type||"\x3c"!=m.string.charAt(0)||m.start!=k.ch-1))return e.Pass;if("xml"!=g.mode.name)if("htmlmixed"==b.getMode().name&&"javascript"==g.mode.name)g=f+"script";else if("htmlmixed"==b.getMode().name&&"css"==g.mode.name)g=f+"style";else return e.Pass;else{if(!l.context||!l.context.tagName||q(b,l.context.tagName,k,l))return e.Pass;g=f+l.context.tagName}"\x3e"!=b.getLine(k.line).charAt(m.end)&&(g+="\x3e");d[c]=g}b.replaceSelections(d);a=b.listSelections();for(c=0;c<
a.length;c++)(c==a.length-1||a[c].head.line<a[c+1].head.line)&&b.indentLine(a[c].head.line)}function p(b,e){if(b.indexOf)return b.indexOf(e);for(var a=0,d=b.length;a<d;++a)if(b[a]==e)return a;return-1}function q(b,h,a,d,f){if(!e.scanForClosingTag)return!1;var c=Math.min(b.lastLine()+1,a.line+500);a=e.scanForClosingTag(b,a,null,c);if(!a||a.tag!=h)return!1;d=d.context;for(f=f?1:0;d&&d.tagName==h;d=d.prev)++f;a=a.to;for(d=1;d<f;d++){a=e.scanForClosingTag(b,a,null,c);if(!a||a.tag!=h)return!1;a=a.to}return!0}
e.defineOption("autoCloseTags",!1,function(b,h,a){a!=e.Init&&a&&b.removeKeyMap("autoCloseTags");if(h){a={name:"autoCloseTags"};if("object"!=typeof h||h.whenClosing)a["'/'"]=function(a){a=a.getOption("disableInput")?e.Pass:n(a,!0);return a};if("object"!=typeof h||h.whenOpening)a["'\x3e'"]=function(a){return r(a)};b.addKeyMap(a)}});var t="area base br col command embed hr img input keygen link meta param source track wbr".split(" "),u="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".split(" ");
e.commands.closeTag=function(b){return n(b)}});