X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=_source%2Fcore%2Fdom%2Frange.js;h=3d8b10f0f31a50b2ff1c9af69466f228d25358aa;hb=refs%2Ftags%2Fv3.3.2;hp=264ee4eff190acd6baa08a3c2e12023fbd05a62f;hpb=941b0a9ba4e673e292510d80a5a86806994b8ea6;p=ckeditor.git
diff --git a/_source/core/dom/range.js b/_source/core/dom/range.js
index 264ee4e..3d8b10f 100644
--- a/_source/core/dom/range.js
+++ b/_source/core/dom/range.js
@@ -133,7 +133,7 @@ CKEDITOR.dom.range = function( document )
currentNode = levelStartNode.getNext();
- while( currentNode )
+ while ( currentNode )
{
// Stop processing when the current node matches a node in the
// endParents tree or if it is the endNode.
@@ -180,7 +180,7 @@ CKEDITOR.dom.range = function( document )
{
currentNode = levelStartNode.getPrevious();
- while( currentNode )
+ while ( currentNode )
{
// Stop processing when the current node matches a node in the
// startParents tree or if it is the startNode.
@@ -252,10 +252,10 @@ CKEDITOR.dom.range = function( document )
}
// Cleanup any marked node.
- if( removeStartNode )
+ if ( removeStartNode )
startNode.remove();
- if( removeEndNode && endNode.$.parentNode )
+ if ( removeEndNode && endNode.$.parentNode )
endNode.remove();
};
@@ -278,7 +278,7 @@ CKEDITOR.dom.range = function( document )
if ( CKEDITOR.tools.trim( node.getText() ).length )
return false;
}
- else if( node.type == CKEDITOR.NODE_ELEMENT )
+ else if ( node.type == CKEDITOR.NODE_ELEMENT )
{
// If there are non-empty inline elements (e.g. ), then we're not
// at the start.
@@ -697,7 +697,7 @@ CKEDITOR.dom.range = function( document )
},
/**
- * Move the range out of bookmark nodes if they're been the container.
+ * Move the range out of bookmark nodes if they'd been the container.
*/
optimizeBookmark: function()
{
@@ -742,15 +742,21 @@ CKEDITOR.dom.range = function( document )
startOffset = startContainer.getIndex() + 1;
startContainer = startContainer.getParent();
- // Check if it is necessary to update the end boundary.
- if ( !collapsed && this.startContainer.equals( this.endContainer ) )
+
+ // Check all necessity of updating the end boundary.
+ if ( this.startContainer.equals( this.endContainer ) )
this.setEnd( nextText, this.endOffset - this.startOffset );
+ else if ( startContainer.equals( this.endContainer ) )
+ this.endOffset += 1;
}
this.setStart( startContainer, startOffset );
if ( collapsed )
+ {
this.collapse( true );
+ return;
+ }
}
var endContainer = this.endContainer;
@@ -919,7 +925,7 @@ CKEDITOR.dom.range = function( document )
siblingText = sibling.getText();
- if ( !(/[^\s\ufeff]/).test( siblingText ) ) // Spaces + Zero Width No-Break Space (U+FEFF)
+ if ( (/[^\s\ufeff]/).test( siblingText ) ) // Spaces + Zero Width No-Break Space (U+FEFF)
sibling = null;
else
{
@@ -1078,7 +1084,7 @@ CKEDITOR.dom.range = function( document )
siblingText = sibling.getText();
- if ( !(/[^\s\ufeff]/).test( siblingText ) )
+ if ( (/[^\s\ufeff]/).test( siblingText ) )
sibling = null;
else
{
@@ -1160,13 +1166,13 @@ CKEDITOR.dom.range = function( document )
var walker = new CKEDITOR.dom.walker( walkerRange ),
blockBoundary, // The node on which the enlarging should stop.
- tailBr, //
- defaultGuard = CKEDITOR.dom.walker.blockBoundary(
+ tailBr, // In case BR as block boundary.
+ notBlockBoundary = CKEDITOR.dom.walker.blockBoundary(
( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ? { br : 1 } : null ),
// Record the encountered 'blockBoundary' for later use.
boundaryGuard = function( node )
{
- var retval = defaultGuard( node );
+ var retval = notBlockBoundary( node );
if ( !retval )
blockBoundary = node;
return retval;
@@ -1187,8 +1193,9 @@ CKEDITOR.dom.range = function( document )
// It's the body which stop the enlarging if no block boundary found.
blockBoundary = blockBoundary || body;
- // Start the range at different position by comparing
- // the document position of it with 'enlargeable' node.
+ // Start the range either after the end of found block (
...
[text)
+ // or at the start of block ([text...), by comparing the document position
+ // with 'enlargeable' node.
this.setStartAt(
blockBoundary,
!blockBoundary.is( 'br' ) &&
@@ -1214,8 +1221,8 @@ CKEDITOR.dom.range = function( document )
// It's the body which stop the enlarging if no block boundary found.
blockBoundary = blockBoundary || body;
- // Start the range at different position by comparing
- // the document position of it with 'enlargeable' node.
+ // Close the range either before the found block start (text]
...
) or at the block end (...text])
+ // by comparing the document position with 'enlargeable' node.
this.setEndAt(
blockBoundary,
( !enlargeable && this.checkEndOfBlock()
@@ -1230,6 +1237,107 @@ CKEDITOR.dom.range = function( document )
},
/**
+ * Descrease the range to make sure that boundaries
+ * always anchor beside text nodes or innermost element.
+ * @param {Number} mode ( CKEDITOR.SHRINK_ELEMENT | CKEDITOR.SHRINK_TEXT ) The shrinking mode.
+ *
+ * - CKEDITOR.SHRINK_ELEMENT
+ * - Shrink the range boundaries to the edge of the innermost element.
+ * - CKEDITOR.SHRINK_TEXT
+ * - Shrink the range boudaries to anchor by the side of enclosed text node, range remains if there's no text nodes on boundaries at all.
+ *
+ * @param {Boolean} selectContents Whether result range anchors at the inner OR outer boundary of the node.
+ */
+ shrink : function( mode, selectContents )
+ {
+ // Unable to shrink a collapsed range.
+ if ( !this.collapsed )
+ {
+ mode = mode || CKEDITOR.SHRINK_TEXT;
+
+ var walkerRange = this.clone();
+
+ var startContainer = this.startContainer,
+ endContainer = this.endContainer,
+ startOffset = this.startOffset,
+ endOffset = this.endOffset,
+ collapsed = this.collapsed;
+
+ // Whether the start/end boundary is moveable.
+ var moveStart = 1,
+ moveEnd = 1;
+
+ if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
+ {
+ if ( !startOffset )
+ walkerRange.setStartBefore( startContainer );
+ else if ( startOffset >= startContainer.getLength( ) )
+ walkerRange.setStartAfter( startContainer );
+ else
+ {
+ // Enlarge the range properly to avoid walker making
+ // DOM changes caused by triming the text nodes later.
+ walkerRange.setStartBefore( startContainer );
+ moveStart = 0;
+ }
+ }
+
+ if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
+ {
+ if ( !endOffset )
+ walkerRange.setEndBefore( endContainer );
+ else if ( endOffset >= endContainer.getLength( ) )
+ walkerRange.setEndAfter( endContainer );
+ else
+ {
+ walkerRange.setEndAfter( endContainer );
+ moveEnd = 0;
+ }
+ }
+
+ var walker = new CKEDITOR.dom.walker( walkerRange );
+
+ walker.evaluator = function( node )
+ {
+ return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ?
+ CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );
+ };
+
+ var currentElement;
+ walker.guard = function( node, movingOut )
+ {
+ // Stop when we're shrink in element mode while encountering a text node.
+ if ( mode == CKEDITOR.SHRINK_ELEMENT && node.type == CKEDITOR.NODE_TEXT )
+ return false;
+
+ // Stop when we've already walked "through" an element.
+ if ( movingOut && node.equals( currentElement ) )
+ return false;
+
+ if ( !movingOut && node.type == CKEDITOR.NODE_ELEMENT )
+ currentElement = node;
+
+ return true;
+ };
+
+ if ( moveStart )
+ {
+ var textStart = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastForward' : 'next']();
+ textStart && this.setStartAt( textStart, selectContents ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_START );
+ }
+
+ if ( moveEnd )
+ {
+ walker.reset();
+ var textEnd = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastBackward' : 'previous']();
+ textEnd && this.setEndAt( textEnd, selectContents ? CKEDITOR.POSITION_BEFORE_END : CKEDITOR.POSITION_AFTER_END );
+ }
+
+ return !!( moveStart || moveEnd );
+ }
+ },
+
+ /**
* Inserts a node at the start of the range. The range will be expanded
* the contain the node.
*/
@@ -1282,6 +1390,11 @@ CKEDITOR.dom.range = function( document )
// we will not need this check for our use of this class so we can
// ignore it for now.
+ // Fixing invalid range start inside dtd empty elements.
+ if( startNode.type == CKEDITOR.NODE_ELEMENT
+ && CKEDITOR.dtd.$empty[ startNode.getName() ] )
+ startNode = startNode.getParent(), startOffset = startNode.getIndex();
+
this.startContainer = startNode;
this.startOffset = startOffset;
@@ -1308,6 +1421,11 @@ CKEDITOR.dom.range = function( document )
// will not need this check for our use of this class so we can ignore
// it for now.
+ // Fixing invalid range end inside dtd empty elements.
+ if( endNode.type == CKEDITOR.NODE_ELEMENT
+ && CKEDITOR.dtd.$empty[ endNode.getName() ] )
+ endNode = endNode.getParent(), endOffset = endNode.getIndex() + 1;
+
this.endContainer = endNode;
this.endOffset = endOffset;
@@ -1528,12 +1646,12 @@ CKEDITOR.dom.range = function( document )
CKEDITOR.POSITION_AFTER_START
: CKEDITOR.POSITION_BEFORE_END );
- var walker = new CKEDITOR.dom.walker( walkerRange ),
- retval = false;
+ var walker = new CKEDITOR.dom.walker( walkerRange );
walker.evaluator = elementBoundaryEval;
return walker[ checkType == CKEDITOR.START ?
'checkBackward' : 'checkForward' ]();
},
+
// Calls to this function may produce changes to the DOM. The range may
// be updated to reflect such changes.
checkStartOfBlock : function()
@@ -1617,6 +1735,10 @@ CKEDITOR.dom.range = function( document )
{
var isEditable;
+ // Empty elements are rejected.
+ if ( CKEDITOR.dtd.$empty[ el.getName() ] )
+ return false;
+
while ( el && el.type == CKEDITOR.NODE_ELEMENT )
{
isEditable = el.isEditable();
@@ -1675,8 +1797,15 @@ CKEDITOR.dom.range = function( document )
*/
getEnclosedNode : function()
{
- var walkerRange = this.clone(),
- walker = new CKEDITOR.dom.walker( walkerRange ),
+ var walkerRange = this.clone();
+
+ // Optimize and analyze the range to avoid DOM destructive nature of walker. (#5780)
+ walkerRange.optimize();
+ if ( walkerRange.startContainer.type != CKEDITOR.NODE_ELEMENT
+ || walkerRange.endContainer.type != CKEDITOR.NODE_ELEMENT )
+ return null;
+
+ var walker = new CKEDITOR.dom.walker( walkerRange ),
isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),
isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
evaluator = function( node )
@@ -1722,8 +1851,15 @@ CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;
/**
* Check boundary types.
- * @see CKEDITOR.dom.range::checkBoundaryOfElement
+ * @see CKEDITOR.dom.range.prototype.checkBoundaryOfElement
*/
CKEDITOR.START = 1;
CKEDITOR.END = 2;
CKEDITOR.STARTEND = 3;
+
+/**
+ * Shrink range types.
+ * @see CKEDITOR.dom.range.prototype.shrink
+ */
+CKEDITOR.SHRINK_ELEMENT = 1;
+CKEDITOR.SHRINK_TEXT = 2;