XMLHttpRequestEventTarget.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const EventHandlerNonNull = require("./EventHandlerNonNull.js");
  5. const implSymbol = utils.implSymbol;
  6. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  7. const EventTarget = require("./EventTarget.js");
  8. const interfaceName = "XMLHttpRequestEventTarget";
  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 'XMLHttpRequestEventTarget'.`);
  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]["XMLHttpRequestEventTarget"].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. EventTarget._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", "DedicatedWorker", "SharedWorker"]);
  69. exports.install = (globalObject, globalNames) => {
  70. if (!globalNames.some(globalName => exposed.has(globalName))) {
  71. return;
  72. }
  73. const ctorRegistry = utils.initCtorRegistry(globalObject);
  74. class XMLHttpRequestEventTarget extends globalObject.EventTarget {
  75. constructor() {
  76. throw new globalObject.TypeError("Illegal constructor");
  77. }
  78. get onloadstart() {
  79. const esValue = this !== null && this !== undefined ? this : globalObject;
  80. if (!exports.is(esValue)) {
  81. throw new globalObject.TypeError(
  82. "'get onloadstart' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  83. );
  84. }
  85. return utils.tryWrapperForImpl(esValue[implSymbol]["onloadstart"]);
  86. }
  87. set onloadstart(V) {
  88. const esValue = this !== null && this !== undefined ? this : globalObject;
  89. if (!exports.is(esValue)) {
  90. throw new globalObject.TypeError(
  91. "'set onloadstart' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  92. );
  93. }
  94. if (!utils.isObject(V)) {
  95. V = null;
  96. } else {
  97. V = EventHandlerNonNull.convert(globalObject, V, {
  98. context: "Failed to set the 'onloadstart' property on 'XMLHttpRequestEventTarget': The provided value"
  99. });
  100. }
  101. esValue[implSymbol]["onloadstart"] = V;
  102. }
  103. get onprogress() {
  104. const esValue = this !== null && this !== undefined ? this : globalObject;
  105. if (!exports.is(esValue)) {
  106. throw new globalObject.TypeError(
  107. "'get onprogress' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  108. );
  109. }
  110. return utils.tryWrapperForImpl(esValue[implSymbol]["onprogress"]);
  111. }
  112. set onprogress(V) {
  113. const esValue = this !== null && this !== undefined ? this : globalObject;
  114. if (!exports.is(esValue)) {
  115. throw new globalObject.TypeError(
  116. "'set onprogress' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  117. );
  118. }
  119. if (!utils.isObject(V)) {
  120. V = null;
  121. } else {
  122. V = EventHandlerNonNull.convert(globalObject, V, {
  123. context: "Failed to set the 'onprogress' property on 'XMLHttpRequestEventTarget': The provided value"
  124. });
  125. }
  126. esValue[implSymbol]["onprogress"] = V;
  127. }
  128. get onabort() {
  129. const esValue = this !== null && this !== undefined ? this : globalObject;
  130. if (!exports.is(esValue)) {
  131. throw new globalObject.TypeError(
  132. "'get onabort' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  133. );
  134. }
  135. return utils.tryWrapperForImpl(esValue[implSymbol]["onabort"]);
  136. }
  137. set onabort(V) {
  138. const esValue = this !== null && this !== undefined ? this : globalObject;
  139. if (!exports.is(esValue)) {
  140. throw new globalObject.TypeError(
  141. "'set onabort' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  142. );
  143. }
  144. if (!utils.isObject(V)) {
  145. V = null;
  146. } else {
  147. V = EventHandlerNonNull.convert(globalObject, V, {
  148. context: "Failed to set the 'onabort' property on 'XMLHttpRequestEventTarget': The provided value"
  149. });
  150. }
  151. esValue[implSymbol]["onabort"] = V;
  152. }
  153. get onerror() {
  154. const esValue = this !== null && this !== undefined ? this : globalObject;
  155. if (!exports.is(esValue)) {
  156. throw new globalObject.TypeError(
  157. "'get onerror' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  158. );
  159. }
  160. return utils.tryWrapperForImpl(esValue[implSymbol]["onerror"]);
  161. }
  162. set onerror(V) {
  163. const esValue = this !== null && this !== undefined ? this : globalObject;
  164. if (!exports.is(esValue)) {
  165. throw new globalObject.TypeError(
  166. "'set onerror' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  167. );
  168. }
  169. if (!utils.isObject(V)) {
  170. V = null;
  171. } else {
  172. V = EventHandlerNonNull.convert(globalObject, V, {
  173. context: "Failed to set the 'onerror' property on 'XMLHttpRequestEventTarget': The provided value"
  174. });
  175. }
  176. esValue[implSymbol]["onerror"] = V;
  177. }
  178. get onload() {
  179. const esValue = this !== null && this !== undefined ? this : globalObject;
  180. if (!exports.is(esValue)) {
  181. throw new globalObject.TypeError(
  182. "'get onload' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  183. );
  184. }
  185. return utils.tryWrapperForImpl(esValue[implSymbol]["onload"]);
  186. }
  187. set onload(V) {
  188. const esValue = this !== null && this !== undefined ? this : globalObject;
  189. if (!exports.is(esValue)) {
  190. throw new globalObject.TypeError(
  191. "'set onload' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  192. );
  193. }
  194. if (!utils.isObject(V)) {
  195. V = null;
  196. } else {
  197. V = EventHandlerNonNull.convert(globalObject, V, {
  198. context: "Failed to set the 'onload' property on 'XMLHttpRequestEventTarget': The provided value"
  199. });
  200. }
  201. esValue[implSymbol]["onload"] = V;
  202. }
  203. get ontimeout() {
  204. const esValue = this !== null && this !== undefined ? this : globalObject;
  205. if (!exports.is(esValue)) {
  206. throw new globalObject.TypeError(
  207. "'get ontimeout' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  208. );
  209. }
  210. return utils.tryWrapperForImpl(esValue[implSymbol]["ontimeout"]);
  211. }
  212. set ontimeout(V) {
  213. const esValue = this !== null && this !== undefined ? this : globalObject;
  214. if (!exports.is(esValue)) {
  215. throw new globalObject.TypeError(
  216. "'set ontimeout' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  217. );
  218. }
  219. if (!utils.isObject(V)) {
  220. V = null;
  221. } else {
  222. V = EventHandlerNonNull.convert(globalObject, V, {
  223. context: "Failed to set the 'ontimeout' property on 'XMLHttpRequestEventTarget': The provided value"
  224. });
  225. }
  226. esValue[implSymbol]["ontimeout"] = V;
  227. }
  228. get onloadend() {
  229. const esValue = this !== null && this !== undefined ? this : globalObject;
  230. if (!exports.is(esValue)) {
  231. throw new globalObject.TypeError(
  232. "'get onloadend' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  233. );
  234. }
  235. return utils.tryWrapperForImpl(esValue[implSymbol]["onloadend"]);
  236. }
  237. set onloadend(V) {
  238. const esValue = this !== null && this !== undefined ? this : globalObject;
  239. if (!exports.is(esValue)) {
  240. throw new globalObject.TypeError(
  241. "'set onloadend' called on an object that is not a valid instance of XMLHttpRequestEventTarget."
  242. );
  243. }
  244. if (!utils.isObject(V)) {
  245. V = null;
  246. } else {
  247. V = EventHandlerNonNull.convert(globalObject, V, {
  248. context: "Failed to set the 'onloadend' property on 'XMLHttpRequestEventTarget': The provided value"
  249. });
  250. }
  251. esValue[implSymbol]["onloadend"] = V;
  252. }
  253. }
  254. Object.defineProperties(XMLHttpRequestEventTarget.prototype, {
  255. onloadstart: { enumerable: true },
  256. onprogress: { enumerable: true },
  257. onabort: { enumerable: true },
  258. onerror: { enumerable: true },
  259. onload: { enumerable: true },
  260. ontimeout: { enumerable: true },
  261. onloadend: { enumerable: true },
  262. [Symbol.toStringTag]: { value: "XMLHttpRequestEventTarget", configurable: true }
  263. });
  264. ctorRegistry[interfaceName] = XMLHttpRequestEventTarget;
  265. Object.defineProperty(globalObject, interfaceName, {
  266. configurable: true,
  267. writable: true,
  268. value: XMLHttpRequestEventTarget
  269. });
  270. };
  271. const Impl = require("../../jsdom/living/xhr/XMLHttpRequestEventTarget-impl.js");