xml.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 lower(tagName) {
  179. return tagName && tagName.toLowerCase();
  180. }
  181. function Context(state, tagName, startOfLine) {
  182. this.prev = state.context;
  183. this.tagName = tagName || "";
  184. this.indent = state.indented;
  185. this.startOfLine = startOfLine;
  186. if (config.doNotIndent.hasOwnProperty(tagName) || (state.context && state.context.noIndent))
  187. this.noIndent = true;
  188. }
  189. function popContext(state) {
  190. if (state.context) state.context = state.context.prev;
  191. }
  192. function maybePopContext(state, nextTagName) {
  193. var parentTagName;
  194. while (true) {
  195. if (!state.context) {
  196. return;
  197. }
  198. parentTagName = state.context.tagName;
  199. if (!config.contextGrabbers.hasOwnProperty(lower(parentTagName)) ||
  200. !config.contextGrabbers[lower(parentTagName)].hasOwnProperty(lower(nextTagName))) {
  201. return;
  202. }
  203. popContext(state);
  204. }
  205. }
  206. function baseState(type, stream, state) {
  207. if (type == "openTag") {
  208. state.tagStart = stream.column();
  209. return tagNameState;
  210. } else if (type == "closeTag") {
  211. return closeTagNameState;
  212. } else {
  213. return baseState;
  214. }
  215. }
  216. function tagNameState(type, stream, state) {
  217. if (type == "word") {
  218. state.tagName = stream.current();
  219. setStyle = "tag";
  220. return attrState;
  221. } else if (config.allowMissingTagName && type == "endTag") {
  222. setStyle = "tag bracket";
  223. return attrState(type, stream, state);
  224. } else {
  225. setStyle = "error";
  226. return tagNameState;
  227. }
  228. }
  229. function closeTagNameState(type, stream, state) {
  230. if (type == "word") {
  231. var tagName = stream.current();
  232. if (state.context && state.context.tagName != tagName &&
  233. config.implicitlyClosed.hasOwnProperty(lower(state.context.tagName)))
  234. popContext(state);
  235. if ((state.context && state.context.tagName == tagName) || config.matchClosing === false) {
  236. setStyle = "tag";
  237. return closeState;
  238. } else {
  239. setStyle = "tag error";
  240. return closeStateErr;
  241. }
  242. } else if (config.allowMissingTagName && type == "endTag") {
  243. setStyle = "tag bracket";
  244. return closeState(type, stream, state);
  245. } else {
  246. setStyle = "error";
  247. return closeStateErr;
  248. }
  249. }
  250. function closeState(type, _stream, state) {
  251. if (type != "endTag") {
  252. setStyle = "error";
  253. return closeState;
  254. }
  255. popContext(state);
  256. return baseState;
  257. }
  258. function closeStateErr(type, stream, state) {
  259. setStyle = "error";
  260. return closeState(type, stream, state);
  261. }
  262. function attrState(type, _stream, state) {
  263. if (type == "word") {
  264. setStyle = "attribute";
  265. return attrEqState;
  266. } else if (type == "endTag" || type == "selfcloseTag") {
  267. var tagName = state.tagName, tagStart = state.tagStart;
  268. state.tagName = state.tagStart = null;
  269. if (type == "selfcloseTag" ||
  270. config.autoSelfClosers.hasOwnProperty(lower(tagName))) {
  271. maybePopContext(state, tagName);
  272. } else {
  273. maybePopContext(state, tagName);
  274. state.context = new Context(state, tagName, tagStart == state.indented);
  275. }
  276. return baseState;
  277. }
  278. setStyle = "error";
  279. return attrState;
  280. }
  281. function attrEqState(type, stream, state) {
  282. if (type == "equals") return attrValueState;
  283. if (!config.allowMissing) setStyle = "error";
  284. return attrState(type, stream, state);
  285. }
  286. function attrValueState(type, stream, state) {
  287. if (type == "string") return attrContinuedState;
  288. if (type == "word" && config.allowUnquoted) {setStyle = "string"; return attrState;}
  289. setStyle = "error";
  290. return attrState(type, stream, state);
  291. }
  292. function attrContinuedState(type, stream, state) {
  293. if (type == "string") return attrContinuedState;
  294. return attrState(type, stream, state);
  295. }
  296. return {
  297. startState: function(baseIndent) {
  298. var state = {tokenize: inText,
  299. state: baseState,
  300. indented: baseIndent || 0,
  301. tagName: null, tagStart: null,
  302. context: null}
  303. if (baseIndent != null) state.baseIndent = baseIndent
  304. return state
  305. },
  306. token: function(stream, state) {
  307. if (!state.tagName && stream.sol())
  308. state.indented = stream.indentation();
  309. if (stream.eatSpace()) return null;
  310. type = null;
  311. var style = state.tokenize(stream, state);
  312. if ((style || type) && style != "comment") {
  313. setStyle = null;
  314. state.state = state.state(type || style, stream, state);
  315. if (setStyle)
  316. style = setStyle == "error" ? style + " error" : setStyle;
  317. }
  318. return style;
  319. },
  320. indent: function(state, textAfter, fullLine) {
  321. var context = state.context;
  322. // Indent multi-line strings (e.g. css).
  323. if (state.tokenize.isInAttribute) {
  324. if (state.tagStart == state.indented)
  325. return state.stringStartCol + 1;
  326. else
  327. return state.indented + indentUnit;
  328. }
  329. if (context && context.noIndent) return CodeMirror.Pass;
  330. if (state.tokenize != inTag && state.tokenize != inText)
  331. return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0;
  332. // Indent the starts of attribute names.
  333. if (state.tagName) {
  334. if (config.multilineTagIndentPastTag !== false)
  335. return state.tagStart + state.tagName.length + 2;
  336. else
  337. return state.tagStart + indentUnit * (config.multilineTagIndentFactor || 1);
  338. }
  339. if (config.alignCDATA && /<!\[CDATA\[/.test(textAfter)) return 0;
  340. var tagAfter = textAfter && /^<(\/)?([\w_:\.-]*)/.exec(textAfter);
  341. if (tagAfter && tagAfter[1]) { // Closing tag spotted
  342. while (context) {
  343. if (context.tagName == tagAfter[2]) {
  344. context = context.prev;
  345. break;
  346. } else if (config.implicitlyClosed.hasOwnProperty(lower(context.tagName))) {
  347. context = context.prev;
  348. } else {
  349. break;
  350. }
  351. }
  352. } else if (tagAfter) { // Opening tag spotted
  353. while (context) {
  354. var grabbers = config.contextGrabbers[lower(context.tagName)];
  355. if (grabbers && grabbers.hasOwnProperty(lower(tagAfter[2])))
  356. context = context.prev;
  357. else
  358. break;
  359. }
  360. }
  361. while (context && context.prev && !context.startOfLine)
  362. context = context.prev;
  363. if (context) return context.indent + indentUnit;
  364. else return state.baseIndent || 0;
  365. },
  366. electricInput: /<\/[\s\w:]+>$/,
  367. blockCommentStart: "<!--",
  368. blockCommentEnd: "-->",
  369. configuration: config.htmlMode ? "html" : "xml",
  370. helperType: config.htmlMode ? "html" : "xml",
  371. skipAttribute: function(state) {
  372. if (state.state == attrValueState)
  373. state.state = attrState
  374. },
  375. xmlCurrentTag: function(state) {
  376. return state.tagName ? {name: state.tagName, close: state.type == "closeTag"} : null
  377. },
  378. xmlCurrentContext: function(state) {
  379. var context = []
  380. for (var cx = state.context; cx; cx = cx.prev)
  381. context.push(cx.tagName)
  382. return context.reverse()
  383. }
  384. };
  385. });
  386. CodeMirror.defineMIME("text/xml", "xml");
  387. CodeMirror.defineMIME("application/xml", "xml");
  388. if (!CodeMirror.mimeModes.hasOwnProperty("text/html"))
  389. CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true});
  390. });