-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pasteWhiteSpaceFormatParser and integrate it into sanitizing context
- Loading branch information
1 parent
480c729
commit ec8f039
Showing
6 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...tent-model-core/lib/command/createModelFromHtml/createDomToModelContextForSanitizing.1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { containerSizeFormatParser } from 'roosterjs-content-model-core/lib/override/containerSizeFormatParser'; | ||
import { createDomToModelContext } from 'roosterjs-content-model-dom/lib'; | ||
import { createPasteEntityProcessor } from 'roosterjs-content-model-core/lib/override/pasteEntityProcessor'; | ||
import { createPasteGeneralProcessor } from 'roosterjs-content-model-core/lib/override/pasteGeneralProcessor'; | ||
import { DefaultSanitizingOption } from './createDomToModelContextForSanitizing'; | ||
import { getRootComputedStyleForContext } from 'roosterjs-content-model-core/lib/coreApi/createEditorContext/getRootComputedStyleForContext'; | ||
import { pasteBlockEntityParser } from 'roosterjs-content-model-core/lib/override/pasteCopyBlockEntityParser'; | ||
import { pasteDisplayFormatParser } from 'roosterjs-content-model-core/lib/override/pasteDisplayFormatParser'; | ||
import { pasteTextProcessor } from 'roosterjs-content-model-core/lib/override/pasteTextProcessor'; | ||
import type { | ||
ContentModelSegmentFormat, | ||
DomToModelOption, | ||
DomToModelOptionForSanitizing, | ||
DomToModelContext, | ||
} from 'roosterjs-content-model-types/lib'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
|
||
export function createDomToModelContextForSanitizing( | ||
document: Document, | ||
defaultFormat?: ContentModelSegmentFormat, | ||
defaultOption?: DomToModelOption, | ||
additionalSanitizingOption?: Partial<DomToModelOptionForSanitizing> | ||
): DomToModelContext { | ||
const sanitizingOption: DomToModelOptionForSanitizing = { | ||
...DefaultSanitizingOption, | ||
...additionalSanitizingOption, | ||
}; | ||
|
||
return createDomToModelContext( | ||
{ | ||
defaultFormat, | ||
...getRootComputedStyleForContext(document), | ||
experimentalFeatures: [], | ||
}, | ||
defaultOption, | ||
{ | ||
processorOverride: { | ||
'#text': pasteTextProcessor, | ||
entity: createPasteEntityProcessor(sanitizingOption), | ||
'*': createPasteGeneralProcessor(sanitizingOption), | ||
}, | ||
formatParserOverride: { | ||
display: pasteDisplayFormatParser, | ||
whiteSpace: (format, element, context, defaultStyle) => { | ||
if (element.style.whiteSpace != 'pre') { | ||
context.defaultFormatParsers.whiteSpace?.( | ||
format, | ||
element, | ||
context, | ||
defaultStyle | ||
); | ||
} | ||
}, | ||
}, | ||
additionalFormatParsers: { | ||
container: [containerSizeFormatParser], | ||
entity: [pasteBlockEntityParser], | ||
}, | ||
}, | ||
sanitizingOption | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/roosterjs-content-model-core/lib/command/createModelFromHtml/createModelFromHtml.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/roosterjs-content-model-core/lib/command/paste/mergePasteContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/roosterjs-content-model-core/lib/override/pasteWhiteSpaceFormatParser.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { FormatParser, WhiteSpaceFormat } from 'roosterjs-content-model-types/lib'; | ||
|
||
const WhiteSpacePre = 'pre'; | ||
|
||
export const pasteWhiteSpaceFormatParser: FormatParser<WhiteSpaceFormat> = ( | ||
format, | ||
element, | ||
context, | ||
defaultStyle | ||
) => { | ||
if (element.style.whiteSpace != WhiteSpacePre) { | ||
context.defaultFormatParsers.whiteSpace?.(format, element, context, defaultStyle); | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/roosterjs-content-model-core/test/overrides/pasteWhiteSpaceFormatParserTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |