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

Fix copy/paste images in Chrome #1081

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions src/test/system/level_2_input_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ testGroup("Level 2 Input", testOptions, () => {
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", async () => {
const file = await createFile()
const clipboardData = 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 ],
})

await paste({ clipboardData })
const attachments = getDocument().getAttachments()
assert.equal(attachments.length, 1)
expectDocument(`${OBJECT_REPLACEMENT_CHARACTER}\n`)
})

// Pastes from MS Word include an image of the copied text 🙃
// https://input-inspector.now.sh/profiles/QWDITsV60dpEVl1SOZg8
test("pasting text from MS Word", async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/trix/controllers/level_2_input_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Level2InputController extends InputController {
// https://bugs.webkit.org/show_bug.cgi?id=194921
let paste
const href = event.clipboardData?.getData("URL")
if (pasteEventHasFilesOnly(event)) {
if (pasteEventHasAFile(event)) {
event.preventDefault()
return this.attachFiles(event.clipboardData.files)

Expand Down Expand Up @@ -577,10 +577,10 @@ const staticRangeToRange = function(staticRange) {

const dragEventHasFiles = (event) => Array.from(event.dataTransfer?.types || []).includes("Files")

const pasteEventHasFilesOnly = function(event) {
const pasteEventHasAFile = function(event) {
const clipboard = event.clipboardData
if (clipboard) {
return clipboard.types.includes("Files") && clipboard.types.length === 1 && clipboard.files.length >= 1
return clipboard.types.includes("Files") && clipboard.types.length <= 2 && clipboard.files.length >= 1
}
}

Expand Down