2 Copyright (c) 2003-2012, 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(
\r
75 CKEDITOR.tools.extend( { attributes : { 'data-cke-highlight': 1 }, fullMatch : 1, ignoreReadonly : 1, childRule : function(){ return 0; } },
\r
76 editor.config.find_highlight, true ) );
\r
79 * Iterator which walk through the specified range char by char. By
\r
80 * default the walking will not stop at the character boundaries, until
\r
81 * the end of the range is encountered.
\r
82 * @param { CKEDITOR.dom.range } range
\r
83 * @param {Boolean} matchWord Whether the walking will stop at character boundary.
\r
85 var characterWalker = function( range , matchWord )
\r
89 new CKEDITOR.dom.walker( range );
\r
90 walker.guard = matchWord ? nonCharactersBoundary : function( node )
\r
92 !nonCharactersBoundary( node ) && ( self._.matchBoundary = true );
\r
94 walker[ 'evaluator' ] = findEvaluator;
\r
95 walker.breakOnFalse = 1;
\r
97 if ( range.startContainer.type == CKEDITOR.NODE_TEXT )
\r
99 this.textNode = range.startContainer;
\r
100 this.offset = range.startOffset - 1;
\r
104 matchWord : matchWord,
\r
106 matchBoundary : false
\r
110 characterWalker.prototype = {
\r
113 return this.move();
\r
118 return this.move( true );
\r
121 move : function( rtl )
\r
123 var currentTextNode = this.textNode;
\r
124 // Already at the end of document, no more character available.
\r
125 if ( currentTextNode === null )
\r
126 return cursorStep.call( this );
\r
128 this._.matchBoundary = false;
\r
130 // There are more characters in the text node, step forward.
\r
131 if ( currentTextNode
\r
133 && this.offset > 0 )
\r
136 return cursorStep.call( this );
\r
138 else if ( currentTextNode
\r
139 && this.offset < currentTextNode.getLength() - 1 )
\r
142 return cursorStep.call( this );
\r
146 currentTextNode = null;
\r
147 // At the end of the text node, walking foward for the next.
\r
148 while ( !currentTextNode )
\r
151 this._.walker[ rtl ? 'previous' : 'next' ].call( this._.walker );
\r
153 // Stop searching if we're need full word match OR
\r
154 // already reach document end.
\r
155 if ( this._.matchWord && !currentTextNode
\r
156 || this._.walker._.end )
\r
159 // Found a fresh text node.
\r
160 this.textNode = currentTextNode;
\r
161 if ( currentTextNode )
\r
162 this.offset = rtl ? currentTextNode.getLength() - 1 : 0;
\r
167 return cursorStep.call( this );
\r
173 * A range of cursors which represent a trunk of characters which try to
\r
174 * match, it has the same length as the pattern string.
\r
176 var characterRange = function( characterWalker, rangeLength )
\r
179 walker : characterWalker,
\r
181 rangeLength : rangeLength,
\r
182 highlightRange : null,
\r
187 characterRange.prototype = {
\r
189 * Translate this range to {@link CKEDITOR.dom.range}
\r
191 toDomRange : function()
\r
193 var range = new CKEDITOR.dom.range( editor.document );
\r
194 var cursors = this._.cursors;
\r
195 if ( cursors.length < 1 )
\r
197 var textNode = this._.walker.textNode;
\r
199 range.setStartAfter( textNode );
\r
205 var first = cursors[0],
\r
206 last = cursors[ cursors.length - 1 ];
\r
208 range.setStart( first.textNode, first.offset );
\r
209 range.setEnd( last.textNode, last.offset + 1 );
\r
215 * Reflect the latest changes from dom range.
\r
217 updateFromDomRange : function( domRange )
\r
220 walker = new characterWalker( domRange );
\r
221 this._.cursors = [];
\r
224 cursor = walker.next();
\r
225 if ( cursor.character )
\r
226 this._.cursors.push( cursor );
\r
228 while ( cursor.character );
\r
229 this._.rangeLength = this._.cursors.length;
\r
232 setMatched : function()
\r
234 this._.isMatched = true;
\r
237 clearMatched : function()
\r
239 this._.isMatched = false;
\r
242 isMatched : function()
\r
244 return this._.isMatched;
\r
248 * Hightlight the current matched chunk of text.
\r
250 highlight : function()
\r
252 // Do not apply if nothing is found.
\r
253 if ( this._.cursors.length < 1 )
\r
256 // Remove the previous highlight if there's one.
\r
257 if ( this._.highlightRange )
\r
258 this.removeHighlight();
\r
260 // Apply the highlight.
\r
261 var range = this.toDomRange(),
\r
262 bookmark = range.createBookmark();
\r
263 highlightStyle.applyToRange( range );
\r
264 range.moveToBookmark( bookmark );
\r
265 this._.highlightRange = range;
\r
267 // Scroll the editor to the highlighted area.
\r
268 var element = range.startContainer;
\r
269 if ( element.type != CKEDITOR.NODE_ELEMENT )
\r
270 element = element.getParent();
\r
271 element.scrollIntoView();
\r
273 // Update the character cursors.
\r
274 this.updateFromDomRange( range );
\r
278 * Remove highlighted find result.
\r
280 removeHighlight : function()
\r
282 if ( !this._.highlightRange )
\r
285 var bookmark = this._.highlightRange.createBookmark();
\r
286 highlightStyle.removeFromRange( this._.highlightRange );
\r
287 this._.highlightRange.moveToBookmark( bookmark );
\r
288 this.updateFromDomRange( this._.highlightRange );
\r
289 this._.highlightRange = null;
\r
292 isReadOnly : function()
\r
294 if ( !this._.highlightRange )
\r
297 return this._.highlightRange.startContainer.isReadOnly();
\r
300 moveBack : function()
\r
302 var retval = this._.walker.back(),
\r
303 cursors = this._.cursors;
\r
305 if ( retval.hitMatchBoundary )
\r
306 this._.cursors = cursors = [];
\r
308 cursors.unshift( retval );
\r
309 if ( cursors.length > this._.rangeLength )
\r
315 moveNext : function()
\r
317 var retval = this._.walker.next(),
\r
318 cursors = this._.cursors;
\r
320 // Clear the cursors queue if we've crossed a match boundary.
\r
321 if ( retval.hitMatchBoundary )
\r
322 this._.cursors = cursors = [];
\r
324 cursors.push( retval );
\r
325 if ( cursors.length > this._.rangeLength )
\r
331 getEndCharacter : function()
\r
333 var cursors = this._.cursors;
\r
334 if ( cursors.length < 1 )
\r
337 return cursors[ cursors.length - 1 ].character;
\r
340 getNextCharacterRange : function( maxLength )
\r
344 cursors = this._.cursors;
\r
346 if ( ( lastCursor = cursors[ cursors.length - 1 ] ) && lastCursor.textNode )
\r
347 nextRangeWalker = new characterWalker( getRangeAfterCursor( lastCursor ) );
\r
348 // In case it's an empty range (no cursors), figure out next range from walker (#4951).
\r
350 nextRangeWalker = this._.walker;
\r
352 return new characterRange( nextRangeWalker, maxLength );
\r
355 getCursors : function()
\r
357 return this._.cursors;
\r
362 // The remaining document range after the character cursor.
\r
363 function getRangeAfterCursor( cursor , inclusive )
\r
365 var range = new CKEDITOR.dom.range();
\r
366 range.setStart( cursor.textNode,
\r
367 ( inclusive ? cursor.offset : cursor.offset + 1 ) );
\r
368 range.setEndAt( editor.document.getBody(),
\r
369 CKEDITOR.POSITION_BEFORE_END );
\r
373 // The document range before the character cursor.
\r
374 function getRangeBeforeCursor( cursor )
\r
376 var range = new CKEDITOR.dom.range();
\r
377 range.setStartAt( editor.document.getBody(),
\r
378 CKEDITOR.POSITION_AFTER_START );
\r
379 range.setEnd( cursor.textNode, cursor.offset );
\r
383 var KMP_NOMATCH = 0,
\r
387 * Examination the occurrence of a word which implement KMP algorithm.
\r
389 var kmpMatcher = function( pattern, ignoreCase )
\r
391 var overlap = [ -1 ];
\r
393 pattern = pattern.toLowerCase();
\r
394 for ( var i = 0 ; i < pattern.length ; i++ )
\r
396 overlap.push( overlap[i] + 1 );
\r
397 while ( overlap[ i + 1 ] > 0
\r
398 && pattern.charAt( i ) != pattern
\r
399 .charAt( overlap[ i + 1 ] - 1 ) )
\r
400 overlap[ i + 1 ] = overlap[ overlap[ i + 1 ] - 1 ] + 1;
\r
406 ignoreCase : !!ignoreCase,
\r
411 kmpMatcher.prototype =
\r
413 feedCharacter : function( c )
\r
415 if ( this._.ignoreCase )
\r
416 c = c.toLowerCase();
\r
420 if ( c == this._.pattern.charAt( this._.state ) )
\r
423 if ( this._.state == this._.pattern.length )
\r
426 return KMP_MATCHED;
\r
428 return KMP_ADVANCED;
\r
430 else if ( !this._.state )
\r
431 return KMP_NOMATCH;
\r
433 this._.state = this._.overlap[ this._.state ];
\r
445 var wordSeparatorRegex =
\r
446 /[.,"'?!;: \u0085\u00a0\u1680\u280e\u2028\u2029\u202f\u205f\u3000]/;
\r
448 var isWordSeparator = function( c )
\r
452 var code = c.charCodeAt( 0 );
\r
453 return ( code >= 9 && code <= 0xd )
\r
454 || ( code >= 0x2000 && code <= 0x200a )
\r
455 || wordSeparatorRegex.test( c );
\r
459 searchRange : null,
\r
461 find : function( pattern, matchCase, matchWord, matchCyclic, highlightMatched, cyclicRerun )
\r
463 if ( !this.matchRange )
\r
465 new characterRange(
\r
466 new characterWalker( this.searchRange ),
\r
470 this.matchRange.removeHighlight();
\r
471 this.matchRange = this.matchRange.getNextCharacterRange( pattern.length );
\r
474 var matcher = new kmpMatcher( pattern, !matchCase ),
\r
475 matchState = KMP_NOMATCH,
\r
478 while ( character !== null )
\r
480 this.matchRange.moveNext();
\r
481 while ( ( character = this.matchRange.getEndCharacter() ) )
\r
483 matchState = matcher.feedCharacter( character );
\r
484 if ( matchState == KMP_MATCHED )
\r
486 if ( this.matchRange.moveNext().hitMatchBoundary )
\r
490 if ( matchState == KMP_MATCHED )
\r
494 var cursors = this.matchRange.getCursors(),
\r
495 tail = cursors[ cursors.length - 1 ],
\r
496 head = cursors[ 0 ];
\r
498 var headWalker = new characterWalker( getRangeBeforeCursor( head ), true ),
\r
499 tailWalker = new characterWalker( getRangeAfterCursor( tail ), true );
\r
501 if ( ! ( isWordSeparator( headWalker.back().character )
\r
502 && isWordSeparator( tailWalker.next().character ) ) )
\r
505 this.matchRange.setMatched();
\r
506 if ( highlightMatched !== false )
\r
507 this.matchRange.highlight();
\r
512 this.matchRange.clearMatched();
\r
513 this.matchRange.removeHighlight();
\r
514 // Clear current session and restart with the default search
\r
516 // Re-run the finding once for cyclic.(#3517)
\r
517 if ( matchCyclic && !cyclicRerun )
\r
519 this.searchRange = getSearchRange( 1 );
\r
520 this.matchRange = null;
\r
521 return arguments.callee.apply( this,
\r
522 Array.prototype.slice.call( arguments ).concat( [ true ] ) );
\r
529 * Record how much replacement occurred toward one replacing.
\r
531 replaceCounter : 0,
\r
533 replace : function( dialog, pattern, newString, matchCase, matchWord,
\r
534 matchCyclic , isReplaceAll )
\r
538 // Successiveness of current replace/find.
\r
541 // 1. Perform the replace when there's already a match here.
\r
542 // 2. Otherwise perform the find but don't replace it immediately.
\r
543 if ( this.matchRange && this.matchRange.isMatched()
\r
544 && !this.matchRange._.isReplaced && !this.matchRange.isReadOnly() )
\r
546 // Turn off highlight for a while when saving snapshots.
\r
547 this.matchRange.removeHighlight();
\r
548 var domRange = this.matchRange.toDomRange();
\r
549 var text = editor.document.createText( newString );
\r
550 if ( !isReplaceAll )
\r
552 // Save undo snaps before and after the replacement.
\r
553 var selection = editor.getSelection();
\r
554 selection.selectRanges( [ domRange ] );
\r
555 editor.fire( 'saveSnapshot' );
\r
557 domRange.deleteContents();
\r
558 domRange.insertNode( text );
\r
559 if ( !isReplaceAll )
\r
561 selection.selectRanges( [ domRange ] );
\r
562 editor.fire( 'saveSnapshot' );
\r
564 this.matchRange.updateFromDomRange( domRange );
\r
565 if ( !isReplaceAll )
\r
566 this.matchRange.highlight();
\r
567 this.matchRange._.isReplaced = true;
\r
568 this.replaceCounter++;
\r
572 result = this.find( pattern, matchCase, matchWord, matchCyclic, !isReplaceAll );
\r
581 * The range in which find/replace happened, receive from user
\r
584 function getSearchRange( isDefault )
\r
587 sel = editor.getSelection(),
\r
588 body = editor.document.getBody();
\r
589 if ( sel && !isDefault )
\r
591 searchRange = sel.getRanges()[ 0 ].clone();
\r
592 searchRange.collapse( true );
\r
596 searchRange = new CKEDITOR.dom.range();
\r
597 searchRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );
\r
599 searchRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
\r
600 return searchRange;
\r
603 var lang = editor.lang.findAndReplace;
\r
605 title : lang.title,
\r
606 resizable : CKEDITOR.DIALOG_RESIZE_NONE,
\r
609 buttons : [ CKEDITOR.dialog.cancelButton ], // Cancel button only.
\r
619 widths : [ '230px', '90px' ],
\r
624 id : 'txtFindFind',
\r
625 label : lang.findWhat,
\r
627 labelLayout : 'horizontal',
\r
634 style : 'width:100%',
\r
636 onClick : function()
\r
638 var dialog = this.getDialog();
\r
639 if ( !finder.find( dialog.getValueOf( 'find', 'txtFindFind' ),
\r
640 dialog.getValueOf( 'find', 'txtFindCaseChk' ),
\r
641 dialog.getValueOf( 'find', 'txtFindWordChk' ),
\r
642 dialog.getValueOf( 'find', 'txtFindCyclic' ) ) )
\r
651 label : CKEDITOR.tools.htmlEncode( lang.findOptions ),
\r
652 style : 'margin-top:29px',
\r
662 id : 'txtFindCaseChk',
\r
664 label : lang.matchCase
\r
668 id : 'txtFindWordChk',
\r
670 label : lang.matchWord
\r
674 id : 'txtFindCyclic',
\r
677 label : lang.matchCyclic
\r
687 label : lang.replace,
\r
692 widths : [ '230px', '90px' ],
\r
697 id : 'txtFindReplace',
\r
698 label : lang.findWhat,
\r
700 labelLayout : 'horizontal',
\r
705 id : 'btnFindReplace',
\r
707 style : 'width:100%',
\r
708 label : lang.replace,
\r
709 onClick : function()
\r
711 var dialog = this.getDialog();
\r
712 if ( !finder.replace( dialog,
\r
713 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
714 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
715 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
716 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
717 dialog.getValueOf( 'replace', 'txtReplaceCyclic' ) ) )
\r
726 widths : [ '230px', '90px' ],
\r
732 label : lang.replaceWith,
\r
734 labelLayout : 'horizontal',
\r
739 id : 'btnReplaceAll',
\r
741 style : 'width:100%',
\r
742 label : lang.replaceAll,
\r
744 onClick : function()
\r
746 var dialog = this.getDialog();
\r
749 finder.replaceCounter = 0;
\r
751 // Scope to full document.
\r
752 finder.searchRange = getSearchRange( 1 );
\r
753 if ( finder.matchRange )
\r
755 finder.matchRange.removeHighlight();
\r
756 finder.matchRange = null;
\r
758 editor.fire( 'saveSnapshot' );
\r
759 while ( finder.replace( dialog,
\r
760 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
761 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
762 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
763 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
767 if ( finder.replaceCounter )
\r
769 alert( lang.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );
\r
770 editor.fire( 'saveSnapshot' );
\r
773 alert( lang.notFoundMsg );
\r
780 label : CKEDITOR.tools.htmlEncode( lang.findOptions ),
\r
790 id : 'txtReplaceCaseChk',
\r
792 label : lang.matchCase
\r
796 id : 'txtReplaceWordChk',
\r
798 label : lang.matchWord
\r
802 id : 'txtReplaceCyclic',
\r
805 label : lang.matchCyclic
\r
814 onLoad : function()
\r
818 // Keep track of the current pattern field in use.
\r
819 var patternField, wholeWordChkField;
\r
821 // Ignore initial page select on dialog show
\r
822 var isUserSelect = 0;
\r
823 this.on( 'hide', function()
\r
827 this.on( 'show', function()
\r
832 this.selectPage = CKEDITOR.tools.override( this.selectPage, function( originalFunc )
\r
834 return function( pageId )
\r
836 originalFunc.call( dialog, pageId );
\r
838 var currPage = dialog._.tabs[ pageId ];
\r
839 var patternFieldInput, patternFieldId, wholeWordChkFieldId;
\r
840 patternFieldId = pageId === 'find' ? 'txtFindFind' : 'txtFindReplace';
\r
841 wholeWordChkFieldId = pageId === 'find' ? 'txtFindWordChk' : 'txtReplaceWordChk';
\r
843 patternField = dialog.getContentElement( pageId,
\r
845 wholeWordChkField = dialog.getContentElement( pageId,
\r
846 wholeWordChkFieldId );
\r
848 // Prepare for check pattern text filed 'keyup' event
\r
849 if ( !currPage.initialized )
\r
851 patternFieldInput = CKEDITOR.document
\r
852 .getById( patternField._.inputId );
\r
853 currPage.initialized = true;
\r
856 // Synchronize fields on tab switch.
\r
857 if ( isUserSelect )
\r
858 syncFieldsBetweenTabs.call( this, pageId );
\r
863 onShow : function()
\r
865 // Establish initial searching start position.
\r
866 finder.searchRange = getSearchRange();
\r
868 // Fill in the find field with selected text.
\r
869 var selectedText = this.getParentEditor().getSelection().getSelectedText(),
\r
870 patternFieldId = ( startupPage == 'find' ? 'txtFindFind' : 'txtFindReplace' );
\r
872 var field = this.getContentElement( startupPage, patternFieldId );
\r
873 field.setValue( selectedText );
\r
876 this.selectPage( startupPage );
\r
878 this[ ( startupPage == 'find' && this._.editor.readOnly? 'hide' : 'show' ) + 'Page' ]( 'replace');
\r
880 onHide : function()
\r
883 if ( finder.matchRange && finder.matchRange.isMatched() )
\r
885 finder.matchRange.removeHighlight();
\r
888 range = finder.matchRange.toDomRange();
\r
890 editor.getSelection().selectRanges( [ range ] );
\r
893 // Clear current session before dialog close
\r
894 delete finder.matchRange;
\r
896 onFocus : function()
\r
898 if ( startupPage == 'replace' )
\r
899 return this.getContentElement( 'replace', 'txtFindReplace' );
\r
901 return this.getContentElement( 'find', 'txtFindFind' );
\r
906 CKEDITOR.dialog.add( 'find', function( editor )
\r
908 return findDialog( editor, 'find' );
\r
911 CKEDITOR.dialog.add( 'replace', function( editor )
\r
913 return findDialog( editor, 'replace' );
\r