UIEvent.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const UIEventInit = require("./UIEventInit.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const Event = require("./Event.js");
  8. const interfaceName = "UIEvent";
  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 'UIEvent'.`);
  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]["UIEvent"].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. Event._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 UIEvent extends globalObject.Event {
  75. constructor(type) {
  76. if (arguments.length < 1) {
  77. throw new globalObject.TypeError(
  78. `Failed to construct 'UIEvent': 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 'UIEvent': parameter 1",
  86. globals: globalObject
  87. });
  88. args.push(curArg);
  89. }
  90. {
  91. let curArg = arguments[1];
  92. curArg = UIEventInit.convert(globalObject, curArg, { context: "Failed to construct 'UIEvent': parameter 2" });
  93. args.push(curArg);
  94. }
  95. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  96. }
  97. initUIEvent(typeArg) {
  98. const esValue = this !== null && this !== undefined ? this : globalObject;
  99. if (!exports.is(esValue)) {
  100. throw new globalObject.TypeError("'initUIEvent' called on an object that is not a valid instance of UIEvent.");
  101. }
  102. if (arguments.length < 1) {
  103. throw new globalObject.TypeError(
  104. `Failed to execute 'initUIEvent' on 'UIEvent': 1 argument required, but only ${arguments.length} present.`
  105. );
  106. }
  107. const args = [];
  108. {
  109. let curArg = arguments[0];
  110. curArg = conversions["DOMString"](curArg, {
  111. context: "Failed to execute 'initUIEvent' on 'UIEvent': parameter 1",
  112. globals: globalObject
  113. });
  114. args.push(curArg);
  115. }
  116. {
  117. let curArg = arguments[1];
  118. if (curArg !== undefined) {
  119. curArg = conversions["boolean"](curArg, {
  120. context: "Failed to execute 'initUIEvent' on 'UIEvent': parameter 2",
  121. globals: globalObject
  122. });
  123. } else {
  124. curArg = false;
  125. }
  126. args.push(curArg);
  127. }
  128. {
  129. let curArg = arguments[2];
  130. if (curArg !== undefined) {
  131. curArg = conversions["boolean"](curArg, {
  132. context: "Failed to execute 'initUIEvent' on 'UIEvent': parameter 3",
  133. globals: globalObject
  134. });
  135. } else {
  136. curArg = false;
  137. }
  138. args.push(curArg);
  139. }
  140. {
  141. let curArg = arguments[3];
  142. if (curArg !== undefined) {
  143. if (curArg === null || curArg === undefined) {
  144. curArg = null;
  145. } else {
  146. curArg = utils.tryImplForWrapper(curArg);
  147. }
  148. } else {
  149. curArg = null;
  150. }
  151. args.push(curArg);
  152. }
  153. {
  154. let curArg = arguments[4];
  155. if (curArg !== undefined) {
  156. curArg = conversions["long"](curArg, {
  157. context: "Failed to execute 'initUIEvent' on 'UIEvent': parameter 5",
  158. globals: globalObject
  159. });
  160. } else {
  161. curArg = 0;
  162. }
  163. args.push(curArg);
  164. }
  165. return esValue[implSymbol].initUIEvent(...args);
  166. }
  167. get view() {
  168. const esValue = this !== null && this !== undefined ? this : globalObject;
  169. if (!exports.is(esValue)) {
  170. throw new globalObject.TypeError("'get view' called on an object that is not a valid instance of UIEvent.");
  171. }
  172. return utils.tryWrapperForImpl(esValue[implSymbol]["view"]);
  173. }
  174. get detail() {
  175. const esValue = this !== null && this !== undefined ? this : globalObject;
  176. if (!exports.is(esValue)) {
  177. throw new globalObject.TypeError("'get detail' called on an object that is not a valid instance of UIEvent.");
  178. }
  179. return esValue[implSymbol]["detail"];
  180. }
  181. get which() {
  182. const esValue = this !== null && this !== undefined ? this : globalObject;
  183. if (!exports.is(esValue)) {
  184. throw new globalObject.TypeError("'get which' called on an object that is not a valid instance of UIEvent.");
  185. }
  186. return esValue[implSymbol]["which"];
  187. }
  188. }
  189. Object.defineProperties(UIEvent.prototype, {
  190. initUIEvent: { enumerable: true },
  191. view: { enumerable: true },
  192. detail: { enumerable: true },
  193. which: { enumerable: true },
  194. [Symbol.toStringTag]: { value: "UIEvent", configurable: true }
  195. });
  196. ctorRegistry[interfaceName] = UIEvent;
  197. Object.defineProperty(globalObject, interfaceName, {
  198. configurable: true,
  199. writable: true,
  200. value: UIEvent
  201. });
  202. };
  203. const Impl = require("../../jsdom/living/events/UIEvent-impl.js");