HTMLCanvasElement.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. "use strict";
  2. const conversions = require("webidl-conversions");
  3. const utils = require("./utils.js");
  4. const HTMLConstructor_jsdom_living_helpers_html_constructor =
  5. require("../../jsdom/living/helpers/html-constructor.js").HTMLConstructor;
  6. const BlobCallback = require("./BlobCallback.js");
  7. const parseNonNegativeInteger_jsdom_living_helpers_strings =
  8. require("../../jsdom/living/helpers/strings.js").parseNonNegativeInteger;
  9. const ceReactionsPreSteps_jsdom_living_helpers_custom_elements =
  10. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPreSteps;
  11. const ceReactionsPostSteps_jsdom_living_helpers_custom_elements =
  12. require("../../jsdom/living/helpers/custom-elements.js").ceReactionsPostSteps;
  13. const implSymbol = utils.implSymbol;
  14. const ctorRegistrySymbol = utils.ctorRegistrySymbol;
  15. const HTMLElement = require("./HTMLElement.js");
  16. const interfaceName = "HTMLCanvasElement";
  17. exports.is = value => {
  18. return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
  19. };
  20. exports.isImpl = value => {
  21. return utils.isObject(value) && value instanceof Impl.implementation;
  22. };
  23. exports.convert = (globalObject, value, { context = "The provided value" } = {}) => {
  24. if (exports.is(value)) {
  25. return utils.implForWrapper(value);
  26. }
  27. throw new globalObject.TypeError(`${context} is not of type 'HTMLCanvasElement'.`);
  28. };
  29. function makeWrapper(globalObject, newTarget) {
  30. let proto;
  31. if (newTarget !== undefined) {
  32. proto = newTarget.prototype;
  33. }
  34. if (!utils.isObject(proto)) {
  35. proto = globalObject[ctorRegistrySymbol]["HTMLCanvasElement"].prototype;
  36. }
  37. return Object.create(proto);
  38. }
  39. exports.create = (globalObject, constructorArgs, privateData) => {
  40. const wrapper = makeWrapper(globalObject);
  41. return exports.setup(wrapper, globalObject, constructorArgs, privateData);
  42. };
  43. exports.createImpl = (globalObject, constructorArgs, privateData) => {
  44. const wrapper = exports.create(globalObject, constructorArgs, privateData);
  45. return utils.implForWrapper(wrapper);
  46. };
  47. exports._internalSetup = (wrapper, globalObject) => {
  48. HTMLElement._internalSetup(wrapper, globalObject);
  49. };
  50. exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
  51. privateData.wrapper = wrapper;
  52. exports._internalSetup(wrapper, globalObject);
  53. Object.defineProperty(wrapper, implSymbol, {
  54. value: new Impl.implementation(globalObject, constructorArgs, privateData),
  55. configurable: true
  56. });
  57. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  58. if (Impl.init) {
  59. Impl.init(wrapper[implSymbol]);
  60. }
  61. return wrapper;
  62. };
  63. exports.new = (globalObject, newTarget) => {
  64. const wrapper = makeWrapper(globalObject, newTarget);
  65. exports._internalSetup(wrapper, globalObject);
  66. Object.defineProperty(wrapper, implSymbol, {
  67. value: Object.create(Impl.implementation.prototype),
  68. configurable: true
  69. });
  70. wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
  71. if (Impl.init) {
  72. Impl.init(wrapper[implSymbol]);
  73. }
  74. return wrapper[implSymbol];
  75. };
  76. const exposed = new Set(["Window"]);
  77. exports.install = (globalObject, globalNames) => {
  78. if (!globalNames.some(globalName => exposed.has(globalName))) {
  79. return;
  80. }
  81. const ctorRegistry = utils.initCtorRegistry(globalObject);
  82. class HTMLCanvasElement extends globalObject.HTMLElement {
  83. constructor() {
  84. return HTMLConstructor_jsdom_living_helpers_html_constructor(globalObject, interfaceName, new.target);
  85. }
  86. getContext(contextId) {
  87. const esValue = this !== null && this !== undefined ? this : globalObject;
  88. if (!exports.is(esValue)) {
  89. throw new globalObject.TypeError(
  90. "'getContext' called on an object that is not a valid instance of HTMLCanvasElement."
  91. );
  92. }
  93. if (arguments.length < 1) {
  94. throw new globalObject.TypeError(
  95. `Failed to execute 'getContext' on 'HTMLCanvasElement': 1 argument required, but only ${arguments.length} present.`
  96. );
  97. }
  98. const args = [];
  99. {
  100. let curArg = arguments[0];
  101. curArg = conversions["DOMString"](curArg, {
  102. context: "Failed to execute 'getContext' on 'HTMLCanvasElement': parameter 1",
  103. globals: globalObject
  104. });
  105. args.push(curArg);
  106. }
  107. for (let i = 1; i < arguments.length; i++) {
  108. let curArg = arguments[i];
  109. curArg = conversions["any"](curArg, {
  110. context: "Failed to execute 'getContext' on 'HTMLCanvasElement': parameter " + (i + 1),
  111. globals: globalObject
  112. });
  113. args.push(curArg);
  114. }
  115. return utils.tryWrapperForImpl(esValue[implSymbol].getContext(...args));
  116. }
  117. toDataURL() {
  118. const esValue = this !== null && this !== undefined ? this : globalObject;
  119. if (!exports.is(esValue)) {
  120. throw new globalObject.TypeError(
  121. "'toDataURL' called on an object that is not a valid instance of HTMLCanvasElement."
  122. );
  123. }
  124. const args = [];
  125. {
  126. let curArg = arguments[0];
  127. if (curArg !== undefined) {
  128. curArg = conversions["DOMString"](curArg, {
  129. context: "Failed to execute 'toDataURL' on 'HTMLCanvasElement': parameter 1",
  130. globals: globalObject
  131. });
  132. }
  133. args.push(curArg);
  134. }
  135. {
  136. let curArg = arguments[1];
  137. if (curArg !== undefined) {
  138. curArg = conversions["any"](curArg, {
  139. context: "Failed to execute 'toDataURL' on 'HTMLCanvasElement': parameter 2",
  140. globals: globalObject
  141. });
  142. }
  143. args.push(curArg);
  144. }
  145. return esValue[implSymbol].toDataURL(...args);
  146. }
  147. toBlob(callback) {
  148. const esValue = this !== null && this !== undefined ? this : globalObject;
  149. if (!exports.is(esValue)) {
  150. throw new globalObject.TypeError(
  151. "'toBlob' called on an object that is not a valid instance of HTMLCanvasElement."
  152. );
  153. }
  154. if (arguments.length < 1) {
  155. throw new globalObject.TypeError(
  156. `Failed to execute 'toBlob' on 'HTMLCanvasElement': 1 argument required, but only ${arguments.length} present.`
  157. );
  158. }
  159. const args = [];
  160. {
  161. let curArg = arguments[0];
  162. curArg = BlobCallback.convert(globalObject, curArg, {
  163. context: "Failed to execute 'toBlob' on 'HTMLCanvasElement': parameter 1"
  164. });
  165. args.push(curArg);
  166. }
  167. {
  168. let curArg = arguments[1];
  169. if (curArg !== undefined) {
  170. curArg = conversions["DOMString"](curArg, {
  171. context: "Failed to execute 'toBlob' on 'HTMLCanvasElement': parameter 2",
  172. globals: globalObject
  173. });
  174. }
  175. args.push(curArg);
  176. }
  177. {
  178. let curArg = arguments[2];
  179. if (curArg !== undefined) {
  180. curArg = conversions["any"](curArg, {
  181. context: "Failed to execute 'toBlob' on 'HTMLCanvasElement': parameter 3",
  182. globals: globalObject
  183. });
  184. }
  185. args.push(curArg);
  186. }
  187. return esValue[implSymbol].toBlob(...args);
  188. }
  189. get width() {
  190. const esValue = this !== null && this !== undefined ? this : globalObject;
  191. if (!exports.is(esValue)) {
  192. throw new globalObject.TypeError(
  193. "'get width' called on an object that is not a valid instance of HTMLCanvasElement."
  194. );
  195. }
  196. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  197. try {
  198. let value = esValue[implSymbol]._reflectGetTheContentAttribute("width");
  199. if (value !== null) {
  200. value = parseNonNegativeInteger_jsdom_living_helpers_strings(value);
  201. if (value !== null && value >= 0 && value <= 2147483647) {
  202. return value;
  203. }
  204. }
  205. return 300;
  206. } finally {
  207. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  208. }
  209. }
  210. set width(V) {
  211. const esValue = this !== null && this !== undefined ? this : globalObject;
  212. if (!exports.is(esValue)) {
  213. throw new globalObject.TypeError(
  214. "'set width' called on an object that is not a valid instance of HTMLCanvasElement."
  215. );
  216. }
  217. V = conversions["unsigned long"](V, {
  218. context: "Failed to set the 'width' property on 'HTMLCanvasElement': The provided value",
  219. globals: globalObject
  220. });
  221. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  222. try {
  223. const newValue = V <= 2147483647 && V >= 0 ? V : 300;
  224. esValue[implSymbol]._reflectSetTheContentAttribute("width", String(newValue));
  225. } finally {
  226. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  227. }
  228. }
  229. get height() {
  230. const esValue = this !== null && this !== undefined ? this : globalObject;
  231. if (!exports.is(esValue)) {
  232. throw new globalObject.TypeError(
  233. "'get height' called on an object that is not a valid instance of HTMLCanvasElement."
  234. );
  235. }
  236. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  237. try {
  238. let value = esValue[implSymbol]._reflectGetTheContentAttribute("height");
  239. if (value !== null) {
  240. value = parseNonNegativeInteger_jsdom_living_helpers_strings(value);
  241. if (value !== null && value >= 0 && value <= 2147483647) {
  242. return value;
  243. }
  244. }
  245. return 150;
  246. } finally {
  247. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  248. }
  249. }
  250. set height(V) {
  251. const esValue = this !== null && this !== undefined ? this : globalObject;
  252. if (!exports.is(esValue)) {
  253. throw new globalObject.TypeError(
  254. "'set height' called on an object that is not a valid instance of HTMLCanvasElement."
  255. );
  256. }
  257. V = conversions["unsigned long"](V, {
  258. context: "Failed to set the 'height' property on 'HTMLCanvasElement': The provided value",
  259. globals: globalObject
  260. });
  261. ceReactionsPreSteps_jsdom_living_helpers_custom_elements(globalObject);
  262. try {
  263. const newValue = V <= 2147483647 && V >= 0 ? V : 150;
  264. esValue[implSymbol]._reflectSetTheContentAttribute("height", String(newValue));
  265. } finally {
  266. ceReactionsPostSteps_jsdom_living_helpers_custom_elements(globalObject);
  267. }
  268. }
  269. }
  270. Object.defineProperties(HTMLCanvasElement.prototype, {
  271. getContext: { enumerable: true },
  272. toDataURL: { enumerable: true },
  273. toBlob: { enumerable: true },
  274. width: { enumerable: true },
  275. height: { enumerable: true },
  276. [Symbol.toStringTag]: { value: "HTMLCanvasElement", configurable: true }
  277. });
  278. ctorRegistry[interfaceName] = HTMLCanvasElement;
  279. Object.defineProperty(globalObject, interfaceName, {
  280. configurable: true,
  281. writable: true,
  282. value: HTMLCanvasElement
  283. });
  284. };
  285. const Impl = require("../../jsdom/living/nodes/HTMLCanvasElement-impl.js");