Skip to content

Commit

Permalink
fix(chromium): select all on macos should work again
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Einbinder committed Jul 17, 2020
1 parent d8bedd8 commit 2e43bac
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class CRBrowser extends BrowserBase {
_backgroundPages = new Map<string, CRPage>();
_serviceWorkers = new Map<string, CRServiceWorker>();
_devtools?: CRDevTools;
_isMac = false;

private _tracingRecording = false;
private _tracingPath: string | null = '';
Expand All @@ -50,6 +51,8 @@ export class CRBrowser extends BrowserBase {
const browser = new CRBrowser(connection, options);
browser._devtools = devtools;
const session = connection.rootSession;
const version = await session.send('Browser.getVersion');
browser._isMac = version.userAgent.includes('Macintosh');
if (!options.persistent) {
await session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true });
return browser;
Expand Down
26 changes: 23 additions & 3 deletions src/chromium/crInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import * as input from '../input';
import * as types from '../types';
import { CRSession } from './crConnection';
import { macEditingCommands } from '../macEditingCommands';
import { helper } from '../helper';

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

export class RawKeyboardImpl implements input.RawKeyboard {
private _client: CRSession;
constructor(
private _client: CRSession,
private _isMac: boolean,
) { }

constructor(client: CRSession) {
this._client = client;
_commandsForCode(code: string, modifiers: Set<types.KeyboardModifier>) {
if (!this._isMac)
return [];
const parts = [];
for (const modifier of (['Shift', 'Control', 'Alt', 'Meta']) as types.KeyboardModifier[]) {
if (modifiers.has(modifier))
parts.push(modifier);
}
parts.push(code);
const shortcut = parts.join('+');
let commands = (this._isMac && macEditingCommands[shortcut]) || [];
if (helper.isString(commands))
commands = [commands];
// remove the trailing : to match the Chromium command names.
return commands.map(c => c.substring(0, c.length - 1));
}

async keydown(modifiers: Set<types.KeyboardModifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise<void> {
const commands = this._commandsForCode(code, modifiers);
await this._client.send('Input.dispatchKeyEvent', {
type: text ? 'keyDown' : 'rawKeyDown',
modifiers: toModifiersMask(modifiers),
windowsVirtualKeyCode: keyCodeWithoutLocation,
code,
commands,
key,
text,
unmodifiedText: text,
Expand Down
2 changes: 1 addition & 1 deletion src/chromium/crPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class CRPage implements PageDelegate {
constructor(client: CRSession, targetId: string, browserContext: CRBrowserContext, opener: CRPage | null, hasUIWindow: boolean) {
this._targetId = targetId;
this._opener = opener;
this.rawKeyboard = new RawKeyboardImpl(client);
this.rawKeyboard = new RawKeyboardImpl(client, browserContext._browser._isMac);
this.rawMouse = new RawMouseImpl(client);
this._pdf = new CRPDF(client);
this._coverage = new CRCoverage(client);
Expand Down
11 changes: 10 additions & 1 deletion test/keyboard.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ describe('Keyboard', function() {
await textarea.type('👹 Tokyo street Japan 🇯🇵');
expect(await frame.$eval('textarea', textarea => textarea.value)).toBe('👹 Tokyo street Japan 🇯🇵');
});
it.skip(CHROMIUM && MAC)('should handle selectAll', async ({page, server}) => {
it('should handle selectAll', async ({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const textarea = await page.$('textarea');
await textarea.type('some text');
Expand Down Expand Up @@ -318,6 +318,15 @@ describe('Keyboard', function() {
await page.keyboard.press('Backspace');
expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some tex');
});
it.skip(!MAC)('should support MacOS shortcuts', async ({page, server}) => {
await page.goto(server.PREFIX + '/input/textarea.html');
const textarea = await page.$('textarea');
await textarea.type('some text');
// select one word backwards
await page.keyboard.press('Shift+Control+Alt+KeyB');
await page.keyboard.press('Backspace');
expect(await page.$eval('textarea', textarea => textarea.value)).toBe('some ');
});
it('should press the meta key', async ({page}) => {
const lastEvent = await captureLastKeydown(page);
await page.keyboard.press('Meta');
Expand Down

0 comments on commit 2e43bac

Please sign in to comment.