/*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
For licensing, see LICENSE.html or http://ckeditor.com/license\r
*/\r
\r
/**\r
- * @class\r
+ * Creates a CKEDITOR.dom.range instance that can be used inside a specific\r
+ * DOM Document.\r
+ * @class Represents a delimited piece of content in a DOM Document.\r
+ * It is contiguous in the sense that it can be characterized as selecting all\r
+ * of the content between a pair of boundary-points.<br>\r
+ * <br>\r
+ * This class shares much of the W3C\r
+ * <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html">Document Object Model Range</a>\r
+ * ideas and features, adding several range manipulation tools to it, but it's\r
+ * not intended to be compatible with it.\r
+ * @param {CKEDITOR.dom.document} document The document into which the range\r
+ * features will be available.\r
+ * @example\r
+ * // Create a range for the entire contents of the editor document body.\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * // Delete the contents.\r
+ * range.deleteContents();\r
*/\r
CKEDITOR.dom.range = function( document )\r
{\r
+ /**\r
+ * Node within which the range begins.\r
+ * @type {CKEDITOR.NODE_ELEMENT|CKEDITOR.NODE_TEXT}\r
+ * @example\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * alert( range.startContainer.getName() ); // "body"\r
+ */\r
this.startContainer = null;\r
+\r
+ /**\r
+ * Offset within the starting node of the range.\r
+ * @type {Number}\r
+ * @example\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * alert( range.startOffset ); // "0"\r
+ */\r
this.startOffset = null;\r
+\r
+ /**\r
+ * Node within which the range ends.\r
+ * @type {CKEDITOR.NODE_ELEMENT|CKEDITOR.NODE_TEXT}\r
+ * @example\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * alert( range.endContainer.getName() ); // "body"\r
+ */\r
this.endContainer = null;\r
+\r
+ /**\r
+ * Offset within the ending node of the range.\r
+ * @type {Number}\r
+ * @example\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * alert( range.endOffset ); // == editor.document.getBody().getChildCount()\r
+ */\r
this.endOffset = null;\r
+\r
+ /**\r
+ * Indicates that this is a collapsed range. A collapsed range has it's\r
+ * start and end boudaries at the very same point so nothing is contained\r
+ * in it.\r
+ * @example\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * alert( range.collapsed ); // "false"\r
+ * range.collapse();\r
+ * alert( range.collapsed ); // "true"\r
+ */\r
this.collapsed = true;\r
\r
+ /**\r
+ * The document within which the range can be used.\r
+ * @type {CKEDITOR.dom.document}\r
+ * @example\r
+ * // Selects the body contents of the range document.\r
+ * range.selectNodeContents( range.document.getBody() );\r
+ */\r
this.document = document;\r
};\r
\r
\r
// Creates the appropriate node evaluator for the dom walker used inside\r
// check(Start|End)OfBlock.\r
- function getCheckStartEndBlockEvalFunction( isStart )\r
+ function getCheckStartEndBlockEvalFunction()\r
{\r
- var hadBr = false, bookmarkEvaluator = CKEDITOR.dom.walker.bookmark( true );\r
+ var skipBogus = false,\r
+ whitespaces = CKEDITOR.dom.walker.whitespaces(),\r
+ bookmarkEvaluator = CKEDITOR.dom.walker.bookmark( true ),\r
+ isBogus = CKEDITOR.dom.walker.bogus();\r
+\r
return function( node )\r
{\r
- // First ignore bookmark nodes.\r
- if ( bookmarkEvaluator( node ) )\r
+ // First skip empty nodes.\r
+ if ( bookmarkEvaluator( node ) || whitespaces( node ) )\r
return true;\r
\r
- if ( node.type == CKEDITOR.NODE_TEXT )\r
+ // Skip the bogus node at the end of block.\r
+ if ( isBogus( node ) &&\r
+ !skipBogus )\r
{\r
- // If there's any visible text, then we're not at the start.\r
- if ( CKEDITOR.tools.trim( node.getText() ).length )\r
- return false;\r
- }\r
- else if ( node.type == CKEDITOR.NODE_ELEMENT )\r
- {\r
- // If there are non-empty inline elements (e.g. <img />), then we're not\r
- // at the start.\r
- if ( !inlineChildReqElements[ node.getName() ] )\r
- {\r
- // If we're working at the end-of-block, forgive the first <br /> in non-IE\r
- // browsers.\r
- if ( !isStart && !CKEDITOR.env.ie && node.getName() == 'br' && !hadBr )\r
- hadBr = true;\r
- else\r
- return false;\r
- }\r
+ skipBogus = true;\r
+ return true;\r
}\r
+\r
+ // If there's any visible text, then we're not at the start.\r
+ if ( node.type == CKEDITOR.NODE_TEXT &&\r
+ ( node.hasAscendant( 'pre' ) ||\r
+ CKEDITOR.tools.trim( node.getText() ).length ) )\r
+ return false;\r
+\r
+ // If there are non-empty inline elements (e.g. <img />), then we're not\r
+ // at the start.\r
+ if ( node.type == CKEDITOR.NODE_ELEMENT && !inlineChildReqElements[ node.getName() ] )\r
+ return false;\r
+\r
return true;\r
};\r
}\r
\r
+\r
+ var isBogus = CKEDITOR.dom.walker.bogus();\r
// Evaluator for CKEDITOR.dom.element::checkBoundaryOfElement, reject any\r
// text node and non-empty elements unless it's being bookmark text.\r
- function elementBoundaryEval( node )\r
+ function elementBoundaryEval( checkStart )\r
{\r
- // Reject any text node unless it's being bookmark\r
- // OR it's spaces. (#3883)\r
- return node.type != CKEDITOR.NODE_TEXT\r
- && node.getName() in CKEDITOR.dtd.$removeEmpty\r
- || !CKEDITOR.tools.trim( node.getText() )\r
- || !!node.getParent().data( 'cke-bookmark' );\r
+ var whitespaces = CKEDITOR.dom.walker.whitespaces(),\r
+ bookmark = CKEDITOR.dom.walker.bookmark( 1 );\r
+\r
+ return function( node )\r
+ {\r
+ // First skip empty nodes.\r
+ if ( bookmark( node ) || whitespaces( node ) )\r
+ return true;\r
+\r
+ // Tolerant bogus br when checking at the end of block.\r
+ // Reject any text node unless it's being bookmark\r
+ // OR it's spaces.\r
+ // Reject any element unless it's being invisible empty. (#3883)\r
+ return !checkStart && isBogus( node ) ||\r
+ node.type == CKEDITOR.NODE_ELEMENT &&\r
+ node.getName() in CKEDITOR.dtd.$removeEmpty;\r
+ };\r
}\r
\r
var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),\r
- bookmarkEval = new CKEDITOR.dom.walker.bookmark();\r
+ bookmarkEval = new CKEDITOR.dom.walker.bookmark(),\r
+ nbspRegExp = /^[\t\r\n ]*(?: |\xa0)$/;\r
\r
function nonWhitespaceOrBookmarkEval( node )\r
{\r
if ( serializable )\r
{\r
baseId = 'cke_bm_' + CKEDITOR.tools.getNextNumber();\r
- startNode.setAttribute( 'id', baseId + 'S' );\r
+ startNode.setAttribute( 'id', baseId + ( collapsed ? 'C' : 'S' ) );\r
}\r
\r
// If collapsed, the endNode will not be created.\r
this.moveToPosition( startNode, CKEDITOR.POSITION_AFTER_END );\r
\r
return {\r
- startNode : serializable ? baseId + 'S' : startNode,\r
+ startNode : serializable ? baseId + ( collapsed ? 'C' : 'S' ) : startNode,\r
endNode : serializable ? baseId + 'E' : endNode,\r
serializable : serializable,\r
collapsed : collapsed\r
startContainer = child;\r
startOffset = 0;\r
}\r
+\r
+ // Get the normalized offset.\r
+ if ( child && child.type == CKEDITOR.NODE_ELEMENT )\r
+ startOffset = child.getIndex( 1 );\r
}\r
\r
// Normalize the start.\r
endContainer = child;\r
endOffset = 0;\r
}\r
+\r
+ // Get the normalized offset.\r
+ if ( child && child.type == CKEDITOR.NODE_ELEMENT )\r
+ endOffset = child.getIndex( 1 );\r
}\r
\r
// Normalize the end.\r
// whitespaces at the end.\r
isWhiteSpace = false;\r
\r
- if ( sibling.type == CKEDITOR.NODE_TEXT )\r
+ if ( sibling.type == CKEDITOR.NODE_COMMENT )\r
+ {\r
+ sibling = sibling.getPrevious();\r
+ continue;\r
+ }\r
+ else if ( sibling.type == CKEDITOR.NODE_TEXT )\r
{\r
siblingText = sibling.getText();\r
\r
sibling = null;\r
else\r
{\r
- var allChildren = sibling.$.all || sibling.$.getElementsByTagName( '*' );\r
+ var allChildren = sibling.$.getElementsByTagName( '*' );\r
for ( var i = 0, child ; child = allChildren[ i++ ] ; )\r
{\r
if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )\r
\r
isWhiteSpace = /^[\s\ufeff]/.test( siblingText );\r
}\r
- else\r
+ else if ( sibling.type == CKEDITOR.NODE_ELEMENT )\r
{\r
// If this is a visible element.\r
// We need to check for the bookmark attribute because IE insists on\r
sibling = null;\r
else\r
{\r
- allChildren = sibling.$.all || sibling.$.getElementsByTagName( '*' );\r
+ allChildren = sibling.$.getElementsByTagName( '*' );\r
for ( i = 0 ; child = allChildren[ i++ ] ; )\r
{\r
if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )\r
sibling = null;\r
}\r
}\r
+ else\r
+ isWhiteSpace = 1;\r
\r
if ( isWhiteSpace )\r
{\r
CKEDITOR.POSITION_AFTER_START :\r
CKEDITOR.POSITION_AFTER_END );\r
\r
+ // Avoid enlarging the range further when end boundary spans right after the BR. (#7490)\r
+ if ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS )\r
+ {\r
+ var theRange = this.clone();\r
+ walker = new CKEDITOR.dom.walker( theRange );\r
+\r
+ var whitespaces = CKEDITOR.dom.walker.whitespaces(),\r
+ bookmark = CKEDITOR.dom.walker.bookmark();\r
+\r
+ walker.evaluator = function( node ) { return !whitespaces( node ) && !bookmark( node ); };\r
+ var previous = walker.previous();\r
+ if ( previous && previous.type == CKEDITOR.NODE_ELEMENT && previous.is( 'br' ) )\r
+ return;\r
+ }\r
+\r
+\r
// Enlarging the end boundary.\r
walkerRange = this.clone();\r
walkerRange.collapse();\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
+ walker.evaluator = elementBoundaryEval( checkStart );\r
\r
return walker[ checkStart ? 'checkBackward' : 'checkForward' ]();\r
},\r
var startContainer = this.startContainer,\r
startOffset = this.startOffset;\r
\r
- // If the starting node is a text node, and non-empty before the offset,\r
- // then we're surely not at the start of block.\r
- if ( startOffset && startContainer.type == CKEDITOR.NODE_TEXT )\r
+ // [IE] Special handling for range start in text with a leading NBSP,\r
+ // we it to be isolated, for bogus check.\r
+ if ( CKEDITOR.env.ie && startOffset && startContainer.type == CKEDITOR.NODE_TEXT )\r
{\r
var textBefore = CKEDITOR.tools.ltrim( startContainer.substring( 0, startOffset ) );\r
- if ( textBefore.length )\r
- return false;\r
+ if ( nbspRegExp.test( textBefore ) )\r
+ this.trim( 0, 1 );\r
}\r
\r
- // Antecipate the trim() call here, so the walker will not make\r
- // changes to the DOM, which would not get reflected into this\r
- // range otherwise.\r
- this.trim();\r
-\r
// We need to grab the block element holding the start boundary, so\r
// let's use an element path for it.\r
var path = new CKEDITOR.dom.elementPath( this.startContainer );\r
walkerRange.setStartAt( path.block || path.blockLimit, CKEDITOR.POSITION_AFTER_START );\r
\r
var walker = new CKEDITOR.dom.walker( walkerRange );\r
- walker.evaluator = getCheckStartEndBlockEvalFunction( true );\r
+ walker.evaluator = getCheckStartEndBlockEvalFunction();\r
\r
return walker.checkBackward();\r
},\r
var endContainer = this.endContainer,\r
endOffset = this.endOffset;\r
\r
- // If the ending node is a text node, and non-empty after the offset,\r
- // then we're surely not at the end of block.\r
- if ( endContainer.type == CKEDITOR.NODE_TEXT )\r
+ // [IE] Special handling for range end in text with a following NBSP,\r
+ // we it to be isolated, for bogus check.\r
+ if ( CKEDITOR.env.ie && endContainer.type == CKEDITOR.NODE_TEXT )\r
{\r
var textAfter = CKEDITOR.tools.rtrim( endContainer.substring( endOffset ) );\r
- if ( textAfter.length )\r
- return false;\r
+ if ( nbspRegExp.test( textAfter ) )\r
+ this.trim( 1, 0 );\r
}\r
\r
- // Antecipate the trim() call here, so the walker will not make\r
- // changes to the DOM, which would not get reflected into this\r
- // range otherwise.\r
- this.trim();\r
-\r
// We need to grab the block element holding the start boundary, so\r
// let's use an element path for it.\r
var path = new CKEDITOR.dom.elementPath( this.endContainer );\r
walkerRange.setEndAt( path.block || path.blockLimit, CKEDITOR.POSITION_BEFORE_END );\r
\r
var walker = new CKEDITOR.dom.walker( walkerRange );\r
- walker.evaluator = getCheckStartEndBlockEvalFunction( false );\r
+ walker.evaluator = getCheckStartEndBlockEvalFunction();\r
\r
return walker.checkForward();\r
},\r
\r
/**\r
- * Check if elements at which the range boundaries anchor are read-only,\r
- * with respect to "contenteditable" attribute.\r
+ * Traverse with {@link CKEDITOR.dom.walker} to retrieve the previous element before the range start.\r
+ * @param {Function} evaluator Function used as the walker's evaluator.\r
+ * @param {Function} [guard] Function used as the walker's guard.\r
+ * @param {CKEDITOR.dom.element} [boundary] A range ancestor element in which the traversal is limited,\r
+ * default to the root editable if not defined.\r
+ *\r
+ * @return {CKEDITOR.dom.element|null} The returned node from the traversal.\r
+ */\r
+ getPreviousNode : function( evaluator, guard, boundary ) {\r
+\r
+ var walkerRange = this.clone();\r
+ walkerRange.collapse( 1 );\r
+ walkerRange.setStartAt( boundary || this.document.getBody(), CKEDITOR.POSITION_AFTER_START );\r
+\r
+ var walker = new CKEDITOR.dom.walker( walkerRange );\r
+ walker.evaluator = evaluator;\r
+ walker.guard = guard;\r
+ return walker.previous();\r
+ },\r
+\r
+ /**\r
+ * Traverse with {@link CKEDITOR.dom.walker} to retrieve the next element before the range start.\r
+ * @param {Function} evaluator Function used as the walker's evaluator.\r
+ * @param {Function} [guard] Function used as the walker's guard.\r
+ * @param {CKEDITOR.dom.element} [boundary] A range ancestor element in which the traversal is limited,\r
+ * default to the root editable if not defined.\r
+ *\r
+ * @return {CKEDITOR.dom.element|null} The returned node from the traversal.\r
*/\r
+ getNextNode: function( evaluator, guard, boundary )\r
+ {\r
+ var walkerRange = this.clone();\r
+ walkerRange.collapse();\r
+ walkerRange.setEndAt( boundary || this.document.getBody(), CKEDITOR.POSITION_BEFORE_END );\r
+\r
+ var walker = new CKEDITOR.dom.walker( walkerRange );\r
+ walker.evaluator = evaluator;\r
+ walker.guard = guard;\r
+ return walker.next();\r
+ },\r
+\r
checkReadOnly : ( function()\r
{\r
function checkNodesEditable( node, anotherEnd )\r
return 0;\r
}\r
// Range enclosed entirely in an editable element.\r
- else if ( node.is( 'body' )\r
+ else if ( node.is( 'html' )\r
|| node.getAttribute( 'contentEditable' ) == 'true'\r
&& ( node.contains( anotherEnd ) || node.equals( anotherEnd ) ) )\r
{\r
*/\r
moveToElementEditablePosition : function( el, isMoveToEnd )\r
{\r
- var isEditable;\r
+ function nextDFS( node, childOnly )\r
+ {\r
+ var next;\r
\r
- // Empty elements are rejected.\r
- if ( CKEDITOR.dtd.$empty[ el.getName() ] )\r
- return false;\r
+ if ( node.type == CKEDITOR.NODE_ELEMENT && node.isEditable( false ) )\r
+ next = node[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );\r
+\r
+ if ( !childOnly && !next )\r
+ next = node[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );\r
+\r
+ return next;\r
+ }\r
\r
- while ( el && el.type == CKEDITOR.NODE_ELEMENT )\r
+ // Handle non-editable element e.g. HR.\r
+ if ( el.type == CKEDITOR.NODE_ELEMENT && !el.isEditable( false ) )\r
{\r
- isEditable = el.isEditable();\r
-\r
- // If an editable element is found, move inside it.\r
- if ( isEditable )\r
- this.moveToPosition( el, isMoveToEnd ?\r
- CKEDITOR.POSITION_BEFORE_END :\r
- CKEDITOR.POSITION_AFTER_START );\r
- // Stop immediately if we've found a non editable inline element (e.g <img>).\r
- else if ( CKEDITOR.dtd.$inline[ el.getName() ] )\r
- {\r
- this.moveToPosition( el, isMoveToEnd ?\r
- CKEDITOR.POSITION_AFTER_END :\r
- CKEDITOR.POSITION_BEFORE_START );\r
- return true;\r
- }\r
+ this.moveToPosition( el, isMoveToEnd ?\r
+ CKEDITOR.POSITION_AFTER_END :\r
+ CKEDITOR.POSITION_BEFORE_START );\r
+ return true;\r
+ }\r
\r
- // Non-editable non-inline elements are to be bypassed, getting the next one.\r
- if ( CKEDITOR.dtd.$empty[ el.getName() ] )\r
- el = el[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );\r
- else\r
- el = el[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );\r
+ var found = 0;\r
\r
+ while ( el )\r
+ {\r
// Stop immediately if we've found a text node.\r
- if ( el && el.type == CKEDITOR.NODE_TEXT )\r
+ if ( el.type == CKEDITOR.NODE_TEXT )\r
{\r
- this.moveToPosition( el, isMoveToEnd ?\r
+ // Put cursor before block filler.\r
+ if ( isMoveToEnd && this.checkEndOfBlock() && nbspRegExp.test( el.getText() ) )\r
+ this.moveToPosition( el, CKEDITOR.POSITION_BEFORE_START );\r
+ else\r
+ this.moveToPosition( el, isMoveToEnd ?\r
CKEDITOR.POSITION_AFTER_END :\r
CKEDITOR.POSITION_BEFORE_START );\r
- return true;\r
+ found = 1;\r
+ break;\r
}\r
+\r
+ // If an editable element is found, move inside it, but not stop the searching.\r
+ if ( el.type == CKEDITOR.NODE_ELEMENT )\r
+ {\r
+ if ( el.isEditable() )\r
+ {\r
+ this.moveToPosition( el, isMoveToEnd ?\r
+ CKEDITOR.POSITION_BEFORE_END :\r
+ CKEDITOR.POSITION_AFTER_START );\r
+ found = 1;\r
+ }\r
+ // Put cursor before padding block br.\r
+ else if ( isMoveToEnd && el.is( 'br' ) && this.checkEndOfBlock() )\r
+ this.moveToPosition( el, CKEDITOR.POSITION_BEFORE_START );\r
+ }\r
+\r
+ el = nextDFS( el, found );\r
}\r
\r
- return isEditable;\r
+ return !!found;\r
},\r
\r
/**\r