1 CodeMirror.defineMode("xml", function(config, parserConfig) {
2 var indentUnit = config.indentUnit;
3 var Kludges = parserConfig.htmlMode ? {
4 autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true,
5 'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true,
6 'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true,
7 'track': true, 'wbr': true},
8 implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true,
9 'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true,
10 'th': true, 'tr': true},
12 'dd': {'dd': true, 'dt': true},
13 'dt': {'dd': true, 'dt': true},
15 'option': {'option': true, 'optgroup': true},
16 'optgroup': {'optgroup': true},
17 'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true,
18 'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true,
19 'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true,
20 'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true,
21 'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true},
22 'rp': {'rp': true, 'rt': true},
23 'rt': {'rp': true, 'rt': true},
24 'tbody': {'tbody': true, 'tfoot': true},
25 'td': {'td': true, 'th': true},
26 'tfoot': {'tbody': true},
27 'th': {'td': true, 'th': true},
28 'thead': {'tbody': true, 'tfoot': true},
31 doNotIndent: {"pre": true},
42 var alignCDATA = parserConfig.alignCDATA;
44 // Return variables for tokenizers
47 function inText(stream, state) {
48 function chain(parser) {
49 state.tokenize = parser;
50 return parser(stream, state);
53 var ch = stream.next();
55 if (stream.eat("!")) {
56 if (stream.eat("[")) {
57 if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>"));
60 else if (stream.match("--")) return chain(inBlock("comment", "-->"));
61 else if (stream.match("DOCTYPE", true, true)) {
62 stream.eatWhile(/[\w\._\-]/);
63 return chain(doctype(1));
67 else if (stream.eat("?")) {
68 stream.eatWhile(/[\w\._\-]/);
69 state.tokenize = inBlock("meta", "?>");
73 var isClose = stream.eat("/");
76 while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
77 if (!tagName) return "error";
78 type = isClose ? "closeTag" : "openTag";
79 state.tokenize = inTag;
85 if (stream.eat("#")) {
86 if (stream.eat("x")) {
87 ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";");
89 ok = stream.eatWhile(/[\d]/) && stream.eat(";");
92 ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";");
94 return ok ? "atom" : "error";
97 stream.eatWhile(/[^&<]/);
102 function inTag(stream, state) {
103 var ch = stream.next();
104 if (ch == ">" || (ch == "/" && stream.eat(">"))) {
105 state.tokenize = inText;
106 type = ch == ">" ? "endTag" : "selfcloseTag";
109 else if (ch == "=") {
113 else if (/[\'\"]/.test(ch)) {
114 state.tokenize = inAttribute(ch);
115 return state.tokenize(stream, state);
118 stream.eatWhile(/[^\s\u00a0=<>\"\']/);
123 function inAttribute(quote) {
124 return function(stream, state) {
125 while (!stream.eol()) {
126 if (stream.next() == quote) {
127 state.tokenize = inTag;
135 function inBlock(style, terminator) {
136 return function(stream, state) {
137 while (!stream.eol()) {
138 if (stream.match(terminator)) {
139 state.tokenize = inText;
147 function doctype(depth) {
148 return function(stream, state) {
150 while ((ch = stream.next()) != null) {
152 state.tokenize = doctype(depth + 1);
153 return state.tokenize(stream, state);
154 } else if (ch == ">") {
156 state.tokenize = inText;
159 state.tokenize = doctype(depth - 1);
160 return state.tokenize(stream, state);
168 var curState, setStyle;
170 for (var i = arguments.length - 1; i >= 0; i--) curState.cc.push(arguments[i]);
173 pass.apply(null, arguments);
177 function pushContext(tagName, startOfLine) {
178 var noIndent = Kludges.doNotIndent.hasOwnProperty(tagName) || (curState.context && curState.context.noIndent);
180 prev: curState.context,
182 indent: curState.indented,
183 startOfLine: startOfLine,
187 function popContext() {
188 if (curState.context) curState.context = curState.context.prev;
191 function element(type) {
192 if (type == "openTag") {
193 curState.tagName = tagName;
194 return cont(attributes, endtag(curState.startOfLine));
195 } else if (type == "closeTag") {
197 if (curState.context) {
198 if (curState.context.tagName != tagName) {
199 if (Kludges.implicitlyClosed.hasOwnProperty(curState.context.tagName.toLowerCase())) {
202 err = !curState.context || curState.context.tagName != tagName;
207 if (err) setStyle = "error";
208 return cont(endclosetag(err));
212 function endtag(startOfLine) {
213 return function(type) {
214 var tagName = curState.tagName;
215 curState.tagName = null;
216 if (type == "selfcloseTag" ||
217 (type == "endTag" && Kludges.autoSelfClosers.hasOwnProperty(tagName.toLowerCase()))) {
218 maybePopContext(tagName.toLowerCase());
221 if (type == "endTag") {
222 maybePopContext(tagName.toLowerCase());
223 pushContext(tagName, startOfLine);
229 function endclosetag(err) {
230 return function(type) {
231 if (err) setStyle = "error";
232 if (type == "endTag") { popContext(); return cont(); }
234 return cont(arguments.callee);
237 function maybePopContext(nextTagName) {
240 if (!curState.context) {
243 parentTagName = curState.context.tagName.toLowerCase();
244 if (!Kludges.contextGrabbers.hasOwnProperty(parentTagName) ||
245 !Kludges.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) {
252 function attributes(type) {
253 if (type == "word") {setStyle = "attribute"; return cont(attribute, attributes);}
254 if (type == "endTag" || type == "selfcloseTag") return pass();
256 return cont(attributes);
258 function attribute(type) {
259 if (type == "equals") return cont(attvalue, attributes);
260 if (!Kludges.allowMissing) setStyle = "error";
261 else if (type == "word") setStyle = "attribute";
262 return (type == "endTag" || type == "selfcloseTag") ? pass() : cont();
264 function attvalue(type) {
265 if (type == "string") return cont(attvaluemaybe);
266 if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return cont();}
268 return (type == "endTag" || type == "selfCloseTag") ? pass() : cont();
270 function attvaluemaybe(type) {
271 if (type == "string") return cont(attvaluemaybe);
276 startState: function() {
277 return {tokenize: inText, cc: [], indented: 0, startOfLine: true, tagName: null, context: null};
280 token: function(stream, state) {
282 state.startOfLine = true;
283 state.indented = stream.indentation();
285 if (stream.eatSpace()) return null;
287 setStyle = type = tagName = null;
288 var style = state.tokenize(stream, state);
290 if ((style || type) && style != "comment") {
293 var comb = state.cc.pop() || element;
294 if (comb(type || style)) break;
297 state.startOfLine = false;
298 return setStyle || style;
301 indent: function(state, textAfter, fullLine) {
302 var context = state.context;
303 if ((state.tokenize != inTag && state.tokenize != inText) ||
304 context && context.noIndent)
305 return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0;
306 if (alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0;
307 if (context && /^<\//.test(textAfter))
308 context = context.prev;
309 while (context && !context.startOfLine)
310 context = context.prev;
311 if (context) return context.indent + indentUnit;
317 configuration: parserConfig.htmlMode ? "html" : "xml"
321 CodeMirror.defineMIME("text/xml", "xml");
322 CodeMirror.defineMIME("application/xml", "xml");
323 if (!CodeMirror.mimeModes.hasOwnProperty("text/html"))
324 CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true});