@@ -13,9 +13,10 @@ var CodeMirror = (function() {
1313 if ( defaults . hasOwnProperty ( opt ) )
1414 options [ opt ] = ( givenOptions && givenOptions . hasOwnProperty ( opt ) ? givenOptions : defaults ) [ opt ] ;
1515
16+ var targetDocument = options [ "document" ] ;
1617 // The element in which the editor lives. Takes care of scrolling
1718 // (if enabled).
18- var wrapper = document . createElement ( "div" ) ;
19+ var wrapper = targetDocument . createElement ( "div" ) ;
1920 wrapper . className = "CodeMirror" ;
2021 // Work around http://www.quirksmode.org/bugreports/archives/2006/09/Overflow_Hidden_not_hiding.html
2122 if ( window . ActiveXObject && / M S I E [ 1 - 7 ] [ \b \. ] / . test ( navigator . userAgent ) )
@@ -100,7 +101,7 @@ var CodeMirror = (function() {
100101 connect ( input , "paste" , function ( ) { fastPoll ( ) ; } ) ;
101102 connect ( input , "cut" , function ( ) { fastPoll ( ) ; } ) ;
102103
103- if ( document . activeElement == input ) onFocus ( ) ;
104+ if ( targetDocument . activeElement == input ) onFocus ( ) ;
104105 else onBlur ( ) ;
105106
106107 function isLine ( l ) { return l >= 0 && l < lines . length ; }
@@ -239,12 +240,12 @@ var CodeMirror = (function() {
239240 }
240241 }
241242
242- var move = connect ( document , "mousemove" , operation ( function ( e ) {
243+ var move = connect ( targetDocument , "mousemove" , operation ( function ( e ) {
243244 clearTimeout ( going ) ;
244245 e . stop ( ) ;
245246 extend ( e ) ;
246247 } ) , true ) ;
247- var up = connect ( document , "mouseup" , operation ( function ( e ) {
248+ var up = connect ( targetDocument , "mouseup" , operation ( function ( e ) {
248249 clearTimeout ( going ) ;
249250 var cur = posFromMouse ( e ) ;
250251 if ( cur ) setSelectionUser ( start , cur ) ;
@@ -748,7 +749,7 @@ var CodeMirror = (function() {
748749 // there .innerHTML on PRE nodes is dumb, and discards
749750 // whitespace.
750751 var sfrom = sel . from . line , sto = sel . to . line , off = 0 ,
751- scratch = badInnerHTML && document . createElement ( "div" ) ;
752+ scratch = badInnerHTML && targetDocument . createElement ( "div" ) ;
752753 for ( var i = 0 , e = updates . length ; i < e ; ++ i ) {
753754 var rec = updates [ i ] ;
754755 var extra = ( rec . to - rec . from ) - rec . domSize ;
@@ -758,7 +759,7 @@ var CodeMirror = (function() {
758759 lineDiv . removeChild ( nodeAfter ? nodeAfter . previousSibling : lineDiv . lastChild ) ;
759760 else if ( extra ) {
760761 for ( var j = Math . max ( 0 , extra ) ; j > 0 ; -- j )
761- lineDiv . insertBefore ( document . createElement ( "pre" ) , nodeAfter ) ;
762+ lineDiv . insertBefore ( targetDocument . createElement ( "pre" ) , nodeAfter ) ;
762763 for ( var j = Math . max ( 0 , - extra ) ; j > 0 ; -- j )
763764 lineDiv . removeChild ( nodeAfter ? nodeAfter . previousSibling : lineDiv . lastChild ) ;
764765 }
@@ -805,7 +806,7 @@ var CodeMirror = (function() {
805806 gutterText . innerHTML = html . join ( "" ) ;
806807 var minwidth = String ( lines . length ) . length , firstNode = gutterText . firstChild , val = eltText ( firstNode ) , pad = "" ;
807808 while ( val . length + pad . length < minwidth ) pad += "\u00a0" ;
808- if ( pad ) firstNode . insertBefore ( document . createTextNode ( pad ) , firstNode . firstChild ) ;
809+ if ( pad ) firstNode . insertBefore ( targetDocument . createTextNode ( pad ) , firstNode . firstChild ) ;
809810 gutter . style . display = "" ;
810811 lineSpace . style . marginLeft = gutter . offsetWidth + "px" ;
811812 }
@@ -1420,7 +1421,8 @@ var CodeMirror = (function() {
14201421 workTime : 100 ,
14211422 workDelay : 200 ,
14221423 undoDepth : 40 ,
1423- tabindex : null
1424+ tabindex : null ,
1425+ document : window . document
14241426 } ;
14251427
14261428 // Known modes, by name and by MIME
@@ -1804,11 +1806,13 @@ var CodeMirror = (function() {
18041806 } ,
18051807 pageX : function ( ) {
18061808 if ( this . e . pageX != null ) return this . e . pageX ;
1807- else return this . e . clientX + document . body . scrollLeft + document . documentElement . scrollLeft ;
1809+ var doc = this . target ( ) . ownerDocument ;
1810+ return this . e . clientX + doc . body . scrollLeft + doc . documentElement . scrollLeft ;
18081811 } ,
18091812 pageY : function ( ) {
18101813 if ( this . e . pageY != null ) return this . e . pageY ;
1811- else return this . e . clientY + document . body . scrollTop + document . documentElement . scrollTop ;
1814+ var doc = this . target ( ) . ownerDocument ;
1815+ return this . e . clientY + doc . body . scrollTop + doc . documentElement . scrollTop ;
18121816 }
18131817 } ;
18141818
@@ -1870,7 +1874,7 @@ var CodeMirror = (function() {
18701874 function eltOffset ( node ) {
18711875 var x = 0 , y = 0 ;
18721876 for ( var n = node ; n ; n = n . offsetParent ) { x += n . offsetLeft ; y += n . offsetTop ; }
1873- for ( var n = node . parentNode ; n != document . body ; n = n . parentNode ) { x -= n . scrollLeft ; y -= n . scrollTop ; }
1877+ for ( var n = node . parentNode ; n != node . ownerDocument . body ; n = n . parentNode ) { x -= n . scrollLeft ; y -= n . scrollTop ; }
18741878 return { left : x , top : y } ;
18751879 }
18761880 // Get a node's text content.
@@ -1935,7 +1939,7 @@ var CodeMirror = (function() {
19351939 // IE model. Don't ask.
19361940 else {
19371941 var selRange = function ( te ) {
1938- try { var range = document . selection . createRange ( ) ; }
1942+ try { var range = te . ownerDocument . selection . createRange ( ) ; }
19391943 catch ( e ) { return null ; }
19401944 if ( ! range || range . parentElement ( ) != te ) return null ;
19411945 var val = te . value , len = val . length , localRange = te . createTextRange ( ) ;
0 commit comments