diff --git a/packages/roosterjs-content-model-core/lib/command/createModelFromHtml/createDomToModelContextForSanitizing.ts b/packages/roosterjs-content-model-core/lib/command/createModelFromHtml/createDomToModelContextForSanitizing.ts index b88c24ae8af..d3d62bc0d5d 100644 --- a/packages/roosterjs-content-model-core/lib/command/createModelFromHtml/createDomToModelContextForSanitizing.ts +++ b/packages/roosterjs-content-model-core/lib/command/createModelFromHtml/createDomToModelContextForSanitizing.ts @@ -6,6 +6,7 @@ import { getRootComputedStyleForContext } from '../../coreApi/createEditorContex import { pasteBlockEntityParser } from '../../override/pasteCopyBlockEntityParser'; import { pasteDisplayFormatParser } from '../../override/pasteDisplayFormatParser'; import { pasteTextProcessor } from '../../override/pasteTextProcessor'; +import { pasteWhiteSpaceFormatParser } from '../../override/pasteWhiteSpaceFormatParser'; import type { ContentModelSegmentFormat, DomToModelContext, @@ -52,6 +53,7 @@ export function createDomToModelContextForSanitizing( }, formatParserOverride: { display: pasteDisplayFormatParser, + whiteSpace: pasteWhiteSpaceFormatParser, }, additionalFormatParsers: { container: [containerSizeFormatParser], diff --git a/packages/roosterjs-content-model-core/lib/override/pasteWhiteSpaceFormatParser.ts b/packages/roosterjs-content-model-core/lib/override/pasteWhiteSpaceFormatParser.ts new file mode 100644 index 00000000000..893c1fcef12 --- /dev/null +++ b/packages/roosterjs-content-model-core/lib/override/pasteWhiteSpaceFormatParser.ts @@ -0,0 +1,17 @@ +import type { FormatParser, WhiteSpaceFormat } from 'roosterjs-content-model-types'; + +const WhiteSpacePre = 'pre'; + +/** + * @internal + */ +export const pasteWhiteSpaceFormatParser: FormatParser = ( + format, + element, + context, + defaultStyle +) => { + if (element.style.whiteSpace != WhiteSpacePre) { + context.defaultFormatParsers.whiteSpace?.(format, element, context, defaultStyle); + } +}; diff --git a/packages/roosterjs-content-model-core/test/command/createModelFromHtml/createDomToModelContextForSanitizingTest.ts b/packages/roosterjs-content-model-core/test/command/createModelFromHtml/createDomToModelContextForSanitizingTest.ts index c98a5d0422e..9c406bf9c49 100644 --- a/packages/roosterjs-content-model-core/test/command/createModelFromHtml/createDomToModelContextForSanitizingTest.ts +++ b/packages/roosterjs-content-model-core/test/command/createModelFromHtml/createDomToModelContextForSanitizingTest.ts @@ -7,6 +7,7 @@ import { DomToModelOptionForSanitizing } from 'roosterjs-content-model-types'; import { pasteBlockEntityParser } from '../../../lib/override/pasteCopyBlockEntityParser'; import { pasteDisplayFormatParser } from '../../../lib/override/pasteDisplayFormatParser'; import { pasteTextProcessor } from '../../../lib/override/pasteTextProcessor'; +import { pasteWhiteSpaceFormatParser } from '../../../lib/override/pasteWhiteSpaceFormatParser'; describe('createDomToModelContextForSanitizing', () => { const mockedPasteGeneralProcessor = 'GENERALPROCESSOR' as any; @@ -61,6 +62,7 @@ describe('createDomToModelContextForSanitizing', () => { }, formatParserOverride: { display: pasteDisplayFormatParser, + whiteSpace: pasteWhiteSpaceFormatParser, }, additionalFormatParsers: { container: [containerSizeFormatParser], @@ -106,6 +108,7 @@ describe('createDomToModelContextForSanitizing', () => { }, formatParserOverride: { display: pasteDisplayFormatParser, + whiteSpace: pasteWhiteSpaceFormatParser, }, additionalFormatParsers: { container: [containerSizeFormatParser], diff --git a/packages/roosterjs-content-model-core/test/overrides/pasteWhiteSpaceFormatParserTest.ts b/packages/roosterjs-content-model-core/test/overrides/pasteWhiteSpaceFormatParserTest.ts new file mode 100644 index 00000000000..d153176d9ab --- /dev/null +++ b/packages/roosterjs-content-model-core/test/overrides/pasteWhiteSpaceFormatParserTest.ts @@ -0,0 +1,39 @@ +import { pasteWhiteSpaceFormatParser } from '../../lib/override/pasteWhiteSpaceFormatParser'; +import { WhiteSpaceFormat } from 'roosterjs-content-model-types/lib'; + +describe('pasteWhiteSpaceFormatParser', () => { + let format: WhiteSpaceFormat; + let element: HTMLElement; + let context: any; + let defaultStyle: any; + let defaultParserSpy: jasmine.Spy; + + beforeEach(() => { + format = {}; + element = document.createElement('div'); + defaultParserSpy = jasmine.createSpy(); + context = { + defaultFormatParsers: { + whiteSpace: defaultParserSpy, + }, + }; + defaultStyle = {}; + }); + + it('should call default whiteSpace parser when element.style.whiteSpace is not "pre"', () => { + element.style.whiteSpace = 'normal'; + pasteWhiteSpaceFormatParser(format, element, context, defaultStyle); + expect(context.defaultFormatParsers.whiteSpace).toHaveBeenCalledWith( + format, + element, + context, + defaultStyle + ); + }); + + it('should not call default whiteSpace parser when element.style.whiteSpace is "pre"', () => { + element.style.whiteSpace = 'pre'; + pasteWhiteSpaceFormatParser(format, element, context, defaultStyle); + expect(context.defaultFormatParsers.whiteSpace).not.toHaveBeenCalled(); + }); +});