Skip to content

Commit f0236c4

Browse files
committed
Remove mutations that is not under editor
1 parent 80d79be commit f0236c4

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/roosterjs-content-model-core/lib/corePlugin/cache/textMutationObserver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class TextMutationObserverImpl implements TextMutationObserver {
5959
continue;
6060
} else if (!includedNodes.has(target)) {
6161
if (
62+
!this.domHelper.isNodeInEditor(target) ||
6263
findClosestEntityWrapper(target, this.domHelper) ||
6364
findClosestBlockEntityContainer(target, this.domHelper)
6465
) {

packages/roosterjs-content-model-core/test/corePlugin/cache/textMutationObserverTest.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,55 @@ describe('TextMutationObserverImpl', () => {
384384
expect(onMutation).toHaveBeenCalledTimes(1);
385385
expect(onMutation).toHaveBeenCalledWith({ type: 'unknown' });
386386
});
387+
388+
it('Ignore changes that is not in editor - add node', () => {
389+
const div = document.createElement('div');
390+
const span1 = document.createElement('span');
391+
const span2 = document.createElement('span');
392+
393+
div.appendChild(span1);
394+
395+
const onMutation = jasmine.createSpy('onMutation');
396+
397+
observer = textMutationObserver.createTextMutationObserver(div, onMutation);
398+
observer.startObserving();
399+
400+
span1.textContent = 'test1';
401+
span2.textContent = 'test2';
402+
403+
observer.flushMutations();
404+
405+
expect(onMutation).toHaveBeenCalledTimes(1);
406+
expect(onMutation).toHaveBeenCalledWith({
407+
type: 'childList',
408+
addedNodes: [span1.firstChild],
409+
removedNodes: [],
410+
});
411+
});
412+
413+
it('Ignore changes that is not in editor - remove node', () => {
414+
const div = document.createElement('div');
415+
const span1 = document.createElement('span');
416+
const span2 = document.createElement('span');
417+
418+
span1.appendChild(span2);
419+
div.appendChild(span1);
420+
421+
const onMutation = jasmine.createSpy('onMutation');
422+
423+
observer = textMutationObserver.createTextMutationObserver(div, onMutation);
424+
observer.startObserving();
425+
426+
div.removeChild(span1);
427+
span1.removeChild(span2);
428+
429+
observer.flushMutations();
430+
431+
expect(onMutation).toHaveBeenCalledTimes(1);
432+
expect(onMutation).toHaveBeenCalledWith({
433+
type: 'childList',
434+
addedNodes: [],
435+
removedNodes: [span1],
436+
});
437+
});
387438
});

0 commit comments

Comments
 (0)