(function(h){"object"==typeof exports&&"object"==typeof module?h(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],h):h(CodeMirror)})(function(h){function l(a,b,d,c){this.line=b;this.ch=d;this.cm=a;this.text=a.getLine(b);this.min=c?Math.max(c.from,a.firstLine()):a.firstLine();this.max=c?Math.min(c.to-1,a.lastLine()):a.lastLine()}function n(a,b){return(a=a.cm.getTokenTypeAt(k(a.line,b)))&&/\btag\b/.test(a)}function u(a){if(!(a.line>=a.max))return a.ch=
0,a.text=a.cm.getLine(++a.line),!0}function v(a){if(!(a.line<=a.min))return a.text=a.cm.getLine(--a.line),a.ch=a.text.length,!0}function r(a){for(;;){var b=a.text.indexOf(">",a.ch);if(-1==b)if(u(a))continue;else break;if(n(a,b+1)){var d=a.text.lastIndexOf("/",b);d=-1<d&&!/\S/.test(a.text.slice(d+1,b));a.ch=b+1;return d?"selfClose":"regular"}a.ch=b+1}}function t(a){for(;;){var b=a.ch?a.text.lastIndexOf("<",a.ch-1):-1;if(-1==b)if(v(a))continue;else break;if(n(a,b+1)){p.lastIndex=b;a.ch=b;var d=p.exec(a.text);
if(d&&d.index==b)return d}else a.ch=b}}function w(a){for(;;){p.lastIndex=a.ch;var b=p.exec(a.text);if(!b)if(u(a))continue;else break;if(n(a,b.index+1))return a.ch=b.index+b[0].length,b;a.ch=b.index+1}}function q(a,b){for(var d=[];;){var c=w(a),f,e=a.line,g=a.ch-(c?c[0].length:0);if(!c||!(f=r(a)))break;if("selfClose"!=f)if(c[1]){for(var m=d.length-1;0<=m;--m)if(d[m]==c[2]){d.length=m;break}if(0>m&&(!b||b==c[2]))return{tag:c[2],from:k(e,g),to:k(a.line,a.ch)}}else d.push(c[2])}}function x(a,b){for(var d=
[];;){var c;a:for(c=a;;){var f=c.ch?c.text.lastIndexOf(">",c.ch-1):-1;if(-1==f)if(v(c))continue;else{c=void 0;break a}if(n(c,f+1)){var e=c.text.lastIndexOf("/",f);e=-1<e&&!/\S/.test(c.text.slice(e+1,f));c.ch=f+1;c=e?"selfClose":"regular";break a}else c.ch=f}if(!c)break;if("selfClose"==c)t(a);else{c=a.line;f=a.ch;e=t(a);if(!e)break;if(e[1])d.push(e[2]);else{for(var g=d.length-1;0<=g;--g)if(d[g]==e[2]){d.length=g;break}if(0>g&&(!b||b==e[2]))return{tag:e[2],from:k(a.line,a.ch),to:k(c,f)}}}}}var k=h.Pos,
p=RegExp("<(/?)([A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD][A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD-:.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040]*)","g");h.registerHelper("fold","xml",function(a,b){for(a=new l(a,b.line,0);;){var d=w(a);if(!d||
a.line!=b.line)break;var c=r(a);if(!c)break;if(!d[1]&&"selfClose"!=c){b=k(a.line,a.ch);if(d=a=q(a,d[2]))d=a.from,d=0<(d.line-b.line||d.ch-b.ch);return d?{from:b,to:a.from}:null}}});h.findMatchingTag=function(a,b,d){var c=new l(a,b.line,b.ch,d);if(-1!=c.text.indexOf(">")||-1!=c.text.indexOf("<")){var f=r(c),e=f&&k(c.line,c.ch),g=f&&t(c);if(f&&g&&!(0<(c.line-b.line||c.ch-b.ch))){b={from:k(c.line,c.ch),to:e,tag:g[2]};if("selfClose"==f)return{open:b,close:null,at:"open"};if(g[1])return{open:x(c,g[2]),
close:b,at:"close"};c=new l(a,e.line,e.ch,d);return{open:b,close:q(c,g[2]),at:"open"}}}};h.findEnclosingTag=function(a,b,d,c){for(var f=new l(a,b.line,b.ch,d);;){var e=x(f,c);if(!e)break;var g=new l(a,b.line,b.ch,d);if(g=q(g,e.tag))return{open:e,close:g}}};h.scanForClosingTag=function(a,b,d,c){a=new l(a,b.line,b.ch,c?{from:0,to:c}:null);return q(a,d)}});
