Skip to content

Commit

Permalink
Fix #2807 (#2809)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan Valverde U <[email protected]>
  • Loading branch information
JiuqingSong and BryanValverdeU authored Sep 26, 2024
1 parent 9d3deb2 commit 33531e1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { addSegment } from '../../modelApi/common/addSegment';
import { createBr } from '../../modelApi/creators/createBr';
import { getRegularSelectionOffsets } from '../utils/getRegularSelectionOffsets';
import type { ElementProcessor } from 'roosterjs-content-model-types';

/**
* @internal
*/
export const brProcessor: ElementProcessor<HTMLBRElement> = (group, element, context) => {
const br = createBr(context.segmentFormat);
const [start, end] = getRegularSelectionOffsets(context, element);

if (start >= 0) {
context.isInSelection = true;
}

if (context.isInSelection) {
br.isSelected = true;
}

const paragraph = addSegment(group, br, context.blockFormat);

if (end >= 0) {
context.isInSelection = false;
}

context.domIndexer?.onSegment(element, paragraph, [br]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,74 @@ describe('brProcessor', () => {
});
expect(onSegmentSpy).toHaveBeenCalledWith(br, paragraphModel, [brModel]);
});

it('Selection starts in BR', () => {
const doc = createContentModelDocument();
const div = document.createElement('div');
const br = document.createElement('br');
const range = document.createRange();

div.appendChild(br);
range.setStart(br, 0);
range.setEnd(div, 1);
context.selection = {
type: 'range',
range: range,
isReverted: false,
};

brProcessor(doc, br, context);

const brModel: ContentModelBr = {
segmentType: 'Br',
format: {},
isSelected: true,
};
const paragraphModel: ContentModelParagraph = {
blockType: 'Paragraph',
isImplicit: true,
segments: [brModel],
format: {},
};

expect(doc).toEqual({
blockGroupType: 'Document',
blocks: [paragraphModel],
});
expect(context.isInSelection).toBeTrue();
});

it('Selection ends in BR', () => {
const doc = createContentModelDocument();
const br = document.createElement('br');
const range = document.createRange();

range.setEnd(br, 0);
context.selection = {
type: 'range',
range: range,
isReverted: false,
};
context.isInSelection = true;

brProcessor(doc, br, context);

const brModel: ContentModelBr = {
segmentType: 'Br',
format: {},
isSelected: true,
};
const paragraphModel: ContentModelParagraph = {
blockType: 'Paragraph',
isImplicit: true,
segments: [brModel],
format: {},
};

expect(doc).toEqual({
blockGroupType: 'Document',
blocks: [paragraphModel],
});
expect(context.isInSelection).toBeFalse();
});
});

0 comments on commit 33531e1

Please sign in to comment.