PointerEvent.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const PointerEventInit = require("./PointerEventInit.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const MouseEvent = require("./MouseEvent.js");
  8. const interfaceName = "PointerEvent";
  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 'PointerEvent'.`);
  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]["PointerEvent"].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. MouseEvent._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 PointerEvent extends globalObject.MouseEvent {
  75. constructor(type) {
  76. if (arguments.length < 1) {
  77. throw new globalObject.TypeError(
  78. `Failed to construct 'PointerEvent': 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 'PointerEvent': parameter 1",
  86. globals: globalObject
  87. });
  88. args.push(curArg);
  89. }
  90. {
  91. let curArg = arguments[1];
  92. curArg = PointerEventInit.convert(globalObject, curArg, {
  93. context: "Failed to construct 'PointerEvent': parameter 2"
  94. });
  95. args.push(curArg);
  96. }
  97. return exports.setup(Object.create(new.target.prototype), globalObject, args);
  98. }
  99. getCoalescedEvents() {
  100. const esValue = this !== null && this !== undefined ? this : globalObject;
  101. if (!exports.is(esValue)) {
  102. throw new globalObject.TypeError(
  103. "'getCoalescedEvents' called on an object that is not a valid instance of PointerEvent."
  104. );
  105. }
  106. return utils.tryWrapperForImpl(esValue[implSymbol].getCoalescedEvents());
  107. }
  108. getPredictedEvents() {
  109. const esValue = this !== null && this !== undefined ? this : globalObject;
  110. if (!exports.is(esValue)) {
  111. throw new globalObject.TypeError(
  112. "'getPredictedEvents' called on an object that is not a valid instance of PointerEvent."
  113. );
  114. }
  115. return utils.tryWrapperForImpl(esValue[implSymbol].getPredictedEvents());
  116. }
  117. get pointerId() {
  118. const esValue = this !== null && this !== undefined ? this : globalObject;
  119. if (!exports.is(esValue)) {
  120. throw new globalObject.TypeError(
  121. "'get pointerId' called on an object that is not a valid instance of PointerEvent."
  122. );
  123. }
  124. return esValue[implSymbol]["pointerId"];
  125. }
  126. get width() {
  127. const esValue = this !== null && this !== undefined ? this : globalObject;
  128. if (!exports.is(esValue)) {
  129. throw new globalObject.TypeError(
  130. "'get width' called on an object that is not a valid instance of PointerEvent."
  131. );
  132. }
  133. return esValue[implSymbol]["width"];
  134. }
  135. get height() {
  136. const esValue = this !== null && this !== undefined ? this : globalObject;
  137. if (!exports.is(esValue)) {
  138. throw new globalObject.TypeError(
  139. "'get height' called on an object that is not a valid instance of PointerEvent."
  140. );
  141. }
  142. return esValue[implSymbol]["height"];
  143. }
  144. get pressure() {
  145. const esValue = this !== null && this !== undefined ? this : globalObject;
  146. if (!exports.is(esValue)) {
  147. throw new globalObject.TypeError(
  148. "'get pressure' called on an object that is not a valid instance of PointerEvent."
  149. );
  150. }
  151. return esValue[implSymbol]["pressure"];
  152. }
  153. get tangentialPressure() {
  154. const esValue = this !== null && this !== undefined ? this : globalObject;
  155. if (!exports.is(esValue)) {
  156. throw new globalObject.TypeError(
  157. "'get tangentialPressure' called on an object that is not a valid instance of PointerEvent."
  158. );
  159. }
  160. return esValue[implSymbol]["tangentialPressure"];
  161. }
  162. get tiltX() {
  163. const esValue = this !== null && this !== undefined ? this : globalObject;
  164. if (!exports.is(esValue)) {
  165. throw new globalObject.TypeError(
  166. "'get tiltX' called on an object that is not a valid instance of PointerEvent."
  167. );
  168. }
  169. return esValue[implSymbol]["tiltX"];
  170. }
  171. get tiltY() {
  172. const esValue = this !== null && this !== undefined ? this : globalObject;
  173. if (!exports.is(esValue)) {
  174. throw new globalObject.TypeError(
  175. "'get tiltY' called on an object that is not a valid instance of PointerEvent."
  176. );
  177. }
  178. return esValue[implSymbol]["tiltY"];
  179. }
  180. get twist() {
  181. const esValue = this !== null && this !== undefined ? this : globalObject;
  182. if (!exports.is(esValue)) {
  183. throw new globalObject.TypeError(
  184. "'get twist' called on an object that is not a valid instance of PointerEvent."
  185. );
  186. }
  187. return esValue[implSymbol]["twist"];
  188. }
  189. get altitudeAngle() {
  190. const esValue = this !== null && this !== undefined ? this : globalObject;
  191. if (!exports.is(esValue)) {
  192. throw new globalObject.TypeError(
  193. "'get altitudeAngle' called on an object that is not a valid instance of PointerEvent."
  194. );
  195. }
  196. return esValue[implSymbol]["altitudeAngle"];
  197. }
  198. get azimuthAngle() {
  199. const esValue = this !== null && this !== undefined ? this : globalObject;
  200. if (!exports.is(esValue)) {
  201. throw new globalObject.TypeError(
  202. "'get azimuthAngle' called on an object that is not a valid instance of PointerEvent."
  203. );
  204. }
  205. return esValue[implSymbol]["azimuthAngle"];
  206. }
  207. get pointerType() {
  208. const esValue = this !== null && this !== undefined ? this : globalObject;
  209. if (!exports.is(esValue)) {
  210. throw new globalObject.TypeError(
  211. "'get pointerType' called on an object that is not a valid instance of PointerEvent."
  212. );
  213. }
  214. return esValue[implSymbol]["pointerType"];
  215. }
  216. get isPrimary() {
  217. const esValue = this !== null && this !== undefined ? this : globalObject;
  218. if (!exports.is(esValue)) {
  219. throw new globalObject.TypeError(
  220. "'get isPrimary' called on an object that is not a valid instance of PointerEvent."
  221. );
  222. }
  223. return esValue[implSymbol]["isPrimary"];
  224. }
  225. get persistentDeviceId() {
  226. const esValue = this !== null && this !== undefined ? this : globalObject;
  227. if (!exports.is(esValue)) {
  228. throw new globalObject.TypeError(
  229. "'get persistentDeviceId' called on an object that is not a valid instance of PointerEvent."
  230. );
  231. }
  232. return esValue[implSymbol]["persistentDeviceId"];
  233. }
  234. }
  235. Object.defineProperties(PointerEvent.prototype, {
  236. getCoalescedEvents: { enumerable: true },
  237. getPredictedEvents: { enumerable: true },
  238. pointerId: { enumerable: true },
  239. width: { enumerable: true },
  240. height: { enumerable: true },
  241. pressure: { enumerable: true },
  242. tangentialPressure: { enumerable: true },
  243. tiltX: { enumerable: true },
  244. tiltY: { enumerable: true },
  245. twist: { enumerable: true },
  246. altitudeAngle: { enumerable: true },
  247. azimuthAngle: { enumerable: true },
  248. pointerType: { enumerable: true },
  249. isPrimary: { enumerable: true },
  250. persistentDeviceId: { enumerable: true },
  251. [Symbol.toStringTag]: { value: "PointerEvent", configurable: true }
  252. });
  253. ctorRegistry[interfaceName] = PointerEvent;
  254. Object.defineProperty(globalObject, interfaceName, {
  255. configurable: true,
  256. writable: true,
  257. value: PointerEvent
  258. });
  259. };
  260. const Impl = require("../../jsdom/living/events/PointerEvent-impl.js");