Avoid spurious getActiveRange() exception in IE
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Tue, 26 Jul 2011 13:38:19 -0600
changeset 455 d98c950c9213
parent 454 1353533c89ec
child 456 5b699262da50
Avoid spurious getActiveRange() exception in IE
implementation.js
--- a/implementation.js	Tue Jul 26 13:38:11 2011 -0600
+++ b/implementation.js	Tue Jul 26 13:38:19 2011 -0600
@@ -2848,12 +2848,15 @@
 	&& getActiveRange().endOffset != 0
 	&& getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) {
 		// IE seems to mutate the range incorrectly here, so we need correction
-		// here as well.
-		var newStart = [getActiveRange().startContainer, getActiveRange().startOffset];
-		var newEnd = [getActiveRange().endContainer, getActiveRange().endOffset];
-		getActiveRange().endContainer.splitText(getActiveRange().endOffset);
-		getActiveRange().setStart(newStart[0], newStart[1]);
-		getActiveRange().setEnd(newEnd[0], newEnd[1]);
+		// here as well.  The active range will be temporarily in orphaned
+		// nodes, so calling getActiveRange() after splitText() but before
+		// fixing the range will throw an exception.
+		var activeRange = getActiveRange();
+		var newStart = [activeRange.startContainer, activeRange.startOffset];
+		var newEnd = [activeRange.endContainer, activeRange.endOffset];
+		activeRange.endContainer.splitText(activeRange.endOffset);
+		activeRange.setStart(newStart[0], newStart[1]);
+		activeRange.setEnd(newEnd[0], newEnd[1]);
 	}
 
 	// "Let element list be all editable Elements effectively contained in the