-
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.
Merge pull request #2898 from microsoft/u/juliaroldi/dom-creator
Use DOMCreator instead of TrustedHTMLHandler
- Loading branch information
Showing
24 changed files
with
223 additions
and
95 deletions.
There are no files selected for viewing
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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
packages/roosterjs-content-model-core/lib/utils/domCreator.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,44 @@ | ||
import type { | ||
DOMCreator, | ||
LegacyTrustedHTMLHandler, | ||
TrustedHTMLHandler, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const createTrustedHTMLHandler = (domCreator: DOMCreator): LegacyTrustedHTMLHandler => { | ||
return (html: string) => domCreator.htmlToDOM(html).body.innerHTML; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function createDOMCreator(trustedHTMLHandler?: TrustedHTMLHandler): DOMCreator { | ||
return trustedHTMLHandler && isDOMCreator(trustedHTMLHandler) | ||
? trustedHTMLHandler | ||
: trustedHTMLHandlerToDOMCreator(trustedHTMLHandler as LegacyTrustedHTMLHandler); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function isDOMCreator( | ||
trustedHTMLHandler: TrustedHTMLHandler | ||
): trustedHTMLHandler is DOMCreator { | ||
return typeof (trustedHTMLHandler as DOMCreator).htmlToDOM === 'function'; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const defaultTrustHtmlHandler: LegacyTrustedHTMLHandler = (html: string) => { | ||
return html; | ||
}; | ||
|
||
function trustedHTMLHandlerToDOMCreator(trustedHTMLHandler?: LegacyTrustedHTMLHandler): DOMCreator { | ||
const handler = trustedHTMLHandler || defaultTrustHtmlHandler; | ||
return { | ||
htmlToDOM: (html: string) => new DOMParser().parseFromString(handler(html), 'text/html'), | ||
}; | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
packages/roosterjs-content-model-core/test/utils/domCreatorTest.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,38 @@ | ||
import { createDOMCreator, isDOMCreator } from '../../lib/utils/domCreator'; | ||
|
||
describe('domCreator', () => { | ||
it('isDOMCreator - True', () => { | ||
const trustedHTMLHandler = { | ||
htmlToDOM: (html: string) => new DOMParser().parseFromString(html, 'text/html'), | ||
}; | ||
expect(isDOMCreator(trustedHTMLHandler)).toBe(true); | ||
}); | ||
|
||
it('isDOMCreator - False', () => { | ||
const trustedHTMLHandler = (html: string) => html; | ||
expect(isDOMCreator(trustedHTMLHandler)).toBe(false); | ||
}); | ||
|
||
it('createDOMCreator - isDOMCreator', () => { | ||
const trustedHTMLHandler = { | ||
htmlToDOM: (html: string) => new DOMParser().parseFromString(html, 'text/html'), | ||
}; | ||
const result = createDOMCreator(trustedHTMLHandler); | ||
expect(result).toEqual(trustedHTMLHandler); | ||
}); | ||
|
||
it('createDOMCreator - undefined', () => { | ||
const doc = document.implementation.createHTMLDocument(); | ||
doc.body.appendChild(document.createTextNode('test')); | ||
const result = createDOMCreator(undefined).htmlToDOM('test'); | ||
expect(result.lastChild).toEqual(doc.lastChild); | ||
}); | ||
|
||
it('createDOMCreator - trustedHTML', () => { | ||
const doc = document.implementation.createHTMLDocument(); | ||
doc.body.appendChild(document.createTextNode('test trusted')); | ||
const trustedHTMLHandler = (html: string) => html + ' trusted'; | ||
const result = createDOMCreator(trustedHTMLHandler).htmlToDOM('test'); | ||
expect(result.lastChild).toEqual(doc.lastChild); | ||
}); | ||
}); |
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
Oops, something went wrong.