jest-canvas-mock 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -9
- package/README.md +38 -12
- package/lib/classes/CanvasGradient.js +16 -33
- package/lib/classes/CanvasPattern.js +8 -22
- package/lib/classes/CanvasRenderingContext2D.js +1243 -1263
- package/lib/classes/DOMMatrix.js +72 -79
- package/lib/classes/ImageBitmap.js +10 -24
- package/lib/classes/ImageData.js +50 -38
- package/lib/classes/Path2D.js +17 -33
- package/lib/classes/TextMetrics.js +19 -20
- package/lib/index.js +3 -3
- package/lib/mock/createCanvasEvent.js +4 -4
- package/lib/mock/createImageBitmap.js +13 -15
- package/lib/mock/prototype.js +12 -14
- package/lib/window.js +16 -16
- package/package.json +21 -14
- package/types/index.d.ts +40 -0
package/lib/classes/DOMMatrix.js
CHANGED
|
@@ -3,22 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
6
|
+
exports.default = void 0;
|
|
13
7
|
|
|
14
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function () {
|
|
19
|
-
function DOMMatrix(transform) {
|
|
20
|
-
_classCallCheck(this, DOMMatrix);
|
|
21
|
-
|
|
10
|
+
class DOMMatrix {
|
|
11
|
+
constructor(transform) {
|
|
22
12
|
_defineProperty(this, "_is2D", true);
|
|
23
13
|
|
|
24
14
|
_defineProperty(this, "m11", 1.0);
|
|
@@ -86,77 +76,80 @@ function () {
|
|
|
86
76
|
}
|
|
87
77
|
|
|
88
78
|
if (transform) {
|
|
89
|
-
throw new TypeError(
|
|
79
|
+
throw new TypeError("Failed to construct 'DOMMatrix': The sequence must contain 6 elements for a 2D matrix or 16 elements for a 3D matrix.");
|
|
90
80
|
}
|
|
91
81
|
|
|
92
82
|
this._is2D = false;
|
|
93
83
|
}
|
|
94
84
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
} else {
|
|
101
|
-
return this.m11 = 1.0 && this.m12 === 0.0 && this.m13 === 0.0 && this.m14 === 0.0 && this.m21 === 0.0 && this.m22 === 1.0 && this.m23 === 0.0 && this.m24 === 0.0 && this.m31 === 0.0 && this.m32 === 0.0 && this.m33 === 1.0 && this.m34 === 0.0 && this.m41 === 0.0 && this.m42 === 0.0 && this.m43 === 0.0 && this.m44 === 1.0;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}, {
|
|
105
|
-
key: "a",
|
|
106
|
-
get: function get() {
|
|
107
|
-
return this.m11;
|
|
108
|
-
},
|
|
109
|
-
set: function set(value) {
|
|
110
|
-
this.m11 = value;
|
|
111
|
-
}
|
|
112
|
-
}, {
|
|
113
|
-
key: "b",
|
|
114
|
-
get: function get() {
|
|
115
|
-
return this.m12;
|
|
116
|
-
},
|
|
117
|
-
set: function set(value) {
|
|
118
|
-
this.m12 = value;
|
|
119
|
-
}
|
|
120
|
-
}, {
|
|
121
|
-
key: "c",
|
|
122
|
-
get: function get() {
|
|
123
|
-
return this.m21;
|
|
124
|
-
},
|
|
125
|
-
set: function set(value) {
|
|
126
|
-
this.m21 = value;
|
|
127
|
-
}
|
|
128
|
-
}, {
|
|
129
|
-
key: "d",
|
|
130
|
-
get: function get() {
|
|
131
|
-
return this.m22;
|
|
132
|
-
},
|
|
133
|
-
set: function set(value) {
|
|
134
|
-
this.m22 = value;
|
|
135
|
-
}
|
|
136
|
-
}, {
|
|
137
|
-
key: "e",
|
|
138
|
-
get: function get() {
|
|
139
|
-
return this.m41;
|
|
140
|
-
},
|
|
141
|
-
set: function set(value) {
|
|
142
|
-
this.m41 = value;
|
|
143
|
-
}
|
|
144
|
-
}, {
|
|
145
|
-
key: "f",
|
|
146
|
-
get: function get() {
|
|
147
|
-
return this.m42;
|
|
148
|
-
},
|
|
149
|
-
set: function set(value) {
|
|
150
|
-
this.m42 = value;
|
|
151
|
-
}
|
|
152
|
-
}, {
|
|
153
|
-
key: "is2D",
|
|
154
|
-
get: function get() {
|
|
155
|
-
return this._is2D;
|
|
85
|
+
get isIdentity() {
|
|
86
|
+
if (this._is2D) {
|
|
87
|
+
return this.m11 == 1.0 && this.m12 == 0.0 && this.m21 == 0.0 && this.m22 == 1.0 && this.m41 == 0.0 && this.m42 == 0.0;
|
|
88
|
+
} else {
|
|
89
|
+
return this.m11 = 1.0 && this.m12 === 0.0 && this.m13 === 0.0 && this.m14 === 0.0 && this.m21 === 0.0 && this.m22 === 1.0 && this.m23 === 0.0 && this.m24 === 0.0 && this.m31 === 0.0 && this.m32 === 0.0 && this.m33 === 1.0 && this.m34 === 0.0 && this.m41 === 0.0 && this.m42 === 0.0 && this.m43 === 0.0 && this.m44 === 1.0;
|
|
156
90
|
}
|
|
157
|
-
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get a() {
|
|
94
|
+
return this.m11;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
set a(value) {
|
|
98
|
+
this.m11 = value;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
get b() {
|
|
102
|
+
return this.m12;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
set b(value) {
|
|
106
|
+
this.m12 = value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get c() {
|
|
110
|
+
return this.m21;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
set c(value) {
|
|
114
|
+
this.m21 = value;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get d() {
|
|
118
|
+
return this.m22;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
set d(value) {
|
|
122
|
+
this.m22 = value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
get e() {
|
|
126
|
+
return this.m41;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
set e(value) {
|
|
130
|
+
this.m41 = value;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get f() {
|
|
134
|
+
return this.m42;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
set f(value) {
|
|
138
|
+
this.m42 = value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
get is2D() {
|
|
142
|
+
return this._is2D;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
toFloat32Array() {
|
|
146
|
+
return new Float32Array([this.m11, this.m12, this.m13, this.m14, this.m21, this.m22, this.m23, this.m24, this.m31, this.m32, this.m33, this.m34, this.m41, this.m42, this.m43, this.m44]);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
toFloat64Array() {
|
|
150
|
+
return new Float64Array([this.m11, this.m12, this.m13, this.m14, this.m21, this.m22, this.m23, this.m24, this.m31, this.m32, this.m33, this.m34, this.m41, this.m42, this.m43, this.m44]);
|
|
151
|
+
}
|
|
158
152
|
|
|
159
|
-
|
|
160
|
-
}();
|
|
153
|
+
}
|
|
161
154
|
|
|
162
|
-
exports
|
|
155
|
+
exports.default = DOMMatrix;
|
|
@@ -3,22 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
6
|
+
exports.default = void 0;
|
|
13
7
|
|
|
14
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function () {
|
|
19
|
-
function ImageBitmap(width, height) {
|
|
20
|
-
_classCallCheck(this, ImageBitmap);
|
|
21
|
-
|
|
10
|
+
class ImageBitmap {
|
|
11
|
+
constructor(width, height) {
|
|
22
12
|
_defineProperty(this, "width", 0);
|
|
23
13
|
|
|
24
14
|
_defineProperty(this, "height", 0);
|
|
@@ -30,16 +20,12 @@ function () {
|
|
|
30
20
|
this.close = jest.fn(this.close.bind(this));
|
|
31
21
|
}
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this._closed = true;
|
|
39
|
-
}
|
|
40
|
-
}]);
|
|
23
|
+
close() {
|
|
24
|
+
this.width = 0;
|
|
25
|
+
this.height = 0;
|
|
26
|
+
this._closed = true;
|
|
27
|
+
}
|
|
41
28
|
|
|
42
|
-
|
|
43
|
-
}();
|
|
29
|
+
}
|
|
44
30
|
|
|
45
|
-
exports
|
|
31
|
+
exports.default = ImageBitmap;
|
package/lib/classes/ImageData.js
CHANGED
|
@@ -3,55 +3,67 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
9
|
-
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
11
|
-
|
|
12
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
6
|
+
exports.default = void 0;
|
|
13
7
|
|
|
14
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
key: "width",
|
|
21
|
-
get: function get() {
|
|
22
|
-
return this._width;
|
|
23
|
-
}
|
|
24
|
-
}, {
|
|
25
|
-
key: "height",
|
|
26
|
-
get: function get() {
|
|
27
|
-
return this._height;
|
|
28
|
-
}
|
|
29
|
-
}, {
|
|
30
|
-
key: "data",
|
|
31
|
-
get: function get() {
|
|
32
|
-
return this._data;
|
|
33
|
-
}
|
|
34
|
-
}]);
|
|
10
|
+
class ImageData {
|
|
11
|
+
get width() {
|
|
12
|
+
return this._width;
|
|
13
|
+
}
|
|
35
14
|
|
|
36
|
-
|
|
37
|
-
|
|
15
|
+
get height() {
|
|
16
|
+
return this._height;
|
|
17
|
+
}
|
|
38
18
|
|
|
19
|
+
get data() {
|
|
20
|
+
return this._data;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
constructor(arr, w, h) {
|
|
39
24
|
_defineProperty(this, "_width", 0);
|
|
40
25
|
|
|
41
26
|
_defineProperty(this, "_height", 0);
|
|
42
27
|
|
|
43
28
|
_defineProperty(this, "_data", null);
|
|
44
29
|
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
30
|
+
if (arguments.length === 2) {
|
|
31
|
+
if (arr instanceof Uint8ClampedArray) {
|
|
32
|
+
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
33
|
+
if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
34
|
+
if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
|
|
35
|
+
if (w === 0) throw new RangeError('The width is zero or not a number.');
|
|
36
|
+
this._width = w;
|
|
37
|
+
this._height = arr.length / 4 / w;
|
|
38
|
+
this._data = arr;
|
|
39
|
+
} else {
|
|
40
|
+
const width = arr;
|
|
41
|
+
const height = w;
|
|
42
|
+
if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.');
|
|
43
|
+
if (height === 0) throw new RangeError('The height is zero or not a number.');
|
|
44
|
+
if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.');
|
|
45
|
+
if (width === 0) throw new RangeError('The width is zero or not a number.');
|
|
46
|
+
this._width = width;
|
|
47
|
+
this._height = height;
|
|
48
|
+
this._data = new Uint8ClampedArray(width * height * 4);
|
|
49
|
+
}
|
|
50
|
+
} else if (arguments.length === 3) {
|
|
51
|
+
if (!(arr instanceof Uint8ClampedArray)) throw new TypeError('First argument must be a Uint8ClampedArray when using 3 arguments.');
|
|
52
|
+
if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
53
|
+
if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.');
|
|
54
|
+
if (!Number.isFinite(h)) throw new RangeError('The height is zero or not a number.');
|
|
55
|
+
if (h === 0) throw new RangeError('The height is zero or not a number.');
|
|
56
|
+
if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.');
|
|
57
|
+
if (w === 0) throw new RangeError('The width is zero or not a number.');
|
|
58
|
+
if (arr.length !== w * h * 4) throw new RangeError("Source doesn'n contain the exact number of pixels needed.");
|
|
59
|
+
this._width = w;
|
|
60
|
+
this._height = h;
|
|
61
|
+
this._data = arr;
|
|
62
|
+
} else {
|
|
63
|
+
throw new TypeError('Wrong number of arguments provided.');
|
|
64
|
+
}
|
|
52
65
|
}
|
|
53
66
|
|
|
54
|
-
|
|
55
|
-
}();
|
|
67
|
+
}
|
|
56
68
|
|
|
57
|
-
exports
|
|
69
|
+
exports.default = ImageData;
|
package/lib/classes/Path2D.js
CHANGED
|
@@ -3,32 +3,20 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
|
|
8
8
|
var _CanvasRenderingContext2D = _interopRequireDefault(require("./CanvasRenderingContext2D"));
|
|
9
9
|
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
11
|
-
|
|
12
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
13
|
-
|
|
14
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
15
|
-
|
|
16
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
11
|
|
|
18
12
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
13
|
|
|
20
14
|
// Path2D.prototype
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
var Path2D =
|
|
25
|
-
/*#__PURE__*/
|
|
26
|
-
function () {
|
|
27
|
-
function Path2D() {
|
|
28
|
-
var _this = this;
|
|
29
|
-
|
|
30
|
-
_classCallCheck(this, Path2D);
|
|
15
|
+
const Path2DFunc = ['addPath'];
|
|
16
|
+
const borrowedFromCanvas = ['closePath', 'moveTo', 'lineTo', 'bezierCurveTo', 'quadraticCurveTo', 'arc', 'arcTo', 'ellipse', 'rect'];
|
|
31
17
|
|
|
18
|
+
class Path2D {
|
|
19
|
+
constructor() {
|
|
32
20
|
_defineProperty(this, "_path", []);
|
|
33
21
|
|
|
34
22
|
_defineProperty(this, "_events", []);
|
|
@@ -37,24 +25,20 @@ function () {
|
|
|
37
25
|
|
|
38
26
|
_defineProperty(this, "_transformStack", [[1, 0, 0, 1, 0, 0]]);
|
|
39
27
|
|
|
40
|
-
borrowedFromCanvas.forEach(
|
|
41
|
-
|
|
28
|
+
borrowedFromCanvas.forEach(key => {
|
|
29
|
+
this[key] = jest.fn(_CanvasRenderingContext2D.default.prototype[key].bind(this));
|
|
42
30
|
});
|
|
43
|
-
Path2DFunc.forEach(
|
|
44
|
-
|
|
31
|
+
Path2DFunc.forEach(key => {
|
|
32
|
+
this[key] = jest.fn(this[key].bind(this));
|
|
45
33
|
});
|
|
46
34
|
}
|
|
47
35
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this._path = this._path.concat(path._path);
|
|
54
|
-
}
|
|
55
|
-
}]);
|
|
36
|
+
addPath(path) {
|
|
37
|
+
if (arguments.length < 1) throw new TypeError("Failed to execute 'addPath' on 'Path2D': 1 argument required, but only 0 present.");
|
|
38
|
+
if (!(path instanceof Path2D)) throw new TypeError("Failed to execute 'addPath' on 'Path2D': parameter 1 is not of type 'Path2D'.");
|
|
39
|
+
this._path = this._path.concat(path._path);
|
|
40
|
+
}
|
|
56
41
|
|
|
57
|
-
|
|
58
|
-
}();
|
|
42
|
+
}
|
|
59
43
|
|
|
60
|
-
exports
|
|
44
|
+
exports.default = Path2D;
|
|
@@ -3,40 +3,39 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
7
|
-
|
|
8
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
6
|
+
exports.default = void 0;
|
|
9
7
|
|
|
10
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
class TextMetrics {
|
|
11
|
+
constructor(text) {
|
|
12
|
+
_defineProperty(this, "width", 0);
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
_defineProperty(this, "actualBoundingBoxLeft", 0);
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
_defineProperty(this, "actualBoundingBoxRight", 0);
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
_defineProperty(this, "fontBoundingBoxAscent", 0);
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
_defineProperty(this, "fontBoundingBoxDescent", 0);
|
|
22
21
|
|
|
23
|
-
|
|
22
|
+
_defineProperty(this, "actualBoundingBoxAscent", 0);
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
_defineProperty(this, "actualBoundingBoxDescent", 0);
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
_defineProperty(this, "emHeightAscent", 0);
|
|
28
27
|
|
|
29
|
-
|
|
28
|
+
_defineProperty(this, "emHeightDescent", 0);
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
_defineProperty(this, "hangingBaseline", 0);
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
_defineProperty(this, "alphabeticBaseline", 0);
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
_defineProperty(this, "ideographicBaseline", 0);
|
|
36
35
|
|
|
37
|
-
|
|
36
|
+
this.width = text.length;
|
|
37
|
+
}
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
};
|
|
39
|
+
}
|
|
41
40
|
|
|
42
|
-
exports
|
|
41
|
+
exports.default = TextMetrics;
|
package/lib/index.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.ver = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _window = _interopRequireDefault(require("./window"));
|
|
9
9
|
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Created by hustcc 17/12/25.
|
|
@@ -16,8 +16,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
|
|
|
16
16
|
// mock global window
|
|
17
17
|
// TODO: Force coverage to ignore this branch
|
|
18
18
|
if (typeof window !== 'undefined') {
|
|
19
|
-
global.window = (0, _window
|
|
19
|
+
global.window = (0, _window.default)(window);
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
const ver = "2.3.0";
|
|
23
23
|
exports.ver = ver;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = createCanvasEvent;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* This function returns a CanvasRenderingContext2DEvent. Whenever an operation would modify the canvas
|
|
@@ -22,8 +22,8 @@ exports["default"] = createCanvasEvent;
|
|
|
22
22
|
*/
|
|
23
23
|
function createCanvasEvent(type, transform, props) {
|
|
24
24
|
return {
|
|
25
|
-
type
|
|
26
|
-
transform
|
|
27
|
-
props
|
|
25
|
+
type,
|
|
26
|
+
transform,
|
|
27
|
+
props
|
|
28
28
|
};
|
|
29
29
|
}
|
|
@@ -3,31 +3,29 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
|
|
8
8
|
var _ImageBitmap = _interopRequireDefault(require("../classes/ImageBitmap"));
|
|
9
9
|
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : {
|
|
11
|
-
|
|
12
|
-
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
11
|
|
|
14
12
|
var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, options) {
|
|
15
13
|
var length = arguments.length;
|
|
16
|
-
return new Promise(
|
|
17
|
-
if (length === 0) return reject(new TypeError(
|
|
18
|
-
if (length === 3 || length === 4) return reject(new TypeError(
|
|
19
|
-
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
if (length === 0) return reject(new TypeError("Failed to execute 'createImageBitmap' on 'Window': 1 argument required, but only 0 present."));
|
|
16
|
+
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.'));
|
|
17
|
+
let validImage = false;
|
|
20
18
|
if (img instanceof HTMLImageElement) validImage = true;
|
|
21
19
|
if (img instanceof HTMLVideoElement) validImage = true;
|
|
22
20
|
if (img instanceof HTMLCanvasElement) validImage = true; // checking constructor name is the only reliable way to verify the object's constructing class is "blob-like"
|
|
23
21
|
|
|
24
|
-
if (img instanceof Blob || img && img.constructor && img.constructor.name ===
|
|
25
|
-
if (img instanceof _ImageBitmap
|
|
22
|
+
if (img instanceof Blob || img && img.constructor && img.constructor.name === 'Blob') validImage = true;
|
|
23
|
+
if (img instanceof _ImageBitmap.default) validImage = true;
|
|
26
24
|
if (img instanceof ImageData) validImage = true;
|
|
27
|
-
if (!validImage) return reject(new TypeError(
|
|
25
|
+
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)'"));
|
|
28
26
|
|
|
29
27
|
if (length >= 2) {
|
|
30
|
-
|
|
28
|
+
let index = 6;
|
|
31
29
|
|
|
32
30
|
if (length === 2) {
|
|
33
31
|
index = 2;
|
|
@@ -37,7 +35,7 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
|
|
|
37
35
|
if (length === 5) options = null;
|
|
38
36
|
|
|
39
37
|
if (options !== null && options !== void 0) {
|
|
40
|
-
if (
|
|
38
|
+
if (typeof options !== 'object') throw new TypeError("Failed to execute 'createImageBitmap' on 'Window': parameter " + index + " ('options') is not an object.");
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
|
|
@@ -53,8 +51,8 @@ var _default = jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight,
|
|
|
53
51
|
sHeight = img.height || 1;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
return resolve(new _ImageBitmap
|
|
54
|
+
return resolve(new _ImageBitmap.default(sWidth, sHeight));
|
|
57
55
|
});
|
|
58
56
|
});
|
|
59
57
|
|
|
60
|
-
exports
|
|
58
|
+
exports.default = _default;
|
package/lib/mock/prototype.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports
|
|
6
|
+
exports.default = mockPrototype;
|
|
7
7
|
|
|
8
8
|
function mockPrototype() {
|
|
9
9
|
/**
|
|
@@ -11,21 +11,21 @@ function mockPrototype() {
|
|
|
11
11
|
* jsdom canvases obtained by using the `this` keyword inside the `#getContext('2d')` function
|
|
12
12
|
* call. It's values are the generated `CanvasRenderingContext2D` objects.
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
const generatedContexts = new WeakMap();
|
|
15
15
|
/**
|
|
16
16
|
* Overrides getContext. Every test run will create a new function that overrides the current
|
|
17
17
|
* value of getContext. It attempts to preserve the original getContext function by storing it on
|
|
18
18
|
* the callback as a property.
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const getContext2D = jest.fn(function getContext2d(type) {
|
|
22
22
|
if (type === '2d') {
|
|
23
23
|
/**
|
|
24
24
|
* Contexts must be indempotent. Once they are generated, they should be returned when
|
|
25
25
|
* getContext() is called on the same canvas object multiple times.
|
|
26
26
|
*/
|
|
27
27
|
if (generatedContexts.has(this)) return generatedContexts.get(this);
|
|
28
|
-
|
|
28
|
+
const ctx = new CanvasRenderingContext2D(this);
|
|
29
29
|
generatedContexts.set(this, ctx);
|
|
30
30
|
return ctx;
|
|
31
31
|
}
|
|
@@ -51,9 +51,9 @@ function mockPrototype() {
|
|
|
51
51
|
* we don't know if the canvas is tainted. These kinds of errors will be silent.
|
|
52
52
|
*/
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
if (arguments.length < 1) throw new TypeError(
|
|
56
|
-
if (typeof callback !== 'function') throw new TypeError(
|
|
54
|
+
const toBlobOverride = jest.fn(function toBlobOverride(callback, mimetype) {
|
|
55
|
+
if (arguments.length < 1) throw new TypeError("Failed to execute 'toBlob' on 'HTMLCanvasElement': 1 argument required, but only 0 present.");
|
|
56
|
+
if (typeof callback !== 'function') throw new TypeError("Failed to execute 'toBlob' on 'HTMLCanvasElement': The callback provided as parameter 1 is not a function.");
|
|
57
57
|
/**
|
|
58
58
|
* Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise
|
|
59
59
|
* it's image/png.
|
|
@@ -75,14 +75,12 @@ function mockPrototype() {
|
|
|
75
75
|
*/
|
|
76
76
|
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
const length = this.width * this.height * 4;
|
|
79
|
+
const data = new Uint8Array(length);
|
|
80
|
+
const blob = new window.Blob([data], {
|
|
81
81
|
type: mimetype
|
|
82
82
|
});
|
|
83
|
-
setTimeout(
|
|
84
|
-
return callback(blob);
|
|
85
|
-
}, 0);
|
|
83
|
+
setTimeout(() => callback(blob), 0);
|
|
86
84
|
});
|
|
87
85
|
|
|
88
86
|
if (!jest.isMockFunction(HTMLCanvasElement.prototype.toBlob)) {
|
|
@@ -97,7 +95,7 @@ function mockPrototype() {
|
|
|
97
95
|
* jpeg size is variable, and so is png. TODO: Is there a better way to do this?
|
|
98
96
|
*/
|
|
99
97
|
|
|
100
|
-
|
|
98
|
+
const toDataURLOverride = jest.fn(function toDataURLOverride(type, encoderOptions) {
|
|
101
99
|
switch (type) {
|
|
102
100
|
case 'image/jpeg':
|
|
103
101
|
break;
|