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

View file

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