xml.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. CodeMirror.defineMode("xml", function(config, parserConfig) {
  2. var indentUnit = config.indentUnit;
  3. var multilineTagIndentFactor = parserConfig.multilineTagIndentFactor || 1;
  4. var multilineTagIndentPastTag = parserConfig.multilineTagIndentPastTag;
  5. if (multilineTagIndentPastTag == null) multilineTagIndentPastTag = true;
  6. var Kludges = parserConfig.htmlMode ? {
  7. autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true,
  8. 'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true,
  9. 'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true,
  10. 'track': true, 'wbr': true},
  11. implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true,
  12. 'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true,
  13. 'th': true, 'tr': true},
  14. contextGrabbers: {
  15. 'dd': {'dd': true, 'dt': true},
  16. 'dt': {'dd': true, 'dt': true},
  17. 'li': {'li': true},
  18. 'option': {'option': true, 'optgroup': true},
  19. 'optgroup': {'optgroup': true},
  20. 'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true,
  21. 'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true,
  22. 'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true,
  23. 'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true,
  24. 'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true},
  25. 'rp': {'rp': true, 'rt': true},
  26. 'rt': {'rp': true, 'rt': true},
  27. 'tbody': {'tbody': true, 'tfoot': true},
  28. 'td': {'td': true, 'th': true},
  29. 'tfoot': {'tbody': true},
  30. 'th': {'td': true, 'th': true},
  31. 'thead': {'tbody': true, 'tfoot': true},
  32. 'tr': {'tr': true}
  33. },
  34. doNotIndent: {"pre": true},
  35. allowUnquoted: true,
  36. allowMissing: true
  37. } : {
  38. autoSelfClosers: {},
  39. implicitlyClosed: {},
  40. contextGrabbers: {},
  41. doNotIndent: {},
  42. allowUnquoted: false,
  43. allowMissing: false
  44. };
  45. var alignCDATA = parserConfig.alignCDATA;
  46. // Return variables for tokenizers
  47. var tagName, type, setStyle;
  48. function inText(stream, state) {
  49. function chain(parser) {
  50. state.tokenize = parser;
  51. return parser(stream, state);
  52. }
  53. var ch = stream.next();
  54. if (ch == "<") {
  55. if (stream.eat("!")) {
  56. if (stream.eat("[")) {
  57. if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>"));
  58. else return null;
  59. } else if (stream.match("--")) {
  60. return chain(inBlock("comment", "-->"));
  61. } else if (stream.match("DOCTYPE", true, true)) {
  62. stream.eatWhile(/[\w\._\-]/);
  63. return chain(doctype(1));
  64. } else {
  65. return null;
  66. }
  67. } else if (stream.eat("?")) {
  68. stream.eatWhile(/[\w\._\-]/);
  69. state.tokenize = inBlock("meta", "?>");
  70. return "meta";
  71. } else {
  72. var isClose = stream.eat("/");
  73. tagName = "";
  74. var c;
  75. while ((c = stream.eat(/[^\s\u00a0=<>\"\'\/?]/))) tagName += c;
  76. if (!tagName) return "tag error";
  77. type = isClose ? "closeTag" : "openTag";
  78. state.tokenize = inTag;
  79. return "tag";
  80. }
  81. } else if (ch == "&") {
  82. var ok;
  83. if (stream.eat("#")) {
  84. if (stream.eat("x")) {
  85. ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";");
  86. } else {
  87. ok = stream.eatWhile(/[\d]/) && stream.eat(";");
  88. }
  89. } else {
  90. ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";");
  91. }
  92. return ok ? "atom" : "error";
  93. } else {
  94. stream.eatWhile(/[^&<]/);
  95. return null;
  96. }
  97. }
  98. function inTag(stream, state) {
  99. var ch = stream.next();
  100. if (ch == ">" || (ch == "/" && stream.eat(">"))) {
  101. state.tokenize = inText;
  102. type = ch == ">" ? "endTag" : "selfcloseTag";
  103. return "tag";
  104. } else if (ch == "=") {
  105. type = "equals";
  106. return null;
  107. } else if (ch == "<") {
  108. state.tokenize = inText;
  109. state.state = baseState;
  110. state.tagName = state.tagStart = null;
  111. var next = state.tokenize(stream, state);
  112. return next ? next + " error" : "error";
  113. } else if (/[\'\"]/.test(ch)) {
  114. state.tokenize = inAttribute(ch);
  115. state.stringStartCol = stream.column();
  116. return state.tokenize(stream, state);
  117. } else {
  118. stream.eatWhile(/[^\s\u00a0=<>\"\']/);
  119. return "word";
  120. }
  121. }
  122. function inAttribute(quote) {
  123. var closure = function(stream, state) {
  124. while (!stream.eol()) {
  125. if (stream.next() == quote) {
  126. state.tokenize = inTag;
  127. break;
  128. }
  129. }
  130. return "string";
  131. };
  132. closure.isInAttribute = true;
  133. return closure;
  134. }
  135. function inBlock(style, terminator) {
  136. return function(stream, state) {
  137. while (!stream.eol()) {
  138. if (stream.match(terminator)) {
  139. state.tokenize = inText;
  140. break;
  141. }
  142. stream.next();
  143. }
  144. return style;
  145. };
  146. }
  147. function doctype(depth) {
  148. return function(stream, state) {
  149. var ch;
  150. while ((ch = stream.next()) != null) {
  151. if (ch == "<") {
  152. state.tokenize = doctype(depth + 1);
  153. return state.tokenize(stream, state);
  154. } else if (ch == ">") {
  155. if (depth == 1) {
  156. state.tokenize = inText;
  157. break;
  158. } else {
  159. state.tokenize = doctype(depth - 1);
  160. return state.tokenize(stream, state);
  161. }
  162. }
  163. }
  164. return "meta";
  165. };
  166. }
  167. function Context(state, tagName, startOfLine) {
  168. this.prev = state.context;
  169. this.tagName = tagName;
  170. this.indent = state.indented;
  171. this.startOfLine = startOfLine;
  172. if (Kludges.doNotIndent.hasOwnProperty(tagName) || (state.context && state.context.noIndent))
  173. this.noIndent = true;
  174. }
  175. function popContext(state) {
  176. if (state.context) state.context = state.context.prev;
  177. }
  178. function maybePopContext(state, nextTagName) {
  179. var parentTagName;
  180. while (true) {
  181. if (!state.context) {
  182. return;
  183. }
  184. parentTagName = state.context.tagName.toLowerCase();
  185. if (!Kludges.contextGrabbers.hasOwnProperty(parentTagName) ||
  186. !Kludges.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) {
  187. return;
  188. }
  189. popContext(state);
  190. }
  191. }
  192. function baseState(type, stream, state) {
  193. if (type == "openTag") {
  194. state.tagName = tagName;
  195. state.tagStart = stream.column();
  196. return attrState;
  197. } else if (type == "closeTag") {
  198. var err = false;
  199. if (state.context) {
  200. if (state.context.tagName != tagName) {
  201. if (Kludges.implicitlyClosed.hasOwnProperty(state.context.tagName.toLowerCase()))
  202. popContext(state);
  203. err = !state.context || state.context.tagName != tagName;
  204. }
  205. } else {
  206. err = true;
  207. }
  208. if (err) setStyle = "error";
  209. return err ? closeStateErr : closeState;
  210. } else {
  211. return baseState;
  212. }
  213. }
  214. function closeState(type, _stream, state) {
  215. if (type != "endTag") {
  216. setStyle = "error";
  217. return closeState;
  218. }
  219. popContext(state);
  220. return baseState;
  221. }
  222. function closeStateErr(type, stream, state) {
  223. setStyle = "error";
  224. return closeState(type, stream, state);
  225. }
  226. function attrState(type, _stream, state) {
  227. if (type == "word") {
  228. setStyle = "attribute";
  229. return attrEqState;
  230. } else if (type == "endTag" || type == "selfcloseTag") {
  231. var tagName = state.tagName, tagStart = state.tagStart;
  232. state.tagName = state.tagStart = null;
  233. if (type == "selfcloseTag" ||
  234. Kludges.autoSelfClosers.hasOwnProperty(tagName.toLowerCase())) {
  235. maybePopContext(state, tagName.toLowerCase());
  236. } else {
  237. maybePopContext(state, tagName.toLowerCase());
  238. state.context = new Context(state, tagName, tagStart == state.indented);
  239. }
  240. return baseState;
  241. }
  242. setStyle = "error";
  243. return attrState;
  244. }
  245. function attrEqState(type, stream, state) {
  246. if (type == "equals") return attrValueState;
  247. if (!Kludges.allowMissing) setStyle = "error";
  248. return attrState(type, stream, state);
  249. }
  250. function attrValueState(type, stream, state) {
  251. if (type == "string") return attrContinuedState;
  252. if (type == "word" && Kludges.allowUnquoted) {setStyle = "string"; return attrState;}
  253. setStyle = "error";
  254. return attrState(type, stream, state);
  255. }
  256. function attrContinuedState(type, stream, state) {
  257. if (type == "string") return attrContinuedState;
  258. return attrState(type, stream, state);
  259. }
  260. return {
  261. startState: function() {
  262. return {tokenize: inText,
  263. state: baseState,
  264. indented: 0,
  265. tagName: null, tagStart: null,
  266. context: null};
  267. },
  268. token: function(stream, state) {
  269. if (!state.tagName && stream.sol())
  270. state.indented = stream.indentation();
  271. if (stream.eatSpace()) return null;
  272. tagName = type = null;
  273. var style = state.tokenize(stream, state);
  274. if ((style || type) && style != "comment") {
  275. setStyle = null;
  276. state.state = state.state(type || style, stream, state);
  277. if (setStyle)
  278. style = setStyle == "error" ? style + " error" : setStyle;
  279. }
  280. return style;
  281. },
  282. indent: function(state, textAfter, fullLine) {
  283. var context = state.context;
  284. // Indent multi-line strings (e.g. css).
  285. if (state.tokenize.isInAttribute) {
  286. return state.stringStartCol + 1;
  287. }
  288. if (context && context.noIndent) return CodeMirror.Pass;
  289. if (state.tokenize != inTag && state.tokenize != inText)
  290. return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0;
  291. // Indent the starts of attribute names.
  292. if (state.tagName) {
  293. if (multilineTagIndentPastTag)
  294. return state.tagStart + state.tagName.length + 2;
  295. else
  296. return state.tagStart + indentUnit * multilineTagIndentFactor;
  297. }
  298. if (alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0;
  299. if (context && /^<\//.test(textAfter))
  300. context = context.prev;
  301. while (context && !context.startOfLine)
  302. context = context.prev;
  303. if (context) return context.indent + indentUnit;
  304. else return 0;
  305. },
  306. electricChars: "/",
  307. blockCommentStart: "<!--",
  308. blockCommentEnd: "-->",
  309. configuration: parserConfig.htmlMode ? "html" : "xml",
  310. helperType: parserConfig.htmlMode ? "html" : "xml"
  311. };
  312. });
  313. CodeMirror.defineMIME("text/xml", "xml");
  314. CodeMirror.defineMIME("application/xml", "xml");
  315. if (!CodeMirror.mimeModes.hasOwnProperty("text/html"))
  316. CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true});