1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/LICENSE
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
11 })(function(CodeMirror) {
15 autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true,
16 'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true,
17 'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true,
18 'track': true, 'wbr': true, 'menuitem': true},
19 implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true,
20 'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true,
21 'th': true, 'tr': true},
23 'dd': {'dd': true, 'dt': true},
24 'dt': {'dd': true, 'dt': true},
26 'option': {'option': true, 'optgroup': true},
27 'optgroup': {'optgroup': true},
28 'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true,
29 'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true,
30 'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true,
31 'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true,
32 'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true},
33 'rp': {'rp': true, 'rt': true},
34 'rt': {'rp': true, 'rt': true},
35 'tbody': {'tbody': true, 'tfoot': true},
36 'td': {'td': true, 'th': true},
37 'tfoot': {'tbody': true},
38 'th': {'td': true, 'th': true},
39 'thead': {'tbody': true, 'tfoot': true},
42 doNotIndent: {"pre": true},
55 allowMissingTagName: false,
59 CodeMirror.defineMode("xml", function(editorConf, config_) {
60 var indentUnit = editorConf.indentUnit
62 var defaults = config_.htmlMode ? htmlConfig : xmlConfig
63 for (var prop in defaults) config[prop] = defaults[prop]
64 for (var prop in config_) config[prop] = config_[prop]
66 // Return variables for tokenizers
69 function inText(stream, state) {
70 function chain(parser) {
71 state.tokenize = parser;
72 return parser(stream, state);
75 var ch = stream.next();
77 if (stream.eat("!")) {
78 if (stream.eat("[")) {
79 if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>"));
81 } else if (stream.match("--")) {
82 return chain(inBlock("comment", "-->"));
83 } else if (stream.match("DOCTYPE", true, true)) {
84 stream.eatWhile(/[\w\._\-]/);
85 return chain(doctype(1));
89 } else if (stream.eat("?")) {
90 stream.eatWhile(/[\w\._\-]/);
91 state.tokenize = inBlock("meta", "?>");
94 type = stream.eat("/") ? "closeTag" : "openTag";
95 state.tokenize = inTag;
98 } else if (ch == "&") {
100 if (stream.eat("#")) {
101 if (stream.eat("x")) {
102 ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";");
104 ok = stream.eatWhile(/[\d]/) && stream.eat(";");
107 ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";");
109 return ok ? "atom" : "error";
111 stream.eatWhile(/[^&<]/);
115 inText.isInText = true;
117 function inTag(stream, state) {
118 var ch = stream.next();
119 if (ch == ">" || (ch == "/" && stream.eat(">"))) {
120 state.tokenize = inText;
121 type = ch == ">" ? "endTag" : "selfcloseTag";
122 return "tag bracket";
123 } else if (ch == "=") {
126 } else if (ch == "<") {
127 state.tokenize = inText;
128 state.state = baseState;
129 state.tagName = state.tagStart = null;
130 var next = state.tokenize(stream, state);
131 return next ? next + " tag error" : "tag error";
132 } else if (/[\'\"]/.test(ch)) {
133 state.tokenize = inAttribute(ch);
134 state.stringStartCol = stream.column();
135 return state.tokenize(stream, state);
137 stream.match(/^[^\s\u00a0=<>\"\']*[^\s\u00a0=<>\"\'\/]/);
142 function inAttribute(quote) {
143 var closure = function(stream, state) {
144 while (!stream.eol()) {
145 if (stream.next() == quote) {
146 state.tokenize = inTag;
152 closure.isInAttribute = true;
156 function inBlock(style, terminator) {
157 return function(stream, state) {
158 while (!stream.eol()) {
159 if (stream.match(terminator)) {
160 state.tokenize = inText;
169 function doctype(depth) {
170 return function(stream, state) {
172 while ((ch = stream.next()) != null) {
174 state.tokenize = doctype(depth + 1);
175 return state.tokenize(stream, state);
176 } else if (ch == ">") {
178 state.tokenize = inText;
181 state.tokenize = doctype(depth - 1);
182 return state.tokenize(stream, state);
190 function Context(state, tagName, startOfLine) {
191 this.prev = state.context;
192 this.tagName = tagName;
193 this.indent = state.indented;
194 this.startOfLine = startOfLine;
195 if (config.doNotIndent.hasOwnProperty(tagName) || (state.context && state.context.noIndent))
196 this.noIndent = true;
198 function popContext(state) {
199 if (state.context) state.context = state.context.prev;
201 function maybePopContext(state, nextTagName) {
204 if (!state.context) {
207 parentTagName = state.context.tagName;
208 if (!config.contextGrabbers.hasOwnProperty(parentTagName) ||
209 !config.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) {
216 function baseState(type, stream, state) {
217 if (type == "openTag") {
218 state.tagStart = stream.column();
220 } else if (type == "closeTag") {
221 return closeTagNameState;
226 function tagNameState(type, stream, state) {
227 if (type == "word") {
228 state.tagName = stream.current();
231 } else if (config.allowMissingTagName && type == "endTag") {
232 setStyle = "tag bracket";
233 return attrState(type, stream, state);
239 function closeTagNameState(type, stream, state) {
240 if (type == "word") {
241 var tagName = stream.current();
242 if (state.context && state.context.tagName != tagName &&
243 config.implicitlyClosed.hasOwnProperty(state.context.tagName))
245 if ((state.context && state.context.tagName == tagName) || config.matchClosing === false) {
249 setStyle = "tag error";
250 return closeStateErr;
252 } else if (config.allowMissingTagName && type == "endTag") {
253 setStyle = "tag bracket";
254 return closeState(type, stream, state);
257 return closeStateErr;
261 function closeState(type, _stream, state) {
262 if (type != "endTag") {
269 function closeStateErr(type, stream, state) {
271 return closeState(type, stream, state);
274 function attrState(type, _stream, state) {
275 if (type == "word") {
276 setStyle = "attribute";
278 } else if (type == "endTag" || type == "selfcloseTag") {
279 var tagName = state.tagName, tagStart = state.tagStart;
280 state.tagName = state.tagStart = null;
281 if (type == "selfcloseTag" ||
282 config.autoSelfClosers.hasOwnProperty(tagName)) {
283 maybePopContext(state, tagName);
285 maybePopContext(state, tagName);
286 state.context = new Context(state, tagName, tagStart == state.indented);
293 function attrEqState(type, stream, state) {
294 if (type == "equals") return attrValueState;
295 if (!config.allowMissing) setStyle = "error";
296 return attrState(type, stream, state);
298 function attrValueState(type, stream, state) {
299 if (type == "string") return attrContinuedState;
300 if (type == "word" && config.allowUnquoted) {setStyle = "string"; return attrState;}
302 return attrState(type, stream, state);
304 function attrContinuedState(type, stream, state) {
305 if (type == "string") return attrContinuedState;
306 return attrState(type, stream, state);
310 startState: function(baseIndent) {
311 var state = {tokenize: inText,
313 indented: baseIndent || 0,
314 tagName: null, tagStart: null,
316 if (baseIndent != null) state.baseIndent = baseIndent
320 token: function(stream, state) {
321 if (!state.tagName && stream.sol())
322 state.indented = stream.indentation();
324 if (stream.eatSpace()) return null;
326 var style = state.tokenize(stream, state);
327 if ((style || type) && style != "comment") {
329 state.state = state.state(type || style, stream, state);
331 style = setStyle == "error" ? style + " error" : setStyle;
336 indent: function(state, textAfter, fullLine) {
337 var context = state.context;
338 // Indent multi-line strings (e.g. css).
339 if (state.tokenize.isInAttribute) {
340 if (state.tagStart == state.indented)
341 return state.stringStartCol + 1;
343 return state.indented + indentUnit;
345 if (context && context.noIndent) return CodeMirror.Pass;
346 if (state.tokenize != inTag && state.tokenize != inText)
347 return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0;
348 // Indent the starts of attribute names.
350 if (config.multilineTagIndentPastTag !== false)
351 return state.tagStart + state.tagName.length + 2;
353 return state.tagStart + indentUnit * (config.multilineTagIndentFactor || 1);
355 if (config.alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0;
356 var tagAfter = textAfter && /^<(\/)?([\w_:\.-]*)/.exec(textAfter);
357 if (tagAfter && tagAfter[1]) { // Closing tag spotted
359 if (context.tagName == tagAfter[2]) {
360 context = context.prev;
362 } else if (config.implicitlyClosed.hasOwnProperty(context.tagName)) {
363 context = context.prev;
368 } else if (tagAfter) { // Opening tag spotted
370 var grabbers = config.contextGrabbers[context.tagName];
371 if (grabbers && grabbers.hasOwnProperty(tagAfter[2]))
372 context = context.prev;
377 while (context && context.prev && !context.startOfLine)
378 context = context.prev;
379 if (context) return context.indent + indentUnit;
380 else return state.baseIndent || 0;
383 electricInput: /<\/[\s\w:]+>$/,
384 blockCommentStart: "<!--",
385 blockCommentEnd: "-->",
387 configuration: config.htmlMode ? "html" : "xml",
388 helperType: config.htmlMode ? "html" : "xml",
390 skipAttribute: function(state) {
391 if (state.state == attrValueState)
392 state.state = attrState
395 xmlCurrentTag: function(state) {
396 return state.tagName ? {name: state.tagName, close: state.type == "closeTag"} : null
399 xmlCurrentContext: function(state) {
401 for (var cx = state.context; cx; cx = cx.prev)
402 if (cx.tagName) context.push(cx.tagName)
403 return context.reverse()
408 CodeMirror.defineMIME("text/xml", "xml");
409 CodeMirror.defineMIME("application/xml", "xml");
410 if (!CodeMirror.mimeModes.hasOwnProperty("text/html"))
411 CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true});