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

Trusted Types API violation fix #5561

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/utilities/createStyleTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function createStyleTag(style: string, nonce?: string, suffix?: string):
}

styleNode.setAttribute(`data-tiptap-style${suffix ? `-${suffix}` : ''}`, '')
styleNode.innerHTML = style
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually equivalent?

styleNode.textContent = style
document.getElementsByTagName('head')[0].appendChild(styleNode)

return styleNode
Expand Down
30 changes: 29 additions & 1 deletion packages/core/src/utilities/elementFromString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't make sense on why it's being repeated

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>`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does prosemirror support this because I am not willing to use this if prosemirror does not even support this.

A link to the source would be enough to convince me. Probably in prosemirror view since that touches the DOM


const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body

Expand Down