Skip to content

Commit 409c65e

Browse files
authored
Trust HTML for rich text clipboard (#6755)
1 parent 4e1a3f4 commit 409c65e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
"@types/prismjs": "^1.26.0",
137137
"@types/react": "^18.0.8",
138138
"@types/react-dom": "^18.0.3",
139+
"@types/trusted-types": "^2.0.7",
139140
"@typescript-eslint/eslint-plugin": "^7.8.0",
140141
"@typescript-eslint/parser": "^7.8.0",
141142
"child-process-promise": "^2.2.1",

packages/lexical-clipboard/src/clipboard.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ export function $insertDataTransferForRichText(
154154
if (htmlString) {
155155
try {
156156
const parser = new DOMParser();
157-
const dom = parser.parseFromString(htmlString, 'text/html');
157+
const dom = parser.parseFromString(
158+
trustHTML(htmlString) as string,
159+
'text/html',
160+
);
158161
const nodes = $generateNodesFromDOM(editor, dom);
159162
return $insertGeneratedNodes(editor, nodes, selection);
160163
} catch {
@@ -192,6 +195,16 @@ export function $insertDataTransferForRichText(
192195
}
193196
}
194197

198+
function trustHTML(html: string): string | TrustedHTML {
199+
if (window.trustedTypes && window.trustedTypes.createPolicy) {
200+
const policy = window.trustedTypes.createPolicy('lexical', {
201+
createHTML: (input) => input,
202+
});
203+
return policy.createHTML(html);
204+
}
205+
return html;
206+
}
207+
195208
/**
196209
* Inserts Lexical nodes into the editor using different strategies depending on
197210
* some simple selection-based heuristics. If you're looking for a generic way to

0 commit comments

Comments
 (0)