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

Fix inserting partial inlines in insertTreeFragmentIntoRange

Needs to account for browser "<br>"s inside blocks.
This commit is contained in:
Neil Jenkins 2015-06-19 10:00:55 +07:00
parent a1ff33da9f
commit e90f18dba9
3 changed files with 22 additions and 10 deletions

View file

@ -805,13 +805,16 @@ var insertTreeFragmentIntoRange = function ( range, frag ) {
endContainer = nodeAfterSplit, endContainer = nodeAfterSplit,
endOffset = 0, endOffset = 0,
parent = nodeAfterSplit.parentNode, parent = nodeAfterSplit.parentNode,
child, node, prev, next; child, node, prev, next, startAnchor;
// 2. Move down into edge either side of split and insert any inline // 2. Move down into edge either side of split and insert any inline
// nodes at the beginning/end of the fragment // nodes at the beginning/end of the fragment
while ( ( child = startContainer.lastChild ) && while ( ( child = startContainer.lastChild ) &&
child.nodeType === ELEMENT_NODE && child.nodeType === ELEMENT_NODE ) {
child.nodeName !== 'BR' ) { if ( child.nodeName === 'BR' ) {
startOffset -= 1;
break;
}
startContainer = child; startContainer = child;
startOffset = startContainer.childNodes.length; startOffset = startContainer.childNodes.length;
} }
@ -820,8 +823,9 @@ var insertTreeFragmentIntoRange = function ( range, frag ) {
child.nodeName !== 'BR' ) { child.nodeName !== 'BR' ) {
endContainer = child; endContainer = child;
} }
startAnchor = startContainer.childNodes[ startOffset ] || null;
while ( ( child = frag.firstChild ) && isInline( child ) ) { while ( ( child = frag.firstChild ) && isInline( child ) ) {
startContainer.appendChild( child ); startContainer.insertBefore( child, startAnchor );
} }
while ( ( child = frag.lastChild ) && isInline( child ) ) { while ( ( child = frag.lastChild ) && isInline( child ) ) {
endContainer.insertBefore( child, endContainer.firstChild ); endContainer.insertBefore( child, endContainer.firstChild );
@ -1049,6 +1053,7 @@ var rangeDoesStartAtBlockBoundary = function ( range ) {
startOffset = range.startOffset; startOffset = range.startOffset;
// If in the middle or end of a text node, we're not at the boundary. // If in the middle or end of a text node, we're not at the boundary.
contentWalker.root = null;
if ( startContainer.nodeType === TEXT_NODE ) { if ( startContainer.nodeType === TEXT_NODE ) {
if ( startOffset ) { if ( startOffset ) {
return false; return false;
@ -1071,6 +1076,7 @@ var rangeDoesEndAtBlockBoundary = function ( range ) {
// If in a text node with content, and not at the end, we're not // If in a text node with content, and not at the end, we're not
// at the boundary // at the boundary
contentWalker.root = null;
if ( endContainer.nodeType === TEXT_NODE ) { if ( endContainer.nodeType === TEXT_NODE ) {
length = endContainer.data.length; length = endContainer.data.length;
if ( length && endOffset < length ) { if ( length && endOffset < length ) {

File diff suppressed because one or more lines are too long

View file

@ -204,13 +204,16 @@ var insertTreeFragmentIntoRange = function ( range, frag ) {
endContainer = nodeAfterSplit, endContainer = nodeAfterSplit,
endOffset = 0, endOffset = 0,
parent = nodeAfterSplit.parentNode, parent = nodeAfterSplit.parentNode,
child, node, prev, next; child, node, prev, next, startAnchor;
// 2. Move down into edge either side of split and insert any inline // 2. Move down into edge either side of split and insert any inline
// nodes at the beginning/end of the fragment // nodes at the beginning/end of the fragment
while ( ( child = startContainer.lastChild ) && while ( ( child = startContainer.lastChild ) &&
child.nodeType === ELEMENT_NODE && child.nodeType === ELEMENT_NODE ) {
child.nodeName !== 'BR' ) { if ( child.nodeName === 'BR' ) {
startOffset -= 1;
break;
}
startContainer = child; startContainer = child;
startOffset = startContainer.childNodes.length; startOffset = startContainer.childNodes.length;
} }
@ -219,8 +222,9 @@ var insertTreeFragmentIntoRange = function ( range, frag ) {
child.nodeName !== 'BR' ) { child.nodeName !== 'BR' ) {
endContainer = child; endContainer = child;
} }
startAnchor = startContainer.childNodes[ startOffset ] || null;
while ( ( child = frag.firstChild ) && isInline( child ) ) { while ( ( child = frag.firstChild ) && isInline( child ) ) {
startContainer.appendChild( child ); startContainer.insertBefore( child, startAnchor );
} }
while ( ( child = frag.lastChild ) && isInline( child ) ) { while ( ( child = frag.lastChild ) && isInline( child ) ) {
endContainer.insertBefore( child, endContainer.firstChild ); endContainer.insertBefore( child, endContainer.firstChild );
@ -448,6 +452,7 @@ var rangeDoesStartAtBlockBoundary = function ( range ) {
startOffset = range.startOffset; startOffset = range.startOffset;
// If in the middle or end of a text node, we're not at the boundary. // If in the middle or end of a text node, we're not at the boundary.
contentWalker.root = null;
if ( startContainer.nodeType === TEXT_NODE ) { if ( startContainer.nodeType === TEXT_NODE ) {
if ( startOffset ) { if ( startOffset ) {
return false; return false;
@ -470,6 +475,7 @@ var rangeDoesEndAtBlockBoundary = function ( range ) {
// If in a text node with content, and not at the end, we're not // If in a text node with content, and not at the end, we're not
// at the boundary // at the boundary
contentWalker.root = null;
if ( endContainer.nodeType === TEXT_NODE ) { if ( endContainer.nodeType === TEXT_NODE ) {
length = endContainer.data.length; length = endContainer.data.length;
if ( length && endOffset < length ) { if ( length && endOffset < length ) {