UNPKG

@wordpress/e2e-test-utils

Version:
153 lines (152 loc) 5.71 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var press_key_with_modifier_exports = {}; __export(press_key_with_modifier_exports, { pressKeyWithModifier: () => pressKeyWithModifier, setClipboardData: () => setClipboardData }); module.exports = __toCommonJS(press_key_with_modifier_exports); var import_change_case = require("change-case"); var import_keycodes = require("@wordpress/keycodes"); async function emulateSelectAll() { await page.evaluate(() => { const isMac = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform); const canvasDoc = document.activeElement.contentDocument ?? document; canvasDoc.activeElement.dispatchEvent( new KeyboardEvent("keydown", { bubbles: true, cancelable: true, key: isMac ? "Meta" : "Control", code: isMac ? "MetaLeft" : "ControlLeft", location: window.KeyboardEvent.DOM_KEY_LOCATION_LEFT, getModifierState: (keyArg) => keyArg === (isMac ? "Meta" : "Control"), ctrlKey: !isMac, metaKey: isMac, charCode: 0, keyCode: isMac ? 93 : 17, which: isMac ? 93 : 17 }) ); const preventableEvent = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, key: "a", code: "KeyA", location: window.KeyboardEvent.DOM_KEY_LOCATION_STANDARD, getModifierState: (keyArg) => keyArg === (isMac ? "Meta" : "Control"), ctrlKey: !isMac, metaKey: isMac, charCode: 0, keyCode: 65, which: 65 }); const wasPrevented = !canvasDoc.activeElement.dispatchEvent(preventableEvent) || preventableEvent.defaultPrevented; if (!wasPrevented) { canvasDoc.execCommand("selectall", false, null); } canvasDoc.activeElement.dispatchEvent( new KeyboardEvent("keyup", { bubbles: true, cancelable: true, key: isMac ? "Meta" : "Control", code: isMac ? "MetaLeft" : "ControlLeft", location: window.KeyboardEvent.DOM_KEY_LOCATION_LEFT, getModifierState: () => false, charCode: 0, keyCode: isMac ? 93 : 17, which: isMac ? 93 : 17 }) ); }); } async function setClipboardData({ plainText = "", html = "" }) { await page.evaluate( (_plainText, _html) => { window._clipboardData = new DataTransfer(); window._clipboardData.setData("text/plain", _plainText); window._clipboardData.setData("text/html", _html); }, plainText, html ); } async function emulateClipboard(type) { await page.evaluate((_type) => { const canvasDoc = document.activeElement.contentDocument ?? document; if (_type !== "paste") { window._clipboardData = new DataTransfer(); const selection = canvasDoc.defaultView.getSelection(); const plainText = selection.toString(); let html = plainText; if (selection.rangeCount) { const range = selection.getRangeAt(0); const fragment = range.cloneContents(); html = Array.from(fragment.childNodes).map((node) => node.outerHTML || node.nodeValue).join(""); } window._clipboardData.setData("text/plain", plainText); window._clipboardData.setData("text/html", html); } canvasDoc.activeElement.dispatchEvent( new ClipboardEvent(_type, { bubbles: true, cancelable: true, clipboardData: window._clipboardData }) ); }, type); } async function pressKeyWithModifier(modifier, key) { if (modifier.toLowerCase() === "primary" && key.toLowerCase() === "a") { return await emulateSelectAll(); } if (modifier.toLowerCase() === "primary" && key.toLowerCase() === "c") { return await emulateClipboard("copy"); } if (modifier.toLowerCase() === "primary" && key.toLowerCase() === "x") { return await emulateClipboard("cut"); } if (modifier.toLowerCase() === "primary" && key.toLowerCase() === "v") { return await emulateClipboard("paste"); } const isAppleOS = () => process.platform === "darwin"; const overWrittenModifiers = { ...import_keycodes.modifiers, shiftAlt: (_isApple) => _isApple() ? [import_keycodes.SHIFT, import_keycodes.ALT] : [import_keycodes.SHIFT, import_keycodes.CTRL] }; const mappedModifiers = overWrittenModifiers[modifier](isAppleOS); const ctrlSwap = (mod) => mod === import_keycodes.CTRL ? "control" : mod; await Promise.all( mappedModifiers.map(async (mod) => { const capitalizedMod = (0, import_change_case.capitalCase)(ctrlSwap(mod)); return page.keyboard.down(capitalizedMod); }) ); await page.keyboard.press(key); await Promise.all( mappedModifiers.map(async (mod) => { const capitalizedMod = (0, import_change_case.capitalCase)(ctrlSwap(mod)); return page.keyboard.up(capitalizedMod); }) ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { pressKeyWithModifier, setClipboardData }); //# sourceMappingURL=press-key-with-modifier.js.map