public/javascripts/codemirror/js/undo.js
function(chain) {
    // Some attempt is made to prevent the cursor from jumping
    // randomly when an undo or redo happens. It still behaves a bit
    // strange sometimes.
    var cursor = select.cursorPos(this.container, false), self = this;

    // Remove all nodes in the DOM tree between from and to (null for
    // start/end of container).
    function removeRange(from, to) {
      var pos = from ? from.nextSibling : self.container.firstChild;
      while (pos != to) {
        var temp = pos.nextSibling;
        removeElement(pos);
        pos = temp;
      }
    }

    var start = chain[0].from, end = chain[chain.length - 1].to;
    // Clear the space where this change has to be made.
    removeRange(start, end);

    // Insert the content specified by the chain into the DOM tree.
    for (var i = 0; i < chain.length; i++) {
      var line = chain[i];
      // The start and end of the space are already correct, but BR
      // tags inside it have to be put back.
      if (i > 0)
        self.container.insertBefore(line.from, end);

      // Add the text.
      var node = makePartSpan(fixSpaces(line.text), this.container.ownerDocument);
      self.container.insertBefore(node, end);
      // See if the cursor was on this line. Put it back, adjusting
      // for changed line length, if it was.
      if (cursor && cursor.node == line.from) {
        var cursordiff = 0;
        var prev = this.after(line.from);
        if (prev && i == chain.length - 1) {
          // Only adjust if the cursor is after the unchanged part of
          // the line.
          for (var match = 0; match < cursor.offset &&
               line.text.charAt(match) == prev.text.charAt(match); match++);
          if (cursor.offset > match)
            cursordiff = line.text.length - prev.text.length;
        }
        select.setCursorPos(this.container, {node: line.from, offset: Math.max(0, cursor.offset + cursordiff)});
      }
      // Cursor was in removed line, this is last new line.
      else if (cursor && (i == chain.length - 1) && cursor.node && cursor.node.parentNode != this.container) {
        select.setCursorPos(this.container, {node: line.from, offset: line.text.length});
      }
    }

    // Anchor the chain in the DOM tree.
    this.linkChain(chain);
    return start;
  }
function(chain) {
    // Some attempt is made to prevent the cursor from jumping
    // randomly when an undo or redo happens. It still behaves a bit
    // strange sometimes.
    var cursor = select.cursorPos(this.container, false), self = this;

    // Remove all nodes in the DOM tree between from and to (null for
    // start/end of container).
    function removeRange(from, to) {
      var pos = from ? from.nextSibling : self.container.firstChild;
      while (pos != to) {
        var temp = pos.nextSibling;
        removeElement(pos);
        pos = temp;
      }
    }

    var start = chain[0].from, end = chain[chain.length - 1].to;
    // Clear the space where this change has to be made.
    removeRange(start, end);

    // Insert the content specified by the chain into the DOM tree.
    for (var i = 0; i < chain.length; i++) {
      var line = chain[i];
      // The start and end of the space are already correct, but BR
      // tags inside it have to be put back.
      if (i > 0)
        self.container.insertBefore(line.from, end);

      // Add the text.
      var node = makePartSpan(fixSpaces(line.text), this.container.ownerDocument);
      self.container.insertBefore(node, end);
      // See if the cursor was on this line. Put it back, adjusting
      // for changed line length, if it was.
      if (cursor && cursor.node == line.from) {
        var cursordiff = 0;
        var prev = this.after(line.from);
        if (prev && i == chain.length - 1) {
          // Only adjust if the cursor is after the unchanged part of
          // the line.
          for (var match = 0; match < cursor.offset &&
               line.text.charAt(match) == prev.text.charAt(match); match++);
          if (cursor.offset > match)
            cursordiff = line.text.length - prev.text.length;
        }
        select.setCursorPos(this.container, {node: line.from, offset: Math.max(0, cursor.offset + cursordiff)});
      }
      // Cursor was in removed line, this is last new line.
      else if (cursor && (i == chain.length - 1) && cursor.node && cursor.node.parentNode != this.container) {
        select.setCursorPos(this.container, {node: line.from, offset: line.text.length});
      }
    }

    // Anchor the chain in the DOM tree.
    this.linkChain(chain);
    return start;
  }
function ( chain ) { // Some attempt is made to prevent the cursor from jumping // randomly when an undo or redo happens. It still behaves a bit // strange sometimes. var cursor = select . cursorPos ( this . container , false ) , self = this ; // Remove all nodes in the DOM tree between from and to (null for // start/end of container). function removeRange ( from , to ) { var pos = from ? from . nextSibling : self . container . firstChild ; while ( pos != to ) { var temp = pos . nextSibling ; removeElement ( pos ) ; pos = temp ; } } var start = chain [ 0 ] . from , end = chain [ chain . length - 1 ] . to ; // Clear the space where this change has to be made. removeRange ( start , end ) ; // Insert the content specified by the chain into the DOM tree. for ( var i = 0 ; i < chain . length ; i ++ ) { var line = chain [ i ] ; // The start and end of the space are already correct, but BR // tags inside it have to be put back. if ( i > 0 ) self . container . insertBefore ( line . from , end ) ; // Add the text. var node = makePartSpan ( fixSpaces ( line . text ) , this . container . ownerDocument ) ; self . container . insertBefore ( node , end ) ; // See if the cursor was on this line. Put it back, adjusting // for changed line length, if it was. if ( cursor && cursor . node == line . from ) { var cursordiff = 0 ; var prev = this . after ( line . from ) ; if ( prev && i == chain . length - 1 ) { // Only adjust if the cursor is after the unchanged part of // the line. for ( var match = 0 ; match < cursor . offset && line . text . charAt ( match ) == prev . text . charAt ( match ) ; match ++ ) ; if ( cursor . offset > match ) cursordiff = line . text . length - prev . text . length ; } select . setCursorPos ( this . container , { node : line . from , offset : Math . max ( 0 , cursor . offset + cursordiff ) } ) ; } // Cursor was in removed line, this is last new line. else if ( cursor && ( i == chain . length - 1 ) && cursor . node && cursor . node . parentNode != this . container ) { select . setCursorPos ( this . container , { node : line . from , offset : line . text . length } ) ; } } // Anchor the chain in the DOM tree. this . linkChain ( chain ) ; return start ; }
Update the DOM tree to contain the lines specified in a given chain, link this chain into the DOM nodes.
Update the DOM tree to contain the lines specified in a given chain link this chain into the DOM nodes .