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