11CodeMirror . defineMode ( "htmlmixed" , function ( config ) {
22 var htmlMode = CodeMirror . getMode ( config , { name : "xml" , htmlMode : true } ) ;
3+ var unknownScriptMode = CodeMirror . getMode ( config , "text/plain" ) ;
34 var jsMode = CodeMirror . getMode ( config , "javascript" ) ;
45 var cssMode = CodeMirror . getMode ( config , "css" ) ;
56
67 function html ( stream , state ) {
78 var style = htmlMode . token ( stream , state . htmlState ) ;
89 if ( / (?: ^ | \s ) t a g (?: \s | $ ) / . test ( style ) && stream . current ( ) == ">" && state . htmlState . context ) {
910 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+ }
1223 }
1324 else if ( / ^ s t y l e $ / i. test ( state . htmlState . context . tagName ) ) {
1425 state . token = css ;
@@ -27,6 +38,15 @@ CodeMirror.defineMode("htmlmixed", function(config) {
2738 }
2839 return style ;
2940 }
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+ }
3050 function javascript ( stream , state ) {
3151 if ( stream . match ( / ^ < \/ \s * s c r i p t \s * > / i, false ) ) {
3252 state . token = html ;
@@ -68,8 +88,10 @@ CodeMirror.defineMode("htmlmixed", function(config) {
6888 return htmlMode . indent ( state . htmlState , textAfter ) ;
6989 else if ( state . token == javascript )
7090 return jsMode . indent ( state . localState , textAfter ) ;
71- else
91+ else if ( state . token == css )
7292 return cssMode . indent ( state . localState , textAfter ) ;
93+ else // unknownScriptMode
94+ return 0 ;
7395 } ,
7496
7597 electricChars : "/{}:" ,
0 commit comments