xml.js 13 KB

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