@@ -74,10 +74,8 @@ var CodeMirror = (function() {
7474 // can be kept static when scrolling.
7575 var maxLine = "" ;
7676
77- // Initialize the content. Somewhat hacky (delayed prepareInput)
78- // to work around browser issues.
77+ // Initialize the content.
7978 operation ( function ( ) { setValue ( options . value || "" ) ; updateInput = false ; } ) ( ) ;
80- setTimeout ( prepareInput , 20 ) ;
8179
8280 // Register our event handlers.
8381 connect ( scroller , "mousedown" , operation ( onMouseDown ) ) ;
@@ -104,7 +102,7 @@ var CodeMirror = (function() {
104102 // IE throws unspecified error in certain cases, when
105103 // trying to access activeElement before onload
106104 var hasFocus ; try { hasFocus = ( targetDocument . activeElement == input ) ; } catch ( e ) { }
107- if ( hasFocus ) onFocus ( ) ;
105+ if ( hasFocus ) setTimeout ( onFocus , 20 ) ;
108106 else onBlur ( ) ;
109107
110108 function isLine ( l ) { return l >= 0 && l < lines . length ; }
@@ -118,7 +116,7 @@ var CodeMirror = (function() {
118116 setValue : operation ( setValue ) ,
119117 getSelection : getSelection ,
120118 replaceSelection : operation ( replaceSelection ) ,
121- focus : function ( ) { focusInput ( ) ; onFocus ( ) ; prepareInput ( ) ; fastPoll ( ) ; } ,
119+ focus : function ( ) { focusInput ( ) ; onFocus ( ) ; fastPoll ( ) ; } ,
122120 setOption : function ( option , value ) {
123121 options [ option ] = value ;
124122 if ( option == "lineNumbers" || option == "gutter" ) gutterChanged ( ) ;
@@ -370,19 +368,24 @@ var CodeMirror = (function() {
370368
371369 function onFocus ( ) {
372370 if ( options . readOnly == "nocursor" ) return ;
373- if ( ! focused && options . onFocus ) options . onFocus ( instance ) ;
374- focused = true ;
371+ if ( ! focused ) {
372+ if ( options . onFocus ) options . onFocus ( instance ) ;
373+ focused = true ;
374+ if ( wrapper . className . search ( / \b C o d e M i r r o r - f o c u s e d \b / ) == - 1 )
375+ wrapper . className += " CodeMirror-focused" ;
376+ prepareInput ( ) ;
377+ }
375378 slowPoll ( ) ;
376- if ( wrapper . className . search ( / \b C o d e M i r r o r - f o c u s e d \b / ) == - 1 )
377- wrapper . className += " CodeMirror-focused" ;
378379 restartBlink ( ) ;
379380 }
380381 function onBlur ( ) {
381- if ( focused && options . onBlur ) options . onBlur ( instance ) ;
382+ if ( focused ) {
383+ if ( options . onBlur ) options . onBlur ( instance ) ;
384+ focused = false ;
385+ wrapper . className = wrapper . className . replace ( " CodeMirror-focused" , "" ) ;
386+ }
382387 clearInterval ( blinker ) ;
383388 setTimeout ( function ( ) { if ( ! focused ) shiftSelecting = null ; } , 150 ) ;
384- focused = false ;
385- wrapper . className = wrapper . className . replace ( " CodeMirror-focused" , "" ) ;
386389 }
387390
388391 // Replace the range from from to to by the strings in newText.
@@ -1342,7 +1345,8 @@ var CodeMirror = (function() {
13421345
13431346 // updateInput can be set to a boolean value to force/prevent an
13441347 // update.
1345- if ( ! leaveInputAlone && ( updateInput === true || ( updateInput !== false && selectionChanged ) ) )
1348+ if ( focused && ! leaveInputAlone &&
1349+ ( updateInput === true || ( updateInput !== false && selectionChanged ) ) )
13461350 prepareInput ( ) ;
13471351
13481352 if ( selectionChanged && options . matchBrackets )
0 commit comments