1
0

xml.js 10 KB

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