Skip to content

Commit

Permalink
fixup! [Clipboard API] Clipboard Custom Formats implementation Part 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
deepak1556 committed Jul 5, 2021
1 parent 6e3dd54 commit 3a1dbc6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
12 changes: 8 additions & 4 deletions docs/api/clipboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const { clipboard } = require('electron')

const hasFormat = clipboard.has('<p>selection</p>')
console.log(hasFormat)
// 'true' or 'false
// 'true' or 'false'
```

### `clipboard.read(format)` _Experimental_
Expand All @@ -208,6 +208,10 @@ console.log(hasFormat)

Returns `String` - Reads `format` type from the clipboard.

`format` should contain valid ASCII characters and have `/` separator.
`a/c`, `a/bc` are valid formats while `/abc`, `abc/`, `a/`, `/a`, `a`
are not valid.

### `clipboard.readBuffer(format)` _Experimental_

* `format` String
Expand All @@ -218,9 +222,9 @@ Returns `Buffer` - Reads `format` type from the clipboard.
const { clipboard } = require('electron')

const buffer = Buffer.from('this is binary', 'utf8')
clipboard.writeBuffer('public.utf8-plain-text', buffer)
clipboard.writeBuffer('public/utf8-plain-text', buffer)

const ret = clipboard.readBuffer('public.utf8-plain-text')
const ret = clipboard.readBuffer('public/utf8-plain-text')

console.log(buffer.equals(out))
// true
Expand All @@ -238,7 +242,7 @@ Writes the `buffer` into the clipboard as `format`.
const { clipboard } = require('electron')

const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public.utf8-plain-text', buffer)
clipboard.writeBuffer('public/utf8-plain-text', buffer)
```

### `clipboard.write(data[, type])`
Expand Down
4 changes: 3 additions & 1 deletion shell/common/api/electron_api_clipboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ bool Clipboard::Has(const std::string& format_string,
gin_helper::Arguments* args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::ClipboardFormatType format(
ui::ClipboardFormatType::GetCustomPlatformType(format_string));
ui::ClipboardFormatType::GetType(format_string));
if (format.GetName().empty())
format = ui::ClipboardFormatType::GetCustomPlatformType(format_string);
return clipboard->IsFormatAvailable(format, GetClipboardBuffer(args),
/* data_dst = */ nullptr);
}
Expand Down
13 changes: 3 additions & 10 deletions spec/api-clipboard-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ const { clipboard, nativeImage } = require('electron');
describe('clipboard module', () => {
const fixtures = path.resolve(__dirname, 'fixtures');

// FIXME(zcbenz): Clipboard tests are failing on WOA.
beforeEach(function () {
if (process.platform === 'win32' && process.arch === 'arm64') {
this.skip();
}
});

describe('clipboard.readImage()', () => {
it('returns NativeImage instance', () => {
const p = path.join(fixtures, 'assets', 'logo.png');
Expand Down Expand Up @@ -115,13 +108,13 @@ describe('clipboard module', () => {
describe('clipboard.readBuffer(format)', () => {
it('writes a Buffer for the specified format', function () {
const buffer = Buffer.from('writeBuffer', 'utf8');
clipboard.writeBuffer('public.utf8-plain-text', buffer);
expect(buffer.equals(clipboard.readBuffer('public.utf8-plain-text'))).to.equal(true);
clipboard.writeBuffer('public/utf8-plain-text', buffer);
expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
});

it('throws an error when a non-Buffer is specified', () => {
expect(() => {
clipboard.writeBuffer('public.utf8-plain-text', 'hello');
clipboard.writeBuffer('public/utf8-plain-text', 'hello');
}).to.throw(/buffer must be a node Buffer/);
});
});
Expand Down

0 comments on commit 3a1dbc6

Please sign in to comment.