michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: michael@0: CodeMirror.defineMode("xml", function(config, parserConfig) { michael@0: var indentUnit = config.indentUnit; michael@0: var multilineTagIndentFactor = parserConfig.multilineTagIndentFactor || 1; michael@0: var multilineTagIndentPastTag = parserConfig.multilineTagIndentPastTag; michael@0: if (multilineTagIndentPastTag == null) multilineTagIndentPastTag = true; michael@0: michael@0: var Kludges = parserConfig.htmlMode ? { michael@0: autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true, michael@0: 'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true, michael@0: 'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true, michael@0: 'track': true, 'wbr': true}, michael@0: implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true, michael@0: 'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true, michael@0: 'th': true, 'tr': true}, michael@0: contextGrabbers: { michael@0: 'dd': {'dd': true, 'dt': true}, michael@0: 'dt': {'dd': true, 'dt': true}, michael@0: 'li': {'li': true}, michael@0: 'option': {'option': true, 'optgroup': true}, michael@0: 'optgroup': {'optgroup': true}, michael@0: 'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true, michael@0: 'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true, michael@0: 'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true, michael@0: 'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true, michael@0: 'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true}, michael@0: 'rp': {'rp': true, 'rt': true}, michael@0: 'rt': {'rp': true, 'rt': true}, michael@0: 'tbody': {'tbody': true, 'tfoot': true}, michael@0: 'td': {'td': true, 'th': true}, michael@0: 'tfoot': {'tbody': true}, michael@0: 'th': {'td': true, 'th': true}, michael@0: 'thead': {'tbody': true, 'tfoot': true}, michael@0: 'tr': {'tr': true} michael@0: }, michael@0: doNotIndent: {"pre": true}, michael@0: allowUnquoted: true, michael@0: allowMissing: true, michael@0: caseFold: true michael@0: } : { michael@0: autoSelfClosers: {}, michael@0: implicitlyClosed: {}, michael@0: contextGrabbers: {}, michael@0: doNotIndent: {}, michael@0: allowUnquoted: false, michael@0: allowMissing: false, michael@0: caseFold: false michael@0: }; michael@0: var alignCDATA = parserConfig.alignCDATA; michael@0: michael@0: // Return variables for tokenizers michael@0: var tagName, type, setStyle; michael@0: michael@0: function inText(stream, state) { michael@0: function chain(parser) { michael@0: state.tokenize = parser; michael@0: return parser(stream, state); michael@0: } michael@0: michael@0: var ch = stream.next(); michael@0: if (ch == "<") { michael@0: if (stream.eat("!")) { michael@0: if (stream.eat("[")) { michael@0: if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>")); michael@0: else return null; michael@0: } else if (stream.match("--")) { michael@0: return chain(inBlock("comment", "-->")); michael@0: } else if (stream.match("DOCTYPE", true, true)) { michael@0: stream.eatWhile(/[\w\._\-]/); michael@0: return chain(doctype(1)); michael@0: } else { michael@0: return null; michael@0: } michael@0: } else if (stream.eat("?")) { michael@0: stream.eatWhile(/[\w\._\-]/); michael@0: state.tokenize = inBlock("meta", "?>"); michael@0: return "meta"; michael@0: } else { michael@0: var isClose = stream.eat("/"); michael@0: tagName = ""; michael@0: var c; michael@0: while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c; michael@0: if (Kludges.caseFold) tagName = tagName.toLowerCase(); michael@0: if (!tagName) return "tag error"; michael@0: type = isClose ? "closeTag" : "openTag"; michael@0: state.tokenize = inTag; michael@0: return "tag"; michael@0: } michael@0: } else if (ch == "&") { michael@0: var ok; michael@0: if (stream.eat("#")) { michael@0: if (stream.eat("x")) { michael@0: ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";"); michael@0: } else { michael@0: ok = stream.eatWhile(/[\d]/) && stream.eat(";"); michael@0: } michael@0: } else { michael@0: ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";"); michael@0: } michael@0: return ok ? "atom" : "error"; michael@0: } else { michael@0: stream.eatWhile(/[^&<]/); michael@0: return null; michael@0: } michael@0: } michael@0: michael@0: function inTag(stream, state) { michael@0: var ch = stream.next(); michael@0: if (ch == ">" || (ch == "/" && stream.eat(">"))) { michael@0: state.tokenize = inText; michael@0: type = ch == ">" ? "endTag" : "selfcloseTag"; michael@0: return "tag"; michael@0: } else if (ch == "=") { michael@0: type = "equals"; michael@0: return null; michael@0: } else if (ch == "<") { michael@0: state.tokenize = inText; michael@0: state.state = baseState; michael@0: state.tagName = state.tagStart = null; michael@0: var next = state.tokenize(stream, state); michael@0: return next ? next + " error" : "error"; michael@0: } else if (/[\'\"]/.test(ch)) { michael@0: state.tokenize = inAttribute(ch); michael@0: state.stringStartCol = stream.column(); michael@0: return state.tokenize(stream, state); michael@0: } else { michael@0: stream.eatWhile(/[^\s\u00a0=<>\"\']/); michael@0: return "word"; michael@0: } michael@0: } michael@0: michael@0: function inAttribute(quote) { michael@0: var closure = function(stream, state) { michael@0: while (!stream.eol()) { michael@0: if (stream.next() == quote) { michael@0: state.tokenize = inTag; michael@0: break; michael@0: } michael@0: } michael@0: return "string"; michael@0: }; michael@0: closure.isInAttribute = true; michael@0: return closure; michael@0: } michael@0: michael@0: function inBlock(style, terminator) { michael@0: return function(stream, state) { michael@0: while (!stream.eol()) { michael@0: if (stream.match(terminator)) { michael@0: state.tokenize = inText; michael@0: break; michael@0: } michael@0: stream.next(); michael@0: } michael@0: return style; michael@0: }; michael@0: } michael@0: function doctype(depth) { michael@0: return function(stream, state) { michael@0: var ch; michael@0: while ((ch = stream.next()) != null) { michael@0: if (ch == "<") { michael@0: state.tokenize = doctype(depth + 1); michael@0: return state.tokenize(stream, state); michael@0: } else if (ch == ">") { michael@0: if (depth == 1) { michael@0: state.tokenize = inText; michael@0: break; michael@0: } else { michael@0: state.tokenize = doctype(depth - 1); michael@0: return state.tokenize(stream, state); michael@0: } michael@0: } michael@0: } michael@0: return "meta"; michael@0: }; michael@0: } michael@0: michael@0: function Context(state, tagName, startOfLine) { michael@0: this.prev = state.context; michael@0: this.tagName = tagName; michael@0: this.indent = state.indented; michael@0: this.startOfLine = startOfLine; michael@0: if (Kludges.doNotIndent.hasOwnProperty(tagName) || (state.context && state.context.noIndent)) michael@0: this.noIndent = true; michael@0: } michael@0: function popContext(state) { michael@0: if (state.context) state.context = state.context.prev; michael@0: } michael@0: function maybePopContext(state, nextTagName) { michael@0: var parentTagName; michael@0: while (true) { michael@0: if (!state.context) { michael@0: return; michael@0: } michael@0: parentTagName = state.context.tagName; michael@0: if (!Kludges.contextGrabbers.hasOwnProperty(parentTagName) || michael@0: !Kludges.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) { michael@0: return; michael@0: } michael@0: popContext(state); michael@0: } michael@0: } michael@0: michael@0: function baseState(type, stream, state) { michael@0: if (type == "openTag") { michael@0: state.tagName = tagName; michael@0: state.tagStart = stream.column(); michael@0: return attrState; michael@0: } else if (type == "closeTag") { michael@0: var err = false; michael@0: if (state.context) { michael@0: if (state.context.tagName != tagName) { michael@0: if (Kludges.implicitlyClosed.hasOwnProperty(state.context.tagName)) michael@0: popContext(state); michael@0: err = !state.context || state.context.tagName != tagName; michael@0: } michael@0: } else { michael@0: err = true; michael@0: } michael@0: if (err) setStyle = "error"; michael@0: return err ? closeStateErr : closeState; michael@0: } else { michael@0: return baseState; michael@0: } michael@0: } michael@0: michael@0: function closeState(type, _stream, state) { michael@0: if (type != "endTag") { michael@0: setStyle = "error"; michael@0: return closeState; michael@0: } michael@0: popContext(state); michael@0: return baseState; michael@0: } michael@0: function closeStateErr(type, stream, state) { michael@0: setStyle = "error"; michael@0: return closeState(type, stream, state); michael@0: } michael@0: michael@0: function attrState(type, _stream, state) { michael@0: if (type == "word") { michael@0: setStyle = "attribute"; michael@0: return attrEqState; michael@0: } else if (type == "endTag" || type == "selfcloseTag") { michael@0: var tagName = state.tagName, tagStart = state.tagStart; michael@0: state.tagName = state.tagStart = null; michael@0: if (type == "selfcloseTag" || michael@0: Kludges.autoSelfClosers.hasOwnProperty(tagName)) { michael@0: maybePopContext(state, tagName); michael@0: } else { michael@0: maybePopContext(state, tagName); michael@0: state.context = new Context(state, tagName, tagStart == state.indented); michael@0: } michael@0: return baseState; michael@0: } michael@0: setStyle = "error"; michael@0: return attrState; michael@0: } michael@0: function attrEqState(type, stream, state) { michael@0: if (type == "equals") return attrValueState; michael@0: if (!Kludges.allowMissing) setStyle = "error"; michael@0: return attrState(type, stream, state); michael@0: } michael@0: function attrValueState(type, stream, state) { michael@0: if (type == "string") return attrContinuedState; michael@0: if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return attrState;} michael@0: setStyle = "error"; michael@0: return attrState(type, stream, state); michael@0: } michael@0: function attrContinuedState(type, stream, state) { michael@0: if (type == "string") return attrContinuedState; michael@0: return attrState(type, stream, state); michael@0: } michael@0: michael@0: return { michael@0: startState: function() { michael@0: return {tokenize: inText, michael@0: state: baseState, michael@0: indented: 0, michael@0: tagName: null, tagStart: null, michael@0: context: null}; michael@0: }, michael@0: michael@0: token: function(stream, state) { michael@0: if (!state.tagName && stream.sol()) michael@0: state.indented = stream.indentation(); michael@0: michael@0: if (stream.eatSpace()) return null; michael@0: tagName = type = null; michael@0: var style = state.tokenize(stream, state); michael@0: if ((style || type) && style != "comment") { michael@0: setStyle = null; michael@0: state.state = state.state(type || style, stream, state); michael@0: if (setStyle) michael@0: style = setStyle == "error" ? style + " error" : setStyle; michael@0: } michael@0: return style; michael@0: }, michael@0: michael@0: indent: function(state, textAfter, fullLine) { michael@0: var context = state.context; michael@0: // Indent multi-line strings (e.g. css). michael@0: if (state.tokenize.isInAttribute) { michael@0: return state.stringStartCol + 1; michael@0: } michael@0: if (context && context.noIndent) return CodeMirror.Pass; michael@0: if (state.tokenize != inTag && state.tokenize != inText) michael@0: return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0; michael@0: // Indent the starts of attribute names. michael@0: if (state.tagName) { michael@0: if (multilineTagIndentPastTag) michael@0: return state.tagStart + state.tagName.length + 2; michael@0: else michael@0: return state.tagStart + indentUnit * multilineTagIndentFactor; michael@0: } michael@0: if (alignCDATA && /", michael@0: michael@0: configuration: parserConfig.htmlMode ? "html" : "xml", michael@0: helperType: parserConfig.htmlMode ? "html" : "xml" michael@0: }; michael@0: }); michael@0: michael@0: CodeMirror.defineMIME("text/xml", "xml"); michael@0: CodeMirror.defineMIME("application/xml", "xml"); michael@0: if (!CodeMirror.mimeModes.hasOwnProperty("text/html")) michael@0: CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true}); michael@0: michael@0: });