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 guardDomWalkerNonEmptyTextNode( 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 checkCharactersBoundary ( node )
\r
18 var dtd = CKEDITOR.dtd;
\r
19 return node.isBlockBoundary(
\r
20 CKEDITOR.tools.extend( {}, dtd.$empty, dtd.$nonEditable ) );
\r
24 * Get the cursor object which represent both current character and it's dom
\r
27 var cursorStep = function()
\r
30 textNode : this.textNode,
\r
31 offset : this.offset,
\r
32 character : this.textNode ?
\r
33 this.textNode.getText().charAt( this.offset ) : null,
\r
34 hitMatchBoundary : this._.matchBoundary
\r
38 var pages = [ 'find', 'replace' ],
\r
40 [ 'txtFindFind', 'txtFindReplace' ],
\r
41 [ 'txtFindCaseChk', 'txtReplaceCaseChk' ],
\r
42 [ 'txtFindWordChk', 'txtReplaceWordChk' ],
\r
43 [ 'txtFindCyclic', 'txtReplaceCyclic' ] ];
\r
46 * Synchronize corresponding filed values between 'replace' and 'find' pages.
\r
47 * @param {String} currentPageId The page id which receive values.
\r
49 function syncFieldsBetweenTabs( currentPageId )
\r
51 var sourceIndex, targetIndex,
\r
52 sourceField, targetField;
\r
54 sourceIndex = currentPageId === 'find' ? 1 : 0;
\r
55 targetIndex = 1 - sourceIndex;
\r
56 var i, l = fieldsMapping.length;
\r
57 for ( i = 0 ; i < l ; i++ )
\r
59 sourceField = this.getContentElement( pages[ sourceIndex ],
\r
60 fieldsMapping[ i ][ sourceIndex ] );
\r
61 targetField = this.getContentElement( pages[ targetIndex ],
\r
62 fieldsMapping[ i ][ targetIndex ] );
\r
64 targetField.setValue( sourceField.getValue() );
\r
68 var findDialog = function( editor, startupPage )
\r
70 // Style object for highlights.
\r
71 var highlightStyle = new CKEDITOR.style( editor.config.find_highlight );
\r
74 * Iterator which walk through the specified range char by char. By
\r
75 * default the walking will not stop at the character boundaries, until
\r
76 * the end of the range is encountered.
\r
77 * @param { CKEDITOR.dom.range } range
\r
78 * @param {Boolean} matchWord Whether the walking will stop at character boundary.
\r
80 var characterWalker = function( range , matchWord )
\r
83 new CKEDITOR.dom.walker( range );
\r
84 walker[ matchWord ? 'guard' : 'evaluator' ] =
\r
85 guardDomWalkerNonEmptyTextNode;
\r
86 walker.breakOnFalse = true;
\r
89 matchWord : matchWord,
\r
91 matchBoundary : false
\r
95 characterWalker.prototype = {
\r
103 return this.move( true );
\r
106 move : function( rtl )
\r
108 var currentTextNode = this.textNode;
\r
109 // Already at the end of document, no more character available.
\r
110 if ( currentTextNode === null )
\r
111 return cursorStep.call( this );
\r
113 this._.matchBoundary = false;
\r
115 // There are more characters in the text node, step forward.
\r
116 if ( currentTextNode
\r
118 && this.offset > 0 )
\r
121 return cursorStep.call( this );
\r
123 else if ( currentTextNode
\r
124 && this.offset < currentTextNode.getLength() - 1 )
\r
127 return cursorStep.call( this );
\r
131 currentTextNode = null;
\r
132 // At the end of the text node, walking foward for the next.
\r
133 while ( !currentTextNode )
\r
136 this._.walker[ rtl ? 'previous' : 'next' ].call( this._.walker );
\r
138 // Stop searching if we're need full word match OR
\r
139 // already reach document end.
\r
140 if ( this._.matchWord && !currentTextNode
\r
141 ||this._.walker._.end )
\r
144 // Marking as match character boundaries.
\r
145 if ( !currentTextNode
\r
146 && checkCharactersBoundary( this._.walker.current ) )
\r
147 this._.matchBoundary = true;
\r
150 // Found a fresh text node.
\r
151 this.textNode = currentTextNode;
\r
152 if ( currentTextNode )
\r
153 this.offset = rtl ? currentTextNode.getLength() - 1 : 0;
\r
158 return cursorStep.call( this );
\r
164 * A range of cursors which represent a trunk of characters which try to
\r
165 * match, it has the same length as the pattern string.
\r
167 var characterRange = function( characterWalker, rangeLength )
\r
170 walker : characterWalker,
\r
172 rangeLength : rangeLength,
\r
173 highlightRange : null,
\r
178 characterRange.prototype = {
\r
180 * Translate this range to {@link CKEDITOR.dom.range}
\r
182 toDomRange : function()
\r
184 var cursors = this._.cursors;
\r
185 if ( cursors.length < 1 )
\r
188 var first = cursors[0],
\r
189 last = cursors[ cursors.length - 1 ],
\r
190 range = new CKEDITOR.dom.range( editor.document );
\r
192 range.setStart( first.textNode, first.offset );
\r
193 range.setEnd( last.textNode, last.offset + 1 );
\r
197 * Reflect the latest changes from dom range.
\r
199 updateFromDomRange : function( domRange )
\r
202 walker = new characterWalker( domRange );
\r
203 this._.cursors = [];
\r
206 cursor = walker.next();
\r
207 if ( cursor.character )
\r
208 this._.cursors.push( cursor );
\r
210 while ( cursor.character );
\r
211 this._.rangeLength = this._.cursors.length;
\r
214 setMatched : function()
\r
216 this._.isMatched = true;
\r
219 clearMatched : function()
\r
221 this._.isMatched = false;
\r
224 isMatched : function()
\r
226 return this._.isMatched;
\r
230 * Hightlight the current matched chunk of text.
\r
232 highlight : function()
\r
234 // Do not apply if nothing is found.
\r
235 if ( this._.cursors.length < 1 )
\r
238 // Remove the previous highlight if there's one.
\r
239 if ( this._.highlightRange )
\r
240 this.removeHighlight();
\r
242 // Apply the highlight.
\r
243 var range = this.toDomRange();
\r
244 highlightStyle.applyToRange( range );
\r
245 this._.highlightRange = range;
\r
247 // Scroll the editor to the highlighted area.
\r
248 var element = range.startContainer;
\r
249 if ( element.type != CKEDITOR.NODE_ELEMENT )
\r
250 element = element.getParent();
\r
251 element.scrollIntoView();
\r
253 // Update the character cursors.
\r
254 this.updateFromDomRange( range );
\r
258 * Remove highlighted find result.
\r
260 removeHighlight : function()
\r
262 if ( !this._.highlightRange )
\r
265 highlightStyle.removeFromRange( this._.highlightRange );
\r
266 this.updateFromDomRange( this._.highlightRange );
\r
267 this._.highlightRange = null;
\r
270 moveBack : function()
\r
272 var retval = this._.walker.back(),
\r
273 cursors = this._.cursors;
\r
275 if ( retval.hitMatchBoundary )
\r
276 this._.cursors = cursors = [];
\r
278 cursors.unshift( retval );
\r
279 if ( cursors.length > this._.rangeLength )
\r
285 moveNext : function()
\r
287 var retval = this._.walker.next(),
\r
288 cursors = this._.cursors;
\r
290 // Clear the cursors queue if we've crossed a match boundary.
\r
291 if ( retval.hitMatchBoundary )
\r
292 this._.cursors = cursors = [];
\r
294 cursors.push( retval );
\r
295 if ( cursors.length > this._.rangeLength )
\r
301 getEndCharacter : function()
\r
303 var cursors = this._.cursors;
\r
304 if ( cursors.length < 1 )
\r
307 return cursors[ cursors.length - 1 ].character;
\r
310 getNextCharacterRange : function( maxLength )
\r
314 cursors = this._.cursors;
\r
316 if ( ( lastCursor = cursors[ cursors.length - 1 ] ) )
\r
317 nextRangeWalker = new characterWalker( getRangeAfterCursor( lastCursor ) );
\r
318 // In case it's an empty range (no cursors), figure out next range from walker (#4951).
\r
320 nextRangeWalker = this._.walker;
\r
322 return new characterRange( nextRangeWalker, maxLength );
\r
325 getCursors : function()
\r
327 return this._.cursors;
\r
332 // The remaining document range after the character cursor.
\r
333 function getRangeAfterCursor( cursor , inclusive )
\r
335 var range = new CKEDITOR.dom.range();
\r
336 range.setStart( cursor.textNode,
\r
337 ( inclusive ? cursor.offset : cursor.offset + 1 ) );
\r
338 range.setEndAt( editor.document.getBody(),
\r
339 CKEDITOR.POSITION_BEFORE_END );
\r
343 // The document range before the character cursor.
\r
344 function getRangeBeforeCursor( cursor )
\r
346 var range = new CKEDITOR.dom.range();
\r
347 range.setStartAt( editor.document.getBody(),
\r
348 CKEDITOR.POSITION_AFTER_START );
\r
349 range.setEnd( cursor.textNode, cursor.offset );
\r
353 var KMP_NOMATCH = 0,
\r
357 * Examination the occurrence of a word which implement KMP algorithm.
\r
359 var kmpMatcher = function( pattern, ignoreCase )
\r
361 var overlap = [ -1 ];
\r
363 pattern = pattern.toLowerCase();
\r
364 for ( var i = 0 ; i < pattern.length ; i++ )
\r
366 overlap.push( overlap[i] + 1 );
\r
367 while ( overlap[ i + 1 ] > 0
\r
368 && pattern.charAt( i ) != pattern
\r
369 .charAt( overlap[ i + 1 ] - 1 ) )
\r
370 overlap[ i + 1 ] = overlap[ overlap[ i + 1 ] - 1 ] + 1;
\r
376 ignoreCase : !!ignoreCase,
\r
381 kmpMatcher.prototype =
\r
383 feedCharacter : function( c )
\r
385 if ( this._.ignoreCase )
\r
386 c = c.toLowerCase();
\r
390 if ( c == this._.pattern.charAt( this._.state ) )
\r
393 if ( this._.state == this._.pattern.length )
\r
396 return KMP_MATCHED;
\r
398 return KMP_ADVANCED;
\r
400 else if ( !this._.state )
\r
401 return KMP_NOMATCH;
\r
403 this._.state = this._.overlap[ this._.state ];
\r
415 var wordSeparatorRegex =
\r
416 /[.,"'?!;: \u0085\u00a0\u1680\u280e\u2028\u2029\u202f\u205f\u3000]/;
\r
418 var isWordSeparator = function( c )
\r
422 var code = c.charCodeAt( 0 );
\r
423 return ( code >= 9 && code <= 0xd )
\r
424 || ( code >= 0x2000 && code <= 0x200a )
\r
425 || wordSeparatorRegex.test( c );
\r
429 searchRange : null,
\r
431 find : function( pattern, matchCase, matchWord, matchCyclic, highlightMatched, cyclicRerun )
\r
433 if ( !this.matchRange )
\r
435 new characterRange(
\r
436 new characterWalker( this.searchRange ),
\r
440 this.matchRange.removeHighlight();
\r
441 this.matchRange = this.matchRange.getNextCharacterRange( pattern.length );
\r
444 var matcher = new kmpMatcher( pattern, !matchCase ),
\r
445 matchState = KMP_NOMATCH,
\r
448 while ( character !== null )
\r
450 this.matchRange.moveNext();
\r
451 while ( ( character = this.matchRange.getEndCharacter() ) )
\r
453 matchState = matcher.feedCharacter( character );
\r
454 if ( matchState == KMP_MATCHED )
\r
456 if ( this.matchRange.moveNext().hitMatchBoundary )
\r
460 if ( matchState == KMP_MATCHED )
\r
464 var cursors = this.matchRange.getCursors(),
\r
465 tail = cursors[ cursors.length - 1 ],
\r
466 head = cursors[ 0 ];
\r
468 var headWalker = new characterWalker( getRangeBeforeCursor( head ), true ),
\r
469 tailWalker = new characterWalker( getRangeAfterCursor( tail ), true );
\r
471 if ( ! ( isWordSeparator( headWalker.back().character )
\r
472 && isWordSeparator( tailWalker.next().character ) ) )
\r
475 this.matchRange.setMatched();
\r
476 if ( highlightMatched !== false )
\r
477 this.matchRange.highlight();
\r
482 this.matchRange.clearMatched();
\r
483 this.matchRange.removeHighlight();
\r
484 // Clear current session and restart with the default search
\r
486 // Re-run the finding once for cyclic.(#3517)
\r
487 if ( matchCyclic && !cyclicRerun )
\r
489 this.searchRange = getSearchRange( true );
\r
490 this.matchRange = null;
\r
491 return arguments.callee.apply( this,
\r
492 Array.prototype.slice.call( arguments ).concat( [ true ] ) );
\r
499 * Record how much replacement occurred toward one replacing.
\r
501 replaceCounter : 0,
\r
503 replace : function( dialog, pattern, newString, matchCase, matchWord,
\r
504 matchCyclic , isReplaceAll )
\r
506 // Successiveness of current replace/find.
\r
507 var result = false;
\r
509 // 1. Perform the replace when there's already a match here.
\r
510 // 2. Otherwise perform the find but don't replace it immediately.
\r
511 if ( this.matchRange && this.matchRange.isMatched()
\r
512 && !this.matchRange._.isReplaced )
\r
514 // Turn off highlight for a while when saving snapshots.
\r
515 this.matchRange.removeHighlight();
\r
516 var domRange = this.matchRange.toDomRange();
\r
517 var text = editor.document.createText( newString );
\r
518 if ( !isReplaceAll )
\r
520 // Save undo snaps before and after the replacement.
\r
521 var selection = editor.getSelection();
\r
522 selection.selectRanges( [ domRange ] );
\r
523 editor.fire( 'saveSnapshot' );
\r
525 domRange.deleteContents();
\r
526 domRange.insertNode( text );
\r
527 if ( !isReplaceAll )
\r
529 selection.selectRanges( [ domRange ] );
\r
530 editor.fire( 'saveSnapshot' );
\r
532 this.matchRange.updateFromDomRange( domRange );
\r
533 if ( !isReplaceAll )
\r
534 this.matchRange.highlight();
\r
535 this.matchRange._.isReplaced = true;
\r
536 this.replaceCounter++;
\r
540 result = this.find( pattern, matchCase, matchWord, matchCyclic, !isReplaceAll );
\r
547 * The range in which find/replace happened, receive from user
\r
550 function getSearchRange( isDefault )
\r
553 sel = editor.getSelection(),
\r
554 body = editor.document.getBody();
\r
555 if ( sel && !isDefault )
\r
557 searchRange = sel.getRanges()[ 0 ].clone();
\r
558 searchRange.collapse( true );
\r
562 searchRange = new CKEDITOR.dom.range();
\r
563 searchRange.setStartAt( body, CKEDITOR.POSITION_AFTER_START );
\r
565 searchRange.setEndAt( body, CKEDITOR.POSITION_BEFORE_END );
\r
566 return searchRange;
\r
570 title : editor.lang.findAndReplace.title,
\r
571 resizable : CKEDITOR.DIALOG_RESIZE_NONE,
\r
574 buttons : [ CKEDITOR.dialog.cancelButton ], //Cancel button only.
\r
578 label : editor.lang.findAndReplace.find,
\r
579 title : editor.lang.findAndReplace.find,
\r
584 widths : [ '230px', '90px' ],
\r
589 id : 'txtFindFind',
\r
590 label : editor.lang.findAndReplace.findWhat,
\r
592 labelLayout : 'horizontal',
\r
598 style : 'width:100%',
\r
599 label : editor.lang.findAndReplace.find,
\r
600 onClick : function()
\r
602 var dialog = this.getDialog();
\r
603 if ( !finder.find( dialog.getValueOf( 'find', 'txtFindFind' ),
\r
604 dialog.getValueOf( 'find', 'txtFindCaseChk' ),
\r
605 dialog.getValueOf( 'find', 'txtFindWordChk' ),
\r
606 dialog.getValueOf( 'find', 'txtFindCyclic' ) ) )
\r
607 alert( editor.lang.findAndReplace
\r
620 id : 'txtFindCaseChk',
\r
622 style : 'margin-top:28px',
\r
623 label : editor.lang.findAndReplace.matchCase
\r
627 id : 'txtFindWordChk',
\r
629 label : editor.lang.findAndReplace.matchWord
\r
633 id : 'txtFindCyclic',
\r
636 label : editor.lang.findAndReplace.matchCyclic
\r
644 label : editor.lang.findAndReplace.replace,
\r
649 widths : [ '230px', '90px' ],
\r
654 id : 'txtFindReplace',
\r
655 label : editor.lang.findAndReplace.findWhat,
\r
657 labelLayout : 'horizontal',
\r
663 style : 'width:100%',
\r
664 label : editor.lang.findAndReplace.replace,
\r
665 onClick : function()
\r
667 var dialog = this.getDialog();
\r
668 if ( !finder.replace( dialog,
\r
669 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
670 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
671 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
672 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
673 dialog.getValueOf( 'replace', 'txtReplaceCyclic' ) ) )
\r
674 alert( editor.lang.findAndReplace
\r
682 widths : [ '230px', '90px' ],
\r
688 label : editor.lang.findAndReplace.replaceWith,
\r
690 labelLayout : 'horizontal',
\r
696 style : 'width:100%',
\r
697 label : editor.lang.findAndReplace.replaceAll,
\r
699 onClick : function()
\r
701 var dialog = this.getDialog();
\r
704 finder.replaceCounter = 0;
\r
706 // Scope to full document.
\r
707 finder.searchRange = getSearchRange( true );
\r
708 if ( finder.matchRange )
\r
710 finder.matchRange.removeHighlight();
\r
711 finder.matchRange = null;
\r
713 editor.fire( 'saveSnapshot' );
\r
714 while ( finder.replace( dialog,
\r
715 dialog.getValueOf( 'replace', 'txtFindReplace' ),
\r
716 dialog.getValueOf( 'replace', 'txtReplace' ),
\r
717 dialog.getValueOf( 'replace', 'txtReplaceCaseChk' ),
\r
718 dialog.getValueOf( 'replace', 'txtReplaceWordChk' ),
\r
722 if ( finder.replaceCounter )
\r
724 alert( editor.lang.findAndReplace.replaceSuccessMsg.replace( /%1/, finder.replaceCounter ) );
\r
725 editor.fire( 'saveSnapshot' );
\r
728 alert( editor.lang.findAndReplace.notFoundMsg );
\r
740 id : 'txtReplaceCaseChk',
\r
742 label : editor.lang.findAndReplace
\r
747 id : 'txtReplaceWordChk',
\r
749 label : editor.lang.findAndReplace
\r
754 id : 'txtReplaceCyclic',
\r
757 label : editor.lang.findAndReplace
\r
765 onLoad : function()
\r
769 //keep track of the current pattern field in use.
\r
770 var patternField, wholeWordChkField;
\r
772 //Ignore initial page select on dialog show
\r
773 var isUserSelect = false;
\r
774 this.on('hide', function()
\r
776 isUserSelect = false;
\r
778 this.on('show', function()
\r
780 isUserSelect = true;
\r
783 this.selectPage = CKEDITOR.tools.override( this.selectPage, function( originalFunc )
\r
785 return function( pageId )
\r
787 originalFunc.call( dialog, pageId );
\r
789 var currPage = dialog._.tabs[ pageId ];
\r
790 var patternFieldInput, patternFieldId, wholeWordChkFieldId;
\r
791 patternFieldId = pageId === 'find' ? 'txtFindFind' : 'txtFindReplace';
\r
792 wholeWordChkFieldId = pageId === 'find' ? 'txtFindWordChk' : 'txtReplaceWordChk';
\r
794 patternField = dialog.getContentElement( pageId,
\r
796 wholeWordChkField = dialog.getContentElement( pageId,
\r
797 wholeWordChkFieldId );
\r
799 // prepare for check pattern text filed 'keyup' event
\r
800 if ( !currPage.initialized )
\r
802 patternFieldInput = CKEDITOR.document
\r
803 .getById( patternField._.inputId );
\r
804 currPage.initialized = true;
\r
807 if ( isUserSelect )
\r
808 // synchronize fields on tab switch.
\r
809 syncFieldsBetweenTabs.call( this, pageId );
\r
814 onShow : function()
\r
816 // Establish initial searching start position.
\r
817 finder.searchRange = getSearchRange();
\r
819 this.selectPage( startupPage );
\r
821 onHide : function()
\r
823 if ( finder.matchRange && finder.matchRange.isMatched() )
\r
825 finder.matchRange.removeHighlight();
\r
827 editor.getSelection().selectRanges(
\r
828 [ finder.matchRange.toDomRange() ] );
\r
831 // Clear current session before dialog close
\r
832 delete finder.matchRange;
\r
834 onFocus : function()
\r
836 if ( startupPage == 'replace' )
\r
837 return this.getContentElement( 'replace', 'txtFindReplace' );
\r
839 return this.getContentElement( 'find', 'txtFindFind' );
\r
844 CKEDITOR.dialog.add( 'find', function( editor )
\r
846 return findDialog( editor, 'find' );
\r
849 CKEDITOR.dialog.add( 'replace', function( editor )
\r
851 return findDialog( editor, 'replace' );
\r