0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Ensure moving boundaries up tree never passes root

This commit is contained in:
Neil Jenkins 2017-01-13 15:15:59 +11:00
parent 3a17d3793b
commit 9596f4ecf8
6 changed files with 24 additions and 16 deletions

View file

@ -865,7 +865,8 @@ var deleteContentsOfRange = function ( range, root ) {
// Move boundaries up as much as possible without exiting block,
// to reduce need to split.
moveRangeBoundariesUpTree( range, startBlock, endBlock );
moveRangeBoundariesDownTree( range );
moveRangeBoundariesUpTree( range, startBlock, endBlock, root );
// Remove selected range
frag = extractContentsOfRange( range, null, root );
@ -1109,7 +1110,7 @@ var moveRangeBoundariesDownTree = function ( range ) {
}
};
var moveRangeBoundariesUpTree = function ( range, startMax, endMax ) {
var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) {
var startContainer = range.startContainer;
var startOffset = range.startOffset;
var endContainer = range.endContainer;
@ -1124,7 +1125,9 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax ) {
endMax = startMax;
}
while ( startContainer !== startMax && !startOffset ) {
while ( !startOffset &&
startContainer !== startMax &&
startContainer !== root ) {
parent = startContainer.parentNode;
startOffset = indexOf.call( parent.childNodes, startContainer );
startContainer = parent;
@ -1139,6 +1142,7 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax ) {
maySkipBR = false;
}
if ( endContainer === endMax ||
endContainer === root ||
endOffset !== getLength( endContainer ) ) {
break;
}
@ -1637,7 +1641,7 @@ var keyHandlers = {
// delete it ourselves, because the browser won't if it is not
// inline.
originalRange = range.cloneRange();
moveRangeBoundariesUpTree( range, root, root );
moveRangeBoundariesUpTree( range, root, root, root );
cursorContainer = range.endContainer;
cursorOffset = range.endOffset;
if ( cursorContainer.nodeType === ELEMENT_NODE ) {
@ -2233,7 +2237,7 @@ var onCopy = function ( event ) {
// passing the copy root node.
range = range.cloneRange();
moveRangeBoundariesDownTree( range );
moveRangeBoundariesUpTree( range, copyRoot, copyRoot );
moveRangeBoundariesUpTree( range, copyRoot, copyRoot, root );
// Extract the contents
parent = range.commonAncestorContainer;
contents = range.cloneContents();
@ -3744,7 +3748,7 @@ proto.modifyBlocks = function ( modify, range ) {
expandRangeToBlockBoundaries( range, root );
// 3. Remove range.
moveRangeBoundariesUpTree( range, root, root );
moveRangeBoundariesUpTree( range, root, root, root );
frag = extractContentsOfRange( range, root, root );
// 4. Modify tree of fragment and reinsert.
@ -4478,7 +4482,7 @@ proto.removeAllFormatting = function ( range ) {
this.saveUndoState( range );
// Avoid splitting where we're already at edges.
moveRangeBoundariesUpTree( range, stopNode, stopNode );
moveRangeBoundariesUpTree( range, stopNode, stopNode, root );
// Split the selection up to the block, or if whole selection in same
// block, expand range boundaries to ends of block and split up to root.

File diff suppressed because one or more lines are too long

View file

@ -107,7 +107,7 @@ var onCopy = function ( event ) {
// passing the copy root node.
range = range.cloneRange();
moveRangeBoundariesDownTree( range );
moveRangeBoundariesUpTree( range, copyRoot, copyRoot );
moveRangeBoundariesUpTree( range, copyRoot, copyRoot, root );
// Extract the contents
parent = range.commonAncestorContainer;
contents = range.cloneContents();

View file

@ -1301,7 +1301,7 @@ proto.modifyBlocks = function ( modify, range ) {
expandRangeToBlockBoundaries( range, root );
// 3. Remove range.
moveRangeBoundariesUpTree( range, root, root );
moveRangeBoundariesUpTree( range, root, root, root );
frag = extractContentsOfRange( range, root, root );
// 4. Modify tree of fragment and reinsert.
@ -2035,7 +2035,7 @@ proto.removeAllFormatting = function ( range ) {
this.saveUndoState( range );
// Avoid splitting where we're already at edges.
moveRangeBoundariesUpTree( range, stopNode, stopNode );
moveRangeBoundariesUpTree( range, stopNode, stopNode, root );
// Split the selection up to the block, or if whole selection in same
// block, expand range boundaries to ends of block and split up to root.

View file

@ -362,7 +362,7 @@ var keyHandlers = {
// delete it ourselves, because the browser won't if it is not
// inline.
originalRange = range.cloneRange();
moveRangeBoundariesUpTree( range, root, root );
moveRangeBoundariesUpTree( range, root, root, root );
cursorContainer = range.endContainer;
cursorOffset = range.endOffset;
if ( cursorContainer.nodeType === ELEMENT_NODE ) {

View file

@ -140,7 +140,8 @@ var deleteContentsOfRange = function ( range, root ) {
// Move boundaries up as much as possible without exiting block,
// to reduce need to split.
moveRangeBoundariesUpTree( range, startBlock, endBlock );
moveRangeBoundariesDownTree( range );
moveRangeBoundariesUpTree( range, startBlock, endBlock, root );
// Remove selected range
frag = extractContentsOfRange( range, null, root );
@ -384,7 +385,7 @@ var moveRangeBoundariesDownTree = function ( range ) {
}
};
var moveRangeBoundariesUpTree = function ( range, startMax, endMax ) {
var moveRangeBoundariesUpTree = function ( range, startMax, endMax, root ) {
var startContainer = range.startContainer;
var startOffset = range.startOffset;
var endContainer = range.endContainer;
@ -399,7 +400,9 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax ) {
endMax = startMax;
}
while ( startContainer !== startMax && !startOffset ) {
while ( !startOffset &&
startContainer !== startMax &&
startContainer !== root ) {
parent = startContainer.parentNode;
startOffset = indexOf.call( parent.childNodes, startContainer );
startContainer = parent;
@ -414,6 +417,7 @@ var moveRangeBoundariesUpTree = function ( range, startMax, endMax ) {
maySkipBR = false;
}
if ( endContainer === endMax ||
endContainer === root ||
endOffset !== getLength( endContainer ) ) {
break;
}