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