Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(core): advance rich-text replacement past inserted text (#7849)

Lucienc6bb8b6030cb
diff --git a/packages/core/src/docs/data-model/__tests__/replacement.spec.ts b/packages/core/src/docs/data-model/__tests__/replacement.spec.ts
index 841897373b..736cda8958 100644
--- a/packages/core/src/docs/data-model/__tests__/replacement.spec.ts
+++ b/packages/core/src/docs/data-model/__tests__/replacement.spec.ts
@@ -15,8 +15,10 @@
*/
import type { IDocumentBody } from '../../../types/interfaces';
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import { BooleanNumber } from '../../../types/enum/text-style';
+import { CustomRangeType } from '../../../types/interfaces/i-document-data';
+import { DocumentDataModel } from '../document-data-model';
import { replaceInDocumentBody } from '../replacement';
function getTestDocumentBody() {
@@ -92,6 +94,58 @@ function getTestDocumentBody() {
}
describe('test case in replaceInDocumentBody utils', () => {
+ it.each([
+ { input: 'cat cat', query: 'cat', target: 'cat', caseSensitive: true, expected: 'cat cat' },
+ { input: 'cat cat', query: 'cat', target: 'cats', caseSensitive: true, expected: 'cats cats' },
+ { input: 'cat CAT', query: 'cat', target: 'Cat', caseSensitive: false, expected: 'Cat Cat' },
+ { input: 'cat CAT', query: 'CAT', target: 'dog', caseSensitive: false, expected: 'dog dog' },
+ { input: 'İcat cat', query: 'CAT', target: 'dog', caseSensitive: false, expected: 'İdog dog' },
+ { input: 'cat CAT', query: 'cat', target: 'dog', caseSensitive: true, expected: 'dog CAT' },
+ { input: 'aaaa', query: 'aa', target: 'a', caseSensitive: true, expected: 'aa' },
+ { input: 'catcat dog cat', query: 'cat', target: '', caseSensitive: true, expected: ' dog ' },
+ { input: 'a.b a+b', query: 'a.b', target: '$&', caseSensitive: true, expected: '$& a+b' },
+ ])('replaces original matches once: $input / $query -> $target ($caseSensitive)', ({ input, query, target, caseSensitive, expected }) => {
+ const originalApply = DocumentDataModel.prototype.apply;
+ let replacements = 0;
+ const applySpy = vi.spyOn(DocumentDataModel.prototype, 'apply').mockImplementation(function (this: DocumentDataModel, actions) {
+ // Bound the old non-terminating implementation so regressions fail without hanging Vitest.
+ if (++replacements > 20) throw new Error('Replacement did not advance past inserted text');
+ return originalApply.call(this, actions);
+ });
+
+ try {
+ const body = { dataStream: `${input}\r\n` };
+ expect(replaceInDocumentBody(body, query, target, caseSensitive).dataStream).toBe(`${expected}\r\n`);
+ } finally {
+ applySpy.mockRestore();
+ }
+ });
+
+ it('preserves styles and hyperlinks while replacing multiple matches', () => {
+ const body: IDocumentBody = {
+ dataStream: 'cat cat\r\n',
+ textRuns: [{ st: 0, ed: 7, ts: { bl: BooleanNumber.TRUE } }],
+ customRanges: [{
+ startIndex: 0,
+ endIndex: 2,
+ rangeId: 'link-1',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: { url: 'https://univer.ai' },
+ }],
+ };
+
+ const result = replaceInDocumentBody(body, 'cat', 'doggy', true);
+ expect(result.dataStream).toBe('doggy doggy\r\n');
+ expect(result.textRuns).toEqual([{ st: 0, ed: 11, ts: { bl: BooleanNumber.TRUE } }]);
+ expect(result.customRanges).toEqual([{
+ startIndex: 0,
+ endIndex: 4,
+ rangeId: 'link-1',
+ rangeType: CustomRangeType.HYPERLINK,
+ properties: { url: 'https://univer.ai' },
+ }]);
+ });
+
it('Should replace all `query` to `target`', () => {
const documentBody = getTestDocumentBody();
const expectedBody = {
diff --git a/packages/core/src/docs/data-model/replacement.ts b/packages/core/src/docs/data-model/replacement.ts
index be01937e10..5dabe7c8ff 100644
--- a/packages/core/src/docs/data-model/replacement.ts
+++ b/packages/core/src/docs/data-model/replacement.ts
@@ -15,6 +15,7 @@
*/
import type { IDocumentBody, IDocumentData } from '../../types/interfaces/i-document-data';
+import { createLiteralRegExp } from '../../common/regexp/escape';
import { DocumentDataModel } from './document-data-model';
import { JSONX } from './json-x/json-x';
import { TextX } from './text-x/text-x';
@@ -34,10 +35,12 @@ export function replaceInDocumentBody(body: IDocumentBody, query: string, target
const documentDataModel = new DocumentDataModel(mockDocumentData);
const queryLen = query.length;
- let index;
+ const expression = createLiteralRegExp(query, caseSensitive ? 'g' : 'gi');
+ let match;
// eslint-disable-next-line no-cond-assign
- while ((index = (caseSensitive ? documentDataModel.getBody()!.dataStream : documentDataModel.getBody()!.dataStream.toLowerCase()).indexOf(query)) >= 0) {
+ while ((match = expression.exec(documentDataModel.getBody()!.dataStream)) !== null) {
+ const index = match.index;
const textX = new TextX();
const jsonX = JSONX.getInstance();
@@ -73,6 +76,8 @@ export function replaceInDocumentBody(body: IDocumentBody, query: string, target
textX.delete(queryLen);
documentDataModel.apply(jsonX.editOp(textX.serialize()));
+ // Continue after the inserted text, including when the replacement is empty.
+ expression.lastIndex = index + target.length;
}
const newBody = documentDataModel.getBody()!;