Mimimum syndical pour en faire un produit zope / cmf.
[ckeditor.git] / skins / ckeditor / _source / plugins / domiterator / plugin.js
diff --git a/skins/ckeditor/_source/plugins/domiterator/plugin.js b/skins/ckeditor/_source/plugins/domiterator/plugin.js
new file mode 100644 (file)
index 0000000..5c092bb
--- /dev/null
@@ -0,0 +1,361 @@
+/*\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @file DOM iterator, which iterates over list items, lines and paragraphs.\r
+ */\r
+\r
+CKEDITOR.plugins.add( 'domiterator' );\r
+\r
+(function()\r
+{\r
+       /**\r
+        * @name CKEDITOR.dom.iterator\r
+        */\r
+       function iterator( range )\r
+       {\r
+               if ( arguments.length < 1 )\r
+                       return;\r
+\r
+               this.range = range;\r
+               this.forceBrBreak = 0;\r
+\r
+               // Whether include <br>s into the enlarged range.(#3730).\r
+               this.enlargeBr = 1;\r
+               this.enforceRealBlocks = 0;\r
+\r
+               this._ || ( this._ = {} );\r
+       }\r
+\r
+       var beginWhitespaceRegex = /^[\r\n\t ]+$/,\r
+               // Ignore bookmark nodes.(#3783)\r
+               bookmarkGuard = CKEDITOR.dom.walker.bookmark( false, true ),\r
+               whitespacesGuard = CKEDITOR.dom.walker.whitespaces( true ),\r
+               skipGuard = function( node ) { return bookmarkGuard( node ) && whitespacesGuard( node ); };\r
+\r
+       // Get a reference for the next element, bookmark nodes are skipped.\r
+       function getNextSourceNode( node, startFromSibling, lastNode )\r
+       {\r
+               var next = node.getNextSourceNode( startFromSibling, null, lastNode );\r
+               while ( !bookmarkGuard( next ) )\r
+                       next = next.getNextSourceNode( startFromSibling, null, lastNode );\r
+               return next;\r
+       }\r
+\r
+       iterator.prototype = {\r
+               getNextParagraph : function( blockTag )\r
+               {\r
+                       // The block element to be returned.\r
+                       var block;\r
+\r
+                       // The range object used to identify the paragraph contents.\r
+                       var range;\r
+\r
+                       // Indicats that the current element in the loop is the last one.\r
+                       var isLast;\r
+\r
+                       // Indicate at least one of the range boundaries is inside a preformat block.\r
+                       var touchPre;\r
+\r
+                       // Instructs to cleanup remaining BRs.\r
+                       var removePreviousBr, removeLastBr;\r
+\r
+                       // This is the first iteration. Let's initialize it.\r
+                       if ( !this._.lastNode )\r
+                       {\r
+                               range = this.range.clone();\r
+\r
+                               // Shrink the range to exclude harmful "noises" (#4087, #4450, #5435).\r
+                               range.shrink( CKEDITOR.NODE_ELEMENT, true );\r
+\r
+                               touchPre = range.endContainer.hasAscendant( 'pre', true )\r
+                                       || range.startContainer.hasAscendant( 'pre', true );\r
+\r
+                               range.enlarge( this.forceBrBreak && !touchPre || !this.enlargeBr ?\r
+                                                          CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS );\r
+\r
+                               var walker = new CKEDITOR.dom.walker( range ),\r
+                                       ignoreBookmarkTextEvaluator = CKEDITOR.dom.walker.bookmark( true, true );\r
+                               // Avoid anchor inside bookmark inner text.\r
+                               walker.evaluator = ignoreBookmarkTextEvaluator;\r
+                               this._.nextNode = walker.next();\r
+                               // TODO: It's better to have walker.reset() used here.\r
+                               walker = new CKEDITOR.dom.walker( range );\r
+                               walker.evaluator = ignoreBookmarkTextEvaluator;\r
+                               var lastNode = walker.previous();\r
+                               this._.lastNode = lastNode.getNextSourceNode( true );\r
+\r
+                               // We may have an empty text node at the end of block due to [3770].\r
+                               // If that node is the lastNode, it would cause our logic to leak to the\r
+                               // next block.(#3887)\r
+                               if ( this._.lastNode &&\r
+                                               this._.lastNode.type == CKEDITOR.NODE_TEXT &&\r
+                                               !CKEDITOR.tools.trim( this._.lastNode.getText() ) &&\r
+                                               this._.lastNode.getParent().isBlockBoundary() )\r
+                               {\r
+                                       var testRange = new CKEDITOR.dom.range( range.document );\r
+                                       testRange.moveToPosition( this._.lastNode, CKEDITOR.POSITION_AFTER_END );\r
+                                       if ( testRange.checkEndOfBlock() )\r
+                                       {\r
+                                               var path = new CKEDITOR.dom.elementPath( testRange.endContainer );\r
+                                               var lastBlock = path.block || path.blockLimit;\r
+                                               this._.lastNode = lastBlock.getNextSourceNode( true );\r
+                                       }\r
+                               }\r
+\r
+                               // Probably the document end is reached, we need a marker node.\r
+                               if ( !this._.lastNode )\r
+                               {\r
+                                       this._.lastNode = this._.docEndMarker = range.document.createText( '' );\r
+                                       this._.lastNode.insertAfter( lastNode );\r
+                               }\r
+\r
+                               // Let's reuse this variable.\r
+                               range = null;\r
+                       }\r
+\r
+                       var currentNode = this._.nextNode;\r
+                       lastNode = this._.lastNode;\r
+\r
+                       this._.nextNode = null;\r
+                       while ( currentNode )\r
+                       {\r
+                               // closeRange indicates that a paragraph boundary has been found,\r
+                               // so the range can be closed.\r
+                               var closeRange = 0,\r
+                                       parentPre = currentNode.hasAscendant( 'pre' );\r
+\r
+                               // includeNode indicates that the current node is good to be part\r
+                               // of the range. By default, any non-element node is ok for it.\r
+                               var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ),\r
+                                       continueFromSibling = 0;\r
+\r
+                               // If it is an element node, let's check if it can be part of the\r
+                               // range.\r
+                               if ( !includeNode )\r
+                               {\r
+                                       var nodeName = currentNode.getName();\r
+\r
+                                       if ( currentNode.isBlockBoundary( this.forceBrBreak &&\r
+                                                       !parentPre && { br : 1 } ) )\r
+                                       {\r
+                                               // <br> boundaries must be part of the range. It will\r
+                                               // happen only if ForceBrBreak.\r
+                                               if ( nodeName == 'br' )\r
+                                                       includeNode = 1;\r
+                                               else if ( !range && !currentNode.getChildCount() && nodeName != 'hr' )\r
+                                               {\r
+                                                       // If we have found an empty block, and haven't started\r
+                                                       // the range yet, it means we must return this block.\r
+                                                       block = currentNode;\r
+                                                       isLast = currentNode.equals( lastNode );\r
+                                                       break;\r
+                                               }\r
+\r
+                                               // The range must finish right before the boundary,\r
+                                               // including possibly skipped empty spaces. (#1603)\r
+                                               if ( range )\r
+                                               {\r
+                                                       range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START );\r
+\r
+                                                       // The found boundary must be set as the next one at this\r
+                                                       // point. (#1717)\r
+                                                       if ( nodeName != 'br' )\r
+                                                               this._.nextNode = currentNode;\r
+                                               }\r
+\r
+                                               closeRange = 1;\r
+                                       }\r
+                                       else\r
+                                       {\r
+                                               // If we have child nodes, let's check them.\r
+                                               if ( currentNode.getFirst() )\r
+                                               {\r
+                                                       // If we don't have a range yet, let's start it.\r
+                                                       if ( !range )\r
+                                                       {\r
+                                                               range = new CKEDITOR.dom.range( this.range.document );\r
+                                                               range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );\r
+                                                       }\r
+\r
+                                                       currentNode = currentNode.getFirst();\r
+                                                       continue;\r
+                                               }\r
+                                               includeNode = 1;\r
+                                       }\r
+                               }\r
+                               else if ( currentNode.type == CKEDITOR.NODE_TEXT )\r
+                               {\r
+                                       // Ignore normal whitespaces (i.e. not including &nbsp; or\r
+                                       // other unicode whitespaces) before/after a block node.\r
+                                       if ( beginWhitespaceRegex.test( currentNode.getText() ) )\r
+                                               includeNode = 0;\r
+                               }\r
+\r
+                               // The current node is good to be part of the range and we are\r
+                               // starting a new range, initialize it first.\r
+                               if ( includeNode && !range )\r
+                               {\r
+                                       range = new CKEDITOR.dom.range( this.range.document );\r
+                                       range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START );\r
+                               }\r
+\r
+                               // The last node has been found.\r
+                               isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) );\r
+\r
+                               // If we are in an element boundary, let's check if it is time\r
+                               // to close the range, otherwise we include the parent within it.\r
+                               if ( range && !closeRange )\r
+                               {\r
+                                       while ( !currentNode.getNext( skipGuard ) && !isLast )\r
+                                       {\r
+                                               var parentNode = currentNode.getParent();\r
+\r
+                                               if ( parentNode.isBlockBoundary( this.forceBrBreak\r
+                                                               && !parentPre && { br : 1 } ) )\r
+                                               {\r
+                                                       closeRange = 1;\r
+                                                       includeNode = 0;\r
+                                                       isLast = isLast || ( parentNode.equals( lastNode) );\r
+                                                       // Make sure range includes bookmarks at the end of the block. (#7359)\r
+                                                       range.setEndAt( parentNode, CKEDITOR.POSITION_BEFORE_END );\r
+                                                       break;\r
+                                               }\r
+\r
+                                               currentNode = parentNode;\r
+                                               includeNode = 1;\r
+                                               isLast = ( currentNode.equals( lastNode ) );\r
+                                               continueFromSibling = 1;\r
+                                       }\r
+                               }\r
+\r
+                               // Now finally include the node.\r
+                               if ( includeNode )\r
+                                       range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END );\r
+\r
+                               currentNode = getNextSourceNode ( currentNode, continueFromSibling, lastNode );\r
+                               isLast = !currentNode;\r
+\r
+                               // We have found a block boundary. Let's close the range and move out of the\r
+                               // loop.\r
+                               if ( isLast || ( closeRange && range ) )\r
+                                               break;\r
+                       }\r
+\r
+                       // Now, based on the processed range, look for (or create) the block to be returned.\r
+                       if ( !block )\r
+                       {\r
+                               // If no range has been found, this is the end.\r
+                               if ( !range )\r
+                               {\r
+                                       this._.docEndMarker && this._.docEndMarker.remove();\r
+                                       this._.nextNode = null;\r
+                                       return null;\r
+                               }\r
+\r
+                               var startPath = new CKEDITOR.dom.elementPath( range.startContainer );\r
+                               var startBlockLimit = startPath.blockLimit,\r
+                                       checkLimits = { div : 1, th : 1, td : 1 };\r
+                               block = startPath.block;\r
+\r
+                               if ( !block\r
+                                               && !this.enforceRealBlocks\r
+                                               && checkLimits[ startBlockLimit.getName() ]\r
+                                               && range.checkStartOfBlock()\r
+                                               && range.checkEndOfBlock() )\r
+                                       block = startBlockLimit;\r
+                               else if ( !block || ( this.enforceRealBlocks && block.getName() == 'li' ) )\r
+                               {\r
+                                       // Create the fixed block.\r
+                                       block = this.range.document.createElement( blockTag || 'p' );\r
+\r
+                                               // Move the contents of the temporary range to the fixed block.\r
+                                               range.extractContents().appendTo( block );\r
+                                               block.trim();\r
+\r
+                                               // Insert the fixed block into the DOM.\r
+                                               range.insertNode( block );\r
+\r
+                                               removePreviousBr = removeLastBr = true;\r
+                                       }\r
+                               else if ( block.getName() != 'li' )\r
+                               {\r
+                                       // If the range doesn't includes the entire contents of the\r
+                                       // block, we must split it, isolating the range in a dedicated\r
+                                       // block.\r
+                                       if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() )\r
+                                       {\r
+                                               // The resulting block will be a clone of the current one.\r
+                                               block = block.clone( false );\r
+\r
+                                               // Extract the range contents, moving it to the new block.\r
+                                               range.extractContents().appendTo( block );\r
+                                               block.trim();\r
+\r
+                                               // Split the block. At this point, the range will be in the\r
+                                               // right position for our intents.\r
+                                               var splitInfo = range.splitBlock();\r
+\r
+                                               removePreviousBr = !splitInfo.wasStartOfBlock;\r
+                                               removeLastBr = !splitInfo.wasEndOfBlock;\r
+\r
+                                               // Insert the new block into the DOM.\r
+                                               range.insertNode( block );\r
+                                       }\r
+                               }\r
+                               else if ( !isLast )\r
+                               {\r
+                                       // LIs are returned as is, with all their children (due to the\r
+                                       // nested lists). But, the next node is the node right after\r
+                                       // the current range, which could be an <li> child (nested\r
+                                       // lists) or the next sibling <li>.\r
+\r
+                                       this._.nextNode = ( block.equals( lastNode ) ? null : getNextSourceNode( range.getBoundaryNodes().endNode, 1, lastNode ) );\r
+                               }\r
+                       }\r
+\r
+                       if ( removePreviousBr )\r
+                       {\r
+                               var previousSibling = block.getPrevious();\r
+                               if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT )\r
+                               {\r
+                                       if ( previousSibling.getName() == 'br' )\r
+                                               previousSibling.remove();\r
+                                       else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' )\r
+                                               previousSibling.getLast().remove();\r
+                               }\r
+                       }\r
+\r
+                       if ( removeLastBr )\r
+                       {\r
+                               var lastChild = block.getLast();\r
+                               if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' )\r
+                               {\r
+                                       // Take care not to remove the block expanding <br> in non-IE browsers.\r
+                                       if ( CKEDITOR.env.ie\r
+                                                || lastChild.getPrevious( bookmarkGuard )\r
+                                                || lastChild.getNext( bookmarkGuard ) )\r
+                                               lastChild.remove();\r
+                               }\r
+                       }\r
+\r
+                       // Get a reference for the next element. This is important because the\r
+                       // above block can be removed or changed, so we can rely on it for the\r
+                       // next interation.\r
+                       if ( !this._.nextNode )\r
+                       {\r
+                               this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null :\r
+                                       getNextSourceNode( block, 1, lastNode );\r
+                       }\r
+\r
+                       return block;\r
+               }\r
+       };\r
+\r
+       CKEDITOR.dom.range.prototype.createIterator = function()\r
+       {\r
+               return new iterator( this );\r
+       };\r
+})();\r