1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: http://codemirror.net/LICENSE
5 if (typeof exports
== "object" && typeof module
== "object") // CommonJS
6 mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"));
7 else if (typeof define
== "function" && define
.amd
) // AMD
8 define(["../../lib/codemirror", "../htmlmixed/htmlmixed"], mod
);
9 else // Plain browser env
11 })(function(CodeMirror
) {
14 CodeMirror
.defineMode("htmlembedded", function(config
, parserConfig
) {
17 var scriptStartRegex
= parserConfig
.scriptStartRegex
|| /^<%/i,
18 scriptEndRegex
= parserConfig
.scriptEndRegex
|| /^%>/i;
21 var scriptingMode
, htmlMixedMode
;
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
);
30 return htmlMixedMode
.token(stream
, state
.htmlState
);
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
);
40 return scriptingMode
.token(stream
, state
.scriptState
);
45 startState: function() {
46 scriptingMode
= scriptingMode
|| CodeMirror
.getMode(config
, parserConfig
.scriptingModeSpec
);
47 htmlMixedMode
= htmlMixedMode
|| CodeMirror
.getMode(config
, "htmlmixed");
49 token
: parserConfig
.startOpen
? scriptingDispatch
: htmlDispatch
,
50 htmlState
: CodeMirror
.startState(htmlMixedMode
),
51 scriptState
: CodeMirror
.startState(scriptingMode
)
55 token: function(stream
, state
) {
56 return state
.token(stream
, state
);
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
);
66 copyState: function(state
) {
69 htmlState
: CodeMirror
.copyState(htmlMixedMode
, state
.htmlState
),
70 scriptState
: CodeMirror
.copyState(scriptingMode
, state
.scriptState
)
74 innerMode: function(state
) {
75 if (state
.token
== scriptingDispatch
) return {state
: state
.scriptState
, mode
: scriptingMode
};
76 else return {state
: state
.htmlState
, mode
: htmlMixedMode
};
81 CodeMirror
.defineMIME("application/x-ejs", { name
: "htmlembedded", scriptingModeSpec
:"javascript"});
82 CodeMirror
.defineMIME("application/x-aspx", { name
: "htmlembedded", scriptingModeSpec
:"text/x-csharp"});
83 CodeMirror
.defineMIME("application/x-jsp", { name
: "htmlembedded", scriptingModeSpec
:"text/x-java"});
84 CodeMirror
.defineMIME("application/x-erb", { name
: "htmlembedded", scriptingModeSpec
:"ruby"});