diff --git a/packages/ckeditor5-find-and-replace/src/utils.js b/packages/ckeditor5-find-and-replace/src/utils.js index 8f71cd042cf..7ba137e04f6 100644 --- a/packages/ckeditor5-find-and-replace/src/utils.js +++ b/packages/ckeditor5-find-and-replace/src/utils.js @@ -33,20 +33,20 @@ import { escapeRegExp } from 'lodash-es'; export function updateFindResultFromRange( range, model, findCallback, startResults ) { const results = startResults || new Collection(); - [ ...range ].forEach( ( { type, item } ) => { - if ( type === 'elementStart' ) { - if ( model.schema.checkChild( item, '$text' ) ) { - const foundItems = findCallback( { - item, - text: rangeToText( model.createRangeIn( item ) ) - } ); - - if ( !foundItems ) { - return; - } + model.change( writer => { + [ ...range ].forEach( ( { type, item } ) => { + if ( type === 'elementStart' ) { + if ( model.schema.checkChild( item, '$text' ) ) { + const foundItems = findCallback( { + item, + text: rangeToText( model.createRangeIn( item ) ) + } ); + + if ( !foundItems ) { + return; + } - foundItems.forEach( foundItem => { - model.change( writer => { + foundItems.forEach( foundItem => { const resultId = `findResult:${ uid() }`; const marker = writer.addMarker( resultId, { usingOperation: false, @@ -68,9 +68,9 @@ export function updateFindResultFromRange( range, model, findCallback, startResu index ); } ); - } ); + } } - } + } ); } ); return results; diff --git a/packages/ckeditor5-find-and-replace/tests/findcommand.js b/packages/ckeditor5-find-and-replace/tests/findcommand.js index 7b92e2dcfb4..0f8c03e46e8 100644 --- a/packages/ckeditor5-find-and-replace/tests/findcommand.js +++ b/packages/ckeditor5-find-and-replace/tests/findcommand.js @@ -65,6 +65,17 @@ describe( 'FindCommand', () => { ); } ); + it( 'calls model.change() only once', () => { + setData( model, '[]Foo bar baz. Bam bar bar bar bar bom.' ); + const spy = sinon.spy( model, 'change' ); + + command.execute( 'bar' ); + + // It's called two additional times + // from 'change:highlightedResult' handler in FindAndReplaceEditing. + expect( spy.callCount ).to.equal( 3 ); + } ); + it( 'returns no result if nothing matched', () => { setData( model, '[]Foo bar baz. Bam bar bom.' );