From 6d09a5257ad86d991038a732d513ce7b6be32c7f Mon Sep 17 00:00:00 2001 From: David Wilson Date: Mon, 22 Jan 2018 18:39:18 -0800 Subject: [PATCH] Fix #16538: Middle-click pastes into read only editor on Linux This change fixes an issue where users on Linux are able to paste into read only TextEditors by clicking the middle mouse button. The fix is to check for whether the TextEditorComponent's isInputEnabled method returns true before pasting with middle click on Linux. --- spec/text-editor-component-spec.js | 27 ++++++++++++++++++++++++++- src/text-editor-component.js | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/spec/text-editor-component-spec.js b/spec/text-editor-component-spec.js index 3432a0fea..8a2dcb5fc 100644 --- a/spec/text-editor-component-spec.js +++ b/spec/text-editor-component-spec.js @@ -3374,6 +3374,31 @@ describe('TextEditorComponent', () => { }) expect(editor.lineTextForBufferRow(10)).toBe('var') }) + + it('does not paste into a read only editor when clicking the middle mouse button on Linux', async () => { + spyOn(electron.ipcRenderer, 'send').andCallFake(function (eventName, selectedText) { + if (eventName === 'write-text-to-selection-clipboard') { + clipboard.writeText(selectedText, 'selection') + } + }) + + const {component, editor} = buildComponent({platform: 'linux', readOnly: true}) + + // Select the word 'sort' on line 2 and copy to clipboard + editor.setSelectedBufferRange([[1, 6], [1, 10]]) + await conditionPromise(() => TextEditor.clipboard.read() === 'sort') + + // Middle-click in the buffer at line 11, column 1 + component.didMouseDownOnContent({ + button: 1, + clientX: clientLeftForCharacter(component, 10, 0), + clientY: clientTopForLine(component, 10) + }) + + // Ensure that the correct text was copied but not pasted + expect(TextEditor.clipboard.read()).toBe('sort') + expect(editor.lineTextForBufferRow(10)).toBe('') + }) }) describe('on the line number gutter', () => { @@ -4329,7 +4354,7 @@ describe('TextEditorComponent', () => { function buildEditor (params = {}) { const text = params.text != null ? params.text : SAMPLE_TEXT const buffer = new TextBuffer({text}) - const editorParams = {buffer} + const editorParams = {buffer, readOnly: params.readOnly} if (params.height != null) params.autoHeight = false for (const paramName of ['mini', 'autoHeight', 'autoWidth', 'lineNumberGutterVisible', 'showLineNumbers', 'placeholderText', 'softWrapped', 'scrollSensitivity']) { if (params[paramName] != null) editorParams[paramName] = params[paramName] diff --git a/src/text-editor-component.js b/src/text-editor-component.js index c88aab304..cb8b5c93a 100644 --- a/src/text-editor-component.js +++ b/src/text-editor-component.js @@ -1763,7 +1763,7 @@ class TextEditorComponent { // On Linux, pasting happens on middle click. A textInput event with the // contents of the selection clipboard will be dispatched by the browser // automatically on mouseup. - if (platform === 'linux' && button === 1) model.insertText(clipboard.readText('selection')) + if (platform === 'linux' && button === 1 && this.isInputEnabled()) model.insertText(clipboard.readText('selection')) return }