Skip to content

Commit 631fbce

Browse files
JoelEinbinderJoel Einbinder
andauthored
fix(chromium): select all on macos should work again (#3006)
* fix(chromium): select all on macos should work again * Update src/chromium/crInput.ts Co-authored-by: Joel Einbinder <[email protected]>
1 parent 9140063 commit 631fbce

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

src/chromium/crBrowser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class CRBrowser extends BrowserBase {
4040
_backgroundPages = new Map<string, CRPage>();
4141
_serviceWorkers = new Map<string, CRServiceWorker>();
4242
_devtools?: CRDevTools;
43+
_isMac = false;
4344

4445
private _tracingRecording = false;
4546
private _tracingPath: string | null = '';
@@ -50,6 +51,8 @@ export class CRBrowser extends BrowserBase {
5051
const browser = new CRBrowser(connection, options);
5152
browser._devtools = devtools;
5253
const session = connection.rootSession;
54+
const version = await session.send('Browser.getVersion');
55+
browser._isMac = version.userAgent.includes('Macintosh');
5356
if (!options.persistent) {
5457
await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });
5558
return browser;

src/chromium/crInput.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import * as input from '../input';
1919
import * as types from '../types';
2020
import { CRSession } from './crConnection';
21+
import { macEditingCommands } from '../macEditingCommands';
22+
import { helper } from '../helper';
2123

2224
function toModifiersMask(modifiers: Set<types.KeyboardModifier>): number {
2325
let mask = 0;
@@ -33,18 +35,36 @@ function toModifiersMask(modifiers: Set<types.KeyboardModifier>): number {
3335
}
3436

3537
export class RawKeyboardImpl implements input.RawKeyboard {
36-
private _client: CRSession;
38+
constructor(
39+
private _client: CRSession,
40+
private _isMac: boolean,
41+
) { }
3742

38-
constructor(client: CRSession) {
39-
this._client = client;
43+
_commandsForCode(code: string, modifiers: Set<types.KeyboardModifier>) {
44+
if (!this._isMac)
45+
return [];
46+
const parts = [];
47+
for (const modifier of (['Shift', 'Control', 'Alt', 'Meta']) as types.KeyboardModifier[]) {
48+
if (modifiers.has(modifier))
49+
parts.push(modifier);
50+
}
51+
parts.push(code);
52+
const shortcut = parts.join('+');
53+
let commands = macEditingCommands[shortcut] || [];
54+
if (helper.isString(commands))
55+
commands = [commands];
56+
// remove the trailing : to match the Chromium command names.
57+
return commands.map(c => c.substring(0, c.length - 1));
4058
}
4159

4260
async keydown(modifiers: Set<types.KeyboardModifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise<void> {
61+
const commands = this._commandsForCode(code, modifiers);
4362
await this._client.send('Input.dispatchKeyEvent', {
4463
type: text ? 'keyDown' : 'rawKeyDown',
4564
modifiers: toModifiersMask(modifiers),
4665
windowsVirtualKeyCode: keyCodeWithoutLocation,
4766
code,
67+
commands,
4868
key,
4969
text,
5070
unmodifiedText: text,

src/chromium/crPage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class CRPage implements PageDelegate {
6565
constructor(client: CRSession, targetId: string, browserContext: CRBrowserContext, opener: CRPage | null, hasUIWindow: boolean) {
6666
this._targetId = targetId;
6767
this._opener = opener;
68-
this.rawKeyboard = new RawKeyboardImpl(client);
68+
this.rawKeyboard = new RawKeyboardImpl(client, browserContext._browser._isMac);
6969
this.rawMouse = new RawMouseImpl(client);
7070
this._pdf = new CRPDF(client);
7171
this._coverage = new CRCoverage(client);

test/keyboard.jest.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe('Keyboard', function() {
290290
await textarea.type('👹 Tokyo street Japan 🇯🇵');
291291
expect(await frame.$eval('textarea', textarea => textarea.value)).toBe('👹 Tokyo street Japan 🇯🇵');
292292
});
293-
it.skip(CHROMIUM && MAC)('should handle selectAll', async ({page, server}) => {
293+
it('should handle selectAll', async ({page, server}) => {
294294
await page.goto(server.PREFIX + '/input/textarea.html');
295295
const textarea = await page.$('textarea');
296296
await textarea.type('some text');
@@ -318,6 +318,15 @@ describe('Keyboard', function() {
318318
await page.keyboard.press('Backspace');
319319
expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some tex');
320320
});
321+
it.skip(!MAC)('should support MacOS shortcuts', async ({page, server}) => {
322+
await page.goto(server.PREFIX + '/input/textarea.html');
323+
const textarea = await page.$('textarea');
324+
await textarea.type('some text');
325+
// select one word backwards
326+
await page.keyboard.press('Shift+Control+Alt+KeyB');
327+
await page.keyboard.press('Backspace');
328+
expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some ');
329+
});
321330
it('should press the meta key', async ({page}) => {
322331
const lastEvent = await captureLastKeydown(page);
323332
await page.keyboard.press('Meta');

0 commit comments

Comments
 (0)