0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Fix Range#get(Start|End)Block methods.

This commit is contained in:
Neil Jenkins 2011-10-31 13:08:51 +11:00
parent b8e2b5fb81
commit f245cc0dba
2 changed files with 28 additions and 12 deletions

View file

@ -39,6 +39,7 @@ var swap = function( node, node2 ) {
var ELEMENT_NODE = 1, // Node.ELEMENT_NODE, var ELEMENT_NODE = 1, // Node.ELEMENT_NODE,
TEXT_NODE = 3, // Node.TEXT_NODE, TEXT_NODE = 3, // Node.TEXT_NODE,
DOCUMENT_FRAGMENT_NODE = 11, // Node.DOCUMENT_FRAGMENT_NODE
SHOW_ELEMENT = 1, // NodeFilter.SHOW_ELEMENT, SHOW_ELEMENT = 1, // NodeFilter.SHOW_ELEMENT,
FILTER_ACCEPT = 1, // NodeFilter.FILTER_ACCEPT, FILTER_ACCEPT = 1, // NodeFilter.FILTER_ACCEPT,
FILTER_SKIP = 3; // NodeFilter.FILTER_SKIP; FILTER_SKIP = 3; // NodeFilter.FILTER_SKIP;
@ -54,7 +55,7 @@ var walkForward = function ( current, filter ) {
} }
} }
if ( node.nodeName === 'BODY' ) { if ( !node ) {
return null; return null;
} }
if ( filter( node ) ) { if ( filter( node ) ) {
@ -77,7 +78,8 @@ var walkBackward = function ( current, filter ) {
node = current.parentNode; node = current.parentNode;
} }
if ( node.nodeName === 'BODY' ) { if ( node.nodeName === 'BODY' ||
node.nodeType === DOCUMENT_FRAGMENT_NODE ) {
return null; return null;
} }
if ( filter( node ) ) { if ( filter( node ) ) {

View file

@ -180,7 +180,7 @@ implement( Range, {
endContainer = nodeAfterSplit, endContainer = nodeAfterSplit,
endOffset = 0, endOffset = 0,
parent = nodeAfterSplit.parentNode, parent = nodeAfterSplit.parentNode,
child; child, node;
while ( ( child = startContainer.lastChild ) && while ( ( child = startContainer.lastChild ) &&
child.nodeType === ELEMENT_NODE && child.nodeType === ELEMENT_NODE &&
@ -200,6 +200,13 @@ implement( Range, {
endContainer.insertBefore( child, endContainer.firstChild ); endContainer.insertBefore( child, endContainer.firstChild );
endOffset += 1; endOffset += 1;
} }
// Fix cursor before inserting block:
node = frag;
while ( node = node.getNextBlock() ) {
node.fixCursor();
}
parent.insertBefore( frag, nodeAfterSplit ); parent.insertBefore( frag, nodeAfterSplit );
// 6. Merge containers at edges // 6. Merge containers at edges
@ -318,6 +325,7 @@ implement( Range, {
return this; return this;
}, },
// First block that starts before or at range beginning.
getStartBlock: function () { getStartBlock: function () {
var node = this.startContainer, var node = this.startContainer,
offset = this.startOffset, offset = this.startOffset,
@ -328,22 +336,28 @@ implement( Range, {
} }
if ( !node.isBlock() ) { if ( !node.isBlock() ) {
node = node.getPreviousBlock() || node = node.getPreviousBlock() ||
this.startContainer.ownerDocument.body.getNextBlock(); this.startContainer.ownerDocument
.body.getNextBlock();
} }
return node; return node;
}, },
// First block that starts before the range ends.
getEndBlock: function () { getEndBlock: function () {
var node = this.endContainer, var node = this.endContainer,
offset = this.endOffset, offset = this.endOffset,
children = node.childNodes; children = node.childNodes;
if ( node.nodeType === ELEMENT_NODE &&
offset && offset <= children.length ) { if ( offset < children.length ) {
node = children[ offset - 1 ]; node = children[ offset ].getPreviousBlock();
} } else {
if ( !node.isBlock() ) { while ( node && !node.nextSibling ) {
node = node.getPreviousBlock() || node = node.parentNode;
this.startContainer.ownerDocument.body.getNextBlock(); }
return node ?
node.nextSibling.getPreviousBlock() :
this.startContainer.ownerDocument
.body.lastChild.getPreviousBlock();
} }
return node; return node;
}, },