Skip to content

Commit

Permalink
#349 Ignore cut, copy and paste events that do not support clipboardData
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentGert committed Aug 14, 2019
1 parent 3c69008 commit 851e8fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/MentionsInput.js
Expand Up @@ -324,6 +324,9 @@ class MentionsInput extends React.Component {
if (event.target !== this.inputRef) {
return
}
if (!this.supportsClipboardActions(event)) {
return
}

event.preventDefault()

Expand Down Expand Up @@ -385,10 +388,17 @@ class MentionsInput extends React.Component {
)
}

supportsClipboardActions(event) {
return !!event.clipboardData
}

handleCopy(event) {
if (event.target !== this.inputRef) {
return
}
if (!this.supportsClipboardActions(event)) {
return
}

event.preventDefault()

Expand All @@ -399,6 +409,9 @@ class MentionsInput extends React.Component {
if (event.target !== this.inputRef) {
return
}
if (!this.supportsClipboardActions(event)) {
return
}

event.preventDefault()

Expand Down
45 changes: 45 additions & 0 deletions src/MentionsInput.spec.js
Expand Up @@ -281,6 +281,30 @@ describe('MentionsInput', () => {
}
)

it.each(['cut', 'copy'])(
'should fallback to the browsers behaviour if the "%s" event does not support clipboardData',
eventType => {
// IE 11 has no clipboardData attached to the event and only supports mime type "text"
// therefore, the new mechanism should ignore those events and let the browser handle them
const textarea = component.find('textarea')

const selectionStart = plainTextValue.indexOf('First') + 2
const selectionEnd = plainTextValue.length

textarea.simulate('select', {
target: { selectionStart, selectionEnd },
})

const preventDefault = jest.fn()
const event = new Event(eventType, { bubbles: true })
event.preventDefault = preventDefault

textarea.getDOMNode().dispatchEvent(event)

expect(preventDefault).not.toHaveBeenCalled()
}
)

it('should remove a leading mention from the value when the text is cut.', () => {
const onChange = jest.fn()

Expand Down Expand Up @@ -392,5 +416,26 @@ describe('MentionsInput', () => {
expect(newValue).toMatchSnapshot()
expect(newPlainTextValue).toMatchSnapshot()
})

it('should fallback to the browsers behaviour if the "paste" event does not support clipboardData', () => {
// IE 11 has no clipboardData attached to the event and only supports mime type "text"
// therefore, the new mechanism should ignore those events and let the browser handle them
const textarea = component.find('textarea')

const selectionStart = plainTextValue.indexOf('First') + 2
const selectionEnd = plainTextValue.length

textarea.simulate('select', {
target: { selectionStart, selectionEnd },
})

const preventDefault = jest.fn()
const event = new Event('paste', { bubbles: true })
event.preventDefault = preventDefault

textarea.getDOMNode().dispatchEvent(event)

expect(preventDefault).not.toHaveBeenCalled()
})
})
})

0 comments on commit 851e8fe

Please sign in to comment.