Skip to content

Commit

Permalink
Guard against IE firing beforepaste twice
Browse files Browse the repository at this point in the history
  • Loading branch information
neilj committed Nov 17, 2011
1 parent 3ca646a commit 17cb1e0
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions source/Editor.js
Expand Up @@ -1033,18 +1033,27 @@

// --- Cut and Paste ---

var afterCut = function () {
// If all content removed, ensure div at start of body.
body.fixCursor();
};

doc.addEventListener( isIE ? 'beforecut' : 'cut', function () {
// Save undo checkpoint
var range = getSelection();
recordUndoState( range );
getRangeAndRemoveBookmark( range );
// If all content removed, ensure div at start of body.
setTimeout( function () {
body.fixCursor();
}, 0 );
setTimeout( afterCut, 0 );
}, false );


// IE sometimes fires the beforepaste event twice; make sure it is not run
// again before our after paste function is called.
var awaitingPaste = false;

doc.addEventListener( isIE ? 'beforepaste' : 'paste', function () {
if ( awaitingPaste ) { return; }
awaitingPaste = true;

var range = getSelection(),
startContainer = range.startContainer,
startOffset = range.startOffset,
Expand All @@ -1065,34 +1074,37 @@
setTimeout( function () {
// Get the pasted content and clean
var frag = pasteArea.detach().empty(),
first = frag.firstChild;
first = frag.firstChild,
range = createRange(
startContainer, startOffset, endContainer, endOffset );

// Safari likes putting extra divs around things.
if ( first &&
first === frag.lastChild && first.nodeName === 'DIV' ) {
frag.replaceChild( first.empty(), first );
}

frag.normalize();
cleanTree( frag, false );
cleanupBRs( frag );
// Was anything actually pasted?
if ( first ) {
// Safari likes putting extra divs around things.
if ( first === frag.lastChild && first.nodeName === 'DIV' ) {
frag.replaceChild( first.empty(), first );
}

frag.normalize();
cleanTree( frag, false );
cleanupBRs( frag );

var node = frag;
while ( node = node.getNextBlock() ) {
node.fixCursor();
}

// Insert pasted data
range.insertTreeFragment( frag );
docWasChanged();

var node = frag;
while ( node = node.getNextBlock() ) {
node.fixCursor();
range.collapse( false );
}

// Restore the previous selection
range.setStart( startContainer, startOffset );
range.setEnd( endContainer, endOffset );

// Insert pasted data
range.insertTreeFragment( frag );
docWasChanged();

range.collapse( false );
setSelection( range );
updatePath( 0, true );

awaitingPaste = false;
}, 0 );
}, false );

Expand Down

0 comments on commit 17cb1e0

Please sign in to comment.