2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
6 CKEDITOR.dom.range = function( document )
\r
8 this.startContainer = null;
\r
9 this.startOffset = null;
\r
10 this.endContainer = null;
\r
11 this.endOffset = null;
\r
12 this.collapsed = true;
\r
14 this.document = document;
\r
19 // Updates the "collapsed" property for the given range object.
\r
20 var updateCollapsed = function( range )
\r
23 range.startContainer &&
\r
24 range.endContainer &&
\r
25 range.startContainer.equals( range.endContainer ) &&
\r
26 range.startOffset == range.endOffset );
\r
29 // This is a shared function used to delete, extract and clone the range
\r
32 var execContentsAction = function( range, action, docFrag )
\r
34 range.optimizeBookmark();
\r
36 var startNode = range.startContainer;
\r
37 var endNode = range.endContainer;
\r
39 var startOffset = range.startOffset;
\r
40 var endOffset = range.endOffset;
\r
42 var removeStartNode;
\r
45 // For text containers, we must simply split the node and point to the
\r
46 // second part. The removal will be handled by the rest of the code .
\r
47 if ( endNode.type == CKEDITOR.NODE_TEXT )
\r
48 endNode = endNode.split( endOffset );
\r
51 // If the end container has children and the offset is pointing
\r
52 // to a child, then we should start from it.
\r
53 if ( endNode.getChildCount() > 0 )
\r
55 // If the offset points after the last node.
\r
56 if ( endOffset >= endNode.getChildCount() )
\r
58 // Let's create a temporary node and mark it for removal.
\r
59 endNode = endNode.append( range.document.createText( '' ) );
\r
60 removeEndNode = true;
\r
63 endNode = endNode.getChild( endOffset );
\r
67 // For text containers, we must simply split the node. The removal will
\r
68 // be handled by the rest of the code .
\r
69 if ( startNode.type == CKEDITOR.NODE_TEXT )
\r
71 startNode.split( startOffset );
\r
73 // In cases the end node is the same as the start node, the above
\r
74 // splitting will also split the end, so me must move the end to
\r
75 // the second part of the split.
\r
76 if ( startNode.equals( endNode ) )
\r
77 endNode = startNode.getNext();
\r
81 // If the start container has children and the offset is pointing
\r
82 // to a child, then we should start from its previous sibling.
\r
84 // If the offset points to the first node, we don't have a
\r
85 // sibling, so let's use the first one, but mark it for removal.
\r
88 // Let's create a temporary node and mark it for removal.
\r
89 startNode = startNode.getFirst().insertBeforeMe( range.document.createText( '' ) );
\r
90 removeStartNode = true;
\r
92 else if ( startOffset >= startNode.getChildCount() )
\r
94 // Let's create a temporary node and mark it for removal.
\r
95 startNode = startNode.append( range.document.createText( '' ) );
\r
96 removeStartNode = true;
\r
99 startNode = startNode.getChild( startOffset ).getPrevious();
\r
102 // Get the parent nodes tree for the start and end boundaries.
\r
103 var startParents = startNode.getParents();
\r
104 var endParents = endNode.getParents();
\r
106 // Compare them, to find the top most siblings.
\r
107 var i, topStart, topEnd;
\r
109 for ( i = 0 ; i < startParents.length ; i++ )
\r
111 topStart = startParents[ i ];
\r
112 topEnd = endParents[ i ];
\r
114 // The compared nodes will match until we find the top most
\r
115 // siblings (different nodes that have the same parent).
\r
116 // "i" will hold the index in the parents array for the top
\r
118 if ( !topStart.equals( topEnd ) )
\r
122 var clone = docFrag, levelStartNode, levelClone, currentNode, currentSibling;
\r
124 // Remove all successive sibling nodes for every node in the
\r
125 // startParents tree.
\r
126 for ( var j = i ; j < startParents.length ; j++ )
\r
128 levelStartNode = startParents[j];
\r
130 // For Extract and Clone, we must clone this level.
\r
131 if ( clone && !levelStartNode.equals( startNode ) ) // action = 0 = Delete
\r
132 levelClone = clone.append( levelStartNode.clone() );
\r
134 currentNode = levelStartNode.getNext();
\r
136 while ( currentNode )
\r
138 // Stop processing when the current node matches a node in the
\r
139 // endParents tree or if it is the endNode.
\r
140 if ( currentNode.equals( endParents[ j ] ) || currentNode.equals( endNode ) )
\r
143 // Cache the next sibling.
\r
144 currentSibling = currentNode.getNext();
\r
146 // If cloning, just clone it.
\r
147 if ( action == 2 ) // 2 = Clone
\r
148 clone.append( currentNode.clone( true ) );
\r
151 // Both Delete and Extract will remove the node.
\r
152 currentNode.remove();
\r
154 // When Extracting, move the removed node to the docFrag.
\r
155 if ( action == 1 ) // 1 = Extract
\r
156 clone.append( currentNode );
\r
159 currentNode = currentSibling;
\r
163 clone = levelClone;
\r
168 // Remove all previous sibling nodes for every node in the
\r
169 // endParents tree.
\r
170 for ( var k = i ; k < endParents.length ; k++ )
\r
172 levelStartNode = endParents[ k ];
\r
174 // For Extract and Clone, we must clone this level.
\r
175 if ( action > 0 && !levelStartNode.equals( endNode ) ) // action = 0 = Delete
\r
176 levelClone = clone.append( levelStartNode.clone() );
\r
178 // The processing of siblings may have already been done by the parent.
\r
179 if ( !startParents[ k ] || levelStartNode.$.parentNode != startParents[ k ].$.parentNode )
\r
181 currentNode = levelStartNode.getPrevious();
\r
183 while ( currentNode )
\r
185 // Stop processing when the current node matches a node in the
\r
186 // startParents tree or if it is the startNode.
\r
187 if ( currentNode.equals( startParents[ k ] ) || currentNode.equals( startNode ) )
\r
190 // Cache the next sibling.
\r
191 currentSibling = currentNode.getPrevious();
\r
193 // If cloning, just clone it.
\r
194 if ( action == 2 ) // 2 = Clone
\r
195 clone.$.insertBefore( currentNode.$.cloneNode( true ), clone.$.firstChild ) ;
\r
198 // Both Delete and Extract will remove the node.
\r
199 currentNode.remove();
\r
201 // When Extracting, mode the removed node to the docFrag.
\r
202 if ( action == 1 ) // 1 = Extract
\r
203 clone.$.insertBefore( currentNode.$, clone.$.firstChild );
\r
206 currentNode = currentSibling;
\r
211 clone = levelClone;
\r
214 if ( action == 2 ) // 2 = Clone.
\r
216 // No changes in the DOM should be done, so fix the split text (if any).
\r
218 var startTextNode = range.startContainer;
\r
219 if ( startTextNode.type == CKEDITOR.NODE_TEXT )
\r
221 startTextNode.$.data += startTextNode.$.nextSibling.data;
\r
222 startTextNode.$.parentNode.removeChild( startTextNode.$.nextSibling );
\r
225 var endTextNode = range.endContainer;
\r
226 if ( endTextNode.type == CKEDITOR.NODE_TEXT && endTextNode.$.nextSibling )
\r
228 endTextNode.$.data += endTextNode.$.nextSibling.data;
\r
229 endTextNode.$.parentNode.removeChild( endTextNode.$.nextSibling );
\r
234 // Collapse the range.
\r
236 // If a node has been partially selected, collapse the range between
\r
237 // topStart and topEnd. Otherwise, simply collapse it to the start. (W3C specs).
\r
238 if ( topStart && topEnd && ( startNode.$.parentNode != topStart.$.parentNode || endNode.$.parentNode != topEnd.$.parentNode ) )
\r
240 var endIndex = topEnd.getIndex();
\r
242 // If the start node is to be removed, we must correct the
\r
243 // index to reflect the removal.
\r
244 if ( removeStartNode && topEnd.$.parentNode == startNode.$.parentNode )
\r
247 range.setStart( topEnd.getParent(), endIndex );
\r
250 // Collapse it to the start.
\r
251 range.collapse( true );
\r
254 // Cleanup any marked node.
\r
255 if ( removeStartNode )
\r
256 startNode.remove();
\r
258 if ( removeEndNode && endNode.$.parentNode )
\r
262 var inlineChildReqElements = { abbr:1,acronym:1,b:1,bdo:1,big:1,cite:1,code:1,del:1,dfn:1,em:1,font:1,i:1,ins:1,label:1,kbd:1,q:1,samp:1,small:1,span:1,strike:1,strong:1,sub:1,sup:1,tt:1,u:1,'var':1 };
\r
264 // Creates the appropriate node evaluator for the dom walker used inside
\r
265 // check(Start|End)OfBlock.
\r
266 function getCheckStartEndBlockEvalFunction( isStart )
\r
268 var hadBr = false, bookmarkEvaluator = CKEDITOR.dom.walker.bookmark( true );
\r
269 return function( node )
\r
271 // First ignore bookmark nodes.
\r
272 if ( bookmarkEvaluator( node ) )
\r
275 if ( node.type == CKEDITOR.NODE_TEXT )
\r
277 // If there's any visible text, then we're not at the start.
\r
278 if ( CKEDITOR.tools.trim( node.getText() ).length )
\r
281 else if ( node.type == CKEDITOR.NODE_ELEMENT )
\r
283 // If there are non-empty inline elements (e.g. <img />), then we're not
\r
285 if ( !inlineChildReqElements[ node.getName() ] )
\r
287 // If we're working at the end-of-block, forgive the first <br /> in non-IE
\r
289 if ( !isStart && !CKEDITOR.env.ie && node.getName() == 'br' && !hadBr )
\r
299 // Evaluator for CKEDITOR.dom.element::checkBoundaryOfElement, reject any
\r
300 // text node and non-empty elements unless it's being bookmark text.
\r
301 function elementBoundaryEval( node )
\r
303 // Reject any text node unless it's being bookmark
\r
304 // OR it's spaces. (#3883)
\r
305 return node.type != CKEDITOR.NODE_TEXT
\r
306 && node.getName() in CKEDITOR.dtd.$removeEmpty
\r
307 || !CKEDITOR.tools.trim( node.getText() )
\r
308 || node.getParent().hasAttribute( '_fck_bookmark' );
\r
311 var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),
\r
312 bookmarkEval = new CKEDITOR.dom.walker.bookmark();
\r
314 function nonWhitespaceOrBookmarkEval( node )
\r
316 // Whitespaces and bookmark nodes are to be ignored.
\r
317 return !whitespaceEval( node ) && !bookmarkEval( node );
\r
320 CKEDITOR.dom.range.prototype =
\r
324 var clone = new CKEDITOR.dom.range( this.document );
\r
326 clone.startContainer = this.startContainer;
\r
327 clone.startOffset = this.startOffset;
\r
328 clone.endContainer = this.endContainer;
\r
329 clone.endOffset = this.endOffset;
\r
330 clone.collapsed = this.collapsed;
\r
335 collapse : function( toStart )
\r
339 this.endContainer = this.startContainer;
\r
340 this.endOffset = this.startOffset;
\r
344 this.startContainer = this.endContainer;
\r
345 this.startOffset = this.endOffset;
\r
348 this.collapsed = true;
\r
351 // The selection may be lost when cloning (due to the splitText() call).
\r
352 cloneContents : function()
\r
354 var docFrag = new CKEDITOR.dom.documentFragment( this.document );
\r
356 if ( !this.collapsed )
\r
357 execContentsAction( this, 2, docFrag );
\r
362 deleteContents : function()
\r
364 if ( this.collapsed )
\r
367 execContentsAction( this, 0 );
\r
370 extractContents : function()
\r
372 var docFrag = new CKEDITOR.dom.documentFragment( this.document );
\r
374 if ( !this.collapsed )
\r
375 execContentsAction( this, 1, docFrag );
\r
381 * Creates a bookmark object, which can be later used to restore the
\r
382 * range by using the moveToBookmark function.
\r
383 * This is an "intrusive" way to create a bookmark. It includes <span> tags
\r
384 * in the range boundaries. The advantage of it is that it is possible to
\r
385 * handle DOM mutations when moving back to the bookmark.
\r
386 * Attention: the inclusion of nodes in the DOM is a design choice and
\r
387 * should not be changed as there are other points in the code that may be
\r
388 * using those nodes to perform operations. See GetBookmarkNode.
\r
389 * @param {Boolean} [serializable] Indicates that the bookmark nodes
\r
390 * must contain ids, which can be used to restore the range even
\r
391 * when these nodes suffer mutations (like a clonation or innerHTML
\r
393 * @returns {Object} And object representing a bookmark.
\r
395 createBookmark : function( serializable )
\r
397 var startNode, endNode;
\r
401 startNode = this.document.createElement( 'span' );
\r
402 startNode.setAttribute( '_fck_bookmark', 1 );
\r
403 startNode.setStyle( 'display', 'none' );
\r
405 // For IE, it must have something inside, otherwise it may be
\r
406 // removed during DOM operations.
\r
407 startNode.setHtml( ' ' );
\r
409 if ( serializable )
\r
411 baseId = 'cke_bm_' + CKEDITOR.tools.getNextNumber();
\r
412 startNode.setAttribute( 'id', baseId + 'S' );
\r
415 // If collapsed, the endNode will not be created.
\r
416 if ( !this.collapsed )
\r
418 endNode = startNode.clone();
\r
419 endNode.setHtml( ' ' );
\r
421 if ( serializable )
\r
422 endNode.setAttribute( 'id', baseId + 'E' );
\r
424 clone = this.clone();
\r
426 clone.insertNode( endNode );
\r
429 clone = this.clone();
\r
430 clone.collapse( true );
\r
431 clone.insertNode( startNode );
\r
433 // Update the range position.
\r
436 this.setStartAfter( startNode );
\r
437 this.setEndBefore( endNode );
\r
440 this.moveToPosition( startNode, CKEDITOR.POSITION_AFTER_END );
\r
443 startNode : serializable ? baseId + 'S' : startNode,
\r
444 endNode : serializable ? baseId + 'E' : endNode,
\r
445 serializable : serializable
\r
450 * Creates a "non intrusive" and "mutation sensible" bookmark. This
\r
451 * kind of bookmark should be used only when the DOM is supposed to
\r
452 * remain stable after its creation.
\r
453 * @param {Boolean} [normalized] Indicates that the bookmark must
\r
454 * normalized. When normalized, the successive text nodes are
\r
455 * considered a single node. To sucessful load a normalized
\r
456 * bookmark, the DOM tree must be also normalized before calling
\r
458 * @returns {Object} An object representing the bookmark.
\r
460 createBookmark2 : function( normalized )
\r
462 var startContainer = this.startContainer,
\r
463 endContainer = this.endContainer;
\r
465 var startOffset = this.startOffset,
\r
466 endOffset = this.endOffset;
\r
468 var child, previous;
\r
470 // If there is no range then get out of here.
\r
471 // It happens on initial load in Safari #962 and if the editor it's
\r
472 // hidden also in Firefox
\r
473 if ( !startContainer || !endContainer )
\r
474 return { start : 0, end : 0 };
\r
478 // Find out if the start is pointing to a text node that will
\r
480 if ( startContainer.type == CKEDITOR.NODE_ELEMENT )
\r
482 child = startContainer.getChild( startOffset );
\r
484 // In this case, move the start information to that text
\r
486 if ( child && child.type == CKEDITOR.NODE_TEXT
\r
487 && startOffset > 0 && child.getPrevious().type == CKEDITOR.NODE_TEXT )
\r
489 startContainer = child;
\r
494 // Normalize the start.
\r
495 while ( startContainer.type == CKEDITOR.NODE_TEXT
\r
496 && ( previous = startContainer.getPrevious() )
\r
497 && previous.type == CKEDITOR.NODE_TEXT )
\r
499 startContainer = previous;
\r
500 startOffset += previous.getLength();
\r
503 // Process the end only if not normalized.
\r
504 if ( !this.isCollapsed )
\r
506 // Find out if the start is pointing to a text node that
\r
507 // will be normalized.
\r
508 if ( endContainer.type == CKEDITOR.NODE_ELEMENT )
\r
510 child = endContainer.getChild( endOffset );
\r
512 // In this case, move the start information to that
\r
514 if ( child && child.type == CKEDITOR.NODE_TEXT
\r
515 && endOffset > 0 && child.getPrevious().type == CKEDITOR.NODE_TEXT )
\r
517 endContainer = child;
\r
522 // Normalize the end.
\r
523 while ( endContainer.type == CKEDITOR.NODE_TEXT
\r
524 && ( previous = endContainer.getPrevious() )
\r
525 && previous.type == CKEDITOR.NODE_TEXT )
\r
527 endContainer = previous;
\r
528 endOffset += previous.getLength();
\r
534 start : startContainer.getAddress( normalized ),
\r
535 end : this.isCollapsed ? null : endContainer.getAddress( normalized ),
\r
536 startOffset : startOffset,
\r
537 endOffset : endOffset,
\r
538 normalized : normalized,
\r
539 is2 : true // It's a createBookmark2 bookmark.
\r
543 moveToBookmark : function( bookmark )
\r
545 if ( bookmark.is2 ) // Created with createBookmark2().
\r
547 // Get the start information.
\r
548 var startContainer = this.document.getByAddress( bookmark.start, bookmark.normalized ),
\r
549 startOffset = bookmark.startOffset;
\r
551 // Get the end information.
\r
552 var endContainer = bookmark.end && this.document.getByAddress( bookmark.end, bookmark.normalized ),
\r
553 endOffset = bookmark.endOffset;
\r
555 // Set the start boundary.
\r
556 this.setStart( startContainer, startOffset );
\r
558 // Set the end boundary. If not available, collapse it.
\r
559 if ( endContainer )
\r
560 this.setEnd( endContainer, endOffset );
\r
562 this.collapse( true );
\r
564 else // Created with createBookmark().
\r
566 var serializable = bookmark.serializable,
\r
567 startNode = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,
\r
568 endNode = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;
\r
570 // Set the range start at the bookmark start node position.
\r
571 this.setStartBefore( startNode );
\r
573 // Remove it, because it may interfere in the setEndBefore call.
\r
574 startNode.remove();
\r
576 // Set the range end at the bookmark end node position, or simply
\r
577 // collapse it if it is not available.
\r
580 this.setEndBefore( endNode );
\r
584 this.collapse( true );
\r
588 getBoundaryNodes : function()
\r
590 var startNode = this.startContainer,
\r
591 endNode = this.endContainer,
\r
592 startOffset = this.startOffset,
\r
593 endOffset = this.endOffset,
\r
596 if ( startNode.type == CKEDITOR.NODE_ELEMENT )
\r
598 childCount = startNode.getChildCount();
\r
599 if ( childCount > startOffset )
\r
600 startNode = startNode.getChild( startOffset );
\r
601 else if ( childCount < 1 )
\r
602 startNode = startNode.getPreviousSourceNode();
\r
603 else // startOffset > childCount but childCount is not 0
\r
605 // Try to take the node just after the current position.
\r
606 startNode = startNode.$;
\r
607 while ( startNode.lastChild )
\r
608 startNode = startNode.lastChild;
\r
609 startNode = new CKEDITOR.dom.node( startNode );
\r
611 // Normally we should take the next node in DFS order. But it
\r
612 // is also possible that we've already reached the end of
\r
614 startNode = startNode.getNextSourceNode() || startNode;
\r
617 if ( endNode.type == CKEDITOR.NODE_ELEMENT )
\r
619 childCount = endNode.getChildCount();
\r
620 if ( childCount > endOffset )
\r
621 endNode = endNode.getChild( endOffset ).getPreviousSourceNode( true );
\r
622 else if ( childCount < 1 )
\r
623 endNode = endNode.getPreviousSourceNode();
\r
624 else // endOffset > childCount but childCount is not 0
\r
626 // Try to take the node just before the current position.
\r
627 endNode = endNode.$;
\r
628 while ( endNode.lastChild )
\r
629 endNode = endNode.lastChild;
\r
630 endNode = new CKEDITOR.dom.node( endNode );
\r
634 // Sometimes the endNode will come right before startNode for collapsed
\r
635 // ranges. Fix it. (#3780)
\r
636 if ( startNode.getPosition( endNode ) & CKEDITOR.POSITION_FOLLOWING )
\r
637 startNode = endNode;
\r
639 return { startNode : startNode, endNode : endNode };
\r
643 * Find the node which fully contains the range.
\r
644 * @param includeSelf
\r
645 * @param {Boolean} ignoreTextNode Whether ignore CKEDITOR.NODE_TEXT type.
\r
647 getCommonAncestor : function( includeSelf , ignoreTextNode )
\r
649 var start = this.startContainer,
\r
650 end = this.endContainer,
\r
653 if ( start.equals( end ) )
\r
656 && start.type == CKEDITOR.NODE_ELEMENT
\r
657 && this.startOffset == this.endOffset - 1 )
\r
658 ancestor = start.getChild( this.startOffset );
\r
663 ancestor = start.getCommonAncestor( end );
\r
665 return ignoreTextNode && !ancestor.is ? ancestor.getParent() : ancestor;
\r
669 * Transforms the startContainer and endContainer properties from text
\r
670 * nodes to element nodes, whenever possible. This is actually possible
\r
671 * if either of the boundary containers point to a text node, and its
\r
672 * offset is set to zero, or after the last char in the node.
\r
674 optimize : function()
\r
676 var container = this.startContainer;
\r
677 var offset = this.startOffset;
\r
679 if ( container.type != CKEDITOR.NODE_ELEMENT )
\r
682 this.setStartBefore( container );
\r
683 else if ( offset >= container.getLength() )
\r
684 this.setStartAfter( container );
\r
687 container = this.endContainer;
\r
688 offset = this.endOffset;
\r
690 if ( container.type != CKEDITOR.NODE_ELEMENT )
\r
693 this.setEndBefore( container );
\r
694 else if ( offset >= container.getLength() )
\r
695 this.setEndAfter( container );
\r
700 * Move the range out of bookmark nodes if they'd been the container.
\r
702 optimizeBookmark: function()
\r
704 var startNode = this.startContainer,
\r
705 endNode = this.endContainer;
\r
707 if ( startNode.is && startNode.is( 'span' )
\r
708 && startNode.hasAttribute( '_fck_bookmark' ) )
\r
709 this.setStartAt( startNode, CKEDITOR.POSITION_BEFORE_START );
\r
710 if ( endNode && endNode.is && endNode.is( 'span' )
\r
711 && endNode.hasAttribute( '_fck_bookmark' ) )
\r
712 this.setEndAt( endNode, CKEDITOR.POSITION_AFTER_END );
\r
715 trim : function( ignoreStart, ignoreEnd )
\r
717 var startContainer = this.startContainer,
\r
718 startOffset = this.startOffset,
\r
719 collapsed = this.collapsed;
\r
720 if ( ( !ignoreStart || collapsed )
\r
721 && startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
723 // If the offset is zero, we just insert the new node before
\r
725 if ( !startOffset )
\r
727 startOffset = startContainer.getIndex();
\r
728 startContainer = startContainer.getParent();
\r
730 // If the offset is at the end, we'll insert it after the text
\r
732 else if ( startOffset >= startContainer.getLength() )
\r
734 startOffset = startContainer.getIndex() + 1;
\r
735 startContainer = startContainer.getParent();
\r
737 // In other case, we split the text node and insert the new
\r
738 // node at the split point.
\r
741 var nextText = startContainer.split( startOffset );
\r
743 startOffset = startContainer.getIndex() + 1;
\r
744 startContainer = startContainer.getParent();
\r
746 // Check all necessity of updating the end boundary.
\r
747 if ( this.startContainer.equals( this.endContainer ) )
\r
748 this.setEnd( nextText, this.endOffset - this.startOffset );
\r
749 else if ( startContainer.equals( this.endContainer ) )
\r
750 this.endOffset += 1;
\r
753 this.setStart( startContainer, startOffset );
\r
757 this.collapse( true );
\r
762 var endContainer = this.endContainer;
\r
763 var endOffset = this.endOffset;
\r
765 if ( !( ignoreEnd || collapsed )
\r
766 && endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
768 // If the offset is zero, we just insert the new node before
\r
772 endOffset = endContainer.getIndex();
\r
773 endContainer = endContainer.getParent();
\r
775 // If the offset is at the end, we'll insert it after the text
\r
777 else if ( endOffset >= endContainer.getLength() )
\r
779 endOffset = endContainer.getIndex() + 1;
\r
780 endContainer = endContainer.getParent();
\r
782 // In other case, we split the text node and insert the new
\r
783 // node at the split point.
\r
786 endContainer.split( endOffset );
\r
788 endOffset = endContainer.getIndex() + 1;
\r
789 endContainer = endContainer.getParent();
\r
792 this.setEnd( endContainer, endOffset );
\r
796 enlarge : function( unit )
\r
800 case CKEDITOR.ENLARGE_ELEMENT :
\r
802 if ( this.collapsed )
\r
805 // Get the common ancestor.
\r
806 var commonAncestor = this.getCommonAncestor();
\r
808 var body = this.document.getBody();
\r
810 // For each boundary
\r
811 // a. Depending on its position, find out the first node to be checked (a sibling) or, if not available, to be enlarge.
\r
812 // b. Go ahead checking siblings and enlarging the boundary as much as possible until the common ancestor is not reached. After reaching the common ancestor, just save the enlargeable node to be used later.
\r
814 var startTop, endTop;
\r
816 var enlargeable, sibling, commonReached;
\r
818 // Indicates that the node can be added only if whitespace
\r
819 // is available before it.
\r
820 var needsWhiteSpace = false;
\r
824 // Process the start boundary.
\r
826 var container = this.startContainer;
\r
827 var offset = this.startOffset;
\r
829 if ( container.type == CKEDITOR.NODE_TEXT )
\r
833 // Check if there is any non-space text before the
\r
834 // offset. Otherwise, container is null.
\r
835 container = !CKEDITOR.tools.trim( container.substring( 0, offset ) ).length && container;
\r
837 // If we found only whitespace in the node, it
\r
838 // means that we'll need more whitespace to be able
\r
839 // to expand. For example, <i> can be expanded in
\r
840 // "A <i> [B]</i>", but not in "A<i> [B]</i>".
\r
841 needsWhiteSpace = !!container;
\r
846 if ( !( sibling = container.getPrevious() ) )
\r
847 enlargeable = container.getParent();
\r
852 // If we have offset, get the node preceeding it as the
\r
853 // first sibling to be checked.
\r
855 sibling = container.getChild( offset - 1 ) || container.getLast();
\r
857 // If there is no sibling, mark the container to be
\r
860 enlargeable = container;
\r
863 while ( enlargeable || sibling )
\r
865 if ( enlargeable && !sibling )
\r
867 // If we reached the common ancestor, mark the flag
\r
869 if ( !commonReached && enlargeable.equals( commonAncestor ) )
\r
870 commonReached = true;
\r
872 if ( !body.contains( enlargeable ) )
\r
875 // If we don't need space or this element breaks
\r
876 // the line, then enlarge it.
\r
877 if ( !needsWhiteSpace || enlargeable.getComputedStyle( 'display' ) != 'inline' )
\r
879 needsWhiteSpace = false;
\r
881 // If the common ancestor has been reached,
\r
882 // we'll not enlarge it immediately, but just
\r
883 // mark it to be enlarged later if the end
\r
884 // boundary also enlarges it.
\r
885 if ( commonReached )
\r
886 startTop = enlargeable;
\r
888 this.setStartBefore( enlargeable );
\r
891 sibling = enlargeable.getPrevious();
\r
894 // Check all sibling nodes preceeding the enlargeable
\r
895 // node. The node wil lbe enlarged only if none of them
\r
899 // This flag indicates that this node has
\r
900 // whitespaces at the end.
\r
901 isWhiteSpace = false;
\r
903 if ( sibling.type == CKEDITOR.NODE_TEXT )
\r
905 siblingText = sibling.getText();
\r
907 if ( /[^\s\ufeff]/.test( siblingText ) )
\r
910 isWhiteSpace = /[\s\ufeff]$/.test( siblingText );
\r
914 // If this is a visible element.
\r
915 // We need to check for the bookmark attribute because IE insists on
\r
916 // rendering the display:none nodes we use for bookmarks. (#3363)
\r
917 if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )
\r
919 // We'll accept it only if we need
\r
920 // whitespace, and this is an inline
\r
921 // element with whitespace only.
\r
922 if ( needsWhiteSpace && CKEDITOR.dtd.$removeEmpty[ sibling.getName() ] )
\r
924 // It must contains spaces and inline elements only.
\r
926 siblingText = sibling.getText();
\r
928 if ( (/[^\s\ufeff]/).test( siblingText ) ) // Spaces + Zero Width No-Break Space (U+FEFF)
\r
932 var allChildren = sibling.$.all || sibling.$.getElementsByTagName( '*' );
\r
933 for ( var i = 0, child ; child = allChildren[ i++ ] ; )
\r
935 if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )
\r
944 isWhiteSpace = !!siblingText.length;
\r
951 // A node with whitespaces has been found.
\r
952 if ( isWhiteSpace )
\r
954 // Enlarge the last enlargeable node, if we
\r
955 // were waiting for spaces.
\r
956 if ( needsWhiteSpace )
\r
958 if ( commonReached )
\r
959 startTop = enlargeable;
\r
960 else if ( enlargeable )
\r
961 this.setStartBefore( enlargeable );
\r
964 needsWhiteSpace = true;
\r
969 var next = sibling.getPrevious();
\r
971 if ( !enlargeable && !next )
\r
973 // Set the sibling as enlargeable, so it's
\r
974 // parent will be get later outside this while.
\r
975 enlargeable = sibling;
\r
984 // If sibling has been set to null, then we
\r
985 // need to stop enlarging.
\r
986 enlargeable = null;
\r
991 enlargeable = enlargeable.getParent();
\r
994 // Process the end boundary. This is basically the same
\r
995 // code used for the start boundary, with small changes to
\r
996 // make it work in the oposite side (to the right). This
\r
997 // makes it difficult to reuse the code here. So, fixes to
\r
998 // the above code are likely to be replicated here.
\r
1000 container = this.endContainer;
\r
1001 offset = this.endOffset;
\r
1003 // Reset the common variables.
\r
1004 enlargeable = sibling = null;
\r
1005 commonReached = needsWhiteSpace = false;
\r
1007 if ( container.type == CKEDITOR.NODE_TEXT )
\r
1009 // Check if there is any non-space text after the
\r
1010 // offset. Otherwise, container is null.
\r
1011 container = !CKEDITOR.tools.trim( container.substring( offset ) ).length && container;
\r
1013 // If we found only whitespace in the node, it
\r
1014 // means that we'll need more whitespace to be able
\r
1015 // to expand. For example, <i> can be expanded in
\r
1016 // "A <i> [B]</i>", but not in "A<i> [B]</i>".
\r
1017 needsWhiteSpace = !( container && container.getLength() );
\r
1021 if ( !( sibling = container.getNext() ) )
\r
1022 enlargeable = container.getParent();
\r
1027 // Get the node right after the boudary to be checked
\r
1029 sibling = container.getChild( offset );
\r
1032 enlargeable = container;
\r
1035 while ( enlargeable || sibling )
\r
1037 if ( enlargeable && !sibling )
\r
1039 if ( !commonReached && enlargeable.equals( commonAncestor ) )
\r
1040 commonReached = true;
\r
1042 if ( !body.contains( enlargeable ) )
\r
1045 if ( !needsWhiteSpace || enlargeable.getComputedStyle( 'display' ) != 'inline' )
\r
1047 needsWhiteSpace = false;
\r
1049 if ( commonReached )
\r
1050 endTop = enlargeable;
\r
1051 else if ( enlargeable )
\r
1052 this.setEndAfter( enlargeable );
\r
1055 sibling = enlargeable.getNext();
\r
1060 isWhiteSpace = false;
\r
1062 if ( sibling.type == CKEDITOR.NODE_TEXT )
\r
1064 siblingText = sibling.getText();
\r
1066 if ( /[^\s\ufeff]/.test( siblingText ) )
\r
1069 isWhiteSpace = /^[\s\ufeff]/.test( siblingText );
\r
1073 // If this is a visible element.
\r
1074 // We need to check for the bookmark attribute because IE insists on
\r
1075 // rendering the display:none nodes we use for bookmarks. (#3363)
\r
1076 if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )
\r
1078 // We'll accept it only if we need
\r
1079 // whitespace, and this is an inline
\r
1080 // element with whitespace only.
\r
1081 if ( needsWhiteSpace && CKEDITOR.dtd.$removeEmpty[ sibling.getName() ] )
\r
1083 // It must contains spaces and inline elements only.
\r
1085 siblingText = sibling.getText();
\r
1087 if ( (/[^\s\ufeff]/).test( siblingText ) )
\r
1091 allChildren = sibling.$.all || sibling.$.getElementsByTagName( '*' );
\r
1092 for ( i = 0 ; child = allChildren[ i++ ] ; )
\r
1094 if ( !CKEDITOR.dtd.$removeEmpty[ child.nodeName.toLowerCase() ] )
\r
1103 isWhiteSpace = !!siblingText.length;
\r
1110 if ( isWhiteSpace )
\r
1112 if ( needsWhiteSpace )
\r
1114 if ( commonReached )
\r
1115 endTop = enlargeable;
\r
1117 this.setEndAfter( enlargeable );
\r
1123 next = sibling.getNext();
\r
1125 if ( !enlargeable && !next )
\r
1127 enlargeable = sibling;
\r
1136 // If sibling has been set to null, then we
\r
1137 // need to stop enlarging.
\r
1138 enlargeable = null;
\r
1142 if ( enlargeable )
\r
1143 enlargeable = enlargeable.getParent();
\r
1146 // If the common ancestor can be enlarged by both boundaries, then include it also.
\r
1147 if ( startTop && endTop )
\r
1149 commonAncestor = startTop.contains( endTop ) ? endTop : startTop;
\r
1151 this.setStartBefore( commonAncestor );
\r
1152 this.setEndAfter( commonAncestor );
\r
1156 case CKEDITOR.ENLARGE_BLOCK_CONTENTS:
\r
1157 case CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS:
\r
1159 // Enlarging the start boundary.
\r
1160 var walkerRange = new CKEDITOR.dom.range( this.document );
\r
1162 body = this.document.getBody();
\r
1164 walkerRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );
\r
1165 walkerRange.setEnd( this.startContainer, this.startOffset );
\r
1167 var walker = new CKEDITOR.dom.walker( walkerRange ),
\r
1168 blockBoundary, // The node on which the enlarging should stop.
\r
1169 tailBr, // In case BR as block boundary.
\r
1170 notBlockBoundary = CKEDITOR.dom.walker.blockBoundary(
\r
1171 ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ? { br : 1 } : null ),
\r
1172 // Record the encountered 'blockBoundary' for later use.
\r
1173 boundaryGuard = function( node )
\r
1175 var retval = notBlockBoundary( node );
\r
1177 blockBoundary = node;
\r
1180 // Record the encounted 'tailBr' for later use.
\r
1181 tailBrGuard = function( node )
\r
1183 var retval = boundaryGuard( node );
\r
1184 if ( !retval && node.is && node.is( 'br' ) )
\r
1189 walker.guard = boundaryGuard;
\r
1191 enlargeable = walker.lastBackward();
\r
1193 // It's the body which stop the enlarging if no block boundary found.
\r
1194 blockBoundary = blockBoundary || body;
\r
1196 // Start the range either after the end of found block (<p>...</p>[text)
\r
1197 // or at the start of block (<p>[text...), by comparing the document position
\r
1198 // with 'enlargeable' node.
\r
1201 !blockBoundary.is( 'br' ) &&
\r
1202 ( !enlargeable && this.checkStartOfBlock()
\r
1203 || enlargeable && blockBoundary.contains( enlargeable ) ) ?
\r
1204 CKEDITOR.POSITION_AFTER_START :
\r
1205 CKEDITOR.POSITION_AFTER_END );
\r
1207 // Enlarging the end boundary.
\r
1208 walkerRange = this.clone();
\r
1209 walkerRange.collapse();
\r
1210 walkerRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
\r
1211 walker = new CKEDITOR.dom.walker( walkerRange );
\r
1213 // tailBrGuard only used for on range end.
\r
1214 walker.guard = ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ?
\r
1215 tailBrGuard : boundaryGuard;
\r
1216 blockBoundary = null;
\r
1217 // End the range right before the block boundary node.
\r
1219 enlargeable = walker.lastForward();
\r
1221 // It's the body which stop the enlarging if no block boundary found.
\r
1222 blockBoundary = blockBoundary || body;
\r
1224 // Close the range either before the found block start (text]<p>...</p>) or at the block end (...text]</p>)
\r
1225 // by comparing the document position with 'enlargeable' node.
\r
1228 ( !enlargeable && this.checkEndOfBlock()
\r
1229 || enlargeable && blockBoundary.contains( enlargeable ) ) ?
\r
1230 CKEDITOR.POSITION_BEFORE_END :
\r
1231 CKEDITOR.POSITION_BEFORE_START );
\r
1232 // We must include the <br> at the end of range if there's
\r
1233 // one and we're expanding list item contents
\r
1235 this.setEndAfter( tailBr );
\r
1240 * Descrease the range to make sure that boundaries
\r
1241 * always anchor beside text nodes or innermost element.
\r
1242 * @param {Number} mode ( CKEDITOR.SHRINK_ELEMENT | CKEDITOR.SHRINK_TEXT ) The shrinking mode.
\r
1244 * <dt>CKEDITOR.SHRINK_ELEMENT</dt>
\r
1245 * <dd>Shrink the range boundaries to the edge of the innermost element.</dd>
\r
1246 * <dt>CKEDITOR.SHRINK_TEXT</dt>
\r
1247 * <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
1249 * @param {Boolean} selectContents Whether result range anchors at the inner OR outer boundary of the node.
\r
1251 shrink : function( mode, selectContents )
\r
1253 // Unable to shrink a collapsed range.
\r
1254 if ( !this.collapsed )
\r
1256 mode = mode || CKEDITOR.SHRINK_TEXT;
\r
1258 var walkerRange = this.clone();
\r
1260 var startContainer = this.startContainer,
\r
1261 endContainer = this.endContainer,
\r
1262 startOffset = this.startOffset,
\r
1263 endOffset = this.endOffset,
\r
1264 collapsed = this.collapsed;
\r
1266 // Whether the start/end boundary is moveable.
\r
1267 var moveStart = 1,
\r
1270 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
1272 if ( !startOffset )
\r
1273 walkerRange.setStartBefore( startContainer );
\r
1274 else if ( startOffset >= startContainer.getLength( ) )
\r
1275 walkerRange.setStartAfter( startContainer );
\r
1278 // Enlarge the range properly to avoid walker making
\r
1279 // DOM changes caused by triming the text nodes later.
\r
1280 walkerRange.setStartBefore( startContainer );
\r
1285 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
1288 walkerRange.setEndBefore( endContainer );
\r
1289 else if ( endOffset >= endContainer.getLength( ) )
\r
1290 walkerRange.setEndAfter( endContainer );
\r
1293 walkerRange.setEndAfter( endContainer );
\r
1298 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1300 walker.evaluator = function( node )
\r
1302 return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ?
\r
1303 CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );
\r
1306 var currentElement;
\r
1307 walker.guard = function( node, movingOut )
\r
1309 // Stop when we're shrink in element mode while encountering a text node.
\r
1310 if ( mode == CKEDITOR.SHRINK_ELEMENT && node.type == CKEDITOR.NODE_TEXT )
\r
1313 // Stop when we've already walked "through" an element.
\r
1314 if ( movingOut && node.equals( currentElement ) )
\r
1317 if ( !movingOut && node.type == CKEDITOR.NODE_ELEMENT )
\r
1318 currentElement = node;
\r
1325 var textStart = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastForward' : 'next']();
\r
1326 textStart && this.setStartAt( textStart, selectContents ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_START );
\r
1332 var textEnd = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastBackward' : 'previous']();
\r
1333 textEnd && this.setEndAt( textEnd, selectContents ? CKEDITOR.POSITION_BEFORE_END : CKEDITOR.POSITION_AFTER_END );
\r
1336 return !!( moveStart || moveEnd );
\r
1341 * Inserts a node at the start of the range. The range will be expanded
\r
1342 * the contain the node.
\r
1344 insertNode : function( node )
\r
1346 this.optimizeBookmark();
\r
1347 this.trim( false, true );
\r
1349 var startContainer = this.startContainer;
\r
1350 var startOffset = this.startOffset;
\r
1352 var nextNode = startContainer.getChild( startOffset );
\r
1355 node.insertBefore( nextNode );
\r
1357 startContainer.append( node );
\r
1359 // Check if we need to update the end boundary.
\r
1360 if ( node.getParent().equals( this.endContainer ) )
\r
1363 // Expand the range to embrace the new node.
\r
1364 this.setStartBefore( node );
\r
1367 moveToPosition : function( node, position )
\r
1369 this.setStartAt( node, position );
\r
1370 this.collapse( true );
\r
1373 selectNodeContents : function( node )
\r
1375 this.setStart( node, 0 );
\r
1376 this.setEnd( node, node.type == CKEDITOR.NODE_TEXT ? node.getLength() : node.getChildCount() );
\r
1380 * Sets the start position of a Range.
\r
1381 * @param {CKEDITOR.dom.node} startNode The node to start the range.
\r
1382 * @param {Number} startOffset An integer greater than or equal to zero
\r
1383 * representing the offset for the start of the range from the start
\r
1386 setStart : function( startNode, startOffset )
\r
1388 // W3C requires a check for the new position. If it is after the end
\r
1389 // boundary, the range should be collapsed to the new start. It seams
\r
1390 // we will not need this check for our use of this class so we can
\r
1391 // ignore it for now.
\r
1393 // Fixing invalid range start inside dtd empty elements.
\r
1394 if( startNode.type == CKEDITOR.NODE_ELEMENT
\r
1395 && CKEDITOR.dtd.$empty[ startNode.getName() ] )
\r
1396 startNode = startNode.getParent(), startOffset = startNode.getIndex();
\r
1398 this.startContainer = startNode;
\r
1399 this.startOffset = startOffset;
\r
1401 if ( !this.endContainer )
\r
1403 this.endContainer = startNode;
\r
1404 this.endOffset = startOffset;
\r
1407 updateCollapsed( this );
\r
1411 * Sets the end position of a Range.
\r
1412 * @param {CKEDITOR.dom.node} endNode The node to end the range.
\r
1413 * @param {Number} endOffset An integer greater than or equal to zero
\r
1414 * representing the offset for the end of the range from the start
\r
1417 setEnd : function( endNode, endOffset )
\r
1419 // W3C requires a check for the new position. If it is before the start
\r
1420 // boundary, the range should be collapsed to the new end. It seams we
\r
1421 // will not need this check for our use of this class so we can ignore
\r
1424 // Fixing invalid range end inside dtd empty elements.
\r
1425 if( endNode.type == CKEDITOR.NODE_ELEMENT
\r
1426 && CKEDITOR.dtd.$empty[ endNode.getName() ] )
\r
1427 endNode = endNode.getParent(), endOffset = endNode.getIndex() + 1;
\r
1429 this.endContainer = endNode;
\r
1430 this.endOffset = endOffset;
\r
1432 if ( !this.startContainer )
\r
1434 this.startContainer = endNode;
\r
1435 this.startOffset = endOffset;
\r
1438 updateCollapsed( this );
\r
1441 setStartAfter : function( node )
\r
1443 this.setStart( node.getParent(), node.getIndex() + 1 );
\r
1446 setStartBefore : function( node )
\r
1448 this.setStart( node.getParent(), node.getIndex() );
\r
1451 setEndAfter : function( node )
\r
1453 this.setEnd( node.getParent(), node.getIndex() + 1 );
\r
1456 setEndBefore : function( node )
\r
1458 this.setEnd( node.getParent(), node.getIndex() );
\r
1461 setStartAt : function( node, position )
\r
1463 switch( position )
\r
1465 case CKEDITOR.POSITION_AFTER_START :
\r
1466 this.setStart( node, 0 );
\r
1469 case CKEDITOR.POSITION_BEFORE_END :
\r
1470 if ( node.type == CKEDITOR.NODE_TEXT )
\r
1471 this.setStart( node, node.getLength() );
\r
1473 this.setStart( node, node.getChildCount() );
\r
1476 case CKEDITOR.POSITION_BEFORE_START :
\r
1477 this.setStartBefore( node );
\r
1480 case CKEDITOR.POSITION_AFTER_END :
\r
1481 this.setStartAfter( node );
\r
1484 updateCollapsed( this );
\r
1487 setEndAt : function( node, position )
\r
1489 switch( position )
\r
1491 case CKEDITOR.POSITION_AFTER_START :
\r
1492 this.setEnd( node, 0 );
\r
1495 case CKEDITOR.POSITION_BEFORE_END :
\r
1496 if ( node.type == CKEDITOR.NODE_TEXT )
\r
1497 this.setEnd( node, node.getLength() );
\r
1499 this.setEnd( node, node.getChildCount() );
\r
1502 case CKEDITOR.POSITION_BEFORE_START :
\r
1503 this.setEndBefore( node );
\r
1506 case CKEDITOR.POSITION_AFTER_END :
\r
1507 this.setEndAfter( node );
\r
1510 updateCollapsed( this );
\r
1513 fixBlock : function( isStart, blockTag )
\r
1515 var bookmark = this.createBookmark(),
\r
1516 fixedBlock = this.document.createElement( blockTag );
\r
1518 this.collapse( isStart );
\r
1520 this.enlarge( CKEDITOR.ENLARGE_BLOCK_CONTENTS );
\r
1522 this.extractContents().appendTo( fixedBlock );
\r
1523 fixedBlock.trim();
\r
1525 if ( !CKEDITOR.env.ie )
\r
1526 fixedBlock.appendBogus();
\r
1528 this.insertNode( fixedBlock );
\r
1530 this.moveToBookmark( bookmark );
\r
1532 return fixedBlock;
\r
1535 splitBlock : function( blockTag )
\r
1537 var startPath = new CKEDITOR.dom.elementPath( this.startContainer ),
\r
1538 endPath = new CKEDITOR.dom.elementPath( this.endContainer );
\r
1540 var startBlockLimit = startPath.blockLimit,
\r
1541 endBlockLimit = endPath.blockLimit;
\r
1543 var startBlock = startPath.block,
\r
1544 endBlock = endPath.block;
\r
1546 var elementPath = null;
\r
1547 // Do nothing if the boundaries are in different block limits.
\r
1548 if ( !startBlockLimit.equals( endBlockLimit ) )
\r
1551 // Get or fix current blocks.
\r
1552 if ( blockTag != 'br' )
\r
1554 if ( !startBlock )
\r
1556 startBlock = this.fixBlock( true, blockTag );
\r
1557 endBlock = new CKEDITOR.dom.elementPath( this.endContainer ).block;
\r
1561 endBlock = this.fixBlock( false, blockTag );
\r
1564 // Get the range position.
\r
1565 var isStartOfBlock = startBlock && this.checkStartOfBlock(),
\r
1566 isEndOfBlock = endBlock && this.checkEndOfBlock();
\r
1568 // Delete the current contents.
\r
1569 // TODO: Why is 2.x doing CheckIsEmpty()?
\r
1570 this.deleteContents();
\r
1572 if ( startBlock && startBlock.equals( endBlock ) )
\r
1574 if ( isEndOfBlock )
\r
1576 elementPath = new CKEDITOR.dom.elementPath( this.startContainer );
\r
1577 this.moveToPosition( endBlock, CKEDITOR.POSITION_AFTER_END );
\r
1580 else if ( isStartOfBlock )
\r
1582 elementPath = new CKEDITOR.dom.elementPath( this.startContainer );
\r
1583 this.moveToPosition( startBlock, CKEDITOR.POSITION_BEFORE_START );
\r
1584 startBlock = null;
\r
1588 endBlock = this.splitElement( startBlock );
\r
1590 // In Gecko, the last child node must be a bogus <br>.
\r
1591 // Note: bogus <br> added under <ul> or <ol> would cause
\r
1592 // lists to be incorrectly rendered.
\r
1593 if ( !CKEDITOR.env.ie && !startBlock.is( 'ul', 'ol') )
\r
1594 startBlock.appendBogus() ;
\r
1599 previousBlock : startBlock,
\r
1600 nextBlock : endBlock,
\r
1601 wasStartOfBlock : isStartOfBlock,
\r
1602 wasEndOfBlock : isEndOfBlock,
\r
1603 elementPath : elementPath
\r
1608 * Branch the specified element from the collapsed range position and
\r
1609 * place the caret between the two result branches.
\r
1610 * Note: The range must be collapsed and been enclosed by this element.
\r
1611 * @param {CKEDITOR.dom.element} element
\r
1612 * @return {CKEDITOR.dom.element} Root element of the new branch after the split.
\r
1614 splitElement : function( toSplit )
\r
1616 if ( !this.collapsed )
\r
1619 // Extract the contents of the block from the selection point to the end
\r
1620 // of its contents.
\r
1621 this.setEndAt( toSplit, CKEDITOR.POSITION_BEFORE_END );
\r
1622 var documentFragment = this.extractContents();
\r
1624 // Duplicate the element after it.
\r
1625 var clone = toSplit.clone( false );
\r
1627 // Place the extracted contents into the duplicated element.
\r
1628 documentFragment.appendTo( clone );
\r
1629 clone.insertAfter( toSplit );
\r
1630 this.moveToPosition( toSplit, CKEDITOR.POSITION_AFTER_END );
\r
1635 * Check whether current range is on the inner edge of the specified element.
\r
1636 * @param {Number} checkType ( CKEDITOR.START | CKEDITOR.END ) The checking side.
\r
1637 * @param {CKEDITOR.dom.element} element The target element to check.
\r
1639 checkBoundaryOfElement : function( element, checkType )
\r
1641 var walkerRange = this.clone();
\r
1642 // Expand the range to element boundary.
\r
1643 walkerRange[ checkType == CKEDITOR.START ?
\r
1644 'setStartAt' : 'setEndAt' ]
\r
1645 ( element, checkType == CKEDITOR.START ?
\r
1646 CKEDITOR.POSITION_AFTER_START
\r
1647 : CKEDITOR.POSITION_BEFORE_END );
\r
1649 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1650 walker.evaluator = elementBoundaryEval;
\r
1651 return walker[ checkType == CKEDITOR.START ?
\r
1652 'checkBackward' : 'checkForward' ]();
\r
1655 // Calls to this function may produce changes to the DOM. The range may
\r
1656 // be updated to reflect such changes.
\r
1657 checkStartOfBlock : function()
\r
1659 var startContainer = this.startContainer,
\r
1660 startOffset = this.startOffset;
\r
1662 // If the starting node is a text node, and non-empty before the offset,
\r
1663 // then we're surely not at the start of block.
\r
1664 if ( startOffset && startContainer.type == CKEDITOR.NODE_TEXT )
\r
1666 var textBefore = CKEDITOR.tools.ltrim( startContainer.substring( 0, startOffset ) );
\r
1667 if ( textBefore.length )
\r
1671 // Antecipate the trim() call here, so the walker will not make
\r
1672 // changes to the DOM, which would not get reflected into this
\r
1673 // range otherwise.
\r
1676 // We need to grab the block element holding the start boundary, so
\r
1677 // let's use an element path for it.
\r
1678 var path = new CKEDITOR.dom.elementPath( this.startContainer );
\r
1680 // Creates a range starting at the block start until the range start.
\r
1681 var walkerRange = this.clone();
\r
1682 walkerRange.collapse( true );
\r
1683 walkerRange.setStartAt( path.block || path.blockLimit, CKEDITOR.POSITION_AFTER_START );
\r
1685 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1686 walker.evaluator = getCheckStartEndBlockEvalFunction( true );
\r
1688 return walker.checkBackward();
\r
1691 checkEndOfBlock : function()
\r
1693 var endContainer = this.endContainer,
\r
1694 endOffset = this.endOffset;
\r
1696 // If the ending node is a text node, and non-empty after the offset,
\r
1697 // then we're surely not at the end of block.
\r
1698 if ( endContainer.type == CKEDITOR.NODE_TEXT )
\r
1700 var textAfter = CKEDITOR.tools.rtrim( endContainer.substring( endOffset ) );
\r
1701 if ( textAfter.length )
\r
1705 // Antecipate the trim() call here, so the walker will not make
\r
1706 // changes to the DOM, which would not get reflected into this
\r
1707 // range otherwise.
\r
1710 // We need to grab the block element holding the start boundary, so
\r
1711 // let's use an element path for it.
\r
1712 var path = new CKEDITOR.dom.elementPath( this.endContainer );
\r
1714 // Creates a range starting at the block start until the range start.
\r
1715 var walkerRange = this.clone();
\r
1716 walkerRange.collapse( false );
\r
1717 walkerRange.setEndAt( path.block || path.blockLimit, CKEDITOR.POSITION_BEFORE_END );
\r
1719 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1720 walker.evaluator = getCheckStartEndBlockEvalFunction( false );
\r
1722 return walker.checkForward();
\r
1726 * Moves the range boundaries to the first/end editing point inside an
\r
1727 * element. For example, in an element tree like
\r
1728 * "<p><b><i></i></b> Text</p>", the start editing point is
\r
1729 * "<p><b><i>^</i></b> Text</p>" (inside <i>).
\r
1730 * @param {CKEDITOR.dom.element} el The element into which look for the
\r
1732 * @param {Boolean} isMoveToEnd Whether move to the end editable position.
\r
1734 moveToElementEditablePosition : function( el, isMoveToEnd )
\r
1738 // Empty elements are rejected.
\r
1739 if ( CKEDITOR.dtd.$empty[ el.getName() ] )
\r
1742 while ( el && el.type == CKEDITOR.NODE_ELEMENT )
\r
1744 isEditable = el.isEditable();
\r
1746 // If an editable element is found, move inside it.
\r
1748 this.moveToPosition( el, isMoveToEnd ?
\r
1749 CKEDITOR.POSITION_BEFORE_END :
\r
1750 CKEDITOR.POSITION_AFTER_START );
\r
1751 // Stop immediately if we've found a non editable inline element (e.g <img>).
\r
1752 else if ( CKEDITOR.dtd.$inline[ el.getName() ] )
\r
1754 this.moveToPosition( el, isMoveToEnd ?
\r
1755 CKEDITOR.POSITION_AFTER_END :
\r
1756 CKEDITOR.POSITION_BEFORE_START );
\r
1760 // Non-editable non-inline elements are to be bypassed, getting the next one.
\r
1761 if ( CKEDITOR.dtd.$empty[ el.getName() ] )
\r
1762 el = el[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );
\r
1764 el = el[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );
\r
1766 // Stop immediately if we've found a text node.
\r
1767 if ( el && el.type == CKEDITOR.NODE_TEXT )
\r
1769 this.moveToPosition( el, isMoveToEnd ?
\r
1770 CKEDITOR.POSITION_AFTER_END :
\r
1771 CKEDITOR.POSITION_BEFORE_START );
\r
1776 return isEditable;
\r
1780 *@see {CKEDITOR.dom.range.moveToElementEditablePosition}
\r
1782 moveToElementEditStart : function( target )
\r
1784 return this.moveToElementEditablePosition( target );
\r
1788 *@see {CKEDITOR.dom.range.moveToElementEditablePosition}
\r
1790 moveToElementEditEnd : function( target )
\r
1792 return this.moveToElementEditablePosition( target, true );
\r
1796 * Get the single node enclosed within the range if there's one.
\r
1798 getEnclosedNode : function()
\r
1800 var walkerRange = this.clone();
\r
1802 // Optimize and analyze the range to avoid DOM destructive nature of walker. (#5780)
\r
1803 walkerRange.optimize();
\r
1804 if ( walkerRange.startContainer.type != CKEDITOR.NODE_ELEMENT
\r
1805 || walkerRange.endContainer.type != CKEDITOR.NODE_ELEMENT )
\r
1808 var walker = new CKEDITOR.dom.walker( walkerRange ),
\r
1809 isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),
\r
1810 isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1811 evaluator = function( node )
\r
1813 return isNotWhitespaces( node ) && isNotBookmarks( node );
\r
1815 walkerRange.evaluator = evaluator;
\r
1816 var node = walker.next();
\r
1818 return node && node.equals( walker.previous() ) ? node : null;
\r
1821 getTouchedStartNode : function()
\r
1823 var container = this.startContainer ;
\r
1825 if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )
\r
1826 return container ;
\r
1828 return container.getChild( this.startOffset ) || container ;
\r
1831 getTouchedEndNode : function()
\r
1833 var container = this.endContainer ;
\r
1835 if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )
\r
1836 return container ;
\r
1838 return container.getChild( this.endOffset - 1 ) || container ;
\r
1843 CKEDITOR.POSITION_AFTER_START = 1; // <element>^contents</element> "^text"
\r
1844 CKEDITOR.POSITION_BEFORE_END = 2; // <element>contents^</element> "text^"
\r
1845 CKEDITOR.POSITION_BEFORE_START = 3; // ^<element>contents</element> ^"text"
\r
1846 CKEDITOR.POSITION_AFTER_END = 4; // <element>contents</element>^ "text"
\r
1848 CKEDITOR.ENLARGE_ELEMENT = 1;
\r
1849 CKEDITOR.ENLARGE_BLOCK_CONTENTS = 2;
\r
1850 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;
\r
1853 * Check boundary types.
\r
1854 * @see CKEDITOR.dom.range.prototype.checkBoundaryOfElement
\r
1856 CKEDITOR.START = 1;
\r
1858 CKEDITOR.STARTEND = 3;
\r
1861 * Shrink range types.
\r
1862 * @see CKEDITOR.dom.range.prototype.shrink
\r
1864 CKEDITOR.SHRINK_ELEMENT = 1;
\r
1865 CKEDITOR.SHRINK_TEXT = 2;
\r