KeyboardEvent.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const KeyboardEventInit = require("./KeyboardEventInit.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const UIEvent = require("./UIEvent.js");
  8. const interfaceName = "KeyboardEvent";
  9. exports.is = value => {
  10. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  11. };
  12. exports.isImpl = value => {
  13. return utils.isObject(value) && value instanceof Impl.implementation;
  14. };
  15. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  16. if (exports.is(value)) {
  17. return utils.implForWrapper(value);
  18. }
  19. throw new globalObject.TypeError(`${context} is not of type 'KeyboardEvent'.`);
  20. };
  21. function makeWrapper(globalObject, newTarget) {
  22. let proto;
  23. if (newTarget !== undefined) {
  24. proto = newTarget.prototype;
  25. }
  26. if (!utils.isObject(proto)) {
  27. proto = globalObject[ctorRegistrySymbol]["KeyboardEvent"].prototype;
  28. }
  29. return Object.create(proto);
  30. }
  31. exports.create = (globalObject, constructorArgs, privateData) => {
  32. const wrapper = makeWrapper(globalObject);
  33. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  34. };
  35. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  36. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  37. return utils.implForWrapper(wrapper);
  38. };
  39. exports._internalSetup = (wrapper, globalObject) => {
  40. UIEvent._internalSetup(wrapper, globalObject);
  41. };
  42. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  43. privateData.wrapper = wrapper;
  44. exports._internalSetup(wrapper, globalObject);
  45. Object.defineProperty(wrapper, implSymbol, {
  46. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  47. configurable: true
  48. });
  49. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  50. if (Impl.init) {
  51. Impl.init(wrapper[implSymbol]);
  52. }
  53. return wrapper;
  54. };
  55. exports.new = (globalObject, newTarget) => {
  56. const wrapper = makeWrapper(globalObject, newTarget);
  57. exports._internalSetup(wrapper, globalObject);
  58. Object.defineProperty(wrapper, implSymbol, {
  59. value: Object.create(Impl.implementation.prototype),
  60. configurable: true
  61. });
  62. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  63. if (Impl.init) {
  64. Impl.init(wrapper[implSymbol]);
  65. }
  66. return wrapper[implSymbol];
  67. };
  68. const exposed = new Set(["Window"]);
  69. exports.install = (globalObject, globalNames) => {
  70. if (!globalNames.some(globalName => exposed.has(globalName))) {
  71. return;
  72. }
  73. const ctorRegistry = utils.initCtorRegistry(globalObject);
  74. class KeyboardEvent extends globalObject.UIEvent {
  75. constructor(type) {
  76. if (arguments.length < 1) {
  77. throw new globalObject.TypeError(
  78. `Failed to construct 'KeyboardEvent': 1 argument required, but only ${arguments.length} present.`
  79. );
  80. }
  81. const args = [];
  82. {
  83. let curArg = arguments[0];
  84. curArg = conversions["DOMString"](curArg, {
  85. context: "Failed to construct 'KeyboardEvent': parameter 1",
  86. globals: globalObject
  87. });
  88. args.push(curArg);
  89. }
  90. {
  91. let curArg = arguments[1];
  92. curArg = KeyboardEventInit.convert(globalObject, curArg, {
  93. context: "Failed to construct 'KeyboardEvent': parameter 2"
  94. });
  95. args.push(curArg);
  96. }
  97. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  98. }
  99. getModifierState(keyArg) {
  100. const esValue = this !== null && this !== undefined ? this : globalObject;
  101. if (!exports.is(esValue)) {
  102. throw new globalObject.TypeError(
  103. "'getModifierState' called on an object that is not a valid instance of KeyboardEvent."
  104. );
  105. }
  106. if (arguments.length < 1) {
  107. throw new globalObject.TypeError(
  108. `Failed to execute 'getModifierState' on 'KeyboardEvent': 1 argument required, but only ${arguments.length} present.`
  109. );
  110. }
  111. const args = [];
  112. {
  113. let curArg = arguments[0];
  114. curArg = conversions["DOMString"](curArg, {
  115. context: "Failed to execute 'getModifierState' on 'KeyboardEvent': parameter 1",
  116. globals: globalObject
  117. });
  118. args.push(curArg);
  119. }
  120. return esValue[implSymbol].getModifierState(...args);
  121. }
  122. initKeyboardEvent(typeArg) {
  123. const esValue = this !== null && this !== undefined ? this : globalObject;
  124. if (!exports.is(esValue)) {
  125. throw new globalObject.TypeError(
  126. "'initKeyboardEvent' called on an object that is not a valid instance of KeyboardEvent."
  127. );
  128. }
  129. if (arguments.length < 1) {
  130. throw new globalObject.TypeError(
  131. `Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': 1 argument required, but only ${arguments.length} present.`
  132. );
  133. }
  134. const args = [];
  135. {
  136. let curArg = arguments[0];
  137. curArg = conversions["DOMString"](curArg, {
  138. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 1",
  139. globals: globalObject
  140. });
  141. args.push(curArg);
  142. }
  143. {
  144. let curArg = arguments[1];
  145. if (curArg !== undefined) {
  146. curArg = conversions["boolean"](curArg, {
  147. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 2",
  148. globals: globalObject
  149. });
  150. } else {
  151. curArg = false;
  152. }
  153. args.push(curArg);
  154. }
  155. {
  156. let curArg = arguments[2];
  157. if (curArg !== undefined) {
  158. curArg = conversions["boolean"](curArg, {
  159. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 3",
  160. globals: globalObject
  161. });
  162. } else {
  163. curArg = false;
  164. }
  165. args.push(curArg);
  166. }
  167. {
  168. let curArg = arguments[3];
  169. if (curArg !== undefined) {
  170. if (curArg === null || curArg === undefined) {
  171. curArg = null;
  172. } else {
  173. curArg = utils.tryImplForWrapper(curArg);
  174. }
  175. } else {
  176. curArg = null;
  177. }
  178. args.push(curArg);
  179. }
  180. {
  181. let curArg = arguments[4];
  182. if (curArg !== undefined) {
  183. curArg = conversions["DOMString"](curArg, {
  184. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 5",
  185. globals: globalObject
  186. });
  187. } else {
  188. curArg = "";
  189. }
  190. args.push(curArg);
  191. }
  192. {
  193. let curArg = arguments[5];
  194. if (curArg !== undefined) {
  195. curArg = conversions["unsigned long"](curArg, {
  196. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 6",
  197. globals: globalObject
  198. });
  199. } else {
  200. curArg = 0;
  201. }
  202. args.push(curArg);
  203. }
  204. {
  205. let curArg = arguments[6];
  206. if (curArg !== undefined) {
  207. curArg = conversions["boolean"](curArg, {
  208. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 7",
  209. globals: globalObject
  210. });
  211. } else {
  212. curArg = false;
  213. }
  214. args.push(curArg);
  215. }
  216. {
  217. let curArg = arguments[7];
  218. if (curArg !== undefined) {
  219. curArg = conversions["boolean"](curArg, {
  220. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 8",
  221. globals: globalObject
  222. });
  223. } else {
  224. curArg = false;
  225. }
  226. args.push(curArg);
  227. }
  228. {
  229. let curArg = arguments[8];
  230. if (curArg !== undefined) {
  231. curArg = conversions["boolean"](curArg, {
  232. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 9",
  233. globals: globalObject
  234. });
  235. } else {
  236. curArg = false;
  237. }
  238. args.push(curArg);
  239. }
  240. {
  241. let curArg = arguments[9];
  242. if (curArg !== undefined) {
  243. curArg = conversions["boolean"](curArg, {
  244. context: "Failed to execute 'initKeyboardEvent' on 'KeyboardEvent': parameter 10",
  245. globals: globalObject
  246. });
  247. } else {
  248. curArg = false;
  249. }
  250. args.push(curArg);
  251. }
  252. return esValue[implSymbol].initKeyboardEvent(...args);
  253. }
  254. get key() {
  255. const esValue = this !== null && this !== undefined ? this : globalObject;
  256. if (!exports.is(esValue)) {
  257. throw new globalObject.TypeError(
  258. "'get key' called on an object that is not a valid instance of KeyboardEvent."
  259. );
  260. }
  261. return esValue[implSymbol]["key"];
  262. }
  263. get code() {
  264. const esValue = this !== null && this !== undefined ? this : globalObject;
  265. if (!exports.is(esValue)) {
  266. throw new globalObject.TypeError(
  267. "'get code' called on an object that is not a valid instance of KeyboardEvent."
  268. );
  269. }
  270. return esValue[implSymbol]["code"];
  271. }
  272. get location() {
  273. const esValue = this !== null && this !== undefined ? this : globalObject;
  274. if (!exports.is(esValue)) {
  275. throw new globalObject.TypeError(
  276. "'get location' called on an object that is not a valid instance of KeyboardEvent."
  277. );
  278. }
  279. return esValue[implSymbol]["location"];
  280. }
  281. get ctrlKey() {
  282. const esValue = this !== null && this !== undefined ? this : globalObject;
  283. if (!exports.is(esValue)) {
  284. throw new globalObject.TypeError(
  285. "'get ctrlKey' called on an object that is not a valid instance of KeyboardEvent."
  286. );
  287. }
  288. return esValue[implSymbol]["ctrlKey"];
  289. }
  290. get shiftKey() {
  291. const esValue = this !== null && this !== undefined ? this : globalObject;
  292. if (!exports.is(esValue)) {
  293. throw new globalObject.TypeError(
  294. "'get shiftKey' called on an object that is not a valid instance of KeyboardEvent."
  295. );
  296. }
  297. return esValue[implSymbol]["shiftKey"];
  298. }
  299. get altKey() {
  300. const esValue = this !== null && this !== undefined ? this : globalObject;
  301. if (!exports.is(esValue)) {
  302. throw new globalObject.TypeError(
  303. "'get altKey' called on an object that is not a valid instance of KeyboardEvent."
  304. );
  305. }
  306. return esValue[implSymbol]["altKey"];
  307. }
  308. get metaKey() {
  309. const esValue = this !== null && this !== undefined ? this : globalObject;
  310. if (!exports.is(esValue)) {
  311. throw new globalObject.TypeError(
  312. "'get metaKey' called on an object that is not a valid instance of KeyboardEvent."
  313. );
  314. }
  315. return esValue[implSymbol]["metaKey"];
  316. }
  317. get repeat() {
  318. const esValue = this !== null && this !== undefined ? this : globalObject;
  319. if (!exports.is(esValue)) {
  320. throw new globalObject.TypeError(
  321. "'get repeat' called on an object that is not a valid instance of KeyboardEvent."
  322. );
  323. }
  324. return esValue[implSymbol]["repeat"];
  325. }
  326. get isComposing() {
  327. const esValue = this !== null && this !== undefined ? this : globalObject;
  328. if (!exports.is(esValue)) {
  329. throw new globalObject.TypeError(
  330. "'get isComposing' called on an object that is not a valid instance of KeyboardEvent."
  331. );
  332. }
  333. return esValue[implSymbol]["isComposing"];
  334. }
  335. get charCode() {
  336. const esValue = this !== null && this !== undefined ? this : globalObject;
  337. if (!exports.is(esValue)) {
  338. throw new globalObject.TypeError(
  339. "'get charCode' called on an object that is not a valid instance of KeyboardEvent."
  340. );
  341. }
  342. return esValue[implSymbol]["charCode"];
  343. }
  344. get keyCode() {
  345. const esValue = this !== null && this !== undefined ? this : globalObject;
  346. if (!exports.is(esValue)) {
  347. throw new globalObject.TypeError(
  348. "'get keyCode' called on an object that is not a valid instance of KeyboardEvent."
  349. );
  350. }
  351. return esValue[implSymbol]["keyCode"];
  352. }
  353. }
  354. Object.defineProperties(KeyboardEvent.prototype, {
  355. getModifierState: { enumerable: true },
  356. initKeyboardEvent: { enumerable: true },
  357. key: { enumerable: true },
  358. code: { enumerable: true },
  359. location: { enumerable: true },
  360. ctrlKey: { enumerable: true },
  361. shiftKey: { enumerable: true },
  362. altKey: { enumerable: true },
  363. metaKey: { enumerable: true },
  364. repeat: { enumerable: true },
  365. isComposing: { enumerable: true },
  366. charCode: { enumerable: true },
  367. keyCode: { enumerable: true },
  368. [Symbol.toStringTag]: { value: "KeyboardEvent", configurable: true },
  369. DOM_KEY_LOCATION_STANDARD: { value: 0x00, enumerable: true },
  370. DOM_KEY_LOCATION_LEFT: { value: 0x01, enumerable: true },
  371. DOM_KEY_LOCATION_RIGHT: { value: 0x02, enumerable: true },
  372. DOM_KEY_LOCATION_NUMPAD: { value: 0x03, enumerable: true }
  373. });
  374. Object.defineProperties(KeyboardEvent, {
  375. DOM_KEY_LOCATION_STANDARD: { value: 0x00, enumerable: true },
  376. DOM_KEY_LOCATION_LEFT: { value: 0x01, enumerable: true },
  377. DOM_KEY_LOCATION_RIGHT: { value: 0x02, enumerable: true },
  378. DOM_KEY_LOCATION_NUMPAD: { value: 0x03, enumerable: true }
  379. });
  380. ctorRegistry[interfaceName] = KeyboardEvent;
  381. Object.defineProperty(globalObject, interfaceName, {
  382. configurable: true,
  383. writable: true,
  384. value: KeyboardEvent
  385. });
  386. };
  387. const Impl = require("../../jsdom/living/events/KeyboardEvent-impl.js");