| 1 | /* |
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. |
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * @file DOM iterator, which iterates over list items, lines and paragraphs. |
| 8 | */ |
| 9 | |
| 10 | CKEDITOR.plugins.add( 'domiterator' ); |
| 11 | |
| 12 | (function() |
| 13 | { |
| 14 | // Functions ported over from v2. |
| 15 | function getTouchedStartNode( range ) |
| 16 | { |
| 17 | var container = range.startContainer; |
| 18 | |
| 19 | if ( range.collapsed || container.type != CKEDITOR.NODE_ELEMENT ) |
| 20 | return container; |
| 21 | |
| 22 | return container.getChildCount() > range.startOffset ? container.getChild( range.startOffset ) : container; |
| 23 | } |
| 24 | |
| 25 | function getTouchedEndNode( range ) |
| 26 | { |
| 27 | var container = range.endContainer; |
| 28 | |
| 29 | if ( range.collapsed || container.type != CKEDITOR.NODE_ELEMENT ) |
| 30 | return container; |
| 31 | |
| 32 | return container.getChildCount() > range.endOffset ? container.getChild( range.endOffset ) : container; |
| 33 | } |
| 34 | |
| 35 | function getNextSourceNode( currentNode, startFromSibling, nodeType, stopSearchNode ) |
| 36 | { |
| 37 | if ( !currentNode ) |
| 38 | return null; |
| 39 | |
| 40 | var node; |
| 41 | |
| 42 | if ( !startFromSibling && currentNode.getFirst() ) |
| 43 | node = currentNode.getFirst(); |
| 44 | else |
| 45 | { |
| 46 | if ( stopSearchNode && currentNode.equals( stopSearchNode ) ) |
| 47 | return null; |
| 48 | |
| 49 | node = currentNode.getNext(); |
| 50 | |
| 51 | if ( !node && ( !stopSearchNode || !stopSearchNode.equals( currentNode.parentNode ) ) ) |
| 52 | return getNextSourceNode( currentNode.getParent(), true, nodeType, stopSearchNode ); |
| 53 | } |
| 54 | |
| 55 | if ( nodeType && node && node.type != nodeType ) |
| 56 | return getNextSourceNode( node, false, nodeType, stopSearchNode ); |
| 57 | |
| 58 | return node; |
| 59 | } |
| 60 | |
| 61 | var iterator = function( range ) |
| 62 | { |
| 63 | if ( arguments.length < 1 ) |
| 64 | return; |
| 65 | |
| 66 | this.range = range; |
| 67 | this.forceBrBreak = false; |
| 68 | this.enforceRealBlocks = false; |
| 69 | |
| 70 | this._ || ( this._ = {} ); |
| 71 | }, |
| 72 | beginWhitespaceRegex = /^[\r\n\t ]+$/; |
| 73 | |
| 74 | |
| 75 | iterator.prototype = { |
| 76 | getNextParagraph : function( blockTag ) |
| 77 | { |
| 78 | // The block element to be returned. |
| 79 | var block; |
| 80 | |
| 81 | // The range object used to identify the paragraph contents. |
| 82 | var range; |
| 83 | |
| 84 | // Indicats that the current element in the loop is the last one. |
| 85 | var isLast; |
| 86 | |
| 87 | // Instructs to cleanup remaining BRs. |
| 88 | var removePreviousBr, removeLastBr; |
| 89 | |
| 90 | // Things we consider should be separated into blocks. |
| 91 | var displayMatches = { block : 1, 'list-item' : 1, table : 1, 'table-row-group' : 1, 'table-header-group' : 1, |
| 92 | 'table-footer-group' : 1, 'table-row' : 1, 'table-column-group' : 1, 'table-column' : 1, 'table-cell' : 1, |
| 93 | 'table-caption' : 1 }; |
| 94 | |
| 95 | // This is the first iteration. Let's initialize it. |
| 96 | if ( !this._.lastNode ) |
| 97 | { |
| 98 | range = this.range.clone(); |
| 99 | range.enlarge( this.forceBrBreak ? CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS : CKEDITOR.ENLARGE_BLOCK_CONTENTS ); |
| 100 | |
| 101 | this._.nextNode = getTouchedStartNode( range ); |
| 102 | this._.lastNode = getTouchedEndNode( range ); |
| 103 | |
| 104 | // Let's reuse this variable. |
| 105 | range = null; |
| 106 | } |
| 107 | |
| 108 | var currentNode = this._.nextNode, |
| 109 | lastNode = this._.lastNode; |
| 110 | |
| 111 | this._.nextNode = null; |
| 112 | |
| 113 | while ( currentNode ) |
| 114 | { |
| 115 | // closeRange indicates that a paragraph boundary has been found, |
| 116 | // so the range can be closed. |
| 117 | var closeRange = false; |
| 118 | |
| 119 | // includeNode indicates that the current node is good to be part |
| 120 | // of the range. By default, any non-element node is ok for it. |
| 121 | var includeNode = ( currentNode.type != CKEDITOR.NODE_ELEMENT ), |
| 122 | continueFromSibling = false; |
| 123 | |
| 124 | // If it is an element node, let's check if it can be part of the |
| 125 | // range. |
| 126 | if ( !includeNode ) |
| 127 | { |
| 128 | var nodeName = currentNode.getName(), |
| 129 | nodeDisplay = currentNode.getComputedStyle( 'display' ); |
| 130 | |
| 131 | if ( displayMatches[ nodeDisplay ] || ( this.forceBrBreak && nodeName == 'br' ) ) |
| 132 | { |
| 133 | // <br> boundaries must be part of the range. It will |
| 134 | // happen only if ForceBrBreak. |
| 135 | if ( nodeName == 'br' ) |
| 136 | includeNode = true; |
| 137 | else if ( !range && currentNode.getChildCount() == 0 && nodeName != 'hr' ) |
| 138 | { |
| 139 | // If we have found an empty block, and haven't started |
| 140 | // the range yet, it means we must return this block. |
| 141 | block = currentNode; |
| 142 | isLast = currentNode.equals( lastNode ); |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | // The range must finish right before the boundary, |
| 147 | // including possibly skipped empty spaces. (#1603) |
| 148 | if ( range ) |
| 149 | { |
| 150 | range.setEndAt( currentNode, CKEDITOR.POSITION_BEFORE_START ); |
| 151 | |
| 152 | // The found boundary must be set as the next one at this |
| 153 | // point. (#1717) |
| 154 | if ( nodeName != 'br' ) |
| 155 | this._.nextNode = getNextSourceNode( currentNode, true, null, lastNode ) || currentNode; |
| 156 | } |
| 157 | |
| 158 | closeRange = true; |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | // If we have child nodes, let's check them. |
| 163 | if ( currentNode.getFirst() ) |
| 164 | { |
| 165 | // If we don't have a range yet, let's start it. |
| 166 | if ( !range ) |
| 167 | { |
| 168 | range = new CKEDITOR.dom.range( this.range.document ); |
| 169 | range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START ); |
| 170 | } |
| 171 | |
| 172 | currentNode = currentNode.getFirst(); |
| 173 | continue; |
| 174 | } |
| 175 | includeNode = true; |
| 176 | } |
| 177 | } |
| 178 | else if ( currentNode.type == CKEDITOR.NODE_TEXT ) |
| 179 | { |
| 180 | // Ignore normal whitespaces (i.e. not including or |
| 181 | // other unicode whitespaces) before/after a block node. |
| 182 | if ( beginWhitespaceRegex.test( currentNode.getText() ) ) |
| 183 | includeNode = false; |
| 184 | } |
| 185 | |
| 186 | // The current node is good to be part of the range and we are |
| 187 | // starting a new range, initialize it first. |
| 188 | if ( includeNode && !range ) |
| 189 | { |
| 190 | range = new CKEDITOR.dom.range( this.range.document ); |
| 191 | range.setStartAt( currentNode, CKEDITOR.POSITION_BEFORE_START ); |
| 192 | } |
| 193 | |
| 194 | // The last node has been found. |
| 195 | isLast = ( ( !closeRange || includeNode ) && currentNode.equals( lastNode ) ); |
| 196 | |
| 197 | // If we are in an element boundary, let's check if it is time |
| 198 | // to close the range, otherwise we include the parent within it. |
| 199 | if ( range && !closeRange ) |
| 200 | { |
| 201 | while ( !currentNode.getNext() && !isLast ) |
| 202 | { |
| 203 | var parentNode = currentNode.getParent(); |
| 204 | |
| 205 | if ( displayMatches[ parentNode.getComputedStyle( 'display' ) ] || ( this.forceBrBreak && parentNode.getName() == 'br' ) ) |
| 206 | { |
| 207 | closeRange = true; |
| 208 | isLast = isLast || ( parentNode.equals( lastNode) ); |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | currentNode = parentNode; |
| 213 | includeNode = true; |
| 214 | isLast = ( currentNode.equals( lastNode ) ); |
| 215 | continueFromSibling = true; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Now finally include the node. |
| 220 | if ( includeNode ) |
| 221 | range.setEndAt( currentNode, CKEDITOR.POSITION_AFTER_END ); |
| 222 | |
| 223 | // We have found a block boundary. Let's close the range and move out of the |
| 224 | // loop. |
| 225 | if ( ( closeRange || isLast ) && range ) |
| 226 | { |
| 227 | var boundaryNodes = range.getBoundaryNodes(), |
| 228 | startPath = new CKEDITOR.dom.elementPath( range.startContainer ), |
| 229 | endPath = new CKEDITOR.dom.elementPath( range.endContainer ); |
| 230 | if ( boundaryNodes.startNode.equals( boundaryNodes.endNode ) |
| 231 | && boundaryNodes.startNode.getParent().equals( startPath.blockLimit ) |
| 232 | && boundaryNodes.startNode.type == CKEDITOR.NODE_ELEMENT && boundaryNodes.startNode.getAttribute( '_fck_bookmark' ) ) |
| 233 | range = null; |
| 234 | else |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | if ( isLast ) |
| 239 | break; |
| 240 | |
| 241 | currentNode = getNextSourceNode( currentNode, continueFromSibling, null, lastNode ); |
| 242 | } |
| 243 | |
| 244 | // Now, based on the processed range, look for (or create) the block to be returned. |
| 245 | if ( !block ) |
| 246 | { |
| 247 | // If no range has been found, this is the end. |
| 248 | if ( !range ) |
| 249 | { |
| 250 | this._.nextNode = null; |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | var startPath = new CKEDITOR.dom.elementPath( range.startContainer ), |
| 255 | startBlockLimit = startPath.blockLimit, |
| 256 | checkLimits = { div : 1, th : 1, td : 1 }; |
| 257 | block = startPath.block; |
| 258 | |
| 259 | if ( !block |
| 260 | && !this.enforceRealBlocks |
| 261 | && checkLimits[ startBlockLimit.getName() ] |
| 262 | && range.checkStartOfBlock() |
| 263 | && range.checkEndOfBlock() ) |
| 264 | block = startBlockLimit; |
| 265 | else if ( !block || ( this.enforceRealBlocks && block.getName() == 'li' ) ) |
| 266 | { |
| 267 | // Create the fixed block. |
| 268 | block = this.range.document.createElement( blockTag || 'p' ); |
| 269 | |
| 270 | // Move the contents of the temporary range to the fixed block. |
| 271 | range.extractContents().appendTo( block ); |
| 272 | block.trim(); |
| 273 | |
| 274 | // Insert the fixed block into the DOM. |
| 275 | range.insertNode( block ); |
| 276 | |
| 277 | removePreviousBr = removeLastBr = true; |
| 278 | } |
| 279 | else if ( block.getName() != 'li' ) |
| 280 | { |
| 281 | // If the range doesn't includes the entire contents of the |
| 282 | // block, we must split it, isolating the range in a dedicated |
| 283 | // block. |
| 284 | if ( !range.checkStartOfBlock() || !range.checkEndOfBlock() ) |
| 285 | { |
| 286 | // The resulting block will be a clone of the current one. |
| 287 | block = block.clone( false ); |
| 288 | |
| 289 | // Extract the range contents, moving it to the new block. |
| 290 | range.extractContents().appendTo( block ); |
| 291 | block.trim(); |
| 292 | |
| 293 | // Split the block. At this point, the range will be in the |
| 294 | // right position for our intents. |
| 295 | var splitInfo = range.splitBlock(); |
| 296 | |
| 297 | removePreviousBr = !splitInfo.wasStartOfBlock; |
| 298 | removeLastBr = !splitInfo.wasEndOfBlock; |
| 299 | |
| 300 | // Insert the new block into the DOM. |
| 301 | range.insertNode( block ); |
| 302 | } |
| 303 | } |
| 304 | else if ( !isLast ) |
| 305 | { |
| 306 | // LIs are returned as is, with all their children (due to the |
| 307 | // nested lists). But, the next node is the node right after |
| 308 | // the current range, which could be an <li> child (nested |
| 309 | // lists) or the next sibling <li>. |
| 310 | |
| 311 | this._.nextNode = ( block.equals( lastNode ) ? null : |
| 312 | getNextSourceNode( range.getBoundaryNodes().endNode, true, null, lastNode ) ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if ( removePreviousBr ) |
| 317 | { |
| 318 | var previousSibling = block.getPrevious(); |
| 319 | if ( previousSibling && previousSibling.type == CKEDITOR.NODE_ELEMENT ) |
| 320 | { |
| 321 | if ( previousSibling.getName() == 'br' ) |
| 322 | previousSibling.remove(); |
| 323 | else if ( previousSibling.getLast() && previousSibling.getLast().$.nodeName.toLowerCase() == 'br' ) |
| 324 | previousSibling.getLast().remove(); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if ( removeLastBr ) |
| 329 | { |
| 330 | var lastChild = block.getLast(); |
| 331 | if ( lastChild && lastChild.type == CKEDITOR.NODE_ELEMENT && lastChild.getName() == 'br' ) |
| 332 | lastChild.remove(); |
| 333 | } |
| 334 | |
| 335 | // Get a reference for the next element. This is important because the |
| 336 | // above block can be removed or changed, so we can rely on it for the |
| 337 | // next interation. |
| 338 | if ( !this._.nextNode ) |
| 339 | this._.nextNode = ( isLast || block.equals( lastNode ) ) ? null : getNextSourceNode( block, true, null, lastNode ); |
| 340 | |
| 341 | return block; |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | CKEDITOR.dom.range.prototype.createIterator = function() |
| 346 | { |
| 347 | return new iterator( this ); |
| 348 | }; |
| 349 | })(); |