diff --git a/addon/edit/closetag.js b/addon/edit/closetag.js
index 454dfea5e6beb0a6fb07f54b2fd725f87ffb889c..0bf8881d0890f0342b191579542f65fe47ebf519 100644
--- a/addon/edit/closetag.js
+++ b/addon/edit/closetag.js
@@ -27,9 +27,9 @@
     if (val && (old == CodeMirror.Init || !old)) {
       var map = {name: "autoCloseTags"};
       if (typeof val != "object" || val.whenClosing)
-        map["'/'"] = function(cm) { return autoCloseTag(cm, '/'); };
+        map["'/'"] = function(cm) { return autoCloseSlash(cm); };
       if (typeof val != "object" || val.whenOpening)
-        map["'>'"] = function(cm) { return autoCloseTag(cm, '>'); };
+        map["'>'"] = function(cm) { return autoCloseGT(cm); };
       cm.addKeyMap(map);
     } else if (!val && (old != CodeMirror.Init && old)) {
       cm.removeKeyMap("autoCloseTags");
@@ -41,40 +41,41 @@
   var htmlIndent = ["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 autoCloseTag(cm, ch) {
+  function autoCloseGT(cm) {
     var pos = cm.getCursor(), tok = cm.getTokenAt(pos);
     var inner = CodeMirror.innerMode(cm.getMode(), tok.state), state = inner.state;
-    if (inner.mode.name != "xml") return CodeMirror.Pass;
+    if (inner.mode.name != "xml" || !state.tagName) return CodeMirror.Pass;
 
     var opt = cm.getOption("autoCloseTags"), html = inner.mode.configuration == "html";
     var dontCloseTags = (typeof opt == "object" && opt.dontCloseTags) || (html && htmlDontClose);
     var indentTags = (typeof opt == "object" && opt.indentTags) || (html && htmlIndent);
 
-    if (ch == ">" && state.tagName) {
-      var tagName = state.tagName;
-      if (tok.end > pos.ch) tagName = tagName.slice(0, tagName.length - tok.end + pos.ch);
-      var lowerTagName = tagName.toLowerCase();
-      // Don't process the '>' at the end of an end-tag or self-closing tag
-      if (tok.type == "tag" && state.type == "closeTag" ||
-          tok.string.indexOf("/") == (tok.string.length - 1) || // match something like <someTagName />
-          dontCloseTags && indexOf(dontCloseTags, lowerTagName) > -1)
-        return CodeMirror.Pass;
+    var tagName = state.tagName;
+    if (tok.end > pos.ch) tagName = tagName.slice(0, tagName.length - tok.end + pos.ch);
+    var lowerTagName = tagName.toLowerCase();
+    // Don't process the '>' at the end of an end-tag or self-closing tag
+    if (tok.type == "tag" && state.type == "closeTag" ||
+        tok.string.indexOf("/") == (tok.string.length - 1) || // match something like <someTagName />
+        dontCloseTags && indexOf(dontCloseTags, lowerTagName) > -1)
+      return CodeMirror.Pass;
 
-      var doIndent = indentTags && indexOf(indentTags, lowerTagName) > -1;
-      var curPos = doIndent ? CodeMirror.Pos(pos.line + 1, 0) : CodeMirror.Pos(pos.line, pos.ch + 1);
-      cm.replaceSelection(">" + (doIndent ? "\n\n" : "") + "</" + tagName + ">",
-                          {head: curPos, anchor: curPos});
-      if (doIndent) {
-        cm.indentLine(pos.line + 1);
-        cm.indentLine(pos.line + 2);
-      }
-      return;
-    } else if (ch == "/" && tok.string == "<") {
-      var tagName = state.context && state.context.tagName;
-      if (tagName) cm.replaceSelection("/" + tagName + ">", "end");
-      return;
+    var doIndent = indentTags && indexOf(indentTags, lowerTagName) > -1;
+    var curPos = doIndent ? CodeMirror.Pos(pos.line + 1, 0) : CodeMirror.Pos(pos.line, pos.ch + 1);
+    cm.replaceSelection(">" + (doIndent ? "\n\n" : "") + "</" + tagName + ">",
+                        {head: curPos, anchor: curPos});
+    if (doIndent) {
+      cm.indentLine(pos.line + 1);
+      cm.indentLine(pos.line + 2);
     }
-    return CodeMirror.Pass;
+  }
+
+  function autoCloseSlash(cm) {
+    var pos = cm.getCursor(), tok = cm.getTokenAt(pos);
+    var inner = CodeMirror.innerMode(cm.getMode(), tok.state), state = inner.state;
+    if (tok.string != "<" || inner.mode.name != "xml") return CodeMirror.Pass;
+
+    var tagName = state.context && state.context.tagName;
+    if (tagName) cm.replaceSelection("/" + tagName + ">", "end");
   }
 
   function indexOf(collection, elt) {