Repository: gchq/CyberChef
File: src/web/waiters/InputWaiter.mjs (JavaScript)

Code before:

                // Keymap
                keymap.of([
                    // Explicitly insert a tab rather than indenting the line
                    { key: "Tab", run: insertTab },
                    // Explicitly insert a new line (using the current EOL char) rather
                    // than messing around with indenting, which does not respect EOL chars
                    { key: "Enter", run: insertNewline },
                    ...historyKeymap,
                    ...defaultKeymap,
                    ...searchKeymap
                ]),

                // Event listeners
                EditorView.updateListener.of(e => {
                    if (e.selectionSet)
                        this.manager.highlighter.selectionChange("input", e);
                    if (e.docChanged && !this.silentInputChange)
                        this.inputChange(e);
                    this.silentInputChange = false;
                }),

                // Event handlers
                EditorView.domEventHandlers({
                    paste(event, view) {
                        const clipboardData = event.clipboardData || window.clipboardData;
                        const items = clipboardData.items;
                        event.target.files = [];
                        for (let i = 0; i < items.length; i++) {
                            const item = items[i];
                            if (item.type.indexOf("image") !== -1) {
                                const file = item.getAsFile();
                                event.target.files.push(file);

                                event.preventDefault(); // Prevent the default paste behavior
                            }
                        }
                        setTimeout(() => {
                            self.afterPaste(event);
                        });
                    }
                })
            ]
        });


        if (this.inputEditorView) this.inputEditorView.destroy();
        this.inputEditorView = new EditorView({
            state: initialState,
            parent: this.inputTextEl
        });

Review comment (suggestion):
@t-martine what is the purpose of `window.clipboardData` here? I cannot find any documentation for that property.
`ClipboardEvent.clipboardData` is baseline widely available, so I would have thought it is fine to just use that: https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent/clipboardData#browser_compatibility

Code after:

                // Keymap
                keymap.of([
                    // Explicitly insert a tab rather than indenting the line
                    { key: "Tab", run: insertTab },
                    // Explicitly insert a new line (using the current EOL char) rather
                    // than messing around with indenting, which does not respect EOL chars
                    { key: "Enter", run: insertNewline },
                    ...historyKeymap,
                    ...defaultKeymap,
                    ...searchKeymap
                ]),

                // Event listeners
                EditorView.updateListener.of(e => {
                    if (e.selectionSet)
                        this.manager.highlighter.selectionChange("input", e);
                    if (e.docChanged && !this.silentInputChange)
                        this.inputChange(e);
                    this.silentInputChange = false;
                }),

                // Event handlers
                EditorView.domEventHandlers({
                    paste(event, view) {
                        const clipboardData = event.clipboardData;
                        const items = clipboardData.items;
                        const files = [];
                        for (let i = 0; i < items.length; i++) {
                            const item = items[i];
                            if (item.kind === "file") {
                                const file = item.getAsFile();
                                files.push(file);

                                event.preventDefault(); // Prevent the default paste behavior
                            }
                        }
                        setTimeout(() => {
                            self.afterPaste(files);
                        });
                    }
                })
            ]
        });


        if (this.inputEditorView) this.inputEditorView.destroy();
        this.inputEditorView = new EditorView({
            state: initialState,
            parent: this.inputTextEl
        });