OLD | NEW |
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | 1 // CodeMirror, copyright (c) by Marijn Haverbeke and others |
2 // Distributed under an MIT license: http://codemirror.net/LICENSE | 2 // Distributed under an MIT license: http://codemirror.net/LICENSE |
3 | 3 |
4 (function(mod) { | 4 (function(mod) { |
5 if (typeof exports == "object" && typeof module == "object") // CommonJS | 5 if (typeof exports == "object" && typeof module == "object") // CommonJS |
6 mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed")); | 6 mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"), |
| 7 require("../../addon/mode/multiplex")); |
7 else if (typeof define == "function" && define.amd) // AMD | 8 else if (typeof define == "function" && define.amd) // AMD |
8 define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod); | 9 define(["../../lib/codemirror", "../htmlmixed/htmlmixed", |
| 10 "../../addon/mode/multiplex"], mod); |
9 else // Plain browser env | 11 else // Plain browser env |
10 mod(CodeMirror); | 12 mod(CodeMirror); |
11 })(function(CodeMirror) { | 13 })(function(CodeMirror) { |
12 "use strict"; | 14 "use strict"; |
13 | 15 |
14 CodeMirror.defineMode("htmlembedded", function(config, parserConfig) { | 16 CodeMirror.defineMode("htmlembedded", function(config, parserConfig) { |
| 17 return CodeMirror.multiplexingMode(CodeMirror.getMode(config, "htmlmixed"),
{ |
| 18 open: parserConfig.open || parserConfig.scriptStartRegex || "<%", |
| 19 close: parserConfig.close || parserConfig.scriptEndRegex || "%>", |
| 20 mode: CodeMirror.getMode(config, parserConfig.scriptingModeSpec) |
| 21 }); |
| 22 }, "htmlmixed"); |
15 | 23 |
16 //config settings | 24 CodeMirror.defineMIME("application/x-ejs", {name: "htmlembedded", scriptingMod
eSpec:"javascript"}); |
17 var scriptStartRegex = parserConfig.scriptStartRegex || /^<%/i, | 25 CodeMirror.defineMIME("application/x-aspx", {name: "htmlembedded", scriptingMo
deSpec:"text/x-csharp"}); |
18 scriptEndRegex = parserConfig.scriptEndRegex || /^%>/i; | 26 CodeMirror.defineMIME("application/x-jsp", {name: "htmlembedded", scriptingMod
eSpec:"text/x-java"}); |
19 | 27 CodeMirror.defineMIME("application/x-erb", {name: "htmlembedded", scriptingMod
eSpec:"ruby"}); |
20 //inner modes | |
21 var scriptingMode, htmlMixedMode; | |
22 | |
23 //tokenizer when in html mode | |
24 function htmlDispatch(stream, state) { | |
25 if (stream.match(scriptStartRegex, false)) { | |
26 state.token=scriptingDispatch; | |
27 return scriptingMode.token(stream, state.scriptState); | |
28 } | |
29 else | |
30 return htmlMixedMode.token(stream, state.htmlState); | |
31 } | |
32 | |
33 //tokenizer when in scripting mode | |
34 function scriptingDispatch(stream, state) { | |
35 if (stream.match(scriptEndRegex, false)) { | |
36 state.token=htmlDispatch; | |
37 return htmlMixedMode.token(stream, state.htmlState); | |
38 } | |
39 else | |
40 return scriptingMode.token(stream, state.scriptState); | |
41 } | |
42 | |
43 | |
44 return { | |
45 startState: function() { | |
46 scriptingMode = scriptingMode || CodeMirror.getMode(config, parserConfig.s
criptingModeSpec); | |
47 htmlMixedMode = htmlMixedMode || CodeMirror.getMode(config, "htmlmixed"); | |
48 return { | |
49 token : parserConfig.startOpen ? scriptingDispatch : htmlDispatch, | |
50 htmlState : CodeMirror.startState(htmlMixedMode), | |
51 scriptState : CodeMirror.startState(scriptingMode) | |
52 }; | |
53 }, | |
54 | |
55 token: function(stream, state) { | |
56 return state.token(stream, state); | |
57 }, | |
58 | |
59 indent: function(state, textAfter) { | |
60 if (state.token == htmlDispatch) | |
61 return htmlMixedMode.indent(state.htmlState, textAfter); | |
62 else if (scriptingMode.indent) | |
63 return scriptingMode.indent(state.scriptState, textAfter); | |
64 }, | |
65 | |
66 copyState: function(state) { | |
67 return { | |
68 token : state.token, | |
69 htmlState : CodeMirror.copyState(htmlMixedMode, state.htmlState), | |
70 scriptState : CodeMirror.copyState(scriptingMode, state.scriptState) | |
71 }; | |
72 }, | |
73 | |
74 innerMode: function(state) { | |
75 if (state.token == scriptingDispatch) return {state: state.scriptState, mo
de: scriptingMode}; | |
76 else return {state: state.htmlState, mode: htmlMixedMode}; | |
77 } | |
78 }; | |
79 }, "htmlmixed"); | |
80 | |
81 CodeMirror.defineMIME("application/x-ejs", { name: "htmlembedded", scriptingMode
Spec:"javascript"}); | |
82 CodeMirror.defineMIME("application/x-aspx", { name: "htmlembedded", scriptingMod
eSpec:"text/x-csharp"}); | |
83 CodeMirror.defineMIME("application/x-jsp", { name: "htmlembedded", scriptingMode
Spec:"text/x-java"}); | |
84 CodeMirror.defineMIME("application/x-erb", { name: "htmlembedded", scriptingMode
Spec:"ruby"}); | |
85 | |
86 }); | 28 }); |
OLD | NEW |