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're 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
1170 defaultGuard = 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 = defaultGuard( 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 at different position by comparing
\r
1197 // the document position of it with 'enlargeable' node.
\r
1200 !blockBoundary.is( 'br' ) &&
\r
1201 ( !enlargeable && this.checkStartOfBlock()
\r
1202 || enlargeable && blockBoundary.contains( enlargeable ) ) ?
\r
1203 CKEDITOR.POSITION_AFTER_START :
\r
1204 CKEDITOR.POSITION_AFTER_END );
\r
1206 // Enlarging the end boundary.
\r
1207 walkerRange = this.clone();
\r
1208 walkerRange.collapse();
\r
1209 walkerRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
\r
1210 walker = new CKEDITOR.dom.walker( walkerRange );
\r
1212 // tailBrGuard only used for on range end.
\r
1213 walker.guard = ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ?
\r
1214 tailBrGuard : boundaryGuard;
\r
1215 blockBoundary = null;
\r
1216 // End the range right before the block boundary node.
\r
1218 enlargeable = walker.lastForward();
\r
1220 // It's the body which stop the enlarging if no block boundary found.
\r
1221 blockBoundary = blockBoundary || body;
\r
1223 // Start the range at different position by comparing
\r
1224 // the document position of it with 'enlargeable' node.
\r
1227 ( !enlargeable && this.checkEndOfBlock()
\r
1228 || enlargeable && blockBoundary.contains( enlargeable ) ) ?
\r
1229 CKEDITOR.POSITION_BEFORE_END :
\r
1230 CKEDITOR.POSITION_BEFORE_START );
\r
1231 // We must include the <br> at the end of range if there's
\r
1232 // one and we're expanding list item contents
\r
1234 this.setEndAfter( tailBr );
\r
1239 * Descrease the range to make sure that boundaries
\r
1240 * always anchor beside text nodes or innermost element.
\r
1241 * @param {Number} mode ( CKEDITOR.SHRINK_ELEMENT | CKEDITOR.SHRINK_TEXT ) The shrinking mode.
\r
1243 shrink : function( mode, selectContents )
\r
1245 // Unable to shrink a collapsed range.
\r
1246 if ( !this.collapsed )
\r
1248 mode = mode || CKEDITOR.SHRINK_TEXT;
\r
1250 var walkerRange = this.clone();
\r
1252 var startContainer = this.startContainer,
\r
1253 endContainer = this.endContainer,
\r
1254 startOffset = this.startOffset,
\r
1255 endOffset = this.endOffset,
\r
1256 collapsed = this.collapsed;
\r
1258 // Whether the start/end boundary is moveable.
\r
1259 var moveStart = 1,
\r
1262 if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )
\r
1264 if ( !startOffset )
\r
1265 walkerRange.setStartBefore( startContainer );
\r
1266 else if ( startOffset >= startContainer.getLength( ) )
\r
1267 walkerRange.setStartAfter( startContainer );
\r
1270 // Enlarge the range properly to avoid walker making
\r
1271 // DOM changes caused by triming the text nodes later.
\r
1272 walkerRange.setStartBefore( startContainer );
\r
1277 if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )
\r
1280 walkerRange.setEndBefore( endContainer );
\r
1281 else if ( endOffset >= endContainer.getLength( ) )
\r
1282 walkerRange.setEndAfter( endContainer );
\r
1285 walkerRange.setEndAfter( endContainer );
\r
1290 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1292 walker.evaluator = function( node )
\r
1294 return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ?
\r
1295 CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );
\r
1298 var currentElement;
\r
1299 walker.guard = function( node, movingOut )
\r
1301 // Stop when we're shrink in element mode while encountering a text node.
\r
1302 if ( mode == CKEDITOR.SHRINK_ELEMENT && node.type == CKEDITOR.NODE_TEXT )
\r
1305 // Stop when we've already walked "through" an element.
\r
1306 if ( movingOut && node.equals( currentElement ) )
\r
1309 if ( !movingOut && node.type == CKEDITOR.NODE_ELEMENT )
\r
1310 currentElement = node;
\r
1317 var textStart = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastForward' : 'next']();
\r
1318 textStart && this.setStartAt( textStart, selectContents ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_START );
\r
1324 var textEnd = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastBackward' : 'previous']();
\r
1325 textEnd && this.setEndAt( textEnd, selectContents ? CKEDITOR.POSITION_BEFORE_END : CKEDITOR.POSITION_AFTER_END );
\r
1328 return !!( moveStart || moveEnd );
\r
1333 * Inserts a node at the start of the range. The range will be expanded
\r
1334 * the contain the node.
\r
1336 insertNode : function( node )
\r
1338 this.optimizeBookmark();
\r
1339 this.trim( false, true );
\r
1341 var startContainer = this.startContainer;
\r
1342 var startOffset = this.startOffset;
\r
1344 var nextNode = startContainer.getChild( startOffset );
\r
1347 node.insertBefore( nextNode );
\r
1349 startContainer.append( node );
\r
1351 // Check if we need to update the end boundary.
\r
1352 if ( node.getParent().equals( this.endContainer ) )
\r
1355 // Expand the range to embrace the new node.
\r
1356 this.setStartBefore( node );
\r
1359 moveToPosition : function( node, position )
\r
1361 this.setStartAt( node, position );
\r
1362 this.collapse( true );
\r
1365 selectNodeContents : function( node )
\r
1367 this.setStart( node, 0 );
\r
1368 this.setEnd( node, node.type == CKEDITOR.NODE_TEXT ? node.getLength() : node.getChildCount() );
\r
1372 * Sets the start position of a Range.
\r
1373 * @param {CKEDITOR.dom.node} startNode The node to start the range.
\r
1374 * @param {Number} startOffset An integer greater than or equal to zero
\r
1375 * representing the offset for the start of the range from the start
\r
1378 setStart : function( startNode, startOffset )
\r
1380 // W3C requires a check for the new position. If it is after the end
\r
1381 // boundary, the range should be collapsed to the new start. It seams
\r
1382 // we will not need this check for our use of this class so we can
\r
1383 // ignore it for now.
\r
1385 // Fixing invalid range start inside dtd empty elements.
\r
1386 if( startNode.type == CKEDITOR.NODE_ELEMENT
\r
1387 && CKEDITOR.dtd.$empty[ startNode.getName() ] )
\r
1388 startNode = startNode.getParent(), startOffset = startNode.getIndex();
\r
1390 this.startContainer = startNode;
\r
1391 this.startOffset = startOffset;
\r
1393 if ( !this.endContainer )
\r
1395 this.endContainer = startNode;
\r
1396 this.endOffset = startOffset;
\r
1399 updateCollapsed( this );
\r
1403 * Sets the end position of a Range.
\r
1404 * @param {CKEDITOR.dom.node} endNode The node to end the range.
\r
1405 * @param {Number} endOffset An integer greater than or equal to zero
\r
1406 * representing the offset for the end of the range from the start
\r
1409 setEnd : function( endNode, endOffset )
\r
1411 // W3C requires a check for the new position. If it is before the start
\r
1412 // boundary, the range should be collapsed to the new end. It seams we
\r
1413 // will not need this check for our use of this class so we can ignore
\r
1416 // Fixing invalid range end inside dtd empty elements.
\r
1417 if( endNode.type == CKEDITOR.NODE_ELEMENT
\r
1418 && CKEDITOR.dtd.$empty[ endNode.getName() ] )
\r
1419 endNode = endNode.getParent(), endOffset = endNode.getIndex() + 1;
\r
1421 this.endContainer = endNode;
\r
1422 this.endOffset = endOffset;
\r
1424 if ( !this.startContainer )
\r
1426 this.startContainer = endNode;
\r
1427 this.startOffset = endOffset;
\r
1430 updateCollapsed( this );
\r
1433 setStartAfter : function( node )
\r
1435 this.setStart( node.getParent(), node.getIndex() + 1 );
\r
1438 setStartBefore : function( node )
\r
1440 this.setStart( node.getParent(), node.getIndex() );
\r
1443 setEndAfter : function( node )
\r
1445 this.setEnd( node.getParent(), node.getIndex() + 1 );
\r
1448 setEndBefore : function( node )
\r
1450 this.setEnd( node.getParent(), node.getIndex() );
\r
1453 setStartAt : function( node, position )
\r
1455 switch( position )
\r
1457 case CKEDITOR.POSITION_AFTER_START :
\r
1458 this.setStart( node, 0 );
\r
1461 case CKEDITOR.POSITION_BEFORE_END :
\r
1462 if ( node.type == CKEDITOR.NODE_TEXT )
\r
1463 this.setStart( node, node.getLength() );
\r
1465 this.setStart( node, node.getChildCount() );
\r
1468 case CKEDITOR.POSITION_BEFORE_START :
\r
1469 this.setStartBefore( node );
\r
1472 case CKEDITOR.POSITION_AFTER_END :
\r
1473 this.setStartAfter( node );
\r
1476 updateCollapsed( this );
\r
1479 setEndAt : function( node, position )
\r
1481 switch( position )
\r
1483 case CKEDITOR.POSITION_AFTER_START :
\r
1484 this.setEnd( node, 0 );
\r
1487 case CKEDITOR.POSITION_BEFORE_END :
\r
1488 if ( node.type == CKEDITOR.NODE_TEXT )
\r
1489 this.setEnd( node, node.getLength() );
\r
1491 this.setEnd( node, node.getChildCount() );
\r
1494 case CKEDITOR.POSITION_BEFORE_START :
\r
1495 this.setEndBefore( node );
\r
1498 case CKEDITOR.POSITION_AFTER_END :
\r
1499 this.setEndAfter( node );
\r
1502 updateCollapsed( this );
\r
1505 fixBlock : function( isStart, blockTag )
\r
1507 var bookmark = this.createBookmark(),
\r
1508 fixedBlock = this.document.createElement( blockTag );
\r
1510 this.collapse( isStart );
\r
1512 this.enlarge( CKEDITOR.ENLARGE_BLOCK_CONTENTS );
\r
1514 this.extractContents().appendTo( fixedBlock );
\r
1515 fixedBlock.trim();
\r
1517 if ( !CKEDITOR.env.ie )
\r
1518 fixedBlock.appendBogus();
\r
1520 this.insertNode( fixedBlock );
\r
1522 this.moveToBookmark( bookmark );
\r
1524 return fixedBlock;
\r
1527 splitBlock : function( blockTag )
\r
1529 var startPath = new CKEDITOR.dom.elementPath( this.startContainer ),
\r
1530 endPath = new CKEDITOR.dom.elementPath( this.endContainer );
\r
1532 var startBlockLimit = startPath.blockLimit,
\r
1533 endBlockLimit = endPath.blockLimit;
\r
1535 var startBlock = startPath.block,
\r
1536 endBlock = endPath.block;
\r
1538 var elementPath = null;
\r
1539 // Do nothing if the boundaries are in different block limits.
\r
1540 if ( !startBlockLimit.equals( endBlockLimit ) )
\r
1543 // Get or fix current blocks.
\r
1544 if ( blockTag != 'br' )
\r
1546 if ( !startBlock )
\r
1548 startBlock = this.fixBlock( true, blockTag );
\r
1549 endBlock = new CKEDITOR.dom.elementPath( this.endContainer ).block;
\r
1553 endBlock = this.fixBlock( false, blockTag );
\r
1556 // Get the range position.
\r
1557 var isStartOfBlock = startBlock && this.checkStartOfBlock(),
\r
1558 isEndOfBlock = endBlock && this.checkEndOfBlock();
\r
1560 // Delete the current contents.
\r
1561 // TODO: Why is 2.x doing CheckIsEmpty()?
\r
1562 this.deleteContents();
\r
1564 if ( startBlock && startBlock.equals( endBlock ) )
\r
1566 if ( isEndOfBlock )
\r
1568 elementPath = new CKEDITOR.dom.elementPath( this.startContainer );
\r
1569 this.moveToPosition( endBlock, CKEDITOR.POSITION_AFTER_END );
\r
1572 else if ( isStartOfBlock )
\r
1574 elementPath = new CKEDITOR.dom.elementPath( this.startContainer );
\r
1575 this.moveToPosition( startBlock, CKEDITOR.POSITION_BEFORE_START );
\r
1576 startBlock = null;
\r
1580 endBlock = this.splitElement( startBlock );
\r
1582 // In Gecko, the last child node must be a bogus <br>.
\r
1583 // Note: bogus <br> added under <ul> or <ol> would cause
\r
1584 // lists to be incorrectly rendered.
\r
1585 if ( !CKEDITOR.env.ie && !startBlock.is( 'ul', 'ol') )
\r
1586 startBlock.appendBogus() ;
\r
1591 previousBlock : startBlock,
\r
1592 nextBlock : endBlock,
\r
1593 wasStartOfBlock : isStartOfBlock,
\r
1594 wasEndOfBlock : isEndOfBlock,
\r
1595 elementPath : elementPath
\r
1600 * Branch the specified element from the collapsed range position and
\r
1601 * place the caret between the two result branches.
\r
1602 * Note: The range must be collapsed and been enclosed by this element.
\r
1603 * @param {CKEDITOR.dom.element} element
\r
1604 * @return {CKEDITOR.dom.element} Root element of the new branch after the split.
\r
1606 splitElement : function( toSplit )
\r
1608 if ( !this.collapsed )
\r
1611 // Extract the contents of the block from the selection point to the end
\r
1612 // of its contents.
\r
1613 this.setEndAt( toSplit, CKEDITOR.POSITION_BEFORE_END );
\r
1614 var documentFragment = this.extractContents();
\r
1616 // Duplicate the element after it.
\r
1617 var clone = toSplit.clone( false );
\r
1619 // Place the extracted contents into the duplicated element.
\r
1620 documentFragment.appendTo( clone );
\r
1621 clone.insertAfter( toSplit );
\r
1622 this.moveToPosition( toSplit, CKEDITOR.POSITION_AFTER_END );
\r
1627 * Check whether current range is on the inner edge of the specified element.
\r
1628 * @param {Number} checkType ( CKEDITOR.START | CKEDITOR.END ) The checking side.
\r
1629 * @param {CKEDITOR.dom.element} element The target element to check.
\r
1631 checkBoundaryOfElement : function( element, checkType )
\r
1633 var walkerRange = this.clone();
\r
1634 // Expand the range to element boundary.
\r
1635 walkerRange[ checkType == CKEDITOR.START ?
\r
1636 'setStartAt' : 'setEndAt' ]
\r
1637 ( element, checkType == CKEDITOR.START ?
\r
1638 CKEDITOR.POSITION_AFTER_START
\r
1639 : CKEDITOR.POSITION_BEFORE_END );
\r
1641 var walker = new CKEDITOR.dom.walker( walkerRange ),
\r
1643 walker.evaluator = elementBoundaryEval;
\r
1644 return walker[ checkType == CKEDITOR.START ?
\r
1645 'checkBackward' : 'checkForward' ]();
\r
1647 // Calls to this function may produce changes to the DOM. The range may
\r
1648 // be updated to reflect such changes.
\r
1649 checkStartOfBlock : function()
\r
1651 var startContainer = this.startContainer,
\r
1652 startOffset = this.startOffset;
\r
1654 // If the starting node is a text node, and non-empty before the offset,
\r
1655 // then we're surely not at the start of block.
\r
1656 if ( startOffset && startContainer.type == CKEDITOR.NODE_TEXT )
\r
1658 var textBefore = CKEDITOR.tools.ltrim( startContainer.substring( 0, startOffset ) );
\r
1659 if ( textBefore.length )
\r
1663 // Antecipate the trim() call here, so the walker will not make
\r
1664 // changes to the DOM, which would not get reflected into this
\r
1665 // range otherwise.
\r
1668 // We need to grab the block element holding the start boundary, so
\r
1669 // let's use an element path for it.
\r
1670 var path = new CKEDITOR.dom.elementPath( this.startContainer );
\r
1672 // Creates a range starting at the block start until the range start.
\r
1673 var walkerRange = this.clone();
\r
1674 walkerRange.collapse( true );
\r
1675 walkerRange.setStartAt( path.block || path.blockLimit, CKEDITOR.POSITION_AFTER_START );
\r
1677 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1678 walker.evaluator = getCheckStartEndBlockEvalFunction( true );
\r
1680 return walker.checkBackward();
\r
1683 checkEndOfBlock : function()
\r
1685 var endContainer = this.endContainer,
\r
1686 endOffset = this.endOffset;
\r
1688 // If the ending node is a text node, and non-empty after the offset,
\r
1689 // then we're surely not at the end of block.
\r
1690 if ( endContainer.type == CKEDITOR.NODE_TEXT )
\r
1692 var textAfter = CKEDITOR.tools.rtrim( endContainer.substring( endOffset ) );
\r
1693 if ( textAfter.length )
\r
1697 // Antecipate the trim() call here, so the walker will not make
\r
1698 // changes to the DOM, which would not get reflected into this
\r
1699 // range otherwise.
\r
1702 // We need to grab the block element holding the start boundary, so
\r
1703 // let's use an element path for it.
\r
1704 var path = new CKEDITOR.dom.elementPath( this.endContainer );
\r
1706 // Creates a range starting at the block start until the range start.
\r
1707 var walkerRange = this.clone();
\r
1708 walkerRange.collapse( false );
\r
1709 walkerRange.setEndAt( path.block || path.blockLimit, CKEDITOR.POSITION_BEFORE_END );
\r
1711 var walker = new CKEDITOR.dom.walker( walkerRange );
\r
1712 walker.evaluator = getCheckStartEndBlockEvalFunction( false );
\r
1714 return walker.checkForward();
\r
1718 * Moves the range boundaries to the first/end editing point inside an
\r
1719 * element. For example, in an element tree like
\r
1720 * "<p><b><i></i></b> Text</p>", the start editing point is
\r
1721 * "<p><b><i>^</i></b> Text</p>" (inside <i>).
\r
1722 * @param {CKEDITOR.dom.element} el The element into which look for the
\r
1724 * @param {Boolean} isMoveToEnd Whether move to the end editable position.
\r
1726 moveToElementEditablePosition : function( el, isMoveToEnd )
\r
1730 // Empty elements are rejected.
\r
1731 if ( CKEDITOR.dtd.$empty[ el.getName() ] )
\r
1734 while ( el && el.type == CKEDITOR.NODE_ELEMENT )
\r
1736 isEditable = el.isEditable();
\r
1738 // If an editable element is found, move inside it.
\r
1740 this.moveToPosition( el, isMoveToEnd ?
\r
1741 CKEDITOR.POSITION_BEFORE_END :
\r
1742 CKEDITOR.POSITION_AFTER_START );
\r
1743 // Stop immediately if we've found a non editable inline element (e.g <img>).
\r
1744 else if ( CKEDITOR.dtd.$inline[ el.getName() ] )
\r
1746 this.moveToPosition( el, isMoveToEnd ?
\r
1747 CKEDITOR.POSITION_AFTER_END :
\r
1748 CKEDITOR.POSITION_BEFORE_START );
\r
1752 // Non-editable non-inline elements are to be bypassed, getting the next one.
\r
1753 if ( CKEDITOR.dtd.$empty[ el.getName() ] )
\r
1754 el = el[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );
\r
1756 el = el[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );
\r
1758 // Stop immediately if we've found a text node.
\r
1759 if ( el && el.type == CKEDITOR.NODE_TEXT )
\r
1761 this.moveToPosition( el, isMoveToEnd ?
\r
1762 CKEDITOR.POSITION_AFTER_END :
\r
1763 CKEDITOR.POSITION_BEFORE_START );
\r
1768 return isEditable;
\r
1772 *@see {CKEDITOR.dom.range.moveToElementEditablePosition}
\r
1774 moveToElementEditStart : function( target )
\r
1776 return this.moveToElementEditablePosition( target );
\r
1780 *@see {CKEDITOR.dom.range.moveToElementEditablePosition}
\r
1782 moveToElementEditEnd : function( target )
\r
1784 return this.moveToElementEditablePosition( target, true );
\r
1788 * Get the single node enclosed within the range if there's one.
\r
1790 getEnclosedNode : function()
\r
1792 var walkerRange = this.clone(),
\r
1793 walker = new CKEDITOR.dom.walker( walkerRange ),
\r
1794 isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),
\r
1795 isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),
\r
1796 evaluator = function( node )
\r
1798 return isNotWhitespaces( node ) && isNotBookmarks( node );
\r
1800 walkerRange.evaluator = evaluator;
\r
1801 var node = walker.next();
\r
1803 return node && node.equals( walker.previous() ) ? node : null;
\r
1806 getTouchedStartNode : function()
\r
1808 var container = this.startContainer ;
\r
1810 if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )
\r
1811 return container ;
\r
1813 return container.getChild( this.startOffset ) || container ;
\r
1816 getTouchedEndNode : function()
\r
1818 var container = this.endContainer ;
\r
1820 if ( this.collapsed || container.type != CKEDITOR.NODE_ELEMENT )
\r
1821 return container ;
\r
1823 return container.getChild( this.endOffset - 1 ) || container ;
\r
1828 CKEDITOR.POSITION_AFTER_START = 1; // <element>^contents</element> "^text"
\r
1829 CKEDITOR.POSITION_BEFORE_END = 2; // <element>contents^</element> "text^"
\r
1830 CKEDITOR.POSITION_BEFORE_START = 3; // ^<element>contents</element> ^"text"
\r
1831 CKEDITOR.POSITION_AFTER_END = 4; // <element>contents</element>^ "text"
\r
1833 CKEDITOR.ENLARGE_ELEMENT = 1;
\r
1834 CKEDITOR.ENLARGE_BLOCK_CONTENTS = 2;
\r
1835 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;
\r
1838 * Check boundary types.
\r
1839 * @see CKEDITOR.dom.range::checkBoundaryOfElement
\r
1841 CKEDITOR.START = 1;
\r
1843 CKEDITOR.STARTEND = 3;
\r
1845 CKEDITOR.SHRINK_ELEMENT = 1;
\r
1846 CKEDITOR.SHRINK_TEXT = 2;
\r