2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
8 function nonEmptyText( node )
\r
10 return ( node.type == CKEDITOR.NODE_TEXT && node.getLength() > 0 );
\r
14 * Elements which break characters been considered as sequence.
\r
16 function nonCharactersBoundary ( node )
\r
18 return !( node.type == CKEDITOR.NODE_ELEMENT && node.isBlockBoundary(
\r
19 CKEDITOR.tools.extend( {}, CKEDITOR.dtd.$empty, CKEDITOR.dtd.$nonEditable ) ) );
\r
23 * Get the cursor object which represent both current character and it's dom
\r
26 var cursorStep = function()
\r
29 textNode : this.textNode,
\r
30 offset : this.offset,
\r
31 character : this.textNode ?
\r
32 this.textNode.getText().charAt( this.offset ) : null,
\r
33 hitMatchBoundary : this._.matchBoundary
\r
37 var pages = [ 'find', 'replace' ],
\r
39 [ 'txtFindFind', 'txtFindReplace' ],
\r
40 [ 'txtFindCaseChk', 'txtReplaceCaseChk' ],
\r
41 [ 'txtFindWordChk', 'txtReplaceWordChk' ],
\r
42 [ 'txtFindCyclic', 'txtReplaceCyclic' ] ];
\r
45 * Synchronize corresponding filed values between 'replace' and 'find' pages.
\r
46 * @param {String} currentPageId The page id which receive values.
\r
48 function syncFieldsBetweenTabs( currentPageId )
\r
50 var sourceIndex, targetIndex,
\r
51 sourceField, targetField;
\r
53 sourceIndex = currentPageId === 'find' ? 1 : 0;
\r
54 targetIndex = 1 - sourceIndex;
\r
55 var i, l = fieldsMapping.length;
\r
56 for ( i = 0 ; i < l ; i++ )
\r
58 sourceField = this.getContentElement( pages[ sourceIndex ],
\r
59 fieldsMapping[ i ][ sourceIndex ] );
\r
60 targetField = this.getContentElement( pages[ targetIndex ],
\r
61 fieldsMapping[ i ][ targetIndex ] );
\r
63 targetField.setValue( sourceField.getValue() );
\r
67 var findDialog = function( editor, startupPage )
\r
69 // Style object for highlights: (#5018)
\r
70 // 1. Defined as full match style to avoid compromising ordinary text color styles.
\r
71 // 2. Must be apply onto inner-most text to avoid conflicting with ordinary text color styles visually.
\r
72 var highlightStyle = new CKEDITOR.style( CKEDITOR.tools.extend( { fullMatch : true, childRule : function(){ return false; } },
\r
73 editor.config.find_highlight ) );
\r
76 * Iterator which walk through the specified range char by char. By
\r
77 * default the walking will not stop at the character boundaries, until
\r
78 * the end of the range is encountered.
\r
79 * @param { CKEDITOR.dom.range } range
\r
80 * @param {Boolean} matchWord Whether the walking will stop at character boundary.
\r
82 var characterWalker = function( range , matchWord )
\r
85 new CKEDITOR.dom.walker( range );
\r
86 walker.guard = matchWord ? nonCharactersBoundary : null;
\r
87 walker[ 'evaluator' ] = nonEmptyText;
\r
88 walker.breakOnFalse = true;
\r
91 matchWord : matchWord,
\r
93 matchBoundary : false
\r
97 characterWalker.prototype = {
\r
100 return this.move();
\r
105 return this.move( true );
\r
108 move : function( rtl )
\r
110 var currentTextNode = this.textNode;
\r
111 // Already at the end of document, no more character available.
\r
112 if ( currentTextNode === null )
\r
113 return cursorStep.call( this );
\r
115 this._.matchBoundary = false;
\r
117 // There are more characters in the text node, step forward.
\r
118 if ( currentTextNode
\r
120 && this.offset > 0 )
\r
123 return cursorStep.call( this );
\r
125 else if ( currentTextNode
\r
126 && this.offset < currentTextNode.getLength() - 1 )
\r
129 return cursorStep.call( this );
\r
133 currentTextNode = null;
\r
134 // At the end of the text node, walking foward for the next.
\r
135 while ( !currentTextNode )
\r
138 this._.walker[ rtl ? 'previous' : 'next' ].call( this._.walker );
\r
140 // Stop searching if we're need full word match OR
\r
141 // already reach document end.
\r
142 if ( this._.matchWord && !currentTextNode
\r
143 ||this._.walker._.end )
\r
146 // Marking as match character boundaries.
\r
147 if ( !currentTextNode
\r
148 && !nonCharactersBoundary( this._.walker.current ) )
\r
149 this._.matchBoundary = true;
\r
152 // Found a fresh text node.
\r
153 this.textNode = currentTextNode;
\r
154 if ( currentTextNode )
\r
155 this.offset = rtl ? currentTextNode.getLength() - 1 : 0;
\r
160 return cursorStep.call( this );
\r
166 * A range of cursors which represent a trunk of characters which try to
\r
167 * match, it has the same length as the pattern string.
\r
169 var characterRange = function( characterWalker, rangeLength )
\r
172 walker : characterWalker,
\r
174 rangeLength : rangeLength,
\r
175 highlightRange : null,
\r
180 characterRange.prototype = {
\r
182 * Translate this range to {@link CKEDITOR.dom.range}
\r
184 toDomRange : function()
\r
186 var range = new CKEDITOR.dom.range( editor.document );
\r
187 var cursors = this._.cursors;
\r
188 if ( cursors.length < 1 )
\r
190 var textNode = this._.walker.textNode;
\r
192 range.setStartAfter( textNode );
\r
198 var first = cursors[0],
\r
199 last = cursors[ cursors.length - 1 ];
\r
201 range.setStart( first.textNode, first.offset );
\r
202 range.setEnd( last.textNode, last.offset + 1 );
\r
208 * Reflect the latest changes from dom range.
\r
210 updateFromDomRange : function( domRange )
\r
213 walker = new characterWalker( domRange );
\r
214 this._.cursors = [];
\r
217 cursor = walker.next();
\r
218 if ( cursor.character )
\r
219 this._.cursors.push( cursor );
\r
221 while ( cursor.character );
\r
222 this._.rangeLength = this._.cursors.length;
\r
225 setMatched : function()
\r
227 this._.isMatched = true;
\r
230 clearMatched : function()
\r
232 this._.isMatched = false;
\r
235 isMatched : function()
\r
237 return this._.isMatched;
\r
241 * Hightlight the current matched chunk of text.
\r
243 highlight : function()
\r
245 // Do not apply if nothing is found.
\r
246 if ( this._.cursors.length < 1 )
\r
249 // Remove the previous highlight if there's one.
\r
250 if ( this._.highlightRange )
\r
251 this.removeHighlight();
\r
253 // Apply the highlight.
\r
254 var range = this.toDomRange();
\r
255 highlightStyle.applyToRange( range );
\r
256 this._.highlightRange = range;
\r
258 // Scroll the editor to the highlighted area.
\r
259 var element = range.startContainer;
\r
260 if ( element.type != CKEDITOR.NODE_ELEMENT )
\r
261 element = element.getParent();
\r
262 element.scrollIntoView();
\r
264 // Update the character cursors.
\r
265 this.updateFromDomRange( range );
\r
269 * Remove highlighted find result.
\r
271 removeHighlight : function()
\r
273 if ( !this._.highlightRange )
\r
276 highlightStyle.removeFromRange( this._.highlightRange );
\r
277 this.updateFromDomRange( this._.highlightRange );
\r
278 this._.highlightRange = null;
\r
281 moveBack : function()
\r
283 var retval = this._.walker.back(),
\r
284 cursors = this._.cursors;
\r
286 if ( retval.hitMatchBoundary )
\r
287 this._.cursors = cursors = [];
\r
289 cursors.unshift( retval );
\r
290 if ( cursors.length > this._.rangeLength )
\r
296 moveNext : function()
\r
298 var retval = this._.walker.next(),
\r
299 cursors = this._.cursors;
\r
301 // Clear the cursors queue if we've crossed a match boundary.
\r
302 if ( retval.hitMatchBoundary )
\r
303 this._.cursors = cursors = [];
\r
305 cursors.push( retval );
\r
306 if ( cursors.length > this._.rangeLength )
\r
312 getEndCharacter : function()
\r
314 var cursors = this._.cursors;
\r
315 if ( cursors.length < 1 )
\r
318 return cursors[ cursors.length - 1 ].character;
\r
321 getNextCharacterRange : function( maxLength )
\r
325 cursors = this._.cursors;
\r
327 if ( ( lastCursor = cursors[ cursors.length - 1 ] ) && lastCursor.textNode )
\r
328 nextRangeWalker = new characterWalker( getRangeAfterCursor( lastCursor ) );
\r
329 // In case it's an empty range (no cursors), figure out next range from walker (#4951).
\r
331 nextRangeWalker = this._.walker;
\r
333 return new characterRange( nextRangeWalker, maxLength );
\r
336 getCursors : function()
\r
338 return this._.cursors;
\r
343 // The remaining document range after the character cursor.
\r
344 function getRangeAfterCursor( cursor , inclusive )
\r
346 var range = new CKEDITOR.dom.range();
\r
347 range.setStart( cursor.textNode,
\r
348 ( inclusive ? cursor.offset : cursor.offset + 1 ) );
\r
349 range.setEndAt( editor.document.getBody(),
\r
350 CKEDITOR.POSITION_BEFORE_END );
\r
354 // The document range before the character cursor.
\r
355 function getRangeBeforeCursor( cursor )
\r
357 var range = new CKEDITOR.dom.range();
\r
358 range.setStartAt( editor.document.getBody(),
\r
359 CKEDITOR.POSITION_AFTER_START );
\r
360 range.setEnd( cursor.textNode, cursor.offset );
\r
364 var KMP_NOMATCH = 0,
\r
368 * Examination the occurrence of a word which implement KMP algorithm.
\r
370 var kmpMatcher = function( pattern, ignoreCase )
\r
372 var overlap = [ -1 ];
\r
374 pattern = pattern.toLowerCase();
\r
375 for ( var i = 0 ; i < pattern.length ; i++ )
\r
377 overlap.push( overlap[i] + 1 );
\r
378 while ( overlap[ i + 1 ] > 0
\r
379 && pattern.charAt( i ) != pattern
\r
380 .charAt( overlap[ i + 1 ] - 1 ) )
\r
381 overlap[ i + 1 ] = overlap[ overlap[ i + 1 ] - 1 ] + 1;
\r
387 ignoreCase : !!ignoreCase,
\r
392 kmpMatcher.prototype =
\r
394 feedCharacter : function( c )
\r
396 if ( this._.ignoreCase )
\r
397 c = c.toLowerCase();
\r
401 if ( c == this._.pattern.charAt( this._.state ) )
\r
404 if ( this._.state == this._.pattern.length )
\r
407 return KMP_MATCHED;
\r
409 return KMP_ADVANCED;
\r
411 else if ( !this._.state )
\r
412 return KMP_NOMATCH;
\r
414 this._.state = this._.overlap[ this._.state ];
\r
426 var wordSeparatorRegex =
\r
427 /[.,"'?!;: \u0085\u00a0\u1680\u280e\u2028\u2029\u202f\u205f\u3000]/;
\r
429 var isWordSeparator = function( c )
\r
433 var code = c.charCodeAt( 0 );
\r
434 return ( code >= 9 && code <= 0xd )
\r
435 || ( code >= 0x2000 && code <= 0x200a )
\r
436 || wordSeparatorRegex.test( c );
\r
440 searchRange : null,
\r
442 find : function( pattern, matchCase, matchWord, matchCyclic, highlightMatched, cyclicRerun )
\r
444 if ( !this.matchRange )
\r
446 new characterRange(
\r
447 new characterWalker( this.searchRange ),
\r
451 this.matchRange.removeHighlight();
\r
452 this.matchRange = this.matchRange.getNextCharacterRange( pattern.length );
\r
455 var matcher = new kmpMatcher( pattern, !matchCase ),
\r
456 matchState = KMP_NOMATCH,
\r
459 while ( character !== null )
\r
461 this.matchRange.moveNext();
\r
462 while ( ( character = this.matchRange.getEndCharacter() ) )
\r
464 matchState = matcher.feedCharacter( character );
\r
465 if ( matchState == KMP_MATCHED )
\r
467 if ( this.matchRange.moveNext().hitMatchBoundary )
\r
471 if ( matchState == KMP_MATCHED )
\r
475 var cursors = this.matchRange.getCursors(),
\r
476 tail = cursors[ cursors.length - 1 ],
\r
477 head = cursors[ 0 ];
\r
479 var headWalker = new characterWalker( getRangeBeforeCursor( head ), true ),
\r
480 tailWalker = new characterWalker( getRangeAfterCursor( tail ), true );
\r
482 if ( ! ( isWordSeparator( headWalker.back().character )
\r
483 && isWordSeparator( tailWalker.next().character ) ) )
\r
486 this.matchRange.setMatched();
\r
487 if ( highlightMatched !== false )
\r
488 this.matchRange.highlight();
\r
493 this.matchRange.clearMatched();
\r
494 this.matchRange.removeHighlight();
\r
495 // Clear current session and restart with the default search
\r
497 // Re-run the finding once for cyclic.(#3517)
\r
498 if ( matchCyclic && !cyclicRerun )
\r
500 this.searchRange = getSearchRange( true );
\r
501 this.matchRange = null;
\r
502 return arguments.callee.apply( this,
\r
503 Array.prototype.slice.call( arguments ).concat( [ true ] ) );
\r
510 * Record how much replacement occurred toward one replacing.
\r
512 replaceCounter : 0,
\r
514 replace : function( dialog, pattern, newString, matchCase, matchWord,
\r
515 matchCyclic , isReplaceAll )
\r
517 // Successiveness of current replace/find.
\r
518 var result = false;
\r
520 // 1. Perform the replace when there's already a match here.
\r
521 // 2. Otherwise perform the find but don't replace it immediately.
\r
522 if ( this.matchRange && this.matchRange.isMatched()
\r
523 && !this.matchRange._.isReplaced )
\r
525 // Turn off highlight for a while when saving snapshots.
\r
526 this.matchRange.removeHighlight();
\r
527 var domRange = this.matchRange.toDomRange();
\r
528 var text = editor.document.createText( newString );
\r
529 if ( !isReplaceAll )
\r
531 // Save undo snaps before and after the replacement.
\r
532 var selection = editor.getSelection();
\r
533 selection.selectRanges( [ domRange ] );
\r
534 editor.fire( 'saveSnapshot' );
\r
536 domRange.deleteContents();
\r
537 domRange.insertNode( text );
\r
538 if ( !isReplaceAll )
\r
540 selection.selectRanges( [ domRange ] );
\r
541 editor.fire( 'saveSnapshot' );
\r
543 this.matchRange.updateFromDomRange( domRange );
\r
544 if ( !isReplaceAll )
\r
545 this.matchRange.highlight();
\r
546 this.matchRange._.isReplaced = true;
\r
547 this.replaceCounter++;
\r
551 result = this.find( pattern, matchCase, matchWord, matchCyclic, !isReplaceAll );
\r
558 * The range in which find/replace happened, receive from user
\r
561 function getSearchRange( isDefault )
\r
564 sel = editor.getSelection(),
\r
565 body = editor.document.getBody();
\r
566 if ( sel && !isDefault )
\r
568 searchRange = sel.getRanges()[ 0 ].clone();
\r
569 searchRange.collapse( true );
\r
573 searchRange = new CKEDITOR.dom.range();
\r
574 searchRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );
\r
576 searchRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
\r
577 return searchRange;
\r
581 title : editor.lang.findAndReplace.title,
\r
582 resizable : CKEDITOR.DIALOG_RESIZE_NONE,
\r
585 buttons : [ CKEDITOR.dialog.cancelButton ], //Cancel button only.
\r
589 label : editor.lang.findAndReplace.find,
\r
590 title : editor.lang.findAndReplace.find,
\r
595 widths : [ '230px', '90px' ],
\r
600 id : 'txtFindFind',
\r
601 label : editor.lang.findAndReplace.findWhat,
\r
603 labelLayout : 'horizontal',
\r
609 style : 'width:100%',
\r
610 label : editor.lang.findAndReplace.find,
\r
611 onClick : function()
\r
613 var dialog = this.getDialog();
\r
614 if ( !finder.find( dialog.getValueOf( 'find', 'txtFindFind' ),
\r
615 dialog.getValueOf( 'find', 'txtFindCaseChk' ),
\r
616 dialog.getValueOf( 'find', 'txtFindWordChk' ),
\r
617 dialog.getValueOf( 'find', 'txtFindCyclic' ) ) )
\r
618 alert( editor.lang.findAndReplace
\r
631 id : 'txtFindCaseChk',
\r
633 style : 'margin-top:28px',
\r
634 label : editor.lang.findAndReplace.matchCase
\r
638 id : 'txtFindWordChk',
\r
640 label : editor.lang.findAndReplace.matchWord
\r
644 id : 'txtFindCyclic',
\r
647 label : editor.lang.findAndReplace.matchCyclic
\r
655 label : editor.lang.findAndReplace.replace,
\r
660 widths : [ '230px', '90px' ],
\r
665 id : 'txtFindReplace',
\r
666 label : editor.lang.findAndReplace.findWhat,
\r
668 labelLayout : 'horizontal',
\r
674 style : 'width:100%',
\r
675 label : editor.lang.findAndReplace.replace,
\r
676 onClick : function()
\r
678 var dialog = this.getDialog();
\r
679 if ( !finder.replace( dialog,
\r
680 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
681 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
682 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
683 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
684 dialog.getValueOf( 'replace', 'txtReplaceCyclic' ) ) )
\r
685 alert( editor.lang.findAndReplace
\r
693 widths : [ '230px', '90px' ],
\r
699 label : editor.lang.findAndReplace.replaceWith,
\r
701 labelLayout : 'horizontal',
\r
707 style : 'width:100%',
\r
708 label : editor.lang.findAndReplace.replaceAll,
\r
710 onClick : function()
\r
712 var dialog = this.getDialog();
\r
715 finder.replaceCounter = 0;
\r
717 // Scope to full document.
\r
718 finder.searchRange = getSearchRange( true );
\r
719 if ( finder.matchRange )
\r
721 finder.matchRange.removeHighlight();
\r
722 finder.matchRange = null;
\r
724 editor.fire( 'saveSnapshot' );
\r
725 while ( finder.replace( dialog,
\r
726 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
727 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
728 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
729 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
733 if ( finder.replaceCounter )
\r
735 alert( editor.lang.findAndReplace.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );
\r
736 editor.fire( 'saveSnapshot' );
\r
739 alert( editor.lang.findAndReplace.notFoundMsg );
\r
751 id : 'txtReplaceCaseChk',
\r
753 label : editor.lang.findAndReplace
\r
758 id : 'txtReplaceWordChk',
\r
760 label : editor.lang.findAndReplace
\r
765 id : 'txtReplaceCyclic',
\r
768 label : editor.lang.findAndReplace
\r
776 onLoad : function()
\r
780 //keep track of the current pattern field in use.
\r
781 var patternField, wholeWordChkField;
\r
783 //Ignore initial page select on dialog show
\r
784 var isUserSelect = false;
\r
785 this.on('hide', function()
\r
787 isUserSelect = false;
\r
789 this.on('show', function()
\r
791 isUserSelect = true;
\r
794 this.selectPage = CKEDITOR.tools.override( this.selectPage, function( originalFunc )
\r
796 return function( pageId )
\r
798 originalFunc.call( dialog, pageId );
\r
800 var currPage = dialog._.tabs[ pageId ];
\r
801 var patternFieldInput, patternFieldId, wholeWordChkFieldId;
\r
802 patternFieldId = pageId === 'find' ? 'txtFindFind' : 'txtFindReplace';
\r
803 wholeWordChkFieldId = pageId === 'find' ? 'txtFindWordChk' : 'txtReplaceWordChk';
\r
805 patternField = dialog.getContentElement( pageId,
\r
807 wholeWordChkField = dialog.getContentElement( pageId,
\r
808 wholeWordChkFieldId );
\r
810 // prepare for check pattern text filed 'keyup' event
\r
811 if ( !currPage.initialized )
\r
813 patternFieldInput = CKEDITOR.document
\r
814 .getById( patternField._.inputId );
\r
815 currPage.initialized = true;
\r
818 if ( isUserSelect )
\r
819 // synchronize fields on tab switch.
\r
820 syncFieldsBetweenTabs.call( this, pageId );
\r
825 onShow : function()
\r
827 // Establish initial searching start position.
\r
828 finder.searchRange = getSearchRange();
\r
830 this.selectPage( startupPage );
\r
832 onHide : function()
\r
835 if ( finder.matchRange && finder.matchRange.isMatched() )
\r
837 finder.matchRange.removeHighlight();
\r
840 range = finder.matchRange.toDomRange();
\r
842 editor.getSelection().selectRanges( [ range ] );
\r
845 // Clear current session before dialog close
\r
846 delete finder.matchRange;
\r
848 onFocus : function()
\r
850 if ( startupPage == 'replace' )
\r
851 return this.getContentElement( 'replace', 'txtFindReplace' );
\r
853 return this.getContentElement( 'find', 'txtFindFind' );
\r
858 CKEDITOR.dialog.add( 'find', function( editor )
\r
860 return findDialog( editor, 'find' );
\r
863 CKEDITOR.dialog.add( 'replace', function( editor )
\r
865 return findDialog( editor, 'replace' );
\r