createImageBitmap.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _ImageBitmap = _interopRequireDefault(require("../classes/ImageBitmap"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, options) {
  9. var length = arguments.length;
  10. return new Promise((resolve, reject) => {
  11. if (length === 0) return reject(new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': 1 argument required, but only 0 present.'));
  12. if (length === 3 || length === 4) return reject(new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': Valid arities are: [1, 2, 5, 6], but ' + length + ' arguments provided.'));
  13. let validImage = false;
  14. if (img instanceof HTMLImageElement) validImage = true;
  15. if (img instanceof HTMLVideoElement) validImage = true;
  16. if (img instanceof HTMLCanvasElement) validImage = true; // checking constructor name is the only reliable way to verify the object's constructing class is "blob-like"
  17. if (img instanceof Blob || img && img.constructor && img.constructor.name === "Blob") validImage = true;
  18. if (img instanceof _ImageBitmap.default) validImage = true;
  19. if (img instanceof ImageData) validImage = true;
  20. if (!validImage) return reject(new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': The provided value is not of type \'(HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or Blob or ImageData or ImageBitmap or OffscreenCanvas)\''));
  21. if (length >= 2) {
  22. let index = 6;
  23. if (length === 2) {
  24. index = 2;
  25. options = sx;
  26. }
  27. if (length === 5) options = null;
  28. if (options !== null && options !== void 0) {
  29. if (typeof options !== 'object') throw new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': parameter ' + index + ' (\'options\') is not an object.');
  30. }
  31. }
  32. if (length >= 5) {
  33. sWidth = Number(sWidth);
  34. sHeight = Number(sHeight);
  35. if (sWidth === 0 || !Number.isFinite(sWidth)) return reject(new RangeError('The crop rect width is 0.'));
  36. if (sHeight === 0 || !Number.isFinite(sHeight)) return reject(new RangeError('The crop rect height is 0.'));
  37. sWidth = Math.abs(sWidth);
  38. sHeight = Math.abs(sHeight);
  39. } else {
  40. sWidth = img.width || 1;
  41. sHeight = img.height || 1;
  42. }
  43. return resolve(new _ImageBitmap.default(sWidth, sHeight));
  44. });
  45. });
  46. exports.default = _default;