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