Skip to content

Commit

Permalink
Merge branch 'master' into u/juliaroldi/image-edit-protected
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaroldi authored Sep 26, 2024
2 parents 135ad5b + 33531e1 commit a6b302a
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 a6b302a

Please sign in to comment.