JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / core / dom / range.js
index b43a82d..30a78c4 100644 (file)
@@ -1,19 +1,90 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, 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
@@ -32,7 +103,7 @@ CKEDITOR.dom.range = function( document )
        // This is a shared function used to delete, extract and clone the range\r
        // contents.\r
        // V2\r
-       var execContentsAction = function( range, action, docFrag )\r
+       var execContentsAction = function( range, action, docFrag, mergeThen )\r
        {\r
                range.optimizeBookmark();\r
 \r
@@ -247,7 +318,17 @@ CKEDITOR.dom.range = function( document )
                                if ( removeStartNode && topEnd.$.parentNode == startNode.$.parentNode )\r
                                        endIndex--;\r
 \r
-                               range.setStart( topEnd.getParent(), endIndex );\r
+                               // Merge splitted parents.\r
+                               if ( mergeThen && topStart.type == CKEDITOR.NODE_ELEMENT )\r
+                               {\r
+                                       var span = CKEDITOR.dom.element.createFromHtml( '<span ' +\r
+                                               'data-cke-bookmark="1" style="display:none">&nbsp;</span>', range.document );\r
+                                       span.insertAfter( topStart );\r
+                                       topStart.mergeSiblings( false );\r
+                                       range.moveToBookmark( { startNode : span } );\r
+                               }\r
+                               else\r
+                                       range.setStart( topEnd.getParent(), endIndex );\r
                        }\r
 \r
                        // Collapse it to the start.\r
@@ -278,9 +359,9 @@ CKEDITOR.dom.range = function( document )
                        if ( node.type == CKEDITOR.NODE_TEXT )\r
                        {\r
                                // If there's any visible text, then we're not at the start.\r
-                               if ( CKEDITOR.tools.trim( node.getText() ).length )\r
+                               if ( node.hasAscendant( 'pre' ) || CKEDITOR.tools.trim( node.getText() ).length )\r
                                        return false;\r
-                               }\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
@@ -299,16 +380,23 @@ CKEDITOR.dom.range = function( document )
                };\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().hasAttribute( '_cke_bookmark' );\r
+               return function( node )\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_TEXT ?\r
+                                          !CKEDITOR.tools.trim( node.getText() ) || !!node.getParent().data( 'cke-bookmark' )\r
+                                          : node.getName() in CKEDITOR.dtd.$removeEmpty );\r
+               };\r
        }\r
 \r
        var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),\r
@@ -351,7 +439,10 @@ CKEDITOR.dom.range = function( document )
                        this.collapsed = true;\r
                },\r
 \r
-               // The selection may be lost when cloning (due to the splitText() call).\r
+               /**\r
+                *  The content nodes of the range are cloned and added to a document fragment, which is returned.\r
+                *  <strong> Note: </strong> Text selection may lost after invoking this method. (caused by text node splitting).\r
+                */\r
                cloneContents : function()\r
                {\r
                        var docFrag = new CKEDITOR.dom.documentFragment( this.document );\r
@@ -362,20 +453,29 @@ CKEDITOR.dom.range = function( document )
                        return docFrag;\r
                },\r
 \r
-               deleteContents : function()\r
+               /**\r
+                * Deletes the content nodes of the range permanently from the DOM tree.\r
+                * @param {Boolean} [mergeThen] Merge any splitted elements result in DOM true due to partial selection.\r
+                */\r
+               deleteContents : function( mergeThen )\r
                {\r
                        if ( this.collapsed )\r
                                return;\r
 \r
-                       execContentsAction( this, 0 );\r
+                       execContentsAction( this, 0, null, mergeThen );\r
                },\r
 \r
-               extractContents : function()\r
+               /**\r
+                *  The content nodes of the range are cloned and added to a document fragment,\r
+                * meanwhile they're removed permanently from the DOM tree.\r
+                * @param {Boolean} [mergeThen] Merge any splitted elements result in DOM true due to partial selection.\r
+                */\r
+               extractContents : function( mergeThen )\r
                {\r
                        var docFrag = new CKEDITOR.dom.documentFragment( this.document );\r
 \r
                        if ( !this.collapsed )\r
-                               execContentsAction( this, 1, docFrag );\r
+                               execContentsAction( this, 1, docFrag, mergeThen );\r
 \r
                        return docFrag;\r
                },\r
@@ -403,7 +503,7 @@ CKEDITOR.dom.range = function( document )
                        var collapsed = this.collapsed;\r
 \r
                        startNode = this.document.createElement( 'span' );\r
-                       startNode.setAttribute( '_cke_bookmark', 1 );\r
+                       startNode.data( 'cke-bookmark', 1 );\r
                        startNode.setStyle( 'display', 'none' );\r
 \r
                        // For IE, it must have something inside, otherwise it may be\r
@@ -496,6 +596,10 @@ CKEDITOR.dom.range = function( document )
                                                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
@@ -524,6 +628,10 @@ CKEDITOR.dom.range = function( document )
                                                        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
@@ -713,10 +821,10 @@ CKEDITOR.dom.range = function( document )
                                endNode = this.endContainer;\r
 \r
                        if ( startNode.is && startNode.is( 'span' )\r
-                               && startNode.hasAttribute( '_cke_bookmark' ) )\r
+                               && startNode.data( 'cke-bookmark' ) )\r
                                this.setStartAt( startNode, CKEDITOR.POSITION_BEFORE_START );\r
                        if ( endNode && endNode.is && endNode.is( 'span' )\r
-                               && endNode.hasAttribute( '_cke_bookmark' ) )\r
+                               && endNode.data( 'cke-bookmark' ) )\r
                                this.setEndAt( endNode,  CKEDITOR.POSITION_AFTER_END );\r
                },\r
 \r
@@ -801,7 +909,12 @@ CKEDITOR.dom.range = function( document )
                        }\r
                },\r
 \r
-               enlarge : function( unit )\r
+               /**\r
+                * Expands the range so that partial units are completely contained.\r
+                * @param unit {Number} The unit type to expand with.\r
+                * @param {Boolean} [excludeBrs=false] Whether include line-breaks when expanding.\r
+                */\r
+               enlarge : function( unit, excludeBrs )\r
                {\r
                        switch ( unit )\r
                        {\r
@@ -908,7 +1021,12 @@ CKEDITOR.dom.range = function( document )
                                                        // 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
@@ -922,7 +1040,8 @@ CKEDITOR.dom.range = function( document )
                                                                // 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( '_cke_bookmark' ) )\r
+                                                               // Line-breaks (br) are rendered with zero width, which we don't want to include. (#7041)\r
+                                                               if ( ( sibling.$.offsetWidth > 0 || excludeBrs && sibling.is( 'br' ) ) && !sibling.data( 'cke-bookmark' ) )\r
                                                                {\r
                                                                        // We'll accept it only if we need\r
                                                                        // whitespace, and this is an inline\r
@@ -937,7 +1056,7 @@ CKEDITOR.dom.range = function( document )
                                                                                        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
@@ -1076,12 +1195,13 @@ CKEDITOR.dom.range = function( document )
 \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
                                                                // rendering the display:none nodes we use for bookmarks. (#3363)\r
-                                                               if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_cke_bookmark' ) )\r
+                                                               // Line-breaks (br) are rendered with zero width, which we don't want to include. (#7041)\r
+                                                               if ( ( sibling.$.offsetWidth > 0 || excludeBrs && sibling.is( 'br' ) ) && !sibling.data( 'cke-bookmark' ) )\r
                                                                {\r
                                                                        // We'll accept it only if we need\r
                                                                        // whitespace, and this is an inline\r
@@ -1096,7 +1216,7 @@ CKEDITOR.dom.range = function( document )
                                                                                        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
@@ -1114,6 +1234,8 @@ CKEDITOR.dom.range = function( document )
                                                                                sibling = null;\r
                                                                }\r
                                                        }\r
+                                                       else\r
+                                                               isWhiteSpace = 1;\r
 \r
                                                        if ( isWhiteSpace )\r
                                                        {\r
@@ -1212,6 +1334,22 @@ CKEDITOR.dom.range = function( document )
                                                                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
@@ -1405,7 +1543,7 @@ CKEDITOR.dom.range = function( document )
                        // 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
+                               startOffset = startNode.getIndex(), startNode = startNode.getParent();\r
 \r
                        this.startContainer     = startNode;\r
                        this.startOffset        = startOffset;\r
@@ -1669,7 +1807,7 @@ CKEDITOR.dom.range = function( document )
                        // 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
@@ -1745,6 +1883,47 @@ CKEDITOR.dom.range = function( document )
                },\r
 \r
                /**\r
+                * Check if elements at which the range boundaries anchor are read-only,\r
+                * with respect to "contenteditable" attribute.\r
+                */\r
+               checkReadOnly : ( function()\r
+               {\r
+                       function checkNodesEditable( node, anotherEnd )\r
+                       {\r
+                               while( node )\r
+                               {\r
+                                       if ( node.type == CKEDITOR.NODE_ELEMENT )\r
+                                       {\r
+                                               if ( node.getAttribute( 'contentEditable' ) == 'false'\r
+                                                       && !node.data( 'cke-editable' ) )\r
+                                               {\r
+                                                       return 0;\r
+                                               }\r
+                                               // Range enclosed entirely in an editable element.\r
+                                               else if ( node.is( 'html' )\r
+                                                       || node.getAttribute( 'contentEditable' ) == 'true'\r
+                                                       && ( node.contains( anotherEnd ) || node.equals( anotherEnd ) ) )\r
+                                               {\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                                       node = node.getParent();\r
+                               }\r
+\r
+                               return 1;\r
+                       }\r
+\r
+                       return function()\r
+                       {\r
+                               var startNode = this.startContainer,\r
+                                       endNode = this.endContainer;\r
+\r
+                               // Check if elements path at both boundaries are editable.\r
+                               return !( checkNodesEditable( startNode, endNode ) && checkNodesEditable( endNode, startNode ) );\r
+                       };\r
+               })(),\r
+\r
+               /**\r
                 * Moves the range boundaries to the first/end editing point inside an\r
                 * element. For example, in an element tree like\r
                 * "&lt;p&gt;&lt;b&gt;&lt;i&gt;&lt;/i&gt;&lt;/b&gt; Text&lt;/p&gt;", the start editing point is\r
@@ -1755,47 +1934,53 @@ CKEDITOR.dom.range = function( document )
                 */\r
                moveToElementEditablePosition : function( el, isMoveToEnd )\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
+                       function nextDFS( node, childOnly )\r
                        {\r
-                               isEditable = el.isEditable();\r
+                               var next;\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
+                               if ( node.type == CKEDITOR.NODE_ELEMENT\r
+                                               && node.isEditable( false )\r
+                                               && !CKEDITOR.dtd.$nonEditable[ node.getName() ] )\r
                                {\r
-                                       this.moveToPosition( el, isMoveToEnd ?\r
-                                                                CKEDITOR.POSITION_AFTER_END :\r
-                                                                CKEDITOR.POSITION_BEFORE_START );\r
-                                       return true;\r
+                                       next = node[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );\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
+                               if ( !childOnly && !next )\r
+                                       next = node[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );\r
+\r
+                               return next;\r
+                       }\r
+\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
                                                                 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
+                               }\r
+\r
+                               el = nextDFS( el, found );\r
                        }\r
 \r
-                       return isEditable;\r
+                       return !!found;\r
                },\r
 \r
                /**\r