fix: clipboard.read() to recognize custom types (#31623)
This commit is contained in:
parent
a938af3f54
commit
5899a72df9
3 changed files with 25 additions and 17 deletions
|
@ -197,7 +197,7 @@ Returns `Boolean` - Whether the clipboard supports the specified `format`.
|
|||
```js
|
||||
const { clipboard } = require('electron')
|
||||
|
||||
const hasFormat = clipboard.has('<p>selection</p>')
|
||||
const hasFormat = clipboard.has('public/utf8-plain-text')
|
||||
console.log(hasFormat)
|
||||
// 'true' or 'false'
|
||||
```
|
||||
|
|
|
@ -51,17 +51,7 @@ bool Clipboard::Has(const std::string& format_string,
|
|||
|
||||
std::string Clipboard::Read(const std::string& format_string) {
|
||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||
ui::ClipboardFormatType format(
|
||||
ui::ClipboardFormatType::CustomPlatformType(format_string));
|
||||
|
||||
std::string data;
|
||||
clipboard->ReadData(format, /* data_dst = */ nullptr, &data);
|
||||
return data;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
|
||||
gin_helper::Arguments* args) {
|
||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||
std::map<std::string, std::string> custom_format_names;
|
||||
custom_format_names =
|
||||
clipboard->ExtractCustomPlatformNames(ui::ClipboardBuffer::kCopyPaste,
|
||||
|
@ -73,12 +63,24 @@ v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
|
|||
/* data_dst = */ nullptr);
|
||||
}
|
||||
#endif
|
||||
CHECK(custom_format_names.find(format_string) != custom_format_names.end());
|
||||
ui::ClipboardFormatType format(ui::ClipboardFormatType::CustomPlatformType(
|
||||
custom_format_names[format_string]));
|
||||
|
||||
ui::ClipboardFormatType format;
|
||||
if (custom_format_names.find(format_string) != custom_format_names.end()) {
|
||||
format =
|
||||
ui::ClipboardFormatType(ui::ClipboardFormatType::CustomPlatformType(
|
||||
custom_format_names[format_string]));
|
||||
} else {
|
||||
format = ui::ClipboardFormatType(
|
||||
ui::ClipboardFormatType::CustomPlatformType(format_string));
|
||||
}
|
||||
std::string data;
|
||||
clipboard->ReadData(format, /* data_dst = */ nullptr, &data);
|
||||
return data;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> Clipboard::ReadBuffer(const std::string& format_string,
|
||||
gin_helper::Arguments* args) {
|
||||
std::string data = Read(format_string);
|
||||
return node::Buffer::Copy(args->isolate(), data.data(), data.length())
|
||||
.ToLocalChecked();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const { expect } = require('chai');
|
||||
const path = require('path');
|
||||
const { Buffer } = require('buffer');
|
||||
const { ifdescribe } = require('./spec-helpers');
|
||||
const { ifdescribe, ifit } = require('./spec-helpers');
|
||||
|
||||
const { clipboard, nativeImage } = require('electron');
|
||||
|
||||
|
@ -62,14 +62,20 @@ ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard
|
|||
});
|
||||
});
|
||||
|
||||
ifdescribe(process.platform !== 'linux')('clipboard.read()', () => {
|
||||
it('does not crash when reading various custom clipboard types', () => {
|
||||
describe('clipboard.read()', () => {
|
||||
ifit(process.platform !== 'linux')('does not crash when reading various custom clipboard types', () => {
|
||||
const type = process.platform === 'darwin' ? 'NSFilenamesPboardType' : 'FileNameW';
|
||||
|
||||
expect(() => {
|
||||
const result = clipboard.read(type);
|
||||
}).to.not.throw();
|
||||
});
|
||||
it('can read data written with writeBuffer', () => {
|
||||
const testText = 'Testing read';
|
||||
const buffer = Buffer.from(testText, 'utf8');
|
||||
clipboard.writeBuffer('public/utf8-plain-text', buffer);
|
||||
expect(clipboard.read('public/utf8-plain-text')).to.equal(testText);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clipboard.write()', () => {
|
||||
|
|
Loading…
Reference in a new issue