Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DOMCreator instead of TrustedHTMLHandler #2898

Merged
merged 6 commits into from
Dec 9, 2024

Conversation

juliaroldi
Copy link
Contributor

@juliaroldi juliaroldi commented Dec 3, 2024

The current trustedHTMLHandler converts a string into a trusted string, and if necessary, transforms the trusted string into a DOM. To optimize this process, introduce a new DOMCreator that directly converts a string into a trusted DOM. Consequently, the trustedHTMLHandler editor option will accept two types of handlers: one that transforms strings into trusted strings and another that converts strings into Document objects.

@juliaroldi juliaroldi marked this pull request as ready for review December 4, 2024 15:54

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 9 out of 24 changed files in this pull request and generated no suggestions.

Files not reviewed (15)
  • packages/roosterjs-content-model-plugins/test/paste/processPastedContentFromExcelTest.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/lib/paste/WordDesktop/processPastedContentFromWordDesktop.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/lib/paste/Excel/processPastedContentFromExcel.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/test/paste/ContentModelPastePluginTest.ts: Evaluated as low risk
  • packages/roosterjs-content-model-core/test/editor/core/createEditorCoreTest.ts: Evaluated as low risk
  • packages/roosterjs-content-model-core/lib/coreApi/restoreUndoSnapshot/restoreSnapshotHTML.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/lib/paste/WordDesktop/getStyleMetadata.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/lib/paste/PowerPoint/processPastedContentFromPowerPoint.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/test/paste/processPastedContentFromWordDesktopTest.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/test/paste/getStyleMetadataTest.ts: Evaluated as low risk
  • packages/roosterjs-content-model-core/lib/command/createModelFromHtml/createModelFromHtml.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/test/paste/processPastedContentFromPowerPointTest.ts: Evaluated as low risk
  • packages/roosterjs-content-model-core/lib/editor/Editor.ts: Evaluated as low risk
  • packages/roosterjs-content-model-plugins/lib/paste/PastePlugin.ts: Evaluated as low risk
  • packages/roosterjs-content-model-core/lib/command/paste/paste.ts: Evaluated as low risk
Comments skipped due to low confidence (1)

packages/roosterjs-content-model-types/lib/editor/EditorCore.ts:20

  • [nitpick] Consider renaming 'trustedHTMLHandler' to 'legacyTrustedHTMLHandler' for consistency and clarity.
import type { DOMCreator, LegacyTrustedHTMLHandler } from '../parameter/TrustedHTMLHandler';
@@ -1,4 +1,16 @@
/**
* A handler type to convert HTML string to a trust HTML string
*/
export type TrustedHTMLHandler = (html: string) => string;
export type LegacyTrustedHTMLHandler = (html: string) => string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mark this interface as "@deprecated" in comment. So next time we do major version bump, we will remove it.

@@ -193,12 +193,21 @@ export interface IEditor {
hasFocus(): boolean;

/**
* @deprecated use getDOMCreator instead
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good!

trustedHTMLHandler:
options.trustedHTMLHandler && !isDOMCreator(options.trustedHTMLHandler)
? options.trustedHTMLHandler
: defaultTrustHtmlHandler,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If options.trustedHTMLHandler is DOMCreator, we still need to create a valid trustedHTMLHandler, to satisfy the following case:

  1. Pass in a DOMCreator when init editor
  2. call editor.getTrustedHTMLHandler() and use it to sanitize HTML

You can create a function here to use DOMCreator() to return a DOM tree then get its innerHTML and return

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we integrate this into OWA, we can search all places where getTrustedHTMLHandler is used and replace with getDOMCreator(), so this new function should not really be used in OWA but other roosterjs customers may still need it

/**
* @internal
*/
export function domCreator(trustedHTMLHandler?: TrustedHTMLHandler): DOMCreator {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest name it createDOMCreator

@@ -18,6 +19,8 @@ import type {
*/
export function createEditorCore(contentDiv: HTMLDivElement, options: EditorOptions): EditorCore {
const corePlugins = createEditorCorePlugins(options, contentDiv);
const domCreator = createDOMCreator(options.trustedHTMLHandler);
const trustedHTMLHandler = createTrustedHTMLHandler(domCreator);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest create trustedHTMLHandler from options.trustedHTMLHandler directly. So in case user is passing in a legacy handler function, we can use it directly

@juliaroldi juliaroldi merged commit 65b880d into master Dec 9, 2024
7 checks passed
@juliaroldi juliaroldi deleted the u/juliaroldi/dom-creator branch December 9, 2024 13:51
juliaroldi added a commit that referenced this pull request Dec 10, 2024
Use DOMCreator instead of TrustedHTMLHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants