1
1
CodeMirror . defineMode ( "htmlmixed" , function ( config ) {
2
2
var htmlMode = CodeMirror . getMode ( config , { name : "xml" , htmlMode : true } ) ;
3
+ var unknownScriptMode = CodeMirror . getMode ( config , "text/plain" ) ;
3
4
var jsMode = CodeMirror . getMode ( config , "javascript" ) ;
4
5
var cssMode = CodeMirror . getMode ( config , "css" ) ;
5
6
6
7
function html ( stream , state ) {
7
8
var style = htmlMode . token ( stream , state . htmlState ) ;
8
9
if ( / (?: ^ | \s ) t a g (?: \s | $ ) / . test ( style ) && stream . current ( ) == ">" && state . htmlState . context ) {
9
10
if ( / ^ s c r i p t $ / i. test ( state . htmlState . context . tagName ) ) {
10
- state . token = javascript ;
11
- state . localState = jsMode . startState ( htmlMode . indent ( state . htmlState , "" ) ) ;
11
+ // Script block: mode to change to depends on type attribute
12
+ var scriptType = stream . string . match ( / t y p e \s * = \s * [ " ' ] ( .+ ) [ " ' ] / i) ;
13
+ scriptType = scriptType && scriptType [ 1 ] ;
14
+ if ( ! scriptType || scriptType . match ( / ( t e x t | a p p l i c a t i o n ) \/ ( j a v a | e c m a ) s c r i p t / i) ) {
15
+ state . token = javascript ;
16
+ state . localState = jsMode . startState ( htmlMode . indent ( state . htmlState , "" ) ) ;
17
+ } else if ( scriptType . match ( / \/ x - h a n d l e b a r s - t e m p l a t e / i) || scriptType . match ( / \/ x - m u s t a c h e / i) ) {
18
+ // Handlebars or Mustache template: leave it in HTML mode
19
+ } else {
20
+ state . token = unknownScript ;
21
+ state . localState = null ;
22
+ }
12
23
}
13
24
else if ( / ^ s t y l e $ / i. test ( state . htmlState . context . tagName ) ) {
14
25
state . token = css ;
@@ -27,6 +38,15 @@ CodeMirror.defineMode("htmlmixed", function(config) {
27
38
}
28
39
return style ;
29
40
}
41
+ function unknownScript ( stream , state ) {
42
+ if ( stream . match ( / ^ < \/ \s * s c r i p t \s * > / i, false ) ) {
43
+ state . token = html ;
44
+ state . localState = null ;
45
+ return html ( stream , state ) ;
46
+ }
47
+ return maybeBackup ( stream , / < \/ \s * s c r i p t \s * > / ,
48
+ unknownScriptMode . token ( stream , state . localState ) ) ;
49
+ }
30
50
function javascript ( stream , state ) {
31
51
if ( stream . match ( / ^ < \/ \s * s c r i p t \s * > / i, false ) ) {
32
52
state . token = html ;
@@ -68,8 +88,10 @@ CodeMirror.defineMode("htmlmixed", function(config) {
68
88
return htmlMode . indent ( state . htmlState , textAfter ) ;
69
89
else if ( state . token == javascript )
70
90
return jsMode . indent ( state . localState , textAfter ) ;
71
- else
91
+ else if ( state . token == css )
72
92
return cssMode . indent ( state . localState , textAfter ) ;
93
+ else // unknownScriptMode
94
+ return 0 ;
73
95
} ,
74
96
75
97
electricChars : "/{}:" ,
0 commit comments