mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-27 14:59:27 +08:00
Trusted Types API violation fix
This commit is contained in:
parent
9907eb32c2
commit
f69cf5ea28
@ -12,7 +12,7 @@ export function createStyleTag(style: string, nonce?: string, suffix?: string):
|
||||
}
|
||||
|
||||
styleNode.setAttribute(`data-tiptap-style${suffix ? `-${suffix}` : ''}`, '')
|
||||
styleNode.innerHTML = style
|
||||
styleNode.textContent = style
|
||||
document.getElementsByTagName('head')[0].appendChild(styleNode)
|
||||
|
||||
return styleNode
|
||||
|
@ -14,9 +14,37 @@ const removeWhitespaces = (node: HTMLElement) => {
|
||||
return node
|
||||
}
|
||||
|
||||
let policy = {
|
||||
createHTML: (input: any) => input,
|
||||
createScript: (input: any) => input,
|
||||
createScriptURL: (input: any) => input,
|
||||
}
|
||||
|
||||
try {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line no-undef
|
||||
policy = globalThis.trustedTypes.createPolicy('tiptap', {
|
||||
createHTML: (input: any) => input,
|
||||
createScript: (input: any) => input,
|
||||
createScriptURL: (input: any) => input,
|
||||
})
|
||||
} catch (error) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line no-undef
|
||||
if (window.trustedTypes) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line no-undef
|
||||
policy = window.trustedTypes.createPolicy('tiptap', {
|
||||
createHTML: (input: any) => input,
|
||||
createScript: (input: any) => input,
|
||||
createScriptURL: (input: any) => input,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function elementFromString(value: string): HTMLElement {
|
||||
// add a wrapper to preserve leading and trailing whitespace
|
||||
const wrappedValue = `<body>${value}</body>`
|
||||
const wrappedValue = policy.createHTML(`<body>${value}</body>`)
|
||||
|
||||
const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user