For licensing, see LICENSE.html or http://ckeditor.com/license\r
*/\r
\r
+/**\r
+ * @class\r
+ */\r
CKEDITOR.dom.range = function( document )\r
{\r
this.startContainer = null;\r
return node.type != CKEDITOR.NODE_TEXT\r
&& node.getName() in CKEDITOR.dtd.$removeEmpty\r
|| !CKEDITOR.tools.trim( node.getText() )\r
- || node.getParent().hasAttribute( '_fck_bookmark' );\r
+ || node.getParent().hasAttribute( '_cke_bookmark' );\r
}\r
\r
var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),\r
var startNode, endNode;\r
var baseId;\r
var clone;\r
+ var collapsed = this.collapsed;\r
\r
startNode = this.document.createElement( 'span' );\r
- startNode.setAttribute( '_fck_bookmark', 1 );\r
+ startNode.setAttribute( '_cke_bookmark', 1 );\r
startNode.setStyle( 'display', 'none' );\r
\r
// For IE, it must have something inside, otherwise it may be\r
}\r
\r
// If collapsed, the endNode will not be created.\r
- if ( !this.collapsed )\r
+ if ( !collapsed )\r
{\r
endNode = startNode.clone();\r
endNode.setHtml( ' ' );\r
return {\r
startNode : serializable ? baseId + 'S' : startNode,\r
endNode : serializable ? baseId + 'E' : endNode,\r
- serializable : serializable\r
+ serializable : serializable,\r
+ collapsed : collapsed\r
};\r
},\r
\r
var startOffset = this.startOffset,\r
endOffset = this.endOffset;\r
\r
+ var collapsed = this.collapsed;\r
+\r
var child, previous;\r
\r
// If there is no range then get out of here.\r
}\r
\r
// Process the end only if not normalized.\r
- if ( !this.isCollapsed )\r
+ if ( !collapsed )\r
{\r
// Find out if the start is pointing to a text node that\r
// will be normalized.\r
\r
return {\r
start : startContainer.getAddress( normalized ),\r
- end : this.isCollapsed ? null : endContainer.getAddress( normalized ),\r
+ end : collapsed ? null : endContainer.getAddress( normalized ),\r
startOffset : startOffset,\r
endOffset : endOffset,\r
normalized : normalized,\r
+ collapsed : collapsed,\r
is2 : true // It's a createBookmark2 bookmark.\r
};\r
},\r
},\r
\r
/**\r
- * Move the range out of bookmark nodes if they're been the container.\r
+ * Move the range out of bookmark nodes if they'd been the container.\r
*/\r
optimizeBookmark: function()\r
{\r
endNode = this.endContainer;\r
\r
if ( startNode.is && startNode.is( 'span' )\r
- && startNode.hasAttribute( '_fck_bookmark' ) )\r
+ && startNode.hasAttribute( '_cke_bookmark' ) )\r
this.setStartAt( startNode, CKEDITOR.POSITION_BEFORE_START );\r
if ( endNode && endNode.is && endNode.is( 'span' )\r
- && endNode.hasAttribute( '_fck_bookmark' ) )\r
+ && endNode.hasAttribute( '_cke_bookmark' ) )\r
this.setEndAt( endNode, CKEDITOR.POSITION_AFTER_END );\r
},\r
\r
// If this is a visible element.\r
// We need to check for the bookmark attribute because IE insists on\r
// rendering the display:none nodes we use for bookmarks. (#3363)\r
- if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )\r
+ if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_cke_bookmark' ) )\r
{\r
// We'll accept it only if we need\r
// whitespace, and this is an inline\r
\r
siblingText = sibling.getText();\r
\r
- if ( !(/[^\s\ufeff]/).test( siblingText ) ) // Spaces + Zero Width No-Break Space (U+FEFF)\r
+ if ( (/[^\s\ufeff]/).test( siblingText ) ) // Spaces + Zero Width No-Break Space (U+FEFF)\r
sibling = null;\r
else\r
{\r
// If this is a visible element.\r
// We need to check for the bookmark attribute because IE insists on\r
// rendering the display:none nodes we use for bookmarks. (#3363)\r
- if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )\r
+ if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_cke_bookmark' ) )\r
{\r
// We'll accept it only if we need\r
// whitespace, and this is an inline\r
\r
siblingText = sibling.getText();\r
\r
- if ( !(/[^\s\ufeff]/).test( siblingText ) )\r
+ if ( (/[^\s\ufeff]/).test( siblingText ) )\r
sibling = null;\r
else\r
{\r
\r
var walker = new CKEDITOR.dom.walker( walkerRange ),\r
blockBoundary, // The node on which the enlarging should stop.\r
- tailBr, //\r
- defaultGuard = CKEDITOR.dom.walker.blockBoundary(\r
+ tailBr, // In case BR as block boundary.\r
+ notBlockBoundary = CKEDITOR.dom.walker.blockBoundary(\r
( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ? { br : 1 } : null ),\r
// Record the encountered 'blockBoundary' for later use.\r
boundaryGuard = function( node )\r
{\r
- var retval = defaultGuard( node );\r
+ var retval = notBlockBoundary( node );\r
if ( !retval )\r
blockBoundary = node;\r
return retval;\r
// It's the body which stop the enlarging if no block boundary found.\r
blockBoundary = blockBoundary || body;\r
\r
- // Start the range at different position by comparing\r
- // the document position of it with 'enlargeable' node.\r
+ // Start the range either after the end of found block (<p>...</p>[text)\r
+ // or at the start of block (<p>[text...), by comparing the document position\r
+ // with 'enlargeable' node.\r
this.setStartAt(\r
blockBoundary,\r
!blockBoundary.is( 'br' ) &&\r
// It's the body which stop the enlarging if no block boundary found.\r
blockBoundary = blockBoundary || body;\r
\r
- // Start the range at different position by comparing\r
- // the document position of it with 'enlargeable' node.\r
+ // Close the range either before the found block start (text]<p>...</p>) or at the block end (...text]</p>)\r
+ // by comparing the document position with 'enlargeable' node.\r
this.setEndAt(\r
blockBoundary,\r
( !enlargeable && this.checkEndOfBlock()\r
},\r
\r
/**\r
+ * Descrease the range to make sure that boundaries\r
+ * always anchor beside text nodes or innermost element.\r
+ * @param {Number} mode ( CKEDITOR.SHRINK_ELEMENT | CKEDITOR.SHRINK_TEXT ) The shrinking mode.\r
+ * <dl>\r
+ * <dt>CKEDITOR.SHRINK_ELEMENT</dt>\r
+ * <dd>Shrink the range boundaries to the edge of the innermost element.</dd>\r
+ * <dt>CKEDITOR.SHRINK_TEXT</dt>\r
+ * <dd>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.</dd>\r
+ * </dl>\r
+ * @param {Boolean} selectContents Whether result range anchors at the inner OR outer boundary of the node.\r
+ */\r
+ shrink : function( mode, selectContents )\r
+ {\r
+ // Unable to shrink a collapsed range.\r
+ if ( !this.collapsed )\r
+ {\r
+ mode = mode || CKEDITOR.SHRINK_TEXT;\r
+\r
+ var walkerRange = this.clone();\r
+\r
+ var startContainer = this.startContainer,\r
+ endContainer = this.endContainer,\r
+ startOffset = this.startOffset,\r
+ endOffset = this.endOffset,\r
+ collapsed = this.collapsed;\r
+\r
+ // Whether the start/end boundary is moveable.\r
+ var moveStart = 1,\r
+ moveEnd = 1;\r
+\r
+ if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
+ {\r
+ if ( !startOffset )\r
+ walkerRange.setStartBefore( startContainer );\r
+ else if ( startOffset >= startContainer.getLength( ) )\r
+ walkerRange.setStartAfter( startContainer );\r
+ else\r
+ {\r
+ // Enlarge the range properly to avoid walker making\r
+ // DOM changes caused by triming the text nodes later.\r
+ walkerRange.setStartBefore( startContainer );\r
+ moveStart = 0;\r
+ }\r
+ }\r
+\r
+ if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
+ {\r
+ if ( !endOffset )\r
+ walkerRange.setEndBefore( endContainer );\r
+ else if ( endOffset >= endContainer.getLength( ) )\r
+ walkerRange.setEndAfter( endContainer );\r
+ else\r
+ {\r
+ walkerRange.setEndAfter( endContainer );\r
+ moveEnd = 0;\r
+ }\r
+ }\r
+\r
+ var walker = new CKEDITOR.dom.walker( walkerRange ),\r
+ isBookmark = CKEDITOR.dom.walker.bookmark();\r
+\r
+ walker.evaluator = function( node )\r
+ {\r
+ return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ?\r
+ CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );\r
+ };\r
+\r
+ var currentElement;\r
+ walker.guard = function( node, movingOut )\r
+ {\r
+ if ( isBookmark( node ) )\r
+ return true;\r
+\r
+ // Stop when we're shrink in element mode while encountering a text node.\r
+ if ( mode == CKEDITOR.SHRINK_ELEMENT && node.type == CKEDITOR.NODE_TEXT )\r
+ return false;\r
+\r
+ // Stop when we've already walked "through" an element.\r
+ if ( movingOut && node.equals( currentElement ) )\r
+ return false;\r
+\r
+ if ( !movingOut && node.type == CKEDITOR.NODE_ELEMENT )\r
+ currentElement = node;\r
+\r
+ return true;\r
+ };\r
+\r
+ if ( moveStart )\r
+ {\r
+ var textStart = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastForward' : 'next']();\r
+ textStart && this.setStartAt( textStart, selectContents ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_START );\r
+ }\r
+\r
+ if ( moveEnd )\r
+ {\r
+ walker.reset();\r
+ var textEnd = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastBackward' : 'previous']();\r
+ textEnd && this.setEndAt( textEnd, selectContents ? CKEDITOR.POSITION_BEFORE_END : CKEDITOR.POSITION_AFTER_END );\r
+ }\r
+\r
+ return !!( moveStart || moveEnd );\r
+ }\r
+ },\r
+\r
+ /**\r
* Inserts a node at the start of the range. The range will be expanded\r
* the contain the node.\r
*/\r
// we will not need this check for our use of this class so we can\r
// ignore it for now.\r
\r
+ // Fixing invalid range start inside dtd empty elements.\r
+ if( startNode.type == CKEDITOR.NODE_ELEMENT\r
+ && CKEDITOR.dtd.$empty[ startNode.getName() ] )\r
+ startNode = startNode.getParent(), startOffset = startNode.getIndex();\r
+\r
this.startContainer = startNode;\r
this.startOffset = startOffset;\r
\r
// will not need this check for our use of this class so we can ignore\r
// it for now.\r
\r
+ // Fixing invalid range end inside dtd empty elements.\r
+ if( endNode.type == CKEDITOR.NODE_ELEMENT\r
+ && CKEDITOR.dtd.$empty[ endNode.getName() ] )\r
+ endOffset = endNode.getIndex() + 1, endNode = endNode.getParent();\r
+\r
this.endContainer = endNode;\r
this.endOffset = endOffset;\r
\r
},\r
\r
/**\r
- * Check whether current range is on the inner edge of the specified element.\r
- * @param {Number} checkType ( CKEDITOR.START | CKEDITOR.END ) The checking side.\r
+ * Check whether a range boundary is at the inner boundary of a given\r
+ * element.\r
* @param {CKEDITOR.dom.element} element The target element to check.\r
+ * @param {Number} checkType The boundary to check for both the range\r
+ * and the element. It can be CKEDITOR.START or CKEDITOR.END.\r
+ * @returns {Boolean} "true" if the range boundary is at the inner\r
+ * boundary of the element.\r
*/\r
checkBoundaryOfElement : function( element, checkType )\r
{\r
+ var checkStart = ( checkType == CKEDITOR.START );\r
+\r
+ // Create a copy of this range, so we can manipulate it for our checks.\r
var walkerRange = this.clone();\r
+\r
+ // Collapse the range at the proper size.\r
+ walkerRange.collapse( checkStart );\r
+\r
// Expand the range to element boundary.\r
- walkerRange[ checkType == CKEDITOR.START ?\r
- 'setStartAt' : 'setEndAt' ]\r
- ( element, checkType == CKEDITOR.START ?\r
- CKEDITOR.POSITION_AFTER_START\r
- : CKEDITOR.POSITION_BEFORE_END );\r
+ walkerRange[ checkStart ? 'setStartAt' : 'setEndAt' ]\r
+ ( element, checkStart ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_END );\r
\r
- var walker = new CKEDITOR.dom.walker( walkerRange ),\r
- retval = false;\r
+ // Create the walker, which will check if we have anything useful\r
+ // in the range.\r
+ var walker = new CKEDITOR.dom.walker( walkerRange );\r
walker.evaluator = elementBoundaryEval;\r
- return walker[ checkType == CKEDITOR.START ?\r
- 'checkBackward' : 'checkForward' ]();\r
+\r
+ return walker[ checkStart ? 'checkBackward' : 'checkForward' ]();\r
},\r
+\r
// Calls to this function may produce changes to the DOM. The range may\r
// be updated to reflect such changes.\r
checkStartOfBlock : function()\r
{\r
var isEditable;\r
\r
+ // Empty elements are rejected.\r
+ if ( CKEDITOR.dtd.$empty[ el.getName() ] )\r
+ return false;\r
+\r
while ( el && el.type == CKEDITOR.NODE_ELEMENT )\r
{\r
isEditable = el.isEditable();\r
*/\r
getEnclosedNode : function()\r
{\r
- var walkerRange = this.clone(),\r
- walker = new CKEDITOR.dom.walker( walkerRange ),\r
+ var walkerRange = this.clone();\r
+\r
+ // Optimize and analyze the range to avoid DOM destructive nature of walker. (#5780)\r
+ walkerRange.optimize();\r
+ if ( walkerRange.startContainer.type != CKEDITOR.NODE_ELEMENT\r
+ || walkerRange.endContainer.type != CKEDITOR.NODE_ELEMENT )\r
+ return null;\r
+\r
+ var walker = new CKEDITOR.dom.walker( walkerRange ),\r
isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),\r
isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
evaluator = function( node )\r
CKEDITOR.ENLARGE_BLOCK_CONTENTS = 2;\r
CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;\r
\r
-/**\r
- * Check boundary types.\r
- * @see CKEDITOR.dom.range::checkBoundaryOfElement\r
- */\r
+// Check boundary types.\r
+// @see CKEDITOR.dom.range.prototype.checkBoundaryOfElement\r
CKEDITOR.START = 1;\r
CKEDITOR.END = 2;\r
CKEDITOR.STARTEND = 3;\r
+\r
+// Shrink range types.\r
+// @see CKEDITOR.dom.range.prototype.shrink\r
+CKEDITOR.SHRINK_ELEMENT = 1;\r
+CKEDITOR.SHRINK_TEXT = 2;\r