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

Fixes copy/paste of images from browsers like Google Chrome #1000

Closed
Closed
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
6 changes: 3 additions & 3 deletions src/trix/controllers/level_2_input_controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Trix.Level2InputController extends Trix.InputController
# Safe to remove each condition once fixed upstream.
paste: (event) ->
# https://bugs.webkit.org/show_bug.cgi?id=194921
if pasteEventHasFilesOnly(event)
if pasteEventHasAFile(event)
event.preventDefault()
@attachFiles(event.clipboardData.files)

Expand Down Expand Up @@ -415,10 +415,10 @@ class Trix.Level2InputController extends Trix.InputController
dragEventHasFiles = (event) ->
"Files" in (event.dataTransfer?.types ? [])

pasteEventHasFilesOnly = (event) ->
pasteEventHasAFile = (event) ->
if clipboard = event.clipboardData
"Files" in clipboard.types and
clipboard.types.length is 1 and
clipboard.types.length <= 2 and
clipboard.files.length >= 1

pasteEventHasPlainTextOnly = (event) ->
Expand Down
15 changes: 14 additions & 1 deletion test/src/system/level_2_input_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ testGroup "Level 2 Input", testOptions, ->
assert.textAttributes([0, url.length], href: url)
expectDocument "#{url}\n"

# Pastes from Google Chrome include text/html + a file, we want the file!
# https://input-inspector.javan.us/profiles/r1s8c7DqbOQXXjz76mj0
test "pasting image copied from Google Chrome", (expectDocument) ->
createFile (file) ->
clipboardData = dataTransfer = createDataTransfer
"text/html": """<meta charset='utf-8'><img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_light_color_272x92dp.png" alt="Google"/>"""
"Files": [file]

paste {dataTransfer}, ->
attachments = getDocument().getAttachments()
assert.equal attachments.length, 1
expectDocument ""

# Pastes from MS Word include an image of the copied text 🙃
# https://input-inspector.now.sh/profiles/QWDITsV60dpEVl1SOZg8
test "pasting text from MS Word", (expectDocument) ->
Expand All @@ -183,7 +196,7 @@ testGroup "Level 2 Input", testOptions, ->
paste {dataTransfer}, ->
attachments = getDocument().getAttachments()
assert.equal attachments.length, 0
expectDocument "abc\n"
expectDocument "abc\n"

# "beforeinput" event is not fired for Paste and Match Style operations
# - https://bugs.chromium.org/p/chromium/issues/detail?id=934448
Expand Down