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

add support for CSP nonces in createStyleTag #2601

Merged
merged 1 commit into from
May 13, 2022
Merged
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
16 changes: 16 additions & 0 deletions docs/api/editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ new Editor({
})
```

### injectNonce
When you use a [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) with `nonce`, you can specify a `nonce` to be added to dynamically created elements. Here is an example:

```js
import { Editor } from '@tiptap/core'
import StarterKit from '@tiptap/starter-kit'

new Editor({
extensions: [
StarterKit,
],
injectCSS: true,
injectNonce: "your-nonce-here"
})
```

### editorProps
For advanced use cases, you can pass `editorProps` which will be handled by [ProseMirror](https://prosemirror.net/docs/ref/#view.EditorProps). You can use it to override various editor events or change editor DOM element attributes, for example to add some Tailwind classes. Here is an example:

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Editor extends EventEmitter<EditorEvents> {
element: document.createElement('div'),
content: '',
injectCSS: true,
injectNonce: undefined,
extensions: [],
autofocus: false,
editable: true,
Expand Down Expand Up @@ -136,7 +137,7 @@ export class Editor extends EventEmitter<EditorEvents> {
*/
private injectCSS(): void {
if (this.options.injectCSS && document) {
this.css = createStyleTag(style)
this.css = createStyleTag(style, this.options.injectNonce)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface EditorOptions {
content: Content,
extensions: Extensions,
injectCSS: boolean,
injectNonce: string | undefined,
autofocus: FocusPosition,
editable: boolean,
editorProps: EditorProps,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utilities/createStyleTag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function createStyleTag(style: string): HTMLStyleElement {
export function createStyleTag(style: string, nonce?: string): HTMLStyleElement {
const tipTapStyleTag = (<HTMLStyleElement>document.querySelector('style[data-tiptap-style]'))

if (tipTapStyleTag !== null) {
Expand All @@ -7,6 +7,10 @@ export function createStyleTag(style: string): HTMLStyleElement {

const styleNode = document.createElement('style')

if (nonce) {
styleNode.setAttribute('nonce', nonce)
}

styleNode.setAttribute('data-tiptap-style', '')
styleNode.innerHTML = style
document.getElementsByTagName('head')[0].appendChild(styleNode)
Expand Down