diff --git a/source/Node.js b/source/Node.js index 4556518..35f99af 100644 --- a/source/Node.js +++ b/source/Node.js @@ -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 ) ) { diff --git a/source/Range.js b/source/Range.js index d35dbdd..58e5f3e 100644 --- a/source/Range.js +++ b/source/Range.js @@ -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 ( !node.isBlock() ) { - node = node.getPreviousBlock() || - this.startContainer.ownerDocument.body.getNextBlock(); + + if ( offset < children.length ) { + node = children[ offset ].getPreviousBlock(); + } else { + while ( node && !node.nextSibling ) { + node = node.parentNode; + } + return node ? + node.nextSibling.getPreviousBlock() : + this.startContainer.ownerDocument + .body.lastChild.getPreviousBlock(); } return node; },