2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
10 function findEvaluator( node )
\r
12 return node.type == CKEDITOR.NODE_TEXT && node.getLength() > 0 && ( !isReplace || !node.isReadOnly() );
\r
16 * Elements which break characters been considered as sequence.
\r
18 function nonCharactersBoundary( node )
\r
20 return !( node.type == CKEDITOR.NODE_ELEMENT && node.isBlockBoundary(
\r
21 CKEDITOR.tools.extend( {}, CKEDITOR.dtd.$empty, CKEDITOR.dtd.$nonEditable ) ) );
\r
25 * Get the cursor object which represent both current character and it's dom
\r
28 var cursorStep = function()
\r
31 textNode : this.textNode,
\r
32 offset : this.offset,
\r
33 character : this.textNode ?
\r
34 this.textNode.getText().charAt( this.offset ) : null,
\r
35 hitMatchBoundary : this._.matchBoundary
\r
39 var pages = [ 'find', 'replace' ],
\r
41 [ 'txtFindFind', 'txtFindReplace' ],
\r
42 [ 'txtFindCaseChk', 'txtReplaceCaseChk' ],
\r
43 [ 'txtFindWordChk', 'txtReplaceWordChk' ],
\r
44 [ 'txtFindCyclic', 'txtReplaceCyclic' ] ];
\r
47 * Synchronize corresponding filed values between 'replace' and 'find' pages.
\r
48 * @param {String} currentPageId The page id which receive values.
\r
50 function syncFieldsBetweenTabs( currentPageId )
\r
52 var sourceIndex, targetIndex,
\r
53 sourceField, targetField;
\r
55 sourceIndex = currentPageId === 'find' ? 1 : 0;
\r
56 targetIndex = 1 - sourceIndex;
\r
57 var i, l = fieldsMapping.length;
\r
58 for ( i = 0 ; i < l ; i++ )
\r
60 sourceField = this.getContentElement( pages[ sourceIndex ],
\r
61 fieldsMapping[ i ][ sourceIndex ] );
\r
62 targetField = this.getContentElement( pages[ targetIndex ],
\r
63 fieldsMapping[ i ][ targetIndex ] );
\r
65 targetField.setValue( sourceField.getValue() );
\r
69 var findDialog = function( editor, startupPage )
\r
71 // Style object for highlights: (#5018)
\r
72 // 1. Defined as full match style to avoid compromising ordinary text color styles.
\r
73 // 2. Must be apply onto inner-most text to avoid conflicting with ordinary text color styles visually.
\r
74 var highlightStyle = new CKEDITOR.style( CKEDITOR.tools.extend( { fullMatch : true, childRule : function(){ return 0; } },
\r
75 editor.config.find_highlight ) );
\r
78 * Iterator which walk through the specified range char by char. By
\r
79 * default the walking will not stop at the character boundaries, until
\r
80 * the end of the range is encountered.
\r
81 * @param { CKEDITOR.dom.range } range
\r
82 * @param {Boolean} matchWord Whether the walking will stop at character boundary.
\r
84 var characterWalker = function( range , matchWord )
\r
88 new CKEDITOR.dom.walker( range );
\r
89 walker.guard = matchWord ? nonCharactersBoundary : function( node )
\r
91 !nonCharactersBoundary( node ) && ( self._.matchBoundary = true );
\r
93 walker[ 'evaluator' ] = findEvaluator;
\r
94 walker.breakOnFalse = 1;
\r
96 if ( range.startContainer.type == CKEDITOR.NODE_TEXT )
\r
98 this.textNode = range.startContainer;
\r
99 this.offset = range.startOffset - 1;
\r
103 matchWord : matchWord,
\r
105 matchBoundary : false
\r
109 characterWalker.prototype = {
\r
112 return this.move();
\r
117 return this.move( true );
\r
120 move : function( rtl )
\r
122 var currentTextNode = this.textNode;
\r
123 // Already at the end of document, no more character available.
\r
124 if ( currentTextNode === null )
\r
125 return cursorStep.call( this );
\r
127 this._.matchBoundary = false;
\r
129 // There are more characters in the text node, step forward.
\r
130 if ( currentTextNode
\r
132 && this.offset > 0 )
\r
135 return cursorStep.call( this );
\r
137 else if ( currentTextNode
\r
138 && this.offset < currentTextNode.getLength() - 1 )
\r
141 return cursorStep.call( this );
\r
145 currentTextNode = null;
\r
146 // At the end of the text node, walking foward for the next.
\r
147 while ( !currentTextNode )
\r
150 this._.walker[ rtl ? 'previous' : 'next' ].call( this._.walker );
\r
152 // Stop searching if we're need full word match OR
\r
153 // already reach document end.
\r
154 if ( this._.matchWord && !currentTextNode
\r
155 || this._.walker._.end )
\r
158 // Found a fresh text node.
\r
159 this.textNode = currentTextNode;
\r
160 if ( currentTextNode )
\r
161 this.offset = rtl ? currentTextNode.getLength() - 1 : 0;
\r
166 return cursorStep.call( this );
\r
172 * A range of cursors which represent a trunk of characters which try to
\r
173 * match, it has the same length as the pattern string.
\r
175 var characterRange = function( characterWalker, rangeLength )
\r
178 walker : characterWalker,
\r
180 rangeLength : rangeLength,
\r
181 highlightRange : null,
\r
186 characterRange.prototype = {
\r
188 * Translate this range to {@link CKEDITOR.dom.range}
\r
190 toDomRange : function()
\r
192 var range = new CKEDITOR.dom.range( editor.document );
\r
193 var cursors = this._.cursors;
\r
194 if ( cursors.length < 1 )
\r
196 var textNode = this._.walker.textNode;
\r
198 range.setStartAfter( textNode );
\r
204 var first = cursors[0],
\r
205 last = cursors[ cursors.length - 1 ];
\r
207 range.setStart( first.textNode, first.offset );
\r
208 range.setEnd( last.textNode, last.offset + 1 );
\r
214 * Reflect the latest changes from dom range.
\r
216 updateFromDomRange : function( domRange )
\r
219 walker = new characterWalker( domRange );
\r
220 this._.cursors = [];
\r
223 cursor = walker.next();
\r
224 if ( cursor.character )
\r
225 this._.cursors.push( cursor );
\r
227 while ( cursor.character );
\r
228 this._.rangeLength = this._.cursors.length;
\r
231 setMatched : function()
\r
233 this._.isMatched = true;
\r
236 clearMatched : function()
\r
238 this._.isMatched = false;
\r
241 isMatched : function()
\r
243 return this._.isMatched;
\r
247 * Hightlight the current matched chunk of text.
\r
249 highlight : function()
\r
251 // Do not apply if nothing is found.
\r
252 if ( this._.cursors.length < 1 )
\r
255 // Remove the previous highlight if there's one.
\r
256 if ( this._.highlightRange )
\r
257 this.removeHighlight();
\r
259 // Apply the highlight.
\r
260 var range = this.toDomRange(),
\r
261 bookmark = range.createBookmark();
\r
262 highlightStyle.applyToRange( range );
\r
263 range.moveToBookmark( bookmark );
\r
264 this._.highlightRange = range;
\r
266 // Scroll the editor to the highlighted area.
\r
267 var element = range.startContainer;
\r
268 if ( element.type != CKEDITOR.NODE_ELEMENT )
\r
269 element = element.getParent();
\r
270 element.scrollIntoView();
\r
272 // Update the character cursors.
\r
273 this.updateFromDomRange( range );
\r
277 * Remove highlighted find result.
\r
279 removeHighlight : function()
\r
281 if ( !this._.highlightRange )
\r
284 var bookmark = this._.highlightRange.createBookmark();
\r
285 highlightStyle.removeFromRange( this._.highlightRange );
\r
286 this._.highlightRange.moveToBookmark( bookmark );
\r
287 this.updateFromDomRange( this._.highlightRange );
\r
288 this._.highlightRange = null;
\r
291 isReadOnly : function()
\r
293 if ( !this._.highlightRange )
\r
296 return this._.highlightRange.startContainer.isReadOnly();
\r
299 moveBack : function()
\r
301 var retval = this._.walker.back(),
\r
302 cursors = this._.cursors;
\r
304 if ( retval.hitMatchBoundary )
\r
305 this._.cursors = cursors = [];
\r
307 cursors.unshift( retval );
\r
308 if ( cursors.length > this._.rangeLength )
\r
314 moveNext : function()
\r
316 var retval = this._.walker.next(),
\r
317 cursors = this._.cursors;
\r
319 // Clear the cursors queue if we've crossed a match boundary.
\r
320 if ( retval.hitMatchBoundary )
\r
321 this._.cursors = cursors = [];
\r
323 cursors.push( retval );
\r
324 if ( cursors.length > this._.rangeLength )
\r
330 getEndCharacter : function()
\r
332 var cursors = this._.cursors;
\r
333 if ( cursors.length < 1 )
\r
336 return cursors[ cursors.length - 1 ].character;
\r
339 getNextCharacterRange : function( maxLength )
\r
343 cursors = this._.cursors;
\r
345 if ( ( lastCursor = cursors[ cursors.length - 1 ] ) && lastCursor.textNode )
\r
346 nextRangeWalker = new characterWalker( getRangeAfterCursor( lastCursor ) );
\r
347 // In case it's an empty range (no cursors), figure out next range from walker (#4951).
\r
349 nextRangeWalker = this._.walker;
\r
351 return new characterRange( nextRangeWalker, maxLength );
\r
354 getCursors : function()
\r
356 return this._.cursors;
\r
361 // The remaining document range after the character cursor.
\r
362 function getRangeAfterCursor( cursor , inclusive )
\r
364 var range = new CKEDITOR.dom.range();
\r
365 range.setStart( cursor.textNode,
\r
366 ( inclusive ? cursor.offset : cursor.offset + 1 ) );
\r
367 range.setEndAt( editor.document.getBody(),
\r
368 CKEDITOR.POSITION_BEFORE_END );
\r
372 // The document range before the character cursor.
\r
373 function getRangeBeforeCursor( cursor )
\r
375 var range = new CKEDITOR.dom.range();
\r
376 range.setStartAt( editor.document.getBody(),
\r
377 CKEDITOR.POSITION_AFTER_START );
\r
378 range.setEnd( cursor.textNode, cursor.offset );
\r
382 var KMP_NOMATCH = 0,
\r
386 * Examination the occurrence of a word which implement KMP algorithm.
\r
388 var kmpMatcher = function( pattern, ignoreCase )
\r
390 var overlap = [ -1 ];
\r
392 pattern = pattern.toLowerCase();
\r
393 for ( var i = 0 ; i < pattern.length ; i++ )
\r
395 overlap.push( overlap[i] + 1 );
\r
396 while ( overlap[ i + 1 ] > 0
\r
397 && pattern.charAt( i ) != pattern
\r
398 .charAt( overlap[ i + 1 ] - 1 ) )
\r
399 overlap[ i + 1 ] = overlap[ overlap[ i + 1 ] - 1 ] + 1;
\r
405 ignoreCase : !!ignoreCase,
\r
410 kmpMatcher.prototype =
\r
412 feedCharacter : function( c )
\r
414 if ( this._.ignoreCase )
\r
415 c = c.toLowerCase();
\r
419 if ( c == this._.pattern.charAt( this._.state ) )
\r
422 if ( this._.state == this._.pattern.length )
\r
425 return KMP_MATCHED;
\r
427 return KMP_ADVANCED;
\r
429 else if ( !this._.state )
\r
430 return KMP_NOMATCH;
\r
432 this._.state = this._.overlap[ this._.state ];
\r
444 var wordSeparatorRegex =
\r
445 /[.,"'?!;: \u0085\u00a0\u1680\u280e\u2028\u2029\u202f\u205f\u3000]/;
\r
447 var isWordSeparator = function( c )
\r
451 var code = c.charCodeAt( 0 );
\r
452 return ( code >= 9 && code <= 0xd )
\r
453 || ( code >= 0x2000 && code <= 0x200a )
\r
454 || wordSeparatorRegex.test( c );
\r
458 searchRange : null,
\r
460 find : function( pattern, matchCase, matchWord, matchCyclic, highlightMatched, cyclicRerun )
\r
462 if ( !this.matchRange )
\r
464 new characterRange(
\r
465 new characterWalker( this.searchRange ),
\r
469 this.matchRange.removeHighlight();
\r
470 this.matchRange = this.matchRange.getNextCharacterRange( pattern.length );
\r
473 var matcher = new kmpMatcher( pattern, !matchCase ),
\r
474 matchState = KMP_NOMATCH,
\r
477 while ( character !== null )
\r
479 this.matchRange.moveNext();
\r
480 while ( ( character = this.matchRange.getEndCharacter() ) )
\r
482 matchState = matcher.feedCharacter( character );
\r
483 if ( matchState == KMP_MATCHED )
\r
485 if ( this.matchRange.moveNext().hitMatchBoundary )
\r
489 if ( matchState == KMP_MATCHED )
\r
493 var cursors = this.matchRange.getCursors(),
\r
494 tail = cursors[ cursors.length - 1 ],
\r
495 head = cursors[ 0 ];
\r
497 var headWalker = new characterWalker( getRangeBeforeCursor( head ), true ),
\r
498 tailWalker = new characterWalker( getRangeAfterCursor( tail ), true );
\r
500 if ( ! ( isWordSeparator( headWalker.back().character )
\r
501 && isWordSeparator( tailWalker.next().character ) ) )
\r
504 this.matchRange.setMatched();
\r
505 if ( highlightMatched !== false )
\r
506 this.matchRange.highlight();
\r
511 this.matchRange.clearMatched();
\r
512 this.matchRange.removeHighlight();
\r
513 // Clear current session and restart with the default search
\r
515 // Re-run the finding once for cyclic.(#3517)
\r
516 if ( matchCyclic && !cyclicRerun )
\r
518 this.searchRange = getSearchRange( 1 );
\r
519 this.matchRange = null;
\r
520 return arguments.callee.apply( this,
\r
521 Array.prototype.slice.call( arguments ).concat( [ true ] ) );
\r
528 * Record how much replacement occurred toward one replacing.
\r
530 replaceCounter : 0,
\r
532 replace : function( dialog, pattern, newString, matchCase, matchWord,
\r
533 matchCyclic , isReplaceAll )
\r
537 // Successiveness of current replace/find.
\r
540 // 1. Perform the replace when there's already a match here.
\r
541 // 2. Otherwise perform the find but don't replace it immediately.
\r
542 if ( this.matchRange && this.matchRange.isMatched()
\r
543 && !this.matchRange._.isReplaced && !this.matchRange.isReadOnly() )
\r
545 // Turn off highlight for a while when saving snapshots.
\r
546 this.matchRange.removeHighlight();
\r
547 var domRange = this.matchRange.toDomRange();
\r
548 var text = editor.document.createText( newString );
\r
549 if ( !isReplaceAll )
\r
551 // Save undo snaps before and after the replacement.
\r
552 var selection = editor.getSelection();
\r
553 selection.selectRanges( [ domRange ] );
\r
554 editor.fire( 'saveSnapshot' );
\r
556 domRange.deleteContents();
\r
557 domRange.insertNode( text );
\r
558 if ( !isReplaceAll )
\r
560 selection.selectRanges( [ domRange ] );
\r
561 editor.fire( 'saveSnapshot' );
\r
563 this.matchRange.updateFromDomRange( domRange );
\r
564 if ( !isReplaceAll )
\r
565 this.matchRange.highlight();
\r
566 this.matchRange._.isReplaced = true;
\r
567 this.replaceCounter++;
\r
571 result = this.find( pattern, matchCase, matchWord, matchCyclic, !isReplaceAll );
\r
580 * The range in which find/replace happened, receive from user
\r
583 function getSearchRange( isDefault )
\r
586 sel = editor.getSelection(),
\r
587 body = editor.document.getBody();
\r
588 if ( sel && !isDefault )
\r
590 searchRange = sel.getRanges()[ 0 ].clone();
\r
591 searchRange.collapse( true );
\r
595 searchRange = new CKEDITOR.dom.range();
\r
596 searchRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );
\r
598 searchRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
\r
599 return searchRange;
\r
602 var lang = editor.lang.findAndReplace;
\r
604 title : lang.title,
\r
605 resizable : CKEDITOR.DIALOG_RESIZE_NONE,
\r
608 buttons : [ CKEDITOR.dialog.cancelButton ], // Cancel button only.
\r
618 widths : [ '230px', '90px' ],
\r
623 id : 'txtFindFind',
\r
624 label : lang.findWhat,
\r
626 labelLayout : 'horizontal',
\r
632 style : 'width:100%',
\r
634 onClick : function()
\r
636 var dialog = this.getDialog();
\r
637 if ( !finder.find( dialog.getValueOf( 'find', 'txtFindFind' ),
\r
638 dialog.getValueOf( 'find', 'txtFindCaseChk' ),
\r
639 dialog.getValueOf( 'find', 'txtFindWordChk' ),
\r
640 dialog.getValueOf( 'find', 'txtFindCyclic' ) ) )
\r
654 id : 'txtFindCaseChk',
\r
656 style : 'margin-top:28px',
\r
657 label : lang.matchCase
\r
661 id : 'txtFindWordChk',
\r
663 label : lang.matchWord
\r
667 id : 'txtFindCyclic',
\r
670 label : lang.matchCyclic
\r
678 label : lang.replace,
\r
683 widths : [ '230px', '90px' ],
\r
688 id : 'txtFindReplace',
\r
689 label : lang.findWhat,
\r
691 labelLayout : 'horizontal',
\r
697 style : 'width:100%',
\r
698 label : lang.replace,
\r
699 onClick : function()
\r
701 var dialog = this.getDialog();
\r
702 if ( !finder.replace( dialog,
\r
703 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
704 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
705 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
706 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
707 dialog.getValueOf( 'replace', 'txtReplaceCyclic' ) ) )
\r
716 widths : [ '230px', '90px' ],
\r
722 label : lang.replaceWith,
\r
724 labelLayout : 'horizontal',
\r
730 style : 'width:100%',
\r
731 label : lang.replaceAll,
\r
733 onClick : function()
\r
735 var dialog = this.getDialog();
\r
738 finder.replaceCounter = 0;
\r
740 // Scope to full document.
\r
741 finder.searchRange = getSearchRange( 1 );
\r
742 if ( finder.matchRange )
\r
744 finder.matchRange.removeHighlight();
\r
745 finder.matchRange = null;
\r
747 editor.fire( 'saveSnapshot' );
\r
748 while ( finder.replace( dialog,
\r
749 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
750 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
751 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
752 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
756 if ( finder.replaceCounter )
\r
758 alert( lang.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );
\r
759 editor.fire( 'saveSnapshot' );
\r
762 alert( lang.notFoundMsg );
\r
774 id : 'txtReplaceCaseChk',
\r
781 id : 'txtReplaceWordChk',
\r
788 id : 'txtReplaceCyclic',
\r
799 onLoad : function()
\r
803 // Keep track of the current pattern field in use.
\r
804 var patternField, wholeWordChkField;
\r
806 // Ignore initial page select on dialog show
\r
807 var isUserSelect = 0;
\r
808 this.on( 'hide', function()
\r
812 this.on( 'show', function()
\r
817 this.selectPage = CKEDITOR.tools.override( this.selectPage, function( originalFunc )
\r
819 return function( pageId )
\r
821 originalFunc.call( dialog, pageId );
\r
823 var currPage = dialog._.tabs[ pageId ];
\r
824 var patternFieldInput, patternFieldId, wholeWordChkFieldId;
\r
825 patternFieldId = pageId === 'find' ? 'txtFindFind' : 'txtFindReplace';
\r
826 wholeWordChkFieldId = pageId === 'find' ? 'txtFindWordChk' : 'txtReplaceWordChk';
\r
828 patternField = dialog.getContentElement( pageId,
\r
830 wholeWordChkField = dialog.getContentElement( pageId,
\r
831 wholeWordChkFieldId );
\r
833 // Prepare for check pattern text filed 'keyup' event
\r
834 if ( !currPage.initialized )
\r
836 patternFieldInput = CKEDITOR.document
\r
837 .getById( patternField._.inputId );
\r
838 currPage.initialized = true;
\r
841 // Synchronize fields on tab switch.
\r
842 if ( isUserSelect )
\r
843 syncFieldsBetweenTabs.call( this, pageId );
\r
848 onShow : function()
\r
850 // Establish initial searching start position.
\r
851 finder.searchRange = getSearchRange();
\r
853 this.selectPage( startupPage );
\r
855 onHide : function()
\r
858 if ( finder.matchRange && finder.matchRange.isMatched() )
\r
860 finder.matchRange.removeHighlight();
\r
863 range = finder.matchRange.toDomRange();
\r
865 editor.getSelection().selectRanges( [ range ] );
\r
868 // Clear current session before dialog close
\r
869 delete finder.matchRange;
\r
871 onFocus : function()
\r
873 if ( startupPage == 'replace' )
\r
874 return this.getContentElement( 'replace', 'txtFindReplace' );
\r
876 return this.getContentElement( 'find', 'txtFindFind' );
\r
881 CKEDITOR.dialog.add( 'find', function( editor )
\r
883 return findDialog( editor, 'find' );
\r
886 CKEDITOR.dialog.add( 'replace', function( editor )
\r
888 return findDialog( editor, 'replace' );
\r