|
94 | 94 | }
|
95 | 95 | }
|
96 | 96 |
|
97 |
| - function autoCloseSlash(cm) { |
98 |
| - if (cm.getOption("disableInput")) return CodeMirror.Pass; |
| 97 | + function autoCloseCurrent(cm, typingSlash) { |
99 | 98 | var ranges = cm.listSelections(), replacements = [];
|
| 99 | + var head = typingSlash ? "/" : "</"; |
100 | 100 | for (var i = 0; i < ranges.length; i++) {
|
101 | 101 | if (!ranges[i].empty()) return CodeMirror.Pass;
|
102 | 102 | var pos = ranges[i].head, tok = cm.getTokenAt(pos);
|
103 | 103 | var inner = CodeMirror.innerMode(cm.getMode(), tok.state), state = inner.state;
|
104 |
| - if (tok.type == "string" || tok.string.charAt(0) != "<" || |
105 |
| - tok.start != pos.ch - 1) |
| 104 | + if (typingSlash && (tok.type == "string" || tok.string.charAt(0) != "<" || |
| 105 | + tok.start != pos.ch - 1)) |
106 | 106 | return CodeMirror.Pass;
|
107 | 107 | // Kludge to get around the fact that we are not in XML mode
|
108 | 108 | // when completing in JS/CSS snippet in htmlmixed mode. Does not
|
109 | 109 | // work for other XML embedded languages (there is no general
|
110 | 110 | // way to go from a mixed mode to its current XML state).
|
111 | 111 | if (inner.mode.name != "xml") {
|
112 | 112 | if (cm.getMode().name == "htmlmixed" && inner.mode.name == "javascript")
|
113 |
| - replacements[i] = "/script>"; |
| 113 | + replacements[i] = head + "script>"; |
114 | 114 | else if (cm.getMode().name == "htmlmixed" && inner.mode.name == "css")
|
115 |
| - replacements[i] = "/style>"; |
| 115 | + replacements[i] = head + "style>"; |
116 | 116 | else
|
117 | 117 | return CodeMirror.Pass;
|
118 | 118 | } else {
|
119 | 119 | if (!state.context || !state.context.tagName ||
|
120 | 120 | closingTagExists(cm, state.context.tagName, pos, state))
|
121 | 121 | return CodeMirror.Pass;
|
122 |
| - replacements[i] = "/" + state.context.tagName + ">"; |
| 122 | + replacements[i] = head + state.context.tagName + ">"; |
123 | 123 | }
|
124 | 124 | }
|
125 | 125 | cm.replaceSelections(replacements);
|
|
129 | 129 | cm.indentLine(ranges[i].head.line);
|
130 | 130 | }
|
131 | 131 |
|
| 132 | + function autoCloseSlash(cm) { |
| 133 | + if (cm.getOption("disableInput")) return CodeMirror.Pass; |
| 134 | + autoCloseCurrent(cm, true); |
| 135 | + } |
| 136 | + |
| 137 | + CodeMirror.commands.closeTag = function(cm) { return autoCloseCurrent(cm); }; |
| 138 | + |
132 | 139 | function indexOf(collection, elt) {
|
133 | 140 | if (collection.indexOf) return collection.indexOf(elt);
|
134 | 141 | for (var i = 0, e = collection.length; i < e; ++i)
|
|
0 commit comments