htmlmixed.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
  11. var htmlMode = CodeMirror.getMode(config, {name: "xml",
  12. htmlMode: true,
  13. multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
  14. multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag});
  15. var cssMode = CodeMirror.getMode(config, "css");
  16. var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTypes;
  17. scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i,
  18. mode: CodeMirror.getMode(config, "javascript")});
  19. if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) {
  20. var conf = scriptTypesConf[i];
  21. scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMode(config, conf.mode)});
  22. }
  23. scriptTypes.push({matches: /./,
  24. mode: CodeMirror.getMode(config, "text/plain")});
  25. function html(stream, state) {
  26. var tagName = state.htmlState.tagName;
  27. var style = htmlMode.token(stream, state.htmlState);
  28. if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">") {
  29. // Script block: mode to change to depends on type attribute
  30. var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i);
  31. scriptType = scriptType ? scriptType[1] : "";
  32. if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = scriptType.slice(1, scriptType.length - 1);
  33. for (var i = 0; i < scriptTypes.length; ++i) {
  34. var tp = scriptTypes[i];
  35. if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matches.test(scriptType)) {
  36. if (tp.mode) {
  37. state.token = script;
  38. state.localMode = tp.mode;
  39. state.localState = tp.mode.startState && tp.mode.startState(htmlMode.indent(state.htmlState, ""));
  40. }
  41. break;
  42. }
  43. }
  44. } else if (tagName == "style" && /\btag\b/.test(style) && stream.current() == ">") {
  45. state.token = css;
  46. state.localMode = cssMode;
  47. state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
  48. }
  49. return style;
  50. }
  51. function maybeBackup(stream, pat, style) {
  52. var cur = stream.current();
  53. var close = cur.search(pat), m;
  54. if (close > -1) stream.backUp(cur.length - close);
  55. else if (m = cur.match(/<\/?$/)) {
  56. stream.backUp(cur.length);
  57. if (!stream.match(pat, false)) stream.match(cur);
  58. }
  59. return style;
  60. }
  61. function script(stream, state) {
  62. if (stream.match(/^<\/\s*script\s*>/i, false)) {
  63. state.token = html;
  64. state.localState = state.localMode = null;
  65. return html(stream, state);
  66. }
  67. return maybeBackup(stream, /<\/\s*script\s*>/,
  68. state.localMode.token(stream, state.localState));
  69. }
  70. function css(stream, state) {
  71. if (stream.match(/^<\/\s*style\s*>/i, false)) {
  72. state.token = html;
  73. state.localState = state.localMode = null;
  74. return html(stream, state);
  75. }
  76. return maybeBackup(stream, /<\/\s*style\s*>/,
  77. cssMode.token(stream, state.localState));
  78. }
  79. return {
  80. startState: function() {
  81. var state = htmlMode.startState();
  82. return {token: html, localMode: null, localState: null, htmlState: state};
  83. },
  84. copyState: function(state) {
  85. if (state.localState)
  86. var local = CodeMirror.copyState(state.localMode, state.localState);
  87. return {token: state.token, localMode: state.localMode, localState: local,
  88. htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
  89. },
  90. token: function(stream, state) {
  91. return state.token(stream, state);
  92. },
  93. indent: function(state, textAfter) {
  94. if (!state.localMode || /^\s*<\//.test(textAfter))
  95. return htmlMode.indent(state.htmlState, textAfter);
  96. else if (state.localMode.indent)
  97. return state.localMode.indent(state.localState, textAfter);
  98. else
  99. return CodeMirror.Pass;
  100. },
  101. innerMode: function(state) {
  102. return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
  103. }
  104. };
  105. }, "xml", "javascript", "css");
  106. CodeMirror.defineMIME("text/html", "htmlmixed");
  107. });