xml.js
326 lines
| 10.0 KiB
| application/javascript
|
JavascriptLexer
Brian E. Granger
|
r4504 | CodeMirror.defineMode("xml", function(config, parserConfig) { | ||
var indentUnit = config.indentUnit; | ||||
var Kludges = parserConfig.htmlMode ? { | ||||
Matthias BUSSONNIER
|
r8053 | autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true, | ||
'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true, | ||||
'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true, | ||||
'track': true, 'wbr': true}, | ||||
implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true, | ||||
'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true, | ||||
'th': true, 'tr': true}, | ||||
contextGrabbers: { | ||||
'dd': {'dd': true, 'dt': true}, | ||||
'dt': {'dd': true, 'dt': true}, | ||||
'li': {'li': true}, | ||||
'option': {'option': true, 'optgroup': true}, | ||||
'optgroup': {'optgroup': true}, | ||||
'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true, | ||||
'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true, | ||||
'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true, | ||||
'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true, | ||||
'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true}, | ||||
'rp': {'rp': true, 'rt': true}, | ||||
'rt': {'rp': true, 'rt': true}, | ||||
'tbody': {'tbody': true, 'tfoot': true}, | ||||
'td': {'td': true, 'th': true}, | ||||
'tfoot': {'tbody': true}, | ||||
'th': {'td': true, 'th': true}, | ||||
'thead': {'tbody': true, 'tfoot': true}, | ||||
'tr': {'tr': true} | ||||
}, | ||||
Brian Granger
|
r5941 | doNotIndent: {"pre": true}, | ||
Matthias BUSSONNIER
|
r8053 | allowUnquoted: true, | ||
allowMissing: false | ||||
} : { | ||||
autoSelfClosers: {}, | ||||
implicitlyClosed: {}, | ||||
contextGrabbers: {}, | ||||
doNotIndent: {}, | ||||
allowUnquoted: false, | ||||
allowMissing: false | ||||
}; | ||||
Brian E. Granger
|
r4504 | var alignCDATA = parserConfig.alignCDATA; | ||
// Return variables for tokenizers | ||||
var tagName, type; | ||||
function inText(stream, state) { | ||||
function chain(parser) { | ||||
state.tokenize = parser; | ||||
return parser(stream, state); | ||||
} | ||||
var ch = stream.next(); | ||||
if (ch == "<") { | ||||
if (stream.eat("!")) { | ||||
if (stream.eat("[")) { | ||||
if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>")); | ||||
else return null; | ||||
} | ||||
else if (stream.match("--")) return chain(inBlock("comment", "-->")); | ||||
else if (stream.match("DOCTYPE", true, true)) { | ||||
stream.eatWhile(/[\w\._\-]/); | ||||
Brian Granger
|
r5941 | return chain(doctype(1)); | ||
Brian E. Granger
|
r4504 | } | ||
else return null; | ||||
} | ||||
else if (stream.eat("?")) { | ||||
stream.eatWhile(/[\w\._\-]/); | ||||
state.tokenize = inBlock("meta", "?>"); | ||||
return "meta"; | ||||
} | ||||
else { | ||||
type = stream.eat("/") ? "closeTag" : "openTag"; | ||||
stream.eatSpace(); | ||||
tagName = ""; | ||||
var c; | ||||
while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c; | ||||
state.tokenize = inTag; | ||||
return "tag"; | ||||
} | ||||
} | ||||
else if (ch == "&") { | ||||
Brian Granger
|
r5970 | var ok; | ||
if (stream.eat("#")) { | ||||
if (stream.eat("x")) { | ||||
ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";"); | ||||
} else { | ||||
ok = stream.eatWhile(/[\d]/) && stream.eat(";"); | ||||
} | ||||
} else { | ||||
Matthias BUSSONNIER
|
r8053 | ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";"); | ||
Brian Granger
|
r5970 | } | ||
return ok ? "atom" : "error"; | ||||
Brian E. Granger
|
r4504 | } | ||
else { | ||||
stream.eatWhile(/[^&<]/); | ||||
return null; | ||||
} | ||||
} | ||||
function inTag(stream, state) { | ||||
var ch = stream.next(); | ||||
if (ch == ">" || (ch == "/" && stream.eat(">"))) { | ||||
state.tokenize = inText; | ||||
type = ch == ">" ? "endTag" : "selfcloseTag"; | ||||
return "tag"; | ||||
} | ||||
else if (ch == "=") { | ||||
type = "equals"; | ||||
return null; | ||||
} | ||||
else if (/[\'\"]/.test(ch)) { | ||||
state.tokenize = inAttribute(ch); | ||||
return state.tokenize(stream, state); | ||||
} | ||||
else { | ||||
stream.eatWhile(/[^\s\u00a0=<>\"\'\/?]/); | ||||
return "word"; | ||||
} | ||||
} | ||||
function inAttribute(quote) { | ||||
return function(stream, state) { | ||||
while (!stream.eol()) { | ||||
if (stream.next() == quote) { | ||||
state.tokenize = inTag; | ||||
break; | ||||
} | ||||
} | ||||
return "string"; | ||||
}; | ||||
} | ||||
function inBlock(style, terminator) { | ||||
return function(stream, state) { | ||||
while (!stream.eol()) { | ||||
if (stream.match(terminator)) { | ||||
state.tokenize = inText; | ||||
break; | ||||
} | ||||
stream.next(); | ||||
} | ||||
return style; | ||||
}; | ||||
} | ||||
Brian Granger
|
r5941 | function doctype(depth) { | ||
return function(stream, state) { | ||||
var ch; | ||||
while ((ch = stream.next()) != null) { | ||||
if (ch == "<") { | ||||
state.tokenize = doctype(depth + 1); | ||||
return state.tokenize(stream, state); | ||||
} else if (ch == ">") { | ||||
if (depth == 1) { | ||||
state.tokenize = inText; | ||||
break; | ||||
} else { | ||||
state.tokenize = doctype(depth - 1); | ||||
return state.tokenize(stream, state); | ||||
} | ||||
} | ||||
} | ||||
return "meta"; | ||||
}; | ||||
} | ||||
Brian E. Granger
|
r4504 | |||
var curState, setStyle; | ||||
function pass() { | ||||
for (var i = arguments.length - 1; i >= 0; i--) curState.cc.push(arguments[i]); | ||||
} | ||||
function cont() { | ||||
pass.apply(null, arguments); | ||||
return true; | ||||
} | ||||
function pushContext(tagName, startOfLine) { | ||||
var noIndent = Kludges.doNotIndent.hasOwnProperty(tagName) || (curState.context && curState.context.noIndent); | ||||
curState.context = { | ||||
prev: curState.context, | ||||
tagName: tagName, | ||||
indent: curState.indented, | ||||
startOfLine: startOfLine, | ||||
noIndent: noIndent | ||||
}; | ||||
} | ||||
function popContext() { | ||||
if (curState.context) curState.context = curState.context.prev; | ||||
} | ||||
function element(type) { | ||||
Brian Granger
|
r5941 | if (type == "openTag") { | ||
curState.tagName = tagName; | ||||
return cont(attributes, endtag(curState.startOfLine)); | ||||
} else if (type == "closeTag") { | ||||
Brian E. Granger
|
r4504 | var err = false; | ||
if (curState.context) { | ||||
Matthias BUSSONNIER
|
r8053 | if (curState.context.tagName != tagName) { | ||
if (Kludges.implicitlyClosed.hasOwnProperty(curState.context.tagName.toLowerCase())) { | ||||
popContext(); | ||||
} | ||||
err = !curState.context || curState.context.tagName != tagName; | ||||
} | ||||
Brian E. Granger
|
r4504 | } else { | ||
err = true; | ||||
} | ||||
if (err) setStyle = "error"; | ||||
return cont(endclosetag(err)); | ||||
} | ||||
Brian Granger
|
r5941 | return cont(); | ||
Brian E. Granger
|
r4504 | } | ||
function endtag(startOfLine) { | ||||
return function(type) { | ||||
if (type == "selfcloseTag" || | ||||
Matthias BUSSONNIER
|
r8053 | (type == "endTag" && Kludges.autoSelfClosers.hasOwnProperty(curState.tagName.toLowerCase()))) { | ||
maybePopContext(curState.tagName.toLowerCase()); | ||||
Brian E. Granger
|
r4504 | return cont(); | ||
Matthias BUSSONNIER
|
r8053 | } | ||
if (type == "endTag") { | ||||
maybePopContext(curState.tagName.toLowerCase()); | ||||
pushContext(curState.tagName, startOfLine); | ||||
return cont(); | ||||
} | ||||
Brian E. Granger
|
r4504 | return cont(); | ||
}; | ||||
} | ||||
function endclosetag(err) { | ||||
return function(type) { | ||||
if (err) setStyle = "error"; | ||||
Fernando Perez
|
r4933 | if (type == "endTag") { popContext(); return cont(); } | ||
setStyle = "error"; | ||||
return cont(arguments.callee); | ||||
Brian E. Granger
|
r4504 | } | ||
} | ||||
Matthias BUSSONNIER
|
r8053 | function maybePopContext(nextTagName) { | ||
var parentTagName; | ||||
while (true) { | ||||
if (!curState.context) { | ||||
return; | ||||
} | ||||
parentTagName = curState.context.tagName.toLowerCase(); | ||||
if (!Kludges.contextGrabbers.hasOwnProperty(parentTagName) || | ||||
!Kludges.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) { | ||||
return; | ||||
} | ||||
popContext(); | ||||
} | ||||
} | ||||
Brian E. Granger
|
r4504 | |||
function attributes(type) { | ||||
Matthias BUSSONNIER
|
r8053 | if (type == "word") {setStyle = "attribute"; return cont(attribute, attributes);} | ||
if (type == "endTag" || type == "selfcloseTag") return pass(); | ||||
setStyle = "error"; | ||||
return cont(attributes); | ||||
} | ||||
function attribute(type) { | ||||
Brian E. Granger
|
r4504 | if (type == "equals") return cont(attvalue, attributes); | ||
Matthias BUSSONNIER
|
r8053 | if (!Kludges.allowMissing) setStyle = "error"; | ||
return (type == "endTag" || type == "selfcloseTag") ? pass() : cont(); | ||||
Brian E. Granger
|
r4504 | } | ||
function attvalue(type) { | ||||
if (type == "string") return cont(attvaluemaybe); | ||||
Matthias BUSSONNIER
|
r8053 | if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return cont();} | ||
setStyle = "error"; | ||||
return (type == "endTag" || type == "selfCloseTag") ? pass() : cont(); | ||||
Brian E. Granger
|
r4504 | } | ||
function attvaluemaybe(type) { | ||||
if (type == "string") return cont(attvaluemaybe); | ||||
else return pass(); | ||||
} | ||||
return { | ||||
startState: function() { | ||||
return {tokenize: inText, cc: [], indented: 0, startOfLine: true, tagName: null, context: null}; | ||||
}, | ||||
token: function(stream, state) { | ||||
if (stream.sol()) { | ||||
state.startOfLine = true; | ||||
state.indented = stream.indentation(); | ||||
} | ||||
if (stream.eatSpace()) return null; | ||||
setStyle = type = tagName = null; | ||||
var style = state.tokenize(stream, state); | ||||
Brian Granger
|
r5941 | state.type = type; | ||
Brian E. Granger
|
r4504 | if ((style || type) && style != "comment") { | ||
curState = state; | ||||
while (true) { | ||||
var comb = state.cc.pop() || element; | ||||
if (comb(type || style)) break; | ||||
} | ||||
} | ||||
state.startOfLine = false; | ||||
return setStyle || style; | ||||
}, | ||||
Brian Granger
|
r5941 | indent: function(state, textAfter, fullLine) { | ||
Brian E. Granger
|
r4504 | var context = state.context; | ||
Brian Granger
|
r5941 | if ((state.tokenize != inTag && state.tokenize != inText) || | ||
context && context.noIndent) | ||||
return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0; | ||||
Brian E. Granger
|
r4504 | if (alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0; | ||
if (context && /^<\//.test(textAfter)) | ||||
context = context.prev; | ||||
while (context && !context.startOfLine) | ||||
context = context.prev; | ||||
if (context) return context.indent + indentUnit; | ||||
else return 0; | ||||
}, | ||||
compareStates: function(a, b) { | ||||
Brian Granger
|
r5941 | if (a.indented != b.indented || a.tokenize != b.tokenize) return false; | ||
Brian E. Granger
|
r4504 | for (var ca = a.context, cb = b.context; ; ca = ca.prev, cb = cb.prev) { | ||
if (!ca || !cb) return ca == cb; | ||||
Matthias BUSSONNIER
|
r8053 | if (ca.tagName != cb.tagName || ca.indent != cb.indent) return false; | ||
Brian E. Granger
|
r4504 | } | ||
}, | ||||
electricChars: "/" | ||||
}; | ||||
}); | ||||
Matthias BUSSONNIER
|
r8053 | CodeMirror.defineMIME("text/xml", "xml"); | ||
Brian E. Granger
|
r4504 | CodeMirror.defineMIME("application/xml", "xml"); | ||
Matthias BUSSONNIER
|
r8053 | if (!CodeMirror.mimeModes.hasOwnProperty("text/html")) | ||
CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true}); | ||||