|
| 1 | +CodeMirror.defineMode("htmlembedded", function(config, parserConfig) { |
| 2 | + |
| 3 | + //config settings |
| 4 | + var scriptStartRegex = parserConfig.scriptStartRegex || /^<%/i, |
| 5 | + scriptEndRegex = parserConfig.scriptEndRegex || /^%>/i, |
| 6 | + scriptingMode; |
| 7 | + |
| 8 | + var htmlMixedMode = CodeMirror.getMode(config, "htmlmixed"); |
| 9 | + |
| 10 | + //tokenizer when in html mode |
| 11 | + function htmlDispatch(stream, state) { |
| 12 | + if (stream.match(scriptStartRegex, false)) { |
| 13 | + state.token=scriptingDispatch; |
| 14 | + return scriptingMode.token(stream, state.scriptState); |
| 15 | + } |
| 16 | + else |
| 17 | + return htmlMixedMode.token(stream, state.htmlState); |
| 18 | + } |
| 19 | + |
| 20 | + //tokenizer when in scripting mode |
| 21 | + function scriptingDispatch(stream, state) { |
| 22 | + if (stream.match(scriptEndRegex, false)) { |
| 23 | + state.token=htmlDispatch; |
| 24 | + return htmlMixedMode.token(stream, state.htmlState); |
| 25 | + } |
| 26 | + else |
| 27 | + return scriptingMode.token(stream, state.scriptState); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + return { |
| 32 | + startState: function() { |
| 33 | + scriptingMode = scriptingMode || CodeMirror.getMode(config, parserConfig.scriptingModeSpec) |
| 34 | + return { |
| 35 | + token : parserConfig.startOpen ? scriptingDispatch : htmlDispatch, |
| 36 | + htmlState : htmlMixedMode.startState(), |
| 37 | + scriptState : scriptingMode.startState() |
| 38 | + } |
| 39 | + }, |
| 40 | + |
| 41 | + token: function(stream, state) { |
| 42 | + return state.token(stream, state); |
| 43 | + }, |
| 44 | + |
| 45 | + indent: function(state, textAfter) { |
| 46 | + if (state.token == htmlDispatch) |
| 47 | + return htmlMixedMode.indent(state.htmlState, textAfter); |
| 48 | + else |
| 49 | + return scriptingMode.indent(state.scriptState, textAfter); |
| 50 | + }, |
| 51 | + |
| 52 | + copyState: function(state) { |
| 53 | + return { |
| 54 | + token : state.token, |
| 55 | + htmlState : CodeMirror.copyState(htmlMixedMode, state.htmlState), |
| 56 | + scriptState : CodeMirror.copyState(scriptingMode, state.scriptState) |
| 57 | + } |
| 58 | + }, |
| 59 | + |
| 60 | + |
| 61 | + electricChars: "/{}:" |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +CodeMirror.defineMIME("application/x-ejs", { name: "htmlembedded", scriptingModeSpec:"javascript"}); |
| 66 | +CodeMirror.defineMIME("application/x-aspx", { name: "htmlembedded", scriptingModeSpec:"text/x-csharp"}); |
| 67 | +CodeMirror.defineMIME("application/x-jsp", { name: "htmlembedded", scriptingModeSpec:"text/x-java"}); |
| 68 | +/*CodeMirror.defineMIME("application/x-httpd-php", { |
| 69 | + name: "htmlembedded", |
| 70 | + scriptingModeSpec:"text/x-php", |
| 71 | + scriptStartRegex: /^<\?/, |
| 72 | + scriptEndRegex: /^\?>/});*/ |
0 commit comments