You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
12 KiB

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