htmlmixed.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var defaultTags = {
  13. script: [
  14. ["lang", /(javascript|babel)/i, "javascript"],
  15. ["type", /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i, "javascript"],
  16. ["type", /./, "text/plain"],
  17. [null, null, "javascript"]
  18. ],
  19. style: [
  20. ["lang", /^css$/i, "css"],
  21. ["type", /^(text\/)?(x-)?(stylesheet|css)$/i, "css"],
  22. ["type", /./, "text/plain"],
  23. [null, null, "css"]
  24. ]
  25. };
  26. function maybeBackup(stream, pat, style) {
  27. var cur = stream.current(), close = cur.search(pat);
  28. if (close > -1) {
  29. stream.backUp(cur.length - close);
  30. } else if (cur.match(/<\/?$/)) {
  31. stream.backUp(cur.length);
  32. if (!stream.match(pat, false)) stream.match(cur);
  33. }
  34. return style;
  35. }
  36. var attrRegexpCache = {};
  37. function getAttrRegexp(attr) {
  38. var regexp = attrRegexpCache[attr];
  39. if (regexp) return regexp;
  40. return attrRegexpCache[attr] = new RegExp("\\s+" + attr + "\\s*=\\s*('|\")?([^'\"]+)('|\")?\\s*");
  41. }
  42. function getAttrValue(text, attr) {
  43. var match = text.match(getAttrRegexp(attr))
  44. return match ? match[2] : ""
  45. }
  46. function getTagRegexp(tagName, anchored) {
  47. return new RegExp((anchored ? "^" : "") + "<\/\s*" + tagName + "\s*>", "i");
  48. }
  49. function addTags(from, to) {
  50. for (var tag in from) {
  51. var dest = to[tag] || (to[tag] = []);
  52. var source = from[tag];
  53. for (var i = source.length - 1; i >= 0; i--)
  54. dest.unshift(source[i])
  55. }
  56. }
  57. function findMatchingMode(tagInfo, tagText) {
  58. for (var i = 0; i < tagInfo.length; i++) {
  59. var spec = tagInfo[i];
  60. if (!spec[0] || spec[1].test(getAttrValue(tagText, spec[0]))) return spec[2];
  61. }
  62. }
  63. CodeMirror.defineMode("htmlmixed", function (config, parserConfig) {
  64. var htmlMode = CodeMirror.getMode(config, {
  65. name: "xml",
  66. htmlMode: true,
  67. multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
  68. multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag
  69. });
  70. var tags = {};
  71. var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes;
  72. addTags(defaultTags, tags);
  73. if (configTags) addTags(configTags, tags);
  74. if (configScript) for (var i = configScript.length - 1; i >= 0; i--)
  75. tags.script.unshift(["type", configScript[i].matches, configScript[i].mode])
  76. function html(stream, state) {
  77. var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName
  78. if (tag && !/[<>\s\/]/.test(stream.current()) &&
  79. (tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) &&
  80. tags.hasOwnProperty(tagName)) {
  81. state.inTag = tagName + " "
  82. } else if (state.inTag && tag && />$/.test(stream.current())) {
  83. var inTag = /^([\S]+) (.*)/.exec(state.inTag)
  84. state.inTag = null
  85. var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2])
  86. var mode = CodeMirror.getMode(config, modeSpec)
  87. var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false);
  88. state.token = function (stream, state) {
  89. if (stream.match(endTagA, false)) {
  90. state.token = html;
  91. state.localState = state.localMode = null;
  92. return null;
  93. }
  94. return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState));
  95. };
  96. state.localMode = mode;
  97. state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, ""));
  98. } else if (state.inTag) {
  99. state.inTag += stream.current()
  100. if (stream.eol()) state.inTag += " "
  101. }
  102. return style;
  103. };
  104. return {
  105. startState: function () {
  106. var state = htmlMode.startState();
  107. return {token: html, inTag: null, localMode: null, localState: null, htmlState: state};
  108. },
  109. copyState: function (state) {
  110. var local;
  111. if (state.localState) {
  112. local = CodeMirror.copyState(state.localMode, state.localState);
  113. }
  114. return {token: state.token, inTag: state.inTag,
  115. localMode: state.localMode, localState: local,
  116. htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
  117. },
  118. token: function (stream, state) {
  119. return state.token(stream, state);
  120. },
  121. indent: function (state, textAfter) {
  122. if (!state.localMode || /^\s*<\//.test(textAfter))
  123. return htmlMode.indent(state.htmlState, textAfter);
  124. else if (state.localMode.indent)
  125. return state.localMode.indent(state.localState, textAfter);
  126. else
  127. return CodeMirror.Pass;
  128. },
  129. innerMode: function (state) {
  130. return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
  131. }
  132. };
  133. }, "xml", "javascript", "css");
  134. CodeMirror.defineMIME("text/html", "htmlmixed");
  135. });