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.

This commit is contained in:
Neil Jenkins 2011-11-02 16:31:46 +11:00
parent f89490736e
commit 9dcbf45f4a
3 changed files with 49 additions and 34 deletions

View file

@ -998,10 +998,18 @@ document.addEventListener( 'DOMContentLoaded', function () {
var splitNode = range.startContainer,
splitOffset = range.startOffset,
block = range.getStartBlock(),
tag = block.nodeName,
tag = block ? block.nodeName : 'DIV',
splitTag = nextTag[ tag ],
nodeAfterSplit;
if ( !block || event.shift ) {
range._insertNode( createElement( 'BR' ) );
range.collapse( false );
setSelection( range );
docWasChanged();
return;
}
if ( !block.textContent ) {
// Break list
if ( block.nearest( 'UL' ) ) {

View file

@ -45,7 +45,6 @@ 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;

View file

@ -116,8 +116,6 @@ implement( Range, {
this._extractContents();
// If we split into two different blocks, merge the blocks.
this.moveBoundariesDownTree();
var startBlock = this.getStartBlock(),
endBlock = this.getEndBlock();
if ( startBlock && endBlock && startBlock !== endBlock ) {
@ -125,7 +123,7 @@ implement( Range, {
}
// Ensure body has a block-level element in it.
var doc = startBlock.ownerDocument,
var doc = this.endContainer.ownerDocument,
body = doc.body,
bodyFirstChild = body.firstChild;
if ( !bodyFirstChild || bodyFirstChild.nodeName === 'BR' ) {
@ -325,42 +323,49 @@ implement( Range, {
return this;
},
// First block that starts before or at range beginning.
// Returns the first block at least partially contained by the range,
// or null if no block is contained by the range.
getStartBlock: function () {
// 1. Find first node after range start.
var node = this.startContainer,
offset = this.startOffset,
children = node.childNodes;
if ( node.nodeType === ELEMENT_NODE &&
offset < children.length ) {
node = children[ offset ];
}
if ( !node.isBlock() ) {
node = node.getPreviousBlock() ||
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 ) {
if ( offset < children.length ) {
node = children[ offset ].getPreviousBlock();
node = children[ offset ];
} else {
while ( node && !node.nextSibling ) {
node = node.parentNode;
}
if ( node ) {
node = node.nextSibling.getPreviousBlock();
} else {
node = this.startContainer.ownerDocument.body.lastChild;
if ( node ) { node = node.getPreviousBlock(); }
if ( node ) { node = node.nextSibling; }
}
}
return node;
// 2. If not a block, get previous block.
if ( node && !node.isBlock() ) {
node = node.getPreviousBlock();
}
// 3. Check the block does not end before range begins.
return node && this.containsNode( node, true ) ? node : null;
},
// Returns the last block at least partially contained by the range,
// or null if no block is contained by the range.
getEndBlock: function () {
// 1. Find first node before range end.
var node = this.endContainer,
offset = this.startOffset,
children = node.childNodes;
while ( offset && node.nodeType === ELEMENT_NODE ) {
node = children[ offset - 1 ];
children = node.childNodes;
offset = children.length;
}
// 2. If not a block, get previous block.
if ( !node.isBlock() ) {
node = node.getPreviousBlock();
}
// 3. Check the block does not end before range ends.
return node && this.containsNode( node, true ) ? node : null;
},
startsAtBlockBoundary: function () {
@ -412,11 +417,14 @@ implement( Range, {
expandToBlockBoundaries: function () {
var start = this.getStartBlock(),
end = this.getEndBlock(),
parent = start.parentNode;
parent;
if ( start && end ) {
parent = start.parentNode;
this.setStart( parent, indexOf.call( parent.childNodes, start ) );
parent = end.parentNode;
this.setEnd( parent, indexOf.call( parent.childNodes, end ) + 1 );
}
return this;
}