(function (e) {
  if (typeof exports == 'object' && typeof module == 'object') e(require('../../lib/codemirror'));
  else if (typeof define == 'function' && define.amd) define(['../../lib/codemirror'], e);
  else e(CodeMirror);
})(function (e) {
  'use strict';
  e.defineMode('xml', function (t, n) {
    function l(e, t) {
      function n(n) {
        t.tokenize = n;
        return n(e, t);
      }
      var r = e.next();
      if (r == '<') {
        if (e.eat('!')) {
          if (e.eat('[')) {
            if (e.match('CDATA[')) return n(p('atom', ']]>'));
            else return null;
          } else if (e.match('--')) {
            return n(p('comment', '-->'));
          } else if (e.match('DOCTYPE', true, true)) {
            e.eatWhile(/[\w\._\-]/);
            return n(d(1));
          } else {
            return null;
          }
        } else if (e.eat('?')) {
          e.eatWhile(/[\w\._\-]/);
          t.tokenize = p('meta', '?>');
          return 'meta';
        } else {
          a = e.eat('/') ? 'closeTag' : 'openTag';
          t.tokenize = c;
          return 'tag bracket';
        }
      } else if (r == '&') {
        var i;
        if (e.eat('#')) {
          if (e.eat('x')) {
            i = e.eatWhile(/[a-fA-F\d]/) && e.eat(';');
          } else {
            i = e.eatWhile(/[\d]/) && e.eat(';');
          }
        } else {
          i = e.eatWhile(/[\w\.\-:]/) && e.eat(';');
        }
        return i ? 'atom' : 'error';
      } else {
        e.eatWhile(/[^&<]/);
        return null;
      }
    }
    function c(e, t) {
      var n = e.next();
      if (n == '>' || (n == '/' && e.eat('>'))) {
        t.tokenize = l;
        a = n == '>' ? 'endTag' : 'selfcloseTag';
        return 'tag bracket';
      } else if (n == '=') {
        a = 'equals';
        return null;
      } else if (n == '<') {
        t.tokenize = l;
        t.state = y;
        t.tagName = t.tagStart = null;
        var r = t.tokenize(e, t);
        return r ? r + ' tag error' : 'tag error';
      } else if (/[\'\"]/.test(n)) {
        t.tokenize = h(n);
        t.stringStartCol = e.column();
        return t.tokenize(e, t);
      } else {
        e.match(/^[^\s\u00a0=<>\"\']*[^\s\u00a0=<>\"\'\/]/);
        return 'word';
      }
    }
    function h(e) {
      var t = function (t, n) {
        while (!t.eol()) {
          if (t.next() == e) {
            n.tokenize = c;
            break;
          }
        }
        return 'string';
      };
      t.isInAttribute = true;
      return t;
    }
    function p(e, t) {
      return function (n, r) {
        while (!n.eol()) {
          if (n.match(t)) {
            r.tokenize = l;
            break;
          }
          n.next();
        }
        return e;
      };
    }
    function d(e) {
      return function (t, n) {
        var r;
        while ((r = t.next()) != null) {
          if (r == '<') {
            n.tokenize = d(e + 1);
            return n.tokenize(t, n);
          } else if (r == '>') {
            if (e == 1) {
              n.tokenize = l;
              break;
            } else {
              n.tokenize = d(e - 1);
              return n.tokenize(t, n);
            }
          }
        }
        return 'meta';
      };
    }
    function v(e, t, n) {
      this.prev = e.context;
      this.tagName = t;
      this.indent = e.indented;
      this.startOfLine = n;
      if (o.doNotIndent.hasOwnProperty(t) || (e.context && e.context.noIndent)) this.noIndent = true;
    }
    function m(e) {
      if (e.context) e.context = e.context.prev;
    }
    function g(e, t) {
      var n;
      while (true) {
        if (!e.context) {
          return;
        }
        n = e.context.tagName;
        if (!o.contextGrabbers.hasOwnProperty(n) || !o.contextGrabbers[n].hasOwnProperty(t)) {
          return;
        }
        m(e);
      }
    }
    function y(e, t, n) {
      if (e == 'openTag') {
        n.tagStart = t.column();
        return b;
      } else if (e == 'closeTag') {
        return w;
      } else {
        return y;
      }
    }
    function b(e, t, n) {
      if (e == 'word') {
        n.tagName = t.current();
        f = 'tag';
        return x;
      } else {
        f = 'error';
        return b;
      }
    }
    function w(e, t, n) {
      if (e == 'word') {
        var r = t.current();
        if (n.context && n.context.tagName != r && o.implicitlyClosed.hasOwnProperty(n.context.tagName)) m(n);
        if (n.context && n.context.tagName == r) {
          f = 'tag';
          return E;
        } else {
          f = 'tag error';
          return S;
        }
      } else {
        f = 'error';
        return S;
      }
    }
    function E(e, t, n) {
      if (e != 'endTag') {
        f = 'error';
        return E;
      }
      m(n);
      return y;
    }
    function S(e, t, n) {
      f = 'error';
      return E(e, t, n);
    }
    function x(e, t, n) {
      if (e == 'word') {
        f = 'attribute';
        return T;
      } else if (e == 'endTag' || e == 'selfcloseTag') {
        var r = n.tagName,
          i = n.tagStart;
        n.tagName = n.tagStart = null;
        if (e == 'selfcloseTag' || o.autoSelfClosers.hasOwnProperty(r)) {
          g(n, r);
        } else {
          g(n, r);
          n.context = new v(n, r, i == n.indented);
        }
        return y;
      }
      f = 'error';
      return x;
    }
    function T(e, t, n) {
      if (e == 'equals') return N;
      if (!o.allowMissing) f = 'error';
      return x(e, t, n);
    }
    function N(e, t, n) {
      if (e == 'string') return C;
      if (e == 'word' && o.allowUnquoted) {
        f = 'string';
        return x;
      }
      f = 'error';
      return x(e, t, n);
    }
    function C(e, t, n) {
      if (e == 'string') return C;
      return x(e, t, n);
    }
    var r = t.indentUnit;
    var i = n.multilineTagIndentFactor || 1;
    var s = n.multilineTagIndentPastTag;
    if (s == null) s = true;
    var o = n.htmlMode
      ? {
          autoSelfClosers: { area: true, base: true, br: true, col: true, command: true, embed: true, frame: true, hr: true, img: true, input: true, keygen: true, link: true, meta: true, param: true, source: true, track: true, wbr: true, menuitem: true },
          implicitlyClosed: { dd: true, li: true, optgroup: true, option: true, p: true, rp: true, rt: true, tbody: true, td: true, tfoot: true, th: true, tr: true },
          contextGrabbers: {
            dd: { dd: true, dt: true },
            dt: { dd: true, dt: true },
            li: { li: true },
            option: { option: true, optgroup: true },
            optgroup: { optgroup: true },
            p: {
              address: true,
              article: true,
              aside: true,
              blockquote: true,
              dir: true,
              div: true,
              dl: true,
              fieldset: true,
              footer: true,
              form: true,
              h1: true,
              h2: true,
              h3: true,
              h4: true,
              h5: true,
              h6: true,
              header: true,
              hgroup: true,
              hr: true,
              menu: true,
              nav: true,
              ol: true,
              p: true,
              pre: true,
              section: true,
              table: true,
              ul: true,
            },
            rp: { rp: true, rt: true },
            rt: { rp: true, rt: true },
            tbody: { tbody: true, tfoot: true },
            td: { td: true, th: true },
            tfoot: { tbody: true },
            th: { td: true, th: true },
            thead: { tbody: true, tfoot: true },
            tr: { tr: true },
          },
          doNotIndent: { pre: true },
          allowUnquoted: true,
          allowMissing: true,
          caseFold: true,
        }
      : { autoSelfClosers: {}, implicitlyClosed: {}, contextGrabbers: {}, doNotIndent: {}, allowUnquoted: false, allowMissing: false, caseFold: false };
    var u = n.alignCDATA;
    var a, f;
    return {
      startState: function () {
        return { tokenize: l, state: y, indented: 0, tagName: null, tagStart: null, context: null };
      },
      token: function (e, t) {
        if (!t.tagName && e.sol()) t.indented = e.indentation();
        if (e.eatSpace()) return null;
        a = null;
        var n = t.tokenize(e, t);
        if ((n || a) && n != 'comment') {
          f = null;
          t.state = t.state(a || n, e, t);
          if (f) n = f == 'error' ? n + ' error' : f;
        }
        return n;
      },
      indent: function (t, n, a) {
        var f = t.context;
        if (t.tokenize.isInAttribute) {
          if (t.tagStart == t.indented) return t.stringStartCol + 1;
          else return t.indented + r;
        }
        if (f && f.noIndent) return e.Pass;
        if (t.tokenize != c && t.tokenize != l) return a ? a.match(/^(\s*)/)[0].length : 0;
        if (t.tagName) {
          if (s) return t.tagStart + t.tagName.length + 2;
          else return t.tagStart + r * i;
        }
        if (u && /<!\[CDATA\[/.test(n)) return 0;
        var h = n && /^<(\/)?([\w_:\.-]*)/.exec(n);
        if (h && h[1]) {
          while (f) {
            if (f.tagName == h[2]) {
              f = f.prev;
              break;
            } else if (o.implicitlyClosed.hasOwnProperty(f.tagName)) {
              f = f.prev;
            } else {
              break;
            }
          }
        } else if (h) {
          while (f) {
            var p = o.contextGrabbers[f.tagName];
            if (p && p.hasOwnProperty(h[2])) f = f.prev;
            else break;
          }
        }
        while (f && !f.startOfLine) f = f.prev;
        if (f) return f.indent + r;
        else return 0;
      },
      electricInput: /<\/[\s\w:]+>$/,
      blockCommentStart: '<!--',
      blockCommentEnd: '-->',
      configuration: n.htmlMode ? 'html' : 'xml',
      helperType: n.htmlMode ? 'html' : 'xml',
    };
  });
  e.defineMIME('text/xml', 'xml');
  e.defineMIME('application/xml', 'xml');
  if (!e.mimeModes.hasOwnProperty('text/html')) e.defineMIME('text/html', { name: 'xml', htmlMode: true });
});
