diff --git a/.npmignore b/.npmignore deleted file mode 100644 index 96a29e8..0000000 --- a/.npmignore +++ /dev/null @@ -1,7 +0,0 @@ -.babelrc -.travis.yml -__tests__ -.idea -coverage -src -*.tgz diff --git a/.travis.yml b/.travis.yml index 22c2578..287a4cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ language: node_js node_js: - - "8" - - "9" - - "10" - - "11" - - "12" - - "13" + - '8' + - '9' + - '10' + - '11' + - '12' + - '13' after_success: - - npm run coveralls \ No newline at end of file + - npm run coveralls diff --git a/README.md b/README.md index 5f30883..81e0949 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ The function will do `Number` type coercion and verify the inputs exactly like t this is valid input. ```ts -expect(() => ctx.arc("10", "10", "20", "0", "6.14")).not.toThrow(); +expect(() => ctx.arc('10', '10', '20', '0', '6.14')).not.toThrow(); ``` Another part of the strategy is to validate input types. When using the @@ -90,15 +90,15 @@ Another part of the strategy is to validate input types. When using the `TypeError` just like the browser does. ```ts -expect(() => ctx.fill("invalid!")).toThrow(TypeError); -expect(() => ctx.fill(new Path2D(), "invalid!")).toThrow(TypeError); +expect(() => ctx.fill('invalid!')).toThrow(TypeError); +expect(() => ctx.fill(new Path2D(), 'invalid!')).toThrow(TypeError); ``` We try to follow the ECMAScript specification as closely as possible. ## Snapshots -There are multiple ways to validate canvas state using snapshots. There are currently three methods +There are multiple ways to validate canvas state using snapshots. There are currently three methods attached to the `CanvasRenderingContext2D` class. The first way to use this feature is by using the `__getEvents` method. @@ -175,10 +175,10 @@ canvas.toDataURL.mockReturnValueOnce( ## Contributors -* [@hustcc](https://github.com/hustcc) -* [@jtenner](https://github.com/jtenner) -* [@evanoc0](https://github.com/evanoc0) +- [@hustcc](https://github.com/hustcc) +- [@jtenner](https://github.com/jtenner) +- [@evanoc0](https://github.com/evanoc0) ## License -MIT@[hustcc](https://github.com/hustcc). \ No newline at end of file +MIT@[hustcc](https://github.com/hustcc). diff --git a/__tests__/classes/CanvasGradient.js b/__tests__/classes/CanvasGradient.js index 973694f..79d7676 100644 --- a/__tests__/classes/CanvasGradient.js +++ b/__tests__/classes/CanvasGradient.js @@ -9,7 +9,7 @@ beforeEach(() => { describe('CanvasGradient', () => { test('CanvasGradient', () => { expect(grd).toBeDefined(); - grd.addColorStop(1.0, "blue"); + grd.addColorStop(1.0, 'blue'); expect(grd.addColorStop).toBeCalledWith(1.0, 'blue'); const grd2 = ctx.createLinearGradient(2, 3, 4, 5); @@ -22,7 +22,7 @@ describe('CanvasGradient', () => { expect(grd1.addColorStop).not.toBe(grd2.addColorStop); }); - [Infinity, NaN, -Infinity].forEach(value => { + [Infinity, NaN, -Infinity].forEach((value) => { test('CanvasGradient should throw if offset is ' + value, () => { expect(() => { var grd = ctx.createLinearGradient(1, 2, 3, 4); diff --git a/__tests__/classes/CanvasPattern.js b/__tests__/classes/CanvasPattern.js index 54a69b7..e208255 100644 --- a/__tests__/classes/CanvasPattern.js +++ b/__tests__/classes/CanvasPattern.js @@ -11,7 +11,6 @@ beforeEach(() => { }); describe('CanvasPattern', () => { - test('CanvasPattern', () => { const ptrn = ctx.createPattern(img, 'no-repeat'); expect(ptrn).toBeDefined(); diff --git a/__tests__/classes/CanvasRenderingContext2D.__clearDrawCalls.js b/__tests__/classes/CanvasRenderingContext2D.__clearDrawCalls.js index fa25184..e909157 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__clearDrawCalls.js +++ b/__tests__/classes/CanvasRenderingContext2D.__clearDrawCalls.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); afterEach(() => { @@ -11,12 +10,12 @@ afterEach(() => { }); describe('__clearDrawCalls', () => { - it("should clear the list of draw calls", () => { + it('should clear the list of draw calls', () => { ctx.fillRect(1, 2, 3, 4); ctx.__clearDrawCalls(); }); - it("should not prevent additional draw calls from being collected", () => { + it('should not prevent additional draw calls from being collected', () => { ctx.fillRect(1, 2, 3, 4); ctx.__clearDrawCalls(); ctx.fillRect(1, 2, 3, 4); diff --git a/__tests__/classes/CanvasRenderingContext2D.__clearEvents.js b/__tests__/classes/CanvasRenderingContext2D.__clearEvents.js index 6af3841..c7b6cd2 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__clearEvents.js +++ b/__tests__/classes/CanvasRenderingContext2D.__clearEvents.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); afterEach(() => { @@ -11,12 +10,12 @@ afterEach(() => { }); describe('__clearEvents', () => { - it("should clear the list of events", () => { + it('should clear the list of events', () => { ctx.fillRect(1, 2, 3, 4); ctx.__clearEvents(); }); - it("should not prevent additional events from being collected", () => { + it('should not prevent additional events from being collected', () => { ctx.fillRect(1, 2, 3, 4); ctx.__clearEvents(); ctx.fillRect(1, 2, 3, 4); diff --git a/__tests__/classes/CanvasRenderingContext2D.__clearPath.js b/__tests__/classes/CanvasRenderingContext2D.__clearPath.js index c63ac0e..1863099 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__clearPath.js +++ b/__tests__/classes/CanvasRenderingContext2D.__clearPath.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); afterEach(() => { @@ -11,12 +10,12 @@ afterEach(() => { }); describe('__clearEvents', () => { - it("should clear the list of events", () => { + it('should clear the list of events', () => { ctx.arc(1, 2, 3, 4, 5); ctx.__clearPath(); }); - it("should not prevent additional events from being collected", () => { + it('should not prevent additional events from being collected', () => { ctx.arc(1, 2, 3, 4, 5); ctx.__clearPath(); ctx.arc(1, 2, 3, 4, 5); diff --git a/__tests__/classes/CanvasRenderingContext2D.__getClippingPath.js b/__tests__/classes/CanvasRenderingContext2D.__getClippingPath.js index fd2318c..70113ab 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__getClippingPath.js +++ b/__tests__/classes/CanvasRenderingContext2D.__getClippingPath.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); afterEach(() => { @@ -11,11 +10,11 @@ afterEach(() => { }); describe('__getClippingRegion', () => { - it("should be empty when there are no path elements", () => { + it('should be empty when there are no path elements', () => { ctx.clip(); }); - it("should store the clipping region", () => { + it('should store the clipping region', () => { ctx.rect(1, 2, 3, 4); ctx.arc(1, 2, 3, 4, 5); ctx.clip(); @@ -30,7 +29,7 @@ describe('__getClippingRegion', () => { ctx.clip(); }); - it("should save the clipping region correctly when saved", () => { + it('should save the clipping region correctly when saved', () => { ctx.rect(1, 2, 3, 4); ctx.arc(1, 2, 3, 4, 5); ctx.clip(); @@ -39,7 +38,7 @@ describe('__getClippingRegion', () => { expect(region).toStrictEqual(ctx.__getClippingRegion()); }); - it("should save the clipping region correctly when saved", () => { + it('should save the clipping region correctly when saved', () => { ctx.rect(1, 2, 3, 4); ctx.arc(1, 2, 3, 4, 5); ctx.clip(); diff --git a/__tests__/classes/CanvasRenderingContext2D.__getDrawCalls.js b/__tests__/classes/CanvasRenderingContext2D.__getDrawCalls.js index 16440d7..1de00b1 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__getDrawCalls.js +++ b/__tests__/classes/CanvasRenderingContext2D.__getDrawCalls.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); const img = new Image(); diff --git a/__tests__/classes/CanvasRenderingContext2D.__getEvents.js b/__tests__/classes/CanvasRenderingContext2D.__getEvents.js index 4433de0..89af099 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__getEvents.js +++ b/__tests__/classes/CanvasRenderingContext2D.__getEvents.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); const img = new Image(); diff --git a/__tests__/classes/CanvasRenderingContext2D.__getPath.js b/__tests__/classes/CanvasRenderingContext2D.__getPath.js index e6c059f..543f944 100644 --- a/__tests__/classes/CanvasRenderingContext2D.__getPath.js +++ b/__tests__/classes/CanvasRenderingContext2D.__getPath.js @@ -1,8 +1,7 @@ let ctx; beforeEach(() => { // get a new context each test - ctx = document.createElement('canvas') - .getContext('2d'); + ctx = document.createElement('canvas').getContext('2d'); }); const path = new Path2D(); diff --git a/__tests__/classes/CanvasRenderingContext2D.addHitRegion.js b/__tests__/classes/CanvasRenderingContext2D.addHitRegion.js index 96d277e..a95b7e5 100644 --- a/__tests__/classes/CanvasRenderingContext2D.addHitRegion.js +++ b/__tests__/classes/CanvasRenderingContext2D.addHitRegion.js @@ -22,12 +22,18 @@ describe('addHitRegion', () => { expect(() => ctx.addHitRegion()).toThrow(DOMException); }); - it('should throw if fillRule is set and isn\'t \'evenodd\' or \'nonzero\'', () => { - expect(() => ctx.addHitRegion({ id: 'test', fillRule: 'wrong!' })).toThrow(); + it("should throw if fillRule is set and isn't 'evenodd' or 'nonzero'", () => { + expect(() => + ctx.addHitRegion({ id: 'test', fillRule: 'wrong!' }) + ).toThrow(); }); it('should not throw if fillRule is valid', () => { - expect(() => ctx.addHitRegion({ id: 'test', fillRule: 'evenodd' })).not.toThrow(); - expect(() => ctx.addHitRegion({ id: 'test', fillRule: 'nonzero' })).not.toThrow(); + expect(() => + ctx.addHitRegion({ id: 'test', fillRule: 'evenodd' }) + ).not.toThrow(); + expect(() => + ctx.addHitRegion({ id: 'test', fillRule: 'nonzero' }) + ).not.toThrow(); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.arc.js b/__tests__/classes/CanvasRenderingContext2D.arc.js index e92ef0e..548fac1 100644 --- a/__tests__/classes/CanvasRenderingContext2D.arc.js +++ b/__tests__/classes/CanvasRenderingContext2D.arc.js @@ -18,10 +18,10 @@ describe('arc', () => { expect(ctx.arc).toBeCalled(); }); - it('shouldn\'t accept parameters less than 7', () => { + it("shouldn't accept parameters less than 7", () => { expect(() => ctx.arc()).toThrow(TypeError); expect(() => ctx.arc(1)).toThrow(TypeError); - expect(() => ctx.arc(1, 2,)).toThrow(TypeError); + expect(() => ctx.arc(1, 2)).toThrow(TypeError); expect(() => ctx.arc(1, 2, 3)).toThrow(TypeError); expect(() => ctx.arc(1, 2, 3, 4)).toThrow(TypeError); }); @@ -37,7 +37,7 @@ describe('arc', () => { [1, 2, NaN, 4, 5], [1, 2, 3, NaN, 5], [1, 2, 3, 4, NaN], - ].forEach(e => { + ].forEach((e) => { expect(() => ctx.arc(...e)).not.toThrow(); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.arcTo.js b/__tests__/classes/CanvasRenderingContext2D.arcTo.js index 358e42e..3214414 100644 --- a/__tests__/classes/CanvasRenderingContext2D.arcTo.js +++ b/__tests__/classes/CanvasRenderingContext2D.arcTo.js @@ -18,7 +18,7 @@ describe('arcTo', () => { expect(ctx.arcTo).toBeCalled(); }); - it('shouldn\'t accept parameters less than 5', () => { + it("shouldn't accept parameters less than 5", () => { expect(() => ctx.arcTo(1, 2, 3)).toThrow(DOMException); }); @@ -31,7 +31,7 @@ describe('arcTo', () => { [1, 2, 3, 4, 5], [null, void 0, '', NaN, Infinity], [-100, -100, 100, 0, 0], - ].forEach(e => { + ].forEach((e) => { expect(() => ctx.arcTo(...e)).not.toThrow(); }); }); @@ -42,8 +42,8 @@ describe('arcTo', () => { [1, NaN, 2, 3, -1], [1, 2, NaN, 3, -1], [1, 2, 3, NaN, -1], - ].forEach(e => { + ].forEach((e) => { expect(() => ctx.arcTo(...e)).not.toThrow(); }); }); -}) \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.clip.js b/__tests__/classes/CanvasRenderingContext2D.clip.js index 727c18c..c1b2c1b 100644 --- a/__tests__/classes/CanvasRenderingContext2D.clip.js +++ b/__tests__/classes/CanvasRenderingContext2D.clip.js @@ -19,21 +19,20 @@ describe('clip', () => { }); it('should clip paths', () => { - expect(() => ctx.clip(p)).not.toThrow(); + expect(() => ctx.clip(p)).not.toThrow(); }); it('should throw if clipRule is not valid clipRule', () => { - [null, 1, Infinity, NaN, void 0, 'bad!'].forEach(e => { + [null, 1, Infinity, NaN, void 0, 'bad!'].forEach((e) => { expect(() => ctx.clip(p, e)).toThrow(TypeError); expect(() => ctx.clip(e)).toThrow(TypeError); }); }); it('should accept valid fillRules', () => { - ['evenodd', 'nonzero'].forEach(e => { + ['evenodd', 'nonzero'].forEach((e) => { expect(() => ctx.clip(e)).not.toThrow(); expect(() => ctx.clip(p, e)).not.toThrow(); }); }); }); - diff --git a/__tests__/classes/CanvasRenderingContext2D.createImageData.js b/__tests__/classes/CanvasRenderingContext2D.createImageData.js index 5664028..e8b75cf 100644 --- a/__tests__/classes/CanvasRenderingContext2D.createImageData.js +++ b/__tests__/classes/CanvasRenderingContext2D.createImageData.js @@ -42,7 +42,7 @@ describe('createImageData', () => { [NaN, 1], [1, NaN], ['test', null], - ].forEach(value => { + ].forEach((value) => { expect(() => ctx.createImageData(...value)).toThrow(TypeError); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.createImagePattern.js b/__tests__/classes/CanvasRenderingContext2D.createImagePattern.js index 5c0856d..ae0a180 100644 --- a/__tests__/classes/CanvasRenderingContext2D.createImagePattern.js +++ b/__tests__/classes/CanvasRenderingContext2D.createImagePattern.js @@ -16,13 +16,13 @@ describe('createImagePattern', () => { expect(result).toBeInstanceOf(CanvasPattern); }); - it('shouldn\'t create image patterns when argument length is 1', () => { + it("shouldn't create image patterns when argument length is 1", () => { const img = new Image(); img.src = 'http://some-domain.com/my-image.png'; expect(() => ctx.createPattern(img)).toThrow(TypeError); }); - it('shouldn\'t create image patterns when second argument is undefined', () => { + it("shouldn't create image patterns when second argument is undefined", () => { const img = new Image(); img.src = 'http://some-domain.com/my-image.png'; expect(() => ctx.createPattern(img, void 0)).toThrow(TypeError); @@ -40,7 +40,7 @@ describe('createImagePattern', () => { expect(ctx.createPattern(img, '')).toBeInstanceOf(CanvasPattern); }); - it('shouldn\'t create imagePattern when image is not valid', () => { + it("shouldn't create imagePattern when image is not valid", () => { expect(() => ctx.createPattern(null, 'repeat')).toThrow(); }); @@ -55,7 +55,9 @@ describe('createImagePattern', () => { }); it('should create a pattern when image is ImageBitmap', () => { - expect(ctx.createPattern(new ImageBitmap(400, 300), 'repeat')).toBeInstanceOf(CanvasPattern); + expect( + ctx.createPattern(new ImageBitmap(400, 300), 'repeat') + ).toBeInstanceOf(CanvasPattern); }); it('should not create a pattern if the image bitmap is closed', () => { @@ -68,8 +70,8 @@ describe('createImagePattern', () => { const image = new Image(); image.src = 'test/myImage.jpg'; - ['repeat', 'repeat-x', 'repeat-y', 'no-repeat'].forEach(e => { + ['repeat', 'repeat-x', 'repeat-y', 'no-repeat'].forEach((e) => { expect(ctx.createPattern(image, e)).toBeInstanceOf(CanvasPattern); }); }); -}) \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.createLinearGradient.js b/__tests__/classes/CanvasRenderingContext2D.createLinearGradient.js index d78790e..f0908fe 100644 --- a/__tests__/classes/CanvasRenderingContext2D.createLinearGradient.js +++ b/__tests__/classes/CanvasRenderingContext2D.createLinearGradient.js @@ -26,10 +26,18 @@ describe('createLinearGradient', () => { }); it('should not create a linear gradient when any argument is not finite', () => { - expect(() => ctx.createLinearGradient(Infinity, 1, 2, 3)).toThrow(TypeError); - expect(() => ctx.createLinearGradient(0, Infinity, 2, 3)).toThrow(TypeError); - expect(() => ctx.createLinearGradient(0, 1, Infinity, 3)).toThrow(TypeError); - expect(() => ctx.createLinearGradient(0, 1, 2, Infinity)).toThrow(TypeError); + expect(() => ctx.createLinearGradient(Infinity, 1, 2, 3)).toThrow( + TypeError + ); + expect(() => ctx.createLinearGradient(0, Infinity, 2, 3)).toThrow( + TypeError + ); + expect(() => ctx.createLinearGradient(0, 1, Infinity, 3)).toThrow( + TypeError + ); + expect(() => ctx.createLinearGradient(0, 1, 2, Infinity)).toThrow( + TypeError + ); }); it('should create a linear gradient with string values', () => { diff --git a/__tests__/classes/CanvasRenderingContext2D.createRadialGradient.js b/__tests__/classes/CanvasRenderingContext2D.createRadialGradient.js index d6eaed1..8410468 100644 --- a/__tests__/classes/CanvasRenderingContext2D.createRadialGradient.js +++ b/__tests__/classes/CanvasRenderingContext2D.createRadialGradient.js @@ -23,17 +23,33 @@ describe('createRadialGradient', () => { }); it('should not create a radial gradient when any argument is not finite', () => { - expect(() => ctx.createRadialGradient(Infinity, 1, 2, 3, 4, 5)).toThrow(TypeError); - expect(() => ctx.createRadialGradient(0, Infinity, 2, 3, 4, 5)).toThrow(TypeError); - expect(() => ctx.createRadialGradient(0, 1, Infinity, 3, 4, 5)).toThrow(TypeError); - expect(() => ctx.createRadialGradient(0, 1, 2, Infinity, 4, 5)).toThrow(TypeError); - expect(() => ctx.createRadialGradient(0, 1, 2, 3, Infinity, 5)).toThrow(TypeError); - expect(() => ctx.createRadialGradient(0, 1, 2, 3, 4, Infinity)).toThrow(TypeError); + expect(() => ctx.createRadialGradient(Infinity, 1, 2, 3, 4, 5)).toThrow( + TypeError + ); + expect(() => ctx.createRadialGradient(0, Infinity, 2, 3, 4, 5)).toThrow( + TypeError + ); + expect(() => ctx.createRadialGradient(0, 1, Infinity, 3, 4, 5)).toThrow( + TypeError + ); + expect(() => ctx.createRadialGradient(0, 1, 2, Infinity, 4, 5)).toThrow( + TypeError + ); + expect(() => ctx.createRadialGradient(0, 1, 2, 3, Infinity, 5)).toThrow( + TypeError + ); + expect(() => ctx.createRadialGradient(0, 1, 2, 3, 4, Infinity)).toThrow( + TypeError + ); }); it('should not create a radial gradient if any of the radius values are < 0', () => { - expect(() => ctx.createRadialGradient(0, 0, -1, 0, 0, 0)).toThrow(DOMException); - expect(() => ctx.createRadialGradient(0, 0, 0, 0, 0, -1)).toThrow(DOMException); + expect(() => ctx.createRadialGradient(0, 0, -1, 0, 0, 0)).toThrow( + DOMException + ); + expect(() => ctx.createRadialGradient(0, 0, 0, 0, 0, -1)).toThrow( + DOMException + ); }); it('should create a radial gradient with string values', () => { diff --git a/__tests__/classes/CanvasRenderingContext2D.currentTransform.js b/__tests__/classes/CanvasRenderingContext2D.currentTransform.js index 41318d6..0ff5d02 100644 --- a/__tests__/classes/CanvasRenderingContext2D.currentTransform.js +++ b/__tests__/classes/CanvasRenderingContext2D.currentTransform.js @@ -13,7 +13,7 @@ describe('currentTransform', () => { expect(ctx.currentTransform).toBeInstanceOf(DOMMatrix); }); - it('should ignore setting currentTransform if it\'s not a valid DOMMatrix', () => { + it("should ignore setting currentTransform if it's not a valid DOMMatrix", () => { ctx.currentTransform = null; expect(ctx._transformStack[0][0]).toBe(1); expect(ctx._transformStack[0][1]).toBe(0); diff --git a/__tests__/classes/CanvasRenderingContext2D.direction.js b/__tests__/classes/CanvasRenderingContext2D.direction.js index e639bea..db0746b 100644 --- a/__tests__/classes/CanvasRenderingContext2D.direction.js +++ b/__tests__/classes/CanvasRenderingContext2D.direction.js @@ -9,16 +9,16 @@ beforeEach(() => { }); describe('direction', () => { - it('should set the default value direction to \'inherit\'', () => { + it("should set the default value direction to 'inherit'", () => { expect(ctx.direction).toBe('inherit'); }); - it('should set the direction if it\'s a valid direction', () => { + it("should set the direction if it's a valid direction", () => { ctx.direction = 'wrong!'; expect(ctx.direction).toBe('inherit'); }); - it('should set the direction if it\'s a valid direction', () => { + it("should set the direction if it's a valid direction", () => { ctx.direction = 'ltr'; expect(ctx.direction).toBe('ltr'); ctx.direction = 'rtl'; diff --git a/__tests__/classes/CanvasRenderingContext2D.drawFocusIfNeeded.js b/__tests__/classes/CanvasRenderingContext2D.drawFocusIfNeeded.js index 309f61c..d7bff58 100644 --- a/__tests__/classes/CanvasRenderingContext2D.drawFocusIfNeeded.js +++ b/__tests__/classes/CanvasRenderingContext2D.drawFocusIfNeeded.js @@ -25,13 +25,13 @@ describe('drawFocusIfNeeded', () => { }); it('should throw if first argument is not Element', () => { - [1, NaN, Infinity, 'test', {}, []].forEach(e => { + [1, NaN, Infinity, 'test', {}, []].forEach((e) => { expect(() => ctx.drawFocusIfNeeded(e)).toThrow(TypeError); }); }); it('should throw if two parameters are provided and the first parameter is not a path', () => { - [1, NaN, Infinity, 'test', {}, []].forEach(e => { + [1, NaN, Infinity, 'test', {}, []].forEach((e) => { expect(() => ctx.drawFocusIfNeeded(e, elem)).toThrow(TypeError); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.drawImage.js b/__tests__/classes/CanvasRenderingContext2D.drawImage.js index 8c3a971..40daaaf 100644 --- a/__tests__/classes/CanvasRenderingContext2D.drawImage.js +++ b/__tests__/classes/CanvasRenderingContext2D.drawImage.js @@ -53,13 +53,13 @@ describe('drawImage', () => { [img, 1, 2, 3, 4, 5], [img, 1, 2, 3, 4, 5, 6], [img, 1, 2, 3, 4, 5, 6, 7], - ].forEach(e => { + ].forEach((e) => { expect(() => ctx.drawImage(...e)).toThrow(TypeError); }); }); it('should not accept nulls or invalid image types', () => { - [null, 1, NaN, Infinity, 'testing'].forEach(e => { + [null, 1, NaN, Infinity, 'testing'].forEach((e) => { expect(() => ctx.drawImage(e, 1, 2)).toThrow(TypeError); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.ellipse.js b/__tests__/classes/CanvasRenderingContext2D.ellipse.js index db5449a..5d5aee0 100644 --- a/__tests__/classes/CanvasRenderingContext2D.ellipse.js +++ b/__tests__/classes/CanvasRenderingContext2D.ellipse.js @@ -18,10 +18,10 @@ describe('ellipse', () => { expect(ctx.ellipse).toBeCalled(); }); - it('shouldn\'t accept parameters less than 7', () => { + it("shouldn't accept parameters less than 7", () => { expect(() => ctx.ellipse()).toThrow(TypeError); expect(() => ctx.ellipse(1)).toThrow(TypeError); - expect(() => ctx.ellipse(1, 2,)).toThrow(TypeError); + expect(() => ctx.ellipse(1, 2)).toThrow(TypeError); expect(() => ctx.ellipse(1, 2, 3)).toThrow(TypeError); expect(() => ctx.ellipse(1, 2, 3, 4)).toThrow(TypeError); expect(() => ctx.ellipse(1, 2, 3, 4, 5)).toThrow(TypeError); @@ -42,7 +42,7 @@ describe('ellipse', () => { [1, 2, 3, 4, NaN, 6, 7], [1, 2, 3, 4, 5, NaN, 7], [1, 2, 3, 4, 5, 6, NaN], - ].forEach(e => { + ].forEach((e) => { expect(() => ctx.ellipse(...e)).not.toThrow(); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.fill.js b/__tests__/classes/CanvasRenderingContext2D.fill.js index 441421f..88aa58f 100644 --- a/__tests__/classes/CanvasRenderingContext2D.fill.js +++ b/__tests__/classes/CanvasRenderingContext2D.fill.js @@ -19,21 +19,20 @@ describe('fill', () => { }); it('should fill paths', () => { - expect(() => ctx.fill(p)).not.toThrow(); + expect(() => ctx.fill(p)).not.toThrow(); }); it('should throw if fillRule is not valid fillRule', () => { - [null, 1, Infinity, NaN, void 0, 'bad!'].forEach(e => { + [null, 1, Infinity, NaN, void 0, 'bad!'].forEach((e) => { expect(() => ctx.fill(p, e)).toThrow(TypeError); expect(() => ctx.fill(e)).toThrow(TypeError); }); }); it('should accept valid fillRules', () => { - ['evenodd', 'nonzero'].forEach(e => { + ['evenodd', 'nonzero'].forEach((e) => { expect(() => ctx.fill(e)).not.toThrow(); expect(() => ctx.fill(p, e)).not.toThrow(); }); }); }); - diff --git a/__tests__/classes/CanvasRenderingContext2D.fillStyle.js b/__tests__/classes/CanvasRenderingContext2D.fillStyle.js index 54ac394..e2b34ec 100644 --- a/__tests__/classes/CanvasRenderingContext2D.fillStyle.js +++ b/__tests__/classes/CanvasRenderingContext2D.fillStyle.js @@ -9,7 +9,7 @@ beforeEach(() => { }); describe('fillStyle', () => { - it('should parse a css color string \'blue\'', () => { + it("should parse a css color string 'blue'", () => { ctx.fillStyle = 'blue'; expect(ctx.fillStyle).toBe('#0000ff'); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.filter.js b/__tests__/classes/CanvasRenderingContext2D.filter.js index 8d5f717..16871b5 100644 --- a/__tests__/classes/CanvasRenderingContext2D.filter.js +++ b/__tests__/classes/CanvasRenderingContext2D.filter.js @@ -9,7 +9,7 @@ beforeEach(() => { }); describe('filter', () => { - it('should have a filter property default value \'none\'', () => { + it("should have a filter property default value 'none'", () => { expect(ctx.filter).toBe('none'); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.font.js b/__tests__/classes/CanvasRenderingContext2D.font.js index c67b45d..2a76f5a 100644 --- a/__tests__/classes/CanvasRenderingContext2D.font.js +++ b/__tests__/classes/CanvasRenderingContext2D.font.js @@ -16,7 +16,7 @@ describe('font', () => { it('should accept valid fonts', () => { ctx.font = '12pt Times New Roman'; - expect(ctx.font).toBe('16px \"Times New Roman\"'); + expect(ctx.font).toBe('16px "Times New Roman"'); }); it('should save and restore font values', () => { diff --git a/__tests__/classes/CanvasRenderingContext2D.globalAlpha.js b/__tests__/classes/CanvasRenderingContext2D.globalAlpha.js index 3ca180b..cf1bb81 100644 --- a/__tests__/classes/CanvasRenderingContext2D.globalAlpha.js +++ b/__tests__/classes/CanvasRenderingContext2D.globalAlpha.js @@ -10,21 +10,21 @@ beforeEach(() => { describe('globalAlpha', () => { it('should ignore non finite globalAlpha values', () => { - [Infinity, -Infinity, void 0, NaN].forEach(e => { + [Infinity, -Infinity, void 0, NaN].forEach((e) => { ctx.globalAlpha = e; expect(ctx.globalAlpha).toBe(1); }); }); it('should ignore out of range values', () => { - [-1, 1.1].forEach(e => { + [-1, 1.1].forEach((e) => { ctx.globalAlpha = e; expect(ctx.globalAlpha).toBe(1); }); }); it('should not ignore globalAlpha values that are within range', () => { - [0.1, 0.2, 0.3, 0.4].forEach(e => { + [0.1, 0.2, 0.3, 0.4].forEach((e) => { ctx.globalAlpha = e; expect(ctx.globalAlpha).toBe(e); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.globalCompositeOperation.js b/__tests__/classes/CanvasRenderingContext2D.globalCompositeOperation.js index 1c57dea..73b6911 100644 --- a/__tests__/classes/CanvasRenderingContext2D.globalCompositeOperation.js +++ b/__tests__/classes/CanvasRenderingContext2D.globalCompositeOperation.js @@ -9,7 +9,7 @@ beforeEach(() => { }); describe('globalCompositeOperation', () => { - it('should change the global composite operation when it\'s valid', () => { + it("should change the global composite operation when it's valid", () => { const validOperations = [ 'source-over', 'source-in', @@ -38,14 +38,14 @@ describe('globalCompositeOperation', () => { 'color', 'luminosity', ]; - validOperations.forEach(e => { + validOperations.forEach((e) => { ctx.globalCompositeOperation = e; expect(ctx.globalCompositeOperation).toBe(e); }); }); it('should ignore non valid values', () => { - [null, -1, void 0, Infinity, NaN, 'blah', ''].forEach(e => { + [null, -1, void 0, Infinity, NaN, 'blah', ''].forEach((e) => { ctx.globalCompositeOperation = e; expect(ctx.globalCompositeOperation).toBe('source-over'); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.imageSmoothingEnabled.js b/__tests__/classes/CanvasRenderingContext2D.imageSmoothingEnabled.js index 922ab16..f9dec36 100644 --- a/__tests__/classes/CanvasRenderingContext2D.imageSmoothingEnabled.js +++ b/__tests__/classes/CanvasRenderingContext2D.imageSmoothingEnabled.js @@ -10,7 +10,7 @@ beforeEach(() => { describe('imageSmoothingQuality', () => { it('should set the imageSmoothingEnabled values to truthy values', () => { - [true, false, 1, 0, null, '', Infinity, void 0, NaN].forEach(e => { + [true, false, 1, 0, null, '', Infinity, void 0, NaN].forEach((e) => { ctx.imageSmoothingEnabled = e; expect(ctx.imageSmoothingEnabled).toBe(!!e); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.imageSmoothingQuality.js b/__tests__/classes/CanvasRenderingContext2D.imageSmoothingQuality.js index ab55643..bb2da40 100644 --- a/__tests__/classes/CanvasRenderingContext2D.imageSmoothingQuality.js +++ b/__tests__/classes/CanvasRenderingContext2D.imageSmoothingQuality.js @@ -10,17 +10,19 @@ beforeEach(() => { describe('imageSmoothingQuality', () => { it('should accept valid imageSmoothingQuality values', () => { - ['high', 'medium', 'low'].forEach(e => { + ['high', 'medium', 'low'].forEach((e) => { ctx.imageSmoothingQuality = e; expect(ctx.imageSmoothingQuality).toBe(e); }); }); it('should ignore invalid imageSmoothingQuality values', () => { - [true, false, 1, 0, null, '', Infinity, void 0, NaN, 'invalid!'].forEach(e => { - ctx.imageSmoothingQuality = e; - expect(ctx.imageSmoothingQuality).toBe('low'); - }); + [true, false, 1, 0, null, '', Infinity, void 0, NaN, 'invalid!'].forEach( + (e) => { + ctx.imageSmoothingQuality = e; + expect(ctx.imageSmoothingQuality).toBe('low'); + } + ); }); it('should save and restore imageSmoothingQuality values', () => { diff --git a/__tests__/classes/CanvasRenderingContext2D.isPointInPath.js b/__tests__/classes/CanvasRenderingContext2D.isPointInPath.js index 2ce653f..1b809e0 100644 --- a/__tests__/classes/CanvasRenderingContext2D.isPointInPath.js +++ b/__tests__/classes/CanvasRenderingContext2D.isPointInPath.js @@ -40,4 +40,4 @@ describe('isPointInPath', () => { expect(() => ctx.isPointInPath(p, 1, 2, 'evenodd')).not.toThrow(); expect(() => ctx.isPointInPath(p, 1, 2, 'nonzero')).not.toThrow(); }); -}); \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.isPointInStroke.js b/__tests__/classes/CanvasRenderingContext2D.isPointInStroke.js index e12489f..2646902 100644 --- a/__tests__/classes/CanvasRenderingContext2D.isPointInStroke.js +++ b/__tests__/classes/CanvasRenderingContext2D.isPointInStroke.js @@ -28,4 +28,4 @@ describe('isPointInStroke', () => { expect(ctx.isPointInStroke(1, 2)).toBeFalsy(); expect(ctx.isPointInStroke(p, 1, 2)).toBeFalsy(); }); -}); \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.lineCap.js b/__tests__/classes/CanvasRenderingContext2D.lineCap.js index 73aee43..4304c00 100644 --- a/__tests__/classes/CanvasRenderingContext2D.lineCap.js +++ b/__tests__/classes/CanvasRenderingContext2D.lineCap.js @@ -10,17 +10,19 @@ beforeEach(() => { describe('lineCap', () => { it('should accept valid lineCap values', () => { - ['butt', 'round', 'square'].forEach(e => { + ['butt', 'round', 'square'].forEach((e) => { ctx.lineCap = e; expect(ctx.lineCap).toBe(e); }); }); it('should ignore invalid lineCap values', () => { - [true, false, 1, 0, null, '', Infinity, void 0, NaN, 'invalid!'].forEach(e => { - ctx.lineCap = e; - expect(ctx.lineCap).toBe('butt'); - }); + [true, false, 1, 0, null, '', Infinity, void 0, NaN, 'invalid!'].forEach( + (e) => { + ctx.lineCap = e; + expect(ctx.lineCap).toBe('butt'); + } + ); }); it('should save and restore lineCap values', () => { diff --git a/__tests__/classes/CanvasRenderingContext2D.lineDash.js b/__tests__/classes/CanvasRenderingContext2D.lineDash.js index 49377bb..89729d4 100644 --- a/__tests__/classes/CanvasRenderingContext2D.lineDash.js +++ b/__tests__/classes/CanvasRenderingContext2D.lineDash.js @@ -20,23 +20,24 @@ function every(items, callback) { function map(items, callback) { const result = []; - for(let i = 0; i < items.length; i++) { + for (let i = 0; i < items.length; i++) { result.push(callback(items[i])); } return result; } -const isSequence = (value) => [ - Array, - Int8Array, - Uint8Array, - Int16Array, - Uint16Array, - Int32Array, - Uint32Array, - Float32Array, - Float64Array, -].reduce((left, right) => left || value instanceof right, false); +const isSequence = (value) => + [ + Array, + Int8Array, + Uint8Array, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + ].reduce((left, right) => left || value instanceof right, false); describe('lineDash', () => { it('should accept valid lineDash values ignore invalid lineDash values', () => { @@ -54,14 +55,16 @@ describe('lineDash', () => { null, ]; - examples.forEach(e => { + examples.forEach((e) => { ctx.setLineDash([]); // reset the linedash - if(!isSequence(e)) { + if (!isSequence(e)) { expect(() => ctx.setLineDash(e)).toThrow(TypeError); } else { ctx.setLineDash(e); - let result = map(e, val => Number(val)); - const containsFiniteValues = every(result, val => Number.isFinite(val)); + let result = map(e, (val) => Number(val)); + const containsFiniteValues = every(result, (val) => + Number.isFinite(val) + ); if (containsFiniteValues) { result = result.length % 2 === 1 ? result.concat(result) : result; } else { diff --git a/__tests__/classes/CanvasRenderingContext2D.lineDashOffset.js b/__tests__/classes/CanvasRenderingContext2D.lineDashOffset.js index 47494cd..da74338 100644 --- a/__tests__/classes/CanvasRenderingContext2D.lineDashOffset.js +++ b/__tests__/classes/CanvasRenderingContext2D.lineDashOffset.js @@ -14,7 +14,7 @@ describe('lineDashOffset', () => { }); it('should cast js values to numbers and ignore non-finite values when setting the lineDashOffset property', () => { - [0, 10, -Infinity, 'null', null, NaN].forEach(e => { + [0, 10, -Infinity, 'null', null, NaN].forEach((e) => { ctx.lineDashOffset = 0; ctx.lineDashOffset = e; const cast = Number(e); diff --git a/__tests__/classes/CanvasRenderingContext2D.lineJoin.js b/__tests__/classes/CanvasRenderingContext2D.lineJoin.js index 08650e8..efe5745 100644 --- a/__tests__/classes/CanvasRenderingContext2D.lineJoin.js +++ b/__tests__/classes/CanvasRenderingContext2D.lineJoin.js @@ -9,16 +9,16 @@ beforeEach(() => { }); describe('lineJoin', () => { - it('should set the default value lineJoin to \'miter\'', () => { + it("should set the default value lineJoin to 'miter'", () => { expect(ctx.lineJoin).toBe('miter'); }); - it('should set the lineJoin if it\'s a valid lineJoin', () => { + it("should set the lineJoin if it's a valid lineJoin", () => { ctx.lineJoin = 'wrong!'; expect(ctx.lineJoin).toBe('miter'); }); - it('should set the lineJoin if it\'s a valid lineJoin', () => { + it("should set the lineJoin if it's a valid lineJoin", () => { ctx.lineJoin = 'round'; expect(ctx.lineJoin).toBe('round'); ctx.lineJoin = 'bevel'; diff --git a/__tests__/classes/CanvasRenderingContext2D.lineWidth.js b/__tests__/classes/CanvasRenderingContext2D.lineWidth.js index bed3f61..08b2eda 100644 --- a/__tests__/classes/CanvasRenderingContext2D.lineWidth.js +++ b/__tests__/classes/CanvasRenderingContext2D.lineWidth.js @@ -14,21 +14,21 @@ describe('lineWidth', () => { }); it('should ignore non finite lineWidth values', () => { - [Infinity, -Infinity, null, void 0, NaN].forEach(e => { + [Infinity, -Infinity, null, void 0, NaN].forEach((e) => { ctx.lineWidth = e; expect(ctx.lineWidth).toBe(1); }); }); it('should ignore out of range lineWidth values', () => { - [-1, -10, -300, 0].forEach(e => { + [-1, -10, -300, 0].forEach((e) => { ctx.lineWidth = e; expect(ctx.lineWidth).toBe(1); }); }); it('should not ignore lineWidth values that are within range', () => { - [1, 10, '30', '10.2'].forEach(e => { + [1, 10, '30', '10.2'].forEach((e) => { ctx.lineWidth = e; expect(ctx.lineWidth).toBe(Number(e)); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.measureText.js b/__tests__/classes/CanvasRenderingContext2D.measureText.js index 140f42b..4dd3cec 100644 --- a/__tests__/classes/CanvasRenderingContext2D.measureText.js +++ b/__tests__/classes/CanvasRenderingContext2D.measureText.js @@ -29,7 +29,7 @@ describe('measureText', () => { }); it('should return a text metrics object for different kinds of input', () => { - [NaN, 1, null, void 0, 'bleh'].forEach(val => { + [NaN, 1, null, void 0, 'bleh'].forEach((val) => { expect(() => ctx.measureText(val)).not.toThrow(); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.miterLimit.js b/__tests__/classes/CanvasRenderingContext2D.miterLimit.js index 578f571..a591168 100644 --- a/__tests__/classes/CanvasRenderingContext2D.miterLimit.js +++ b/__tests__/classes/CanvasRenderingContext2D.miterLimit.js @@ -14,21 +14,21 @@ describe('miterLimit', () => { }); it('should ignore non finite values', () => { - [Infinity, -Infinity, null, void 0, NaN].forEach(e => { + [Infinity, -Infinity, null, void 0, NaN].forEach((e) => { ctx.miterLimit = e; expect(ctx.miterLimit).toBe(10); }); }); it('should ignore out of range values', () => { - [-1, -10, -300, 0].forEach(e => { + [-1, -10, -300, 0].forEach((e) => { ctx.miterLimit = e; expect(ctx.miterLimit).toBe(10); }); }); it('should not ignore values that are within range', () => { - [1, 10, '30', '10.2'].forEach(e => { + [1, 10, '30', '10.2'].forEach((e) => { ctx.miterLimit = e; expect(ctx.miterLimit).toBe(Number(e)); }); @@ -42,5 +42,4 @@ describe('miterLimit', () => { ctx.restore(); expect(ctx.miterLimit).toBe(2); }); - -}) \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.putImageData.js b/__tests__/classes/CanvasRenderingContext2D.putImageData.js index 5ea6e8f..4d69490 100644 --- a/__tests__/classes/CanvasRenderingContext2D.putImageData.js +++ b/__tests__/classes/CanvasRenderingContext2D.putImageData.js @@ -35,4 +35,4 @@ describe('putImageData', () => { it('should throw when first argument is not of type ImageData', () => { expect(() => ctx.putImageData(null, 1, 2)).toThrow(TypeError); }); -}); \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.resetTransform.js b/__tests__/classes/CanvasRenderingContext2D.resetTransform.js index b581496..5d81b90 100644 --- a/__tests__/classes/CanvasRenderingContext2D.resetTransform.js +++ b/__tests__/classes/CanvasRenderingContext2D.resetTransform.js @@ -24,4 +24,4 @@ describe('resetTransform', () => { ctx.resetTransform(); expect(ctx.currentTransform).toEqual(new DOMMatrix([1, 0, 0, 1, 0, 0])); }); -}) \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.rotate.js b/__tests__/classes/CanvasRenderingContext2D.rotate.js index cc4c107..90217ef 100644 --- a/__tests__/classes/CanvasRenderingContext2D.rotate.js +++ b/__tests__/classes/CanvasRenderingContext2D.rotate.js @@ -21,23 +21,17 @@ describe('rotate', () => { it('should rotate the current transform', () => { ctx.rotate(Math.PI); - expect(ctx.currentTransform).toEqual(new DOMMatrix([ - -0.9999999999999997, - -1.9999999999999996, - -3, - -4, - 5, - 6, - ])); + expect(ctx.currentTransform).toEqual( + new DOMMatrix([-0.9999999999999997, -1.9999999999999996, -3, -4, 5, 6]) + ); }); it('should throw if argument count is less than 2', () => { expect(() => ctx.rotate()).toThrow(TypeError); }); - it('shouldn\'t rotate the transform if any of the values cannot be coerced into finite numbers', () => { + it("shouldn't rotate the transform if any of the values cannot be coerced into finite numbers", () => { ctx.rotate(NaN); expect(ctx.currentTransform).toEqual(new DOMMatrix([1, 2, 3, 4, 5, 6])); }); }); - diff --git a/__tests__/classes/CanvasRenderingContext2D.save.js b/__tests__/classes/CanvasRenderingContext2D.save.js index 2fe16e4..d4fcfdd 100644 --- a/__tests__/classes/CanvasRenderingContext2D.save.js +++ b/__tests__/classes/CanvasRenderingContext2D.save.js @@ -14,7 +14,7 @@ describe('save', () => { }); it('should be callable', () => { - ctx.save() + ctx.save(); expect(ctx.save).toBeCalled(); }); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.scale.js b/__tests__/classes/CanvasRenderingContext2D.scale.js index 3a41819..64a07d1 100644 --- a/__tests__/classes/CanvasRenderingContext2D.scale.js +++ b/__tests__/classes/CanvasRenderingContext2D.scale.js @@ -36,4 +36,3 @@ describe('scale', () => { expect(ctx.currentTransform).toEqual(new DOMMatrix([1, 2, 3, 4, 5, 6])); }); }); - diff --git a/__tests__/classes/CanvasRenderingContext2D.setTransform.js b/__tests__/classes/CanvasRenderingContext2D.setTransform.js index a71ccd0..17b5ddf 100644 --- a/__tests__/classes/CanvasRenderingContext2D.setTransform.js +++ b/__tests__/classes/CanvasRenderingContext2D.setTransform.js @@ -33,10 +33,10 @@ describe('setTransform', () => { [1, 2, 3, 4, 5, 6], [-1, 2, 3, 4, 5, 6], [Infinity, null, 'test', 'bad', NaN, 34], - ].forEach(e => { + ].forEach((e) => { ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.setTransform(...e); - if (every(e, val => Number.isFinite(Number(val)))) { + if (every(e, (val) => Number.isFinite(Number(val)))) { expect(ctx.getTransform()).toEqual(new DOMMatrix(e)); } else { expect(ctx.getTransform().isIdentity).toBeTruthy(); @@ -50,7 +50,7 @@ describe('setTransform', () => { expect(ctx.getTransform()).toEqual(new DOMMatrix([1, 2, 3, 4, 5, 6])); }); - it('should throw when setTransform doesn\'t receive valid DOMMatrix', () => { + it("should throw when setTransform doesn't receive valid DOMMatrix", () => { expect(() => ctx.setTransform({})).toThrow(TypeError); }); @@ -70,7 +70,7 @@ describe('setTransform', () => { [1, 2, 3, NaN, 5, 6], [1, 2, 3, 4, NaN, 6], [1, 2, 3, 4, 5, NaN], - ].forEach(e => { + ].forEach((e) => { ctx.setTransform(...e); expect(ctx.getTransform()).toEqual(identity); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.shadowBlur.js b/__tests__/classes/CanvasRenderingContext2D.shadowBlur.js index 18cdccf..b624ff1 100644 --- a/__tests__/classes/CanvasRenderingContext2D.shadowBlur.js +++ b/__tests__/classes/CanvasRenderingContext2D.shadowBlur.js @@ -14,27 +14,27 @@ describe('shadowBlur', () => { }); it('should ignore non finite values', () => { - [Infinity, -Infinity, null, void 0, NaN].forEach(e => { + [Infinity, -Infinity, null, void 0, NaN].forEach((e) => { ctx.shadowBlur = e; expect(ctx.shadowBlur).toBe(0); }); }); it('should ignore out of range values', () => { - [-1, -10, -300].forEach(e => { + [-1, -10, -300].forEach((e) => { ctx.shadowBlur = e; expect(ctx.shadowBlur).toBe(0); }); }); it('should not ignore values that are within range', () => { - [1, 10, '30', '10.2'].forEach(e => { + [1, 10, '30', '10.2'].forEach((e) => { ctx.shadowBlur = e; expect(ctx.shadowBlur).toBe(Number(e)); }); }); - it ('should save and restore values', () => { + it('should save and restore values', () => { ctx.shadowBlur = 2; ctx.save(); ctx.shadowBlur = 10; @@ -42,4 +42,4 @@ describe('shadowBlur', () => { ctx.restore(); expect(ctx.shadowBlur).toBe(2); }); -}) \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.shadowColor.js b/__tests__/classes/CanvasRenderingContext2D.shadowColor.js index 03f907b..c2e41a8 100644 --- a/__tests__/classes/CanvasRenderingContext2D.shadowColor.js +++ b/__tests__/classes/CanvasRenderingContext2D.shadowColor.js @@ -9,7 +9,7 @@ beforeEach(() => { }); describe('shadowColor', () => { - it('should parse a css color string \'blue\'', () => { + it("should parse a css color string 'blue'", () => { ctx.shadowColor = 'blue'; expect(ctx.shadowColor).toBe('#0000ff'); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.shadowOffsetX.js b/__tests__/classes/CanvasRenderingContext2D.shadowOffsetX.js index 30698dd..e000831 100644 --- a/__tests__/classes/CanvasRenderingContext2D.shadowOffsetX.js +++ b/__tests__/classes/CanvasRenderingContext2D.shadowOffsetX.js @@ -14,20 +14,20 @@ describe('shadowOffsetX', () => { }); it('should ignore non finite values', () => { - [Infinity, -Infinity, null, void 0, NaN].forEach(e => { + [Infinity, -Infinity, null, void 0, NaN].forEach((e) => { ctx.shadowOffsetX = e; expect(ctx.shadowOffsetX).toBe(0); }); }); it('should not ignore values that are within range', () => { - [1, 10, '30', '10.2', '-3'].forEach(e => { + [1, 10, '30', '10.2', '-3'].forEach((e) => { ctx.shadowOffsetX = e; expect(ctx.shadowOffsetX).toBe(Number(e)); }); }); - it ('should save and restore values', () => { + it('should save and restore values', () => { ctx.shadowOffsetX = 2; ctx.save(); ctx.shadowOffsetX = 10; diff --git a/__tests__/classes/CanvasRenderingContext2D.shadowOffsetY.js b/__tests__/classes/CanvasRenderingContext2D.shadowOffsetY.js index bda46db..f30631e 100644 --- a/__tests__/classes/CanvasRenderingContext2D.shadowOffsetY.js +++ b/__tests__/classes/CanvasRenderingContext2D.shadowOffsetY.js @@ -14,20 +14,20 @@ describe('shadowOffsetY', () => { }); it('should ignore non finite values', () => { - [Infinity, -Infinity, null, void 0, NaN].forEach(e => { + [Infinity, -Infinity, null, void 0, NaN].forEach((e) => { ctx.shadowOffsetY = e; expect(ctx.shadowOffsetY).toBe(0); }); }); it('should not ignore values that are within range', () => { - [1, 10, '30', '10.2', '-3'].forEach(e => { + [1, 10, '30', '10.2', '-3'].forEach((e) => { ctx.shadowOffsetY = e; expect(ctx.shadowOffsetY).toBe(Number(e)); }); }); - it ('should save and restore values', () => { + it('should save and restore values', () => { ctx.shadowOffsetY = 2; ctx.save(); ctx.shadowOffsetY = 10; diff --git a/__tests__/classes/CanvasRenderingContext2D.stroke.js b/__tests__/classes/CanvasRenderingContext2D.stroke.js index 6688f10..6bbc76b 100644 --- a/__tests__/classes/CanvasRenderingContext2D.stroke.js +++ b/__tests__/classes/CanvasRenderingContext2D.stroke.js @@ -24,8 +24,8 @@ describe('stroke', () => { }); it('should throw if an arguments is passed to it and it is not a Path2D', () => { - [1, Infinity, 'test', null, void 0, [], {}].forEach(e => { + [1, Infinity, 'test', null, void 0, [], {}].forEach((e) => { expect(() => ctx.stroke(e)).toThrow(TypeError); }); }); -}) \ No newline at end of file +}); diff --git a/__tests__/classes/CanvasRenderingContext2D.strokeStyle.js b/__tests__/classes/CanvasRenderingContext2D.strokeStyle.js index ec62646..66f81d7 100644 --- a/__tests__/classes/CanvasRenderingContext2D.strokeStyle.js +++ b/__tests__/classes/CanvasRenderingContext2D.strokeStyle.js @@ -9,7 +9,7 @@ beforeEach(() => { }); describe('strokeStyle', () => { - it('should parse a css color string \'blue\'', () => { + it("should parse a css color string 'blue'", () => { ctx.strokeStyle = 'blue'; expect(ctx.strokeStyle).toBe('#0000ff'); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.textAlign.js b/__tests__/classes/CanvasRenderingContext2D.textAlign.js index 7682b9e..7e2a8bc 100644 --- a/__tests__/classes/CanvasRenderingContext2D.textAlign.js +++ b/__tests__/classes/CanvasRenderingContext2D.textAlign.js @@ -9,17 +9,17 @@ beforeEach(() => { }); describe('textAlign', () => { - it('should set the default value to \'start\'', () => { + it("should set the default value to 'start'", () => { expect(ctx.textAlign).toBe('start'); }); - it('should not set the value if it\'s not a valid textAlign', () => { + it("should not set the value if it's not a valid textAlign", () => { ctx.textAlign = 'wrong!'; expect(ctx.textAlign).toBe('start'); }); - it('should set the textAlign if it\'s a valid textAlign', () => { - ['left', 'right', 'center', 'start', 'end'].forEach(e => { + it("should set the textAlign if it's a valid textAlign", () => { + ['left', 'right', 'center', 'start', 'end'].forEach((e) => { ctx.textAlign = e; expect(ctx.textAlign).toBe(e); }); diff --git a/__tests__/classes/CanvasRenderingContext2D.textBaseline.js b/__tests__/classes/CanvasRenderingContext2D.textBaseline.js index 2419aef..cbf39cd 100644 --- a/__tests__/classes/CanvasRenderingContext2D.textBaseline.js +++ b/__tests__/classes/CanvasRenderingContext2D.textBaseline.js @@ -9,20 +9,22 @@ beforeEach(() => { }); describe('textBaseline', () => { - it('should set the default value to \'alphabetic\'', () => { + it("should set the default value to 'alphabetic'", () => { expect(ctx.textBaseline).toBe('alphabetic'); }); - it('shouldn\'t set the value if it\'s not a valid textBaseline', () => { + it("shouldn't set the value if it's not a valid textBaseline", () => { ctx.textBaseline = 'wrong!'; expect(ctx.textBaseline).toBe('alphabetic'); }); - it('should set the textBaseline if it\'s a valid textBaseline', () => { - ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'].forEach(e => { - ctx.textBaseline = e; - expect(ctx.textBaseline).toBe(e); - }); + it("should set the textBaseline if it's a valid textBaseline", () => { + ['top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'].forEach( + (e) => { + ctx.textBaseline = e; + expect(ctx.textBaseline).toBe(e); + } + ); }); it('should save and restore textBaseline values', () => { diff --git a/__tests__/classes/CanvasRenderingContext2D.transform.js b/__tests__/classes/CanvasRenderingContext2D.transform.js index b5befcb..64e9e0f 100644 --- a/__tests__/classes/CanvasRenderingContext2D.transform.js +++ b/__tests__/classes/CanvasRenderingContext2D.transform.js @@ -21,7 +21,9 @@ describe('transform', () => { it('should transform the current transform', () => { ctx.transform(1, 2, 3, 4, 5, 6); - expect(ctx.currentTransform).toEqual(new DOMMatrix([7, 10, 15, 22, 28, 40])); + expect(ctx.currentTransform).toEqual( + new DOMMatrix([7, 10, 15, 22, 28, 40]) + ); }); it('should throw if argument count is less than 6', () => { @@ -41,10 +43,9 @@ describe('transform', () => { [1, 2, 3, NaN, 4, 5], [1, 2, 3, 4, NaN, 6], [1, 2, 3, 4, 5, NaN], - ].forEach(e => { + ].forEach((e) => { ctx.transform(...e); expect(ctx.currentTransform).toEqual(new DOMMatrix([1, 2, 3, 4, 5, 6])); }); }); }); - diff --git a/__tests__/classes/CanvasRenderingContext2D.translate.js b/__tests__/classes/CanvasRenderingContext2D.translate.js index c529fd1..3543547 100644 --- a/__tests__/classes/CanvasRenderingContext2D.translate.js +++ b/__tests__/classes/CanvasRenderingContext2D.translate.js @@ -35,4 +35,3 @@ describe('translate', () => { expect(ctx.currentTransform).toEqual(new DOMMatrix([1, 0, 0, 1, 0, 0])); }); }); - diff --git a/__tests__/classes/DOMMatrix.js b/__tests__/classes/DOMMatrix.js index 075e2f9..9e95ab3 100644 --- a/__tests__/classes/DOMMatrix.js +++ b/__tests__/classes/DOMMatrix.js @@ -6,7 +6,7 @@ describe('DOMMatrix class', () => { expect(matrix).toBeInstanceOf(DOMMatrix); }); - it ('should construct a 2d matrix properly', () => { + it('should construct a 2d matrix properly', () => { const matrix = new DOMMatrix([1, 2, 3, 4, 5, 6]); expect(matrix.a).toBe(1); expect(matrix.b).toBe(2); @@ -31,7 +31,24 @@ describe('DOMMatrix class', () => { }); it('should accept an array of 16 length', () => { - const matrix = new DOMMatrix([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + const matrix = new DOMMatrix([ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + ]); expect(matrix).toBeInstanceOf(DOMMatrix); }); diff --git a/__tests__/classes/ImageBitmap.js b/__tests__/classes/ImageBitmap.js index 83d0b56..8c236ca 100644 --- a/__tests__/classes/ImageBitmap.js +++ b/__tests__/classes/ImageBitmap.js @@ -44,15 +44,21 @@ describe('image bitmaps', () => { }); it('should reject non images', () => { - return Promise.all([0, null, void 0, '', 'test', window, Infinity, document] - .map(e => createImageBitmap(e).then(throwError).catch(expectTypeError)) + return Promise.all( + [0, null, void 0, '', 'test', window, Infinity, document].map((e) => + createImageBitmap(e).then(throwError).catch(expectTypeError) + ) ); }); it('should reject if second parameter is not an object', () => { - return Promise.all(['', 0, NaN, Infinity].map(e => { - return createImageBitmap(img, e).then(throwError).catch(expectTypeError); - })); + return Promise.all( + ['', 0, NaN, Infinity].map((e) => { + return createImageBitmap(img, e) + .then(throwError) + .catch(expectTypeError); + }) + ); }); it('should throw if arity is 3 or 4', () => { @@ -68,36 +74,46 @@ describe('image bitmaps', () => { it('should throw if width or height is not finite or 0', () => { return Promise.all([ - createImageBitmap(img, 1, 2, NaN, 3).then(throwError).catch(expectRangeError), - createImageBitmap(img, 1, 2, 0, 3).then(throwError).catch(expectRangeError), - createImageBitmap(img, 1, 2, 3, NaN).then(throwError).catch(expectRangeError), - createImageBitmap(img, 1, 2, 3, 0).then(throwError).catch(expectRangeError), + createImageBitmap(img, 1, 2, NaN, 3) + .then(throwError) + .catch(expectRangeError), + createImageBitmap(img, 1, 2, 0, 3) + .then(throwError) + .catch(expectRangeError), + createImageBitmap(img, 1, 2, 3, NaN) + .then(throwError) + .catch(expectRangeError), + createImageBitmap(img, 1, 2, 3, 0) + .then(throwError) + .catch(expectRangeError), ]); }); it('should throw if last parameter is not object if source rect is provided', () => { return Promise.all( - ['', 0, NaN, Infinity].map( - e => createImageBitmap(img, 1, 2, 3, 4, e).then(throwError).catch(expectTypeError) + ['', 0, NaN, Infinity].map((e) => + createImageBitmap(img, 1, 2, 3, 4, e) + .then(throwError) + .catch(expectTypeError) ) ); }); it('should have a close function', () => { - return createImageBitmap(img).then(e => { + return createImageBitmap(img).then((e) => { expect(typeof e.close).toBe('function'); }); }); it('should have a callable close function', () => { - return createImageBitmap(img).then(e => { + return createImageBitmap(img).then((e) => { e.close(); expect(e.close).toBeCalled(); }); }); it('should close the bitmap', () => { - return createImageBitmap(img).then(e => { + return createImageBitmap(img).then((e) => { e.close(); expect(e.width).toBe(0); expect(e.height).toBe(0); @@ -106,26 +122,34 @@ describe('image bitmaps', () => { }); it('should create image bitmaps from valid sources', () => { - return Promise.all([ - document.createElement('img'), - new Image(), - document.createElement('canvas'), - document.createElement('video'), - new Blob([new Uint8Array(1)]), - new ImageData(400, 300), - new ImageBitmap(100, 100), // this is just to verify class input - ].map(e => createImageBitmap(e).then(expectImageBitmap))); + return Promise.all( + [ + document.createElement('img'), + new Image(), + document.createElement('canvas'), + document.createElement('video'), + new Blob([new Uint8Array(1)]), + new ImageData(400, 300), + new ImageBitmap(100, 100), // this is just to verify class input + ].map((e) => createImageBitmap(e).then(expectImageBitmap)) + ); }); it('should reject if sixth parameter is not an object', () => { - return Promise.all(['', 0, NaN, Infinity].map(e => { - return createImageBitmap(img, 1, 2, 3, 4, e).then(throwError).catch(expectTypeError); - })); + return Promise.all( + ['', 0, NaN, Infinity].map((e) => { + return createImageBitmap(img, 1, 2, 3, 4, e) + .then(throwError) + .catch(expectTypeError); + }) + ); }); it('should accept if sixth parameter is null, undefined, or object', () => { - return Promise.all([null, void 0, {}].map(e => { - return createImageBitmap(img, 1, 2, 3, 4, e).then(expectImageBitmap); - })); + return Promise.all( + [null, void 0, {}].map((e) => { + return createImageBitmap(img, 1, 2, 3, 4, e).then(expectImageBitmap); + }) + ); }); }); diff --git a/__tests__/classes/ImageData.js b/__tests__/classes/ImageData.js index 0381468..c3aa01b 100644 --- a/__tests__/classes/ImageData.js +++ b/__tests__/classes/ImageData.js @@ -4,8 +4,9 @@ describe('ImageData', () => { }); it('should throw if more than 3 arguments are provided', () => { - expect(() => new ImageData(new Uint8ClampedArray([0, 0, 0, 1]), 1, 1, 1)) - .toThrow(TypeError); + expect( + () => new ImageData(new Uint8ClampedArray([0, 0, 0, 1]), 1, 1, 1) + ).toThrow(TypeError); }); describe('new ImageData(width, height)', () => { @@ -57,23 +58,27 @@ describe('ImageData', () => { }); it('should throw if width is not finite', () => { - expect(() => new ImageData(new Uint8ClampedArray(4), Infinity)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(4), Infinity)).toThrow( + RangeError + ); }); it('should throw if width is 0', () => { - expect(() => new ImageData(new Uint8ClampedArray(4), 0)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(4), 0)).toThrow( + RangeError + ); }); it('should throw if source length is 0', () => { - expect(() => new ImageData(new Uint8ClampedArray(0), 100)) - .toThrow(RangeError); - }) + expect(() => new ImageData(new Uint8ClampedArray(0), 100)).toThrow( + RangeError + ); + }); it('should throw if source length is not a multiple of 4', () => { - expect(() => new ImageData(new Uint8ClampedArray(801), 200)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(801), 200)).toThrow( + RangeError + ); }); }); @@ -90,23 +95,27 @@ describe('ImageData', () => { }); it('should throw if width is not finite', () => { - expect(() => new ImageData(new Uint8ClampedArray(4), Infinity, 100)) - .toThrow(RangeError); + expect( + () => new ImageData(new Uint8ClampedArray(4), Infinity, 100) + ).toThrow(RangeError); }); it('should throw if width is 0', () => { - expect(() => new ImageData(new Uint8ClampedArray(4), 0, 100)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(4), 0, 100)).toThrow( + RangeError + ); }); it('should throw if height is not finite', () => { - expect(() => new ImageData(new Uint8ClampedArray(4), 1, Infinity)) - .toThrow(RangeError); + expect( + () => new ImageData(new Uint8ClampedArray(4), 1, Infinity) + ).toThrow(RangeError); }); it('should throw if height is 0', () => { - expect(() => new ImageData(new Uint8ClampedArray(4), 100, 0)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(4), 100, 0)).toThrow( + RangeError + ); }); it('should throw if first argument is not a Uint8ClampedArray', () => { @@ -114,22 +123,27 @@ describe('ImageData', () => { }); it('should throw if source length is 0', () => { - expect(() => new ImageData(new Uint8ClampedArray(0), 100, 100)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(0), 100, 100)).toThrow( + RangeError + ); }); it('should throw if source length is not a multiple of 4', () => { - expect(() => new ImageData(new Uint8ClampedArray(801), 200, 1)) - .toThrow(RangeError); - }); - - it('should throw if width and height aren\'t compatible with source length ', () => { - expect(() => new ImageData(new Uint8ClampedArray(8), 2, 2)) - .toThrow(RangeError); - expect(() => new ImageData(new Uint8ClampedArray(8), 1, 7)) - .toThrow(RangeError); - expect(() => new ImageData(new Uint8ClampedArray(8), 7, 1)) - .toThrow(RangeError); + expect(() => new ImageData(new Uint8ClampedArray(801), 200, 1)).toThrow( + RangeError + ); + }); + + it("should throw if width and height aren't compatible with source length ", () => { + expect(() => new ImageData(new Uint8ClampedArray(8), 2, 2)).toThrow( + RangeError + ); + expect(() => new ImageData(new Uint8ClampedArray(8), 1, 7)).toThrow( + RangeError + ); + expect(() => new ImageData(new Uint8ClampedArray(8), 7, 1)).toThrow( + RangeError + ); }); }); }); diff --git a/__tests__/classes/Path2D.js b/__tests__/classes/Path2D.js index cce6148..71fef0e 100644 --- a/__tests__/classes/Path2D.js +++ b/__tests__/classes/Path2D.js @@ -13,7 +13,6 @@ let borrowedFromCanvas = [ ]; describe('Path2D', () => { - test('Path2D', () => { const path = new Path2D(); expect(path).toBeInstanceOf(Path2D); @@ -32,7 +31,7 @@ describe('Path2D', () => { it('should borrow some path functions from CanvasRenderingContext2D', () => { const p = new Path2D(); - borrowedFromCanvas.forEach(func => { + borrowedFromCanvas.forEach((func) => { expect(typeof p[func]).toBe('function'); }); }); @@ -43,7 +42,7 @@ describe('Path2D', () => { it('should throw if first argument is not Path2D', () => { const p = new Path2D(); - [null, 1, void 0, NaN, Infinity, {}, []].forEach(item => { + [null, 1, void 0, NaN, Infinity, {}, []].forEach((item) => { expect(() => p.addPath(item)).toThrow(TypeError); }); }); diff --git a/__tests__/classes/TextMetrics.js b/__tests__/classes/TextMetrics.js index 39f5000..6adfaa8 100644 --- a/__tests__/classes/TextMetrics.js +++ b/__tests__/classes/TextMetrics.js @@ -11,7 +11,7 @@ const props = [ 'hangingBaseline', 'alphabeticBaseline', 'ideographicBaseline', -] +]; describe('TextMetrics', () => { it('should return a text metrics object', () => { @@ -20,7 +20,7 @@ describe('TextMetrics', () => { }); it('should have a width of text length for testing purposes', () => { - ['one', 'two1', 'three', undefined, 1, null, 102].forEach(val => { + ['one', 'two1', 'three', undefined, 1, null, 102].forEach((val) => { val = String(val); const m = new TextMetrics(val); expect(m.width).toBe(val.length); @@ -29,7 +29,7 @@ describe('TextMetrics', () => { it('should have every property defined in the specification', () => { const m = new TextMetrics(''); - props.forEach(val => { + props.forEach((val) => { expect(m[val]).toBe(0); }); }); diff --git a/__tests__/mock/prototype.js b/__tests__/mock/prototype.js index b3aef79..2e8f22b 100644 --- a/__tests__/mock/prototype.js +++ b/__tests__/mock/prototype.js @@ -20,13 +20,13 @@ describe('mock', () => { }); it('should expect toBlob to be callable', () => { - canvas.toBlob(e => {}); + canvas.toBlob((e) => {}); expect(canvas.toBlob).toBeCalled(); }); it('should expect toBlob to return Blob', () => { return new Promise((resolve, reject) => { - canvas.toBlob(e => { + canvas.toBlob((e) => { var ex; try { expect(e).toBeInstanceOf(window.Blob); @@ -35,7 +35,7 @@ describe('mock', () => { } resolve(); }); - }) + }); }); it('should throw if toBlob is provided less than 1 argument', () => { @@ -48,7 +48,7 @@ describe('mock', () => { it('should accept image/jpeg', () => { return new Promise((resolve, reject) => { - canvas.toBlob(e => { + canvas.toBlob((e) => { var ex; try { expect(e.type).toBe('image/jpeg'); @@ -62,10 +62,10 @@ describe('mock', () => { it('should accept image/webp', () => { return new Promise((resolve, reject) => { - canvas.toBlob(e => { + canvas.toBlob((e) => { var ex; try { - expect(e.type).toBe('image/webp') + expect(e.type).toBe('image/webp'); } catch (ex) { return reject(ex); } @@ -113,8 +113,8 @@ describe('mock', () => { }); it('should return the same context if getContext("2d") is called twice', () => { - const first = canvas.getContext("2d"); - const second = canvas.getContext("2d"); + const first = canvas.getContext('2d'); + const second = canvas.getContext('2d'); expect(first).toBe(second); }); }); diff --git a/package.json b/package.json index 3299ab8..c7005d9 100644 --- a/package.json +++ b/package.json @@ -8,24 +8,26 @@ "test": "jest --no-cache", "build": "babel src --out-dir lib", "coveralls": "cat ./coverage/lcov.info | coveralls", - "prepare": "npm run build" + "prepare": "npm run build", + "prettier": "prettier --write ." }, "dependencies": { "cssfontparser": "^1.2.1", "moo-color": "^1.0.2" }, "devDependencies": { - "@babel/cli": "^7.7.0", - "@babel/core": "^7.7.2", - "@babel/plugin-proposal-class-properties": "^7.7.0", - "@babel/preset-env": "^7.7.1", - "@commitlint/cli": "^8.2.0", - "@commitlint/config-angular": "^8.2.0", - "babel-jest": "^24.9.0", + "@babel/cli": "^7.8.4", + "@babel/core": "^7.9.0", + "@babel/plugin-proposal-class-properties": "^7.8.3", + "@babel/preset-env": "^7.9.5", + "@commitlint/cli": "^8.3.5", + "@commitlint/config-angular": "^8.3.4", + "babel-jest": "^25.3.0", "babel-plugin-version": "^0.2.3", - "coveralls": "^3.0.8", - "husky": "^3.1.0", - "jest": "^24.9.0" + "coveralls": "^3.0.11", + "husky": "^4.2.5", + "jest": "^25.3.0", + "prettier": "^2.0.4" }, "commitlint": { "extends": [ @@ -35,7 +37,7 @@ "husky": { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", - "pre-commit": "npm run test && npm run build" + "pre-commit": "npm run prettier && npm run test && npm run build" } }, "jest": { @@ -61,6 +63,11 @@ "test", "unit" ], + "files": [ + "CHANGELOG.md", + "lib/", + "types/" + ], "author": "hustcc", "license": "MIT", "bugs": { diff --git a/src/classes/CanvasGradient.js b/src/classes/CanvasGradient.js index 7a113e3..6685e88 100644 --- a/src/classes/CanvasGradient.js +++ b/src/classes/CanvasGradient.js @@ -8,12 +8,21 @@ export default class CanvasGradient { addColorStop(offset, color) { const numoffset = Number(offset); if (!Number.isFinite(numoffset) || numoffset < 0 || numoffset > 1) { - throw new DOMException('IndexSizeError', 'Failed to execute \'addColorStop\' on \'CanvasGradient\': The provided value (\'' + numoffset + '\') is outside the range (0.0, 1.0)'); + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'addColorStop' on 'CanvasGradient': The provided value ('" + + numoffset + + "') is outside the range (0.0, 1.0)" + ); } try { new MooColor(color); - } catch(e) { - throw new SyntaxError('Failed to execute \'addColorStop\' on \'CanvasGradient\': The value provided (\'' + color + '\') could not be parsed as a color.'); + } catch (e) { + throw new SyntaxError( + "Failed to execute 'addColorStop' on 'CanvasGradient': The value provided ('" + + color + + "') could not be parsed as a color." + ); } } } diff --git a/src/classes/CanvasPattern.js b/src/classes/CanvasPattern.js index 5e8d186..04033d8 100644 --- a/src/classes/CanvasPattern.js +++ b/src/classes/CanvasPattern.js @@ -1,10 +1,12 @@ - export default class CanvasPattern { constructor() { this.setTransform = jest.fn(this.setTransform.bind(this)); } setTransform(value) { - if (arguments.length > 0 && !(value instanceof Object)) throw new TypeError('Failed to execute \'setTransform\' on \'CanvasPattern\': parameter 1 (\'transform\') is not an object.'); + if (arguments.length > 0 && !(value instanceof Object)) + throw new TypeError( + "Failed to execute 'setTransform' on 'CanvasPattern': parameter 1 ('transform') is not an object." + ); } } diff --git a/src/classes/CanvasRenderingContext2D.js b/src/classes/CanvasRenderingContext2D.js index 02f6259..c44fa96 100644 --- a/src/classes/CanvasRenderingContext2D.js +++ b/src/classes/CanvasRenderingContext2D.js @@ -3,12 +3,83 @@ import CanvasPattern from './CanvasPattern'; import cssfontparser from 'cssfontparser'; import TextMetrics from './TextMetrics'; import createCanvasEvent from '../mock/createCanvasEvent'; -import Path2D from "./Path2D"; +import Path2D from './Path2D'; import { MooColor } from 'moo-color'; - -const testFuncs = ['setLineDash', 'getLineDash', 'setTransform', 'getTransform', 'getImageData', 'save', 'restore', 'createPattern', 'createRadialGradient', 'addHitRegion', 'arc', 'arcTo', 'beginPath', 'clip', 'closePath', 'scale', 'stroke', 'clearHitRegions', 'clearRect', 'fillRect', 'strokeRect', 'rect', 'resetTransform', 'translate', 'moveTo', 'lineTo', 'bezierCurveTo', 'createLinearGradient', 'ellipse', 'measureText', 'rotate', 'drawImage', 'drawFocusIfNeeded', 'isPointInPath', 'isPointInStroke', 'putImageData', 'strokeText', 'fillText', 'quadraticCurveTo', 'removeHitRegion', 'fill', 'transform', 'scrollPathIntoView', 'createImageData']; -const compositeOperations = ['source-over', 'source-in', 'source-out', 'source-atop', 'destination-over', 'destination-in', 'destination-out', 'destination-atop', 'lighter', 'copy', 'xor', 'multiply', 'screen', 'overlay', 'darken', 'lighten', 'color-dodge', 'color-burn', 'hard-light', 'soft-light', 'difference', 'exclusion', 'hue', 'saturation', 'color', 'luminosity']; +const testFuncs = [ + 'setLineDash', + 'getLineDash', + 'setTransform', + 'getTransform', + 'getImageData', + 'save', + 'restore', + 'createPattern', + 'createRadialGradient', + 'addHitRegion', + 'arc', + 'arcTo', + 'beginPath', + 'clip', + 'closePath', + 'scale', + 'stroke', + 'clearHitRegions', + 'clearRect', + 'fillRect', + 'strokeRect', + 'rect', + 'resetTransform', + 'translate', + 'moveTo', + 'lineTo', + 'bezierCurveTo', + 'createLinearGradient', + 'ellipse', + 'measureText', + 'rotate', + 'drawImage', + 'drawFocusIfNeeded', + 'isPointInPath', + 'isPointInStroke', + 'putImageData', + 'strokeText', + 'fillText', + 'quadraticCurveTo', + 'removeHitRegion', + 'fill', + 'transform', + 'scrollPathIntoView', + 'createImageData', +]; +const compositeOperations = [ + 'source-over', + 'source-in', + 'source-out', + 'source-atop', + 'destination-over', + 'destination-in', + 'destination-out', + 'destination-atop', + 'lighter', + 'copy', + 'xor', + 'multiply', + 'screen', + 'overlay', + 'darken', + 'lighten', + 'color-dodge', + 'color-burn', + 'hard-light', + 'soft-light', + 'difference', + 'exclusion', + 'hue', + 'saturation', + 'color', + 'luminosity', +]; function getTransformSlice(ctx) { return ctx._transformStack[ctx._stackIndex].slice(); @@ -18,9 +89,7 @@ function getTransformSlice(ctx) { * Returns the string serialization of a CSS color, according to https://www.w3.org/TR/2dcontext/#serialization-of-a-color */ function serializeColor(value) { - return (value.getAlpha() === 1) - ? value.toHex() - : value.toRgb(); + return value.getAlpha() === 1 ? value.toHex() : value.toRgb(); } export default class CanvasRenderingContext2D { @@ -37,7 +106,7 @@ export default class CanvasRenderingContext2D { * Clear the list of draw calls */ __clearDrawCalls() { - this._drawCalls = [] + this._drawCalls = []; } /** @@ -69,11 +138,7 @@ export default class CanvasRenderingContext2D { * Clear the path and reset it to a single beginPath event. */ __clearPath() { - const event = createCanvasEvent( - 'beginPath', - getTransformSlice(this), - { }, - ); + const event = createCanvasEvent('beginPath', getTransformSlice(this), {}); // The clipping path should start after the initial beginPath instruction this._clipIndex = 1; this._path = [event]; @@ -87,7 +152,7 @@ export default class CanvasRenderingContext2D { } _directionStack = ['inherit']; - _fillStyleStack = [ '#000000' ]; + _fillStyleStack = ['#000000']; _filterStack = ['none']; _fontStack = ['10px sans-serif']; _globalAlphaStack = [1.0]; @@ -101,18 +166,18 @@ export default class CanvasRenderingContext2D { _lineWidthStack = [1]; _miterLimitStack = [10]; _shadowBlurStack = [0]; - _shadowColorStack = [ 'rgba(0, 0, 0, 0)' ]; + _shadowColorStack = ['rgba(0, 0, 0, 0)']; _shadowOffsetXStack = [0]; _shadowOffsetYStack = [0]; _stackIndex = 0; - _strokeStyleStack = [ '#000000' ]; + _strokeStyleStack = ['#000000']; _textAlignStack = ['start']; _textBaselineStack = ['alphabetic']; _transformStack = [[1, 0, 0, 1, 0, 0]]; - _clipStack=[[]]; + _clipStack = [[]]; constructor(canvas) { - testFuncs.forEach(key => { + testFuncs.forEach((key) => { this[key] = jest.fn(CanvasRenderingContext2D.prototype[key].bind(this)); }); this._canvas = canvas; @@ -127,21 +192,46 @@ export default class CanvasRenderingContext2D { cursor, control, label, - role + role, } = options; - if (!path && !id) throw new DOMException('ConstraintError', 'Failed to execute \'addHitRegion\' on \'' + this.constructor.name + '\': Both id and control are null.'); - if (fillRule && fillRule !== 'evenodd' && fillRule !== 'nonzero') throw new TypeError('Failed to execute \'addHitRegion\' on \'' + this.constructor.name + '\': The provided value \'' + fillRule + '\' is not a valid enum value of type CanvasFillRule.'); + if (!path && !id) + throw new DOMException( + 'ConstraintError', + "Failed to execute 'addHitRegion' on '" + + this.constructor.name + + "': Both id and control are null." + ); + if (fillRule && fillRule !== 'evenodd' && fillRule !== 'nonzero') + throw new TypeError( + "Failed to execute 'addHitRegion' on '" + + this.constructor.name + + "': The provided value '" + + fillRule + + "' is not a valid enum value of type CanvasFillRule." + ); this._events.push( - createCanvasEvent( - 'addHitRegion', - getTransformSlice(this), - { path, fillRule, id, parentID, cursor, control, label, role }, - ), + createCanvasEvent('addHitRegion', getTransformSlice(this), { + path, + fillRule, + id, + parentID, + cursor, + control, + label, + role, + }) ); } arc(x, y, radius, startAngle, endAngle, anticlockwise = false) { - if (arguments.length < 5) throw new TypeError('Failed to execute \'arc\' on \'' + this.constructor.name + '\': 5 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 5) + throw new TypeError( + "Failed to execute 'arc' on '" + + this.constructor.name + + "': 5 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); @@ -151,35 +241,73 @@ export default class CanvasRenderingContext2D { const anticlockwiseResult = Boolean(anticlockwise); // quick is finite check - if (!Number.isFinite(xResult + yResult + radiusResult + startAngleResult + endAngleResult)) return; - - if (Number(radius) < 0) throw new DOMException('IndexSizeError', 'Failed to execute \'arc\' on \'' + this.constructor.name + '\': The radius provided (' + radius + ') is negative.'); + if ( + !Number.isFinite( + xResult + yResult + radiusResult + startAngleResult + endAngleResult + ) + ) + return; + + if (Number(radius) < 0) + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'arc' on '" + + this.constructor.name + + "': The radius provided (" + + radius + + ') is negative.' + ); - const event = createCanvasEvent( - 'arc', - getTransformSlice(this), - { x: xResult, y: yResult, radius: radiusResult, startAngle: startAngleResult, endAngle: endAngleResult, anticlockwise: anticlockwiseResult }, - ); + const event = createCanvasEvent('arc', getTransformSlice(this), { + x: xResult, + y: yResult, + radius: radiusResult, + startAngle: startAngleResult, + endAngle: endAngleResult, + anticlockwise: anticlockwiseResult, + }); this._path.push(event); this._events.push(event); } arcTo(cpx1, cpy1, cpx2, cpy2, radius) { - if (arguments.length < 5) throw new DOMException('IndexSizeError', 'Failed to execute \'arcTo\' on \'' + this.constructor.name + '\': 5 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 5) + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'arcTo' on '" + + this.constructor.name + + "': 5 arguments required, but only " + + arguments.length + + ' present.' + ); const cpx1Result = Number(cpx1); const cpy1Result = Number(cpy1); const cpx2Result = Number(cpx2); const cpy2Result = Number(cpy2); const radiusResult = Number(radius); - if (!Number.isFinite(cpx1Result + cpx2Result + cpy1Result + cpy2Result + radiusResult)) return; - if (radiusResult < 0) throw new TypeError('Failed to execute \'arcTo\' on \'' + this.constructor.name + '\': The radius provided (' + radius + ') is negative.'); + if ( + !Number.isFinite( + cpx1Result + cpx2Result + cpy1Result + cpy2Result + radiusResult + ) + ) + return; + if (radiusResult < 0) + throw new TypeError( + "Failed to execute 'arcTo' on '" + + this.constructor.name + + "': The radius provided (" + + radius + + ') is negative.' + ); - const event = createCanvasEvent( - 'arcTo', - getTransformSlice(this), - { cpx1: cpx1Result, cpy1: cpy1Result, cpx2: cpx2Result, cpy2: cpy2Result, radius: radiusResult }, - ); + const event = createCanvasEvent('arcTo', getTransformSlice(this), { + cpx1: cpx1Result, + cpy1: cpy1Result, + cpx2: cpx2Result, + cpy2: cpy2Result, + radius: radiusResult, + }); this._path.push(event); this._events.push(event); @@ -192,7 +320,14 @@ export default class CanvasRenderingContext2D { } bezierCurveTo(cpx1, cpy1, cpx2, cpy2, x, y) { - if (arguments.length < 6) throw new TypeError('Failed to execute \'bezierCurveTo\' on \'' + this.constructor.name + '\': 6 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 6) + throw new TypeError( + "Failed to execute 'bezierCurveTo' on '" + + this.constructor.name + + "': 6 arguments required, but only " + + arguments.length + + ' present.' + ); const cpx1Result = Number(cpx1); const cpy1Result = Number(cpy1); const cpx2Result = Number(cpx2); @@ -200,13 +335,21 @@ export default class CanvasRenderingContext2D { const xResult = Number(x); const yResult = Number(y); - if (!Number.isFinite(cpx1Result + cpy1Result + cpx2Result + cpy2Result + xResult + yResult)) return; - - const event = createCanvasEvent( - 'bezierCurveTo', - getTransformSlice(this), - { cpx1, cpy1, cpx2, cpy2, x, y }, - ); + if ( + !Number.isFinite( + cpx1Result + cpy1Result + cpx2Result + cpy2Result + xResult + yResult + ) + ) + return; + + const event = createCanvasEvent('bezierCurveTo', getTransformSlice(this), { + cpx1, + cpy1, + cpx2, + cpy2, + x, + y, + }); this._path.push(event); this._events.push(event); @@ -220,13 +363,20 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'clearHitRegions', getTransformSlice(this), - { }, + {} ); this._events.push(event); } clearRect(x, y, width, height) { - if (arguments.length < 4) throw new TypeError('Failed to execute \'clearRect\' on \'' + this.constructor.name + '\': 4 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'clearRect' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); @@ -235,14 +385,12 @@ export default class CanvasRenderingContext2D { if (!Number.isFinite(x + y + width + height)) return; - const event = createCanvasEvent( - 'clearRect', - getTransformSlice(this), - { x: xResult, - y: yResult, - width: widthResult, - height: heightResult, }, - ); + const event = createCanvasEvent('clearRect', getTransformSlice(this), { + x: xResult, + y: yResult, + width: widthResult, + height: heightResult, + }); this._events.push(event); this._drawCalls.push(event); @@ -265,23 +413,36 @@ export default class CanvasRenderingContext2D { if (arguments.length === 1) fillRule = 'nonzero'; if (path instanceof Path2D) { fillRule = String(fillRule); - if (fillRule !== 'nonzero' && fillRule !== 'evenodd') throw new TypeError('Failed to execute \'clip\' on \'' + this.constructor.name + '\': The provided value \'' + fillRule + '\' is not a valid enum value of type CanvasFillRule.'); + if (fillRule !== 'nonzero' && fillRule !== 'evenodd') + throw new TypeError( + "Failed to execute 'clip' on '" + + this.constructor.name + + "': The provided value '" + + fillRule + + "' is not a valid enum value of type CanvasFillRule." + ); path = path._path.slice(); clipPath = path; } else { fillRule = String(path); - if (fillRule !== 'nonzero' && fillRule !== 'evenodd') throw new TypeError('Failed to execute \'clip\' on \'' + this.constructor.name + '\': The provided value \'' + fillRule + '\' is not a valid enum value of type CanvasFillRule.'); + if (fillRule !== 'nonzero' && fillRule !== 'evenodd') + throw new TypeError( + "Failed to execute 'clip' on '" + + this.constructor.name + + "': The provided value '" + + fillRule + + "' is not a valid enum value of type CanvasFillRule." + ); path = this._path.slice(); clipPath = path.slice(this._clipIndex); this._clipIndex = path.length; } } - const event = createCanvasEvent( - 'clip', - getTransformSlice(this), - { path, fillRule }, - ); + const event = createCanvasEvent('clip', getTransformSlice(this), { + path, + fillRule, + }); this._path.push(event); this._events.push(event); @@ -290,20 +451,26 @@ export default class CanvasRenderingContext2D { } closePath() { - const event = createCanvasEvent( - 'closePath', - getTransformSlice(this), - { }, - ); + const event = createCanvasEvent('closePath', getTransformSlice(this), {}); this._events.push(event); this._path.push(event); } createImageData(width, height) { - if (arguments.length < 1) throw new TypeError('Failed to execute \'createImageData\' on \'' + this.constructor.name + '\': 1 argument required, but only 0 present.'); + if (arguments.length < 1) + throw new TypeError( + "Failed to execute 'createImageData' on '" + + this.constructor.name + + "': 1 argument required, but only 0 present." + ); else if (arguments.length === 1) { - if (!(width instanceof ImageData)) throw new TypeError('Failed to execute \'createImageData\' on \'' + this.constructor.name + '\': parameter 1 is not of type \'ImageData\'.'); + if (!(width instanceof ImageData)) + throw new TypeError( + "Failed to execute 'createImageData' on '" + + this.constructor.name + + "': parameter 1 is not of type 'ImageData'." + ); let result = new ImageData(width.width, width.height); result.data.set(width.data); const event = createCanvasEvent( @@ -316,8 +483,18 @@ export default class CanvasRenderingContext2D { } else { width = Math.abs(Number(width)); height = Math.abs(Number(height)); - if (!Number.isFinite(width) || width === 0) throw new TypeError('Failed to execute \'createImageData\' on \'' + this.constructor.name + '\': The source width is 0.'); - if (!Number.isFinite(height) || height === 0) throw new TypeError('Failed to execute \'createImageData\' on \'' + this.constructor.name + '\': The source height is 0.'); + if (!Number.isFinite(width) || width === 0) + throw new TypeError( + "Failed to execute 'createImageData' on '" + + this.constructor.name + + "': The source width is 0." + ); + if (!Number.isFinite(height) || height === 0) + throw new TypeError( + "Failed to execute 'createImageData' on '" + + this.constructor.name + + "': The source height is 0." + ); const event = createCanvasEvent( 'createImageData', getTransformSlice(this), @@ -329,18 +506,30 @@ export default class CanvasRenderingContext2D { } createLinearGradient(x0, y0, x1, y1) { - if (arguments.length < 4) throw new TypeError('Failed to execute \'createLinearGradient\' on \'' + this.constructor.name + '\': 4 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'createLinearGradient' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); const x0Result = Number(x0); const y0Result = Number(y0); const x1Result = Number(x1); const y1Result = Number(y1); - if (!Number.isFinite(x0Result + y0Result + x1Result + y1Result)) throw new TypeError('Failed to execute \'createLinearGradient\' on \'' + this.constructor.name + '\': The provided double value is non-finite.'); + if (!Number.isFinite(x0Result + y0Result + x1Result + y1Result)) + throw new TypeError( + "Failed to execute 'createLinearGradient' on '" + + this.constructor.name + + "': The provided double value is non-finite." + ); const event = createCanvasEvent( 'createLinearGradient', getTransformSlice(this), - { x0: x0Result, y0: y0Result, x1: x1Result, y1: y1Result }, + { x0: x0Result, y0: y0Result, x1: x1Result, y1: y1Result } ); this._events.push(event); @@ -349,18 +538,32 @@ export default class CanvasRenderingContext2D { } createPattern(image, type) { - if (arguments.length === 1) throw new TypeError('Failed to execute \'createPattern\' on \'' + this.constructor.name + '\': 2 arguments required, but only 1 present.'); + if (arguments.length === 1) + throw new TypeError( + "Failed to execute 'createPattern' on '" + + this.constructor.name + + "': 2 arguments required, but only 1 present." + ); if (type === null) type = 'repeat'; if (type === '') type = 'repeat'; - if (type === 'repeat' || type === 'repeat-x' || type === 'repeat-y' || type === 'no-repeat') { + if ( + type === 'repeat' || + type === 'repeat-x' || + type === 'repeat-y' || + type === 'no-repeat' + ) { const event = createCanvasEvent( 'createPattern', getTransformSlice(this), - { image, type }, + { image, type } ); if (image instanceof ImageBitmap) { - if (image._closed) throw new DOMException('InvalidStateError', 'Failed to execute \'createPattern\' on \'CanvasRenderingContext2D\': The image source is detached.'); + if (image._closed) + throw new DOMException( + 'InvalidStateError', + "Failed to execute 'createPattern' on 'CanvasRenderingContext2D': The image source is detached." + ); this._events.push(event); return new CanvasPattern(); } @@ -377,14 +580,31 @@ export default class CanvasRenderingContext2D { return new CanvasPattern(); } } else { - throw new TypeError('Failed to execute \'createPattern\' on \'' + this.constructor.name + '\': The provided type (\'' + type + '\') is not one of \'repeat\', \'no-repeat\', \'repeat-x\', or \'repeat-y\'.'); + throw new TypeError( + "Failed to execute 'createPattern' on '" + + this.constructor.name + + "': The provided type ('" + + type + + "') is not one of 'repeat', 'no-repeat', 'repeat-x', or 'repeat-y'." + ); } - throw new TypeError('Failed to execute \'createPattern\' on \'' + this.constructor.name + '\': The provided value is not of type \'(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)\''); + throw new TypeError( + "Failed to execute 'createPattern' on '" + + this.constructor.name + + "': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'" + ); } createRadialGradient(x0, y0, r0, x1, y1, r1) { - if (arguments.length < 6) throw new TypeError('Failed to execute \'createRadialGradient\' on \'' + this.constructor.name + '\': 6 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 6) + throw new TypeError( + "Failed to execute 'createRadialGradient' on '" + + this.constructor.name + + "': 6 arguments required, but only " + + arguments.length + + ' present.' + ); const x0Result = Number(x0); const y0Result = Number(y0); const r0Result = Number(r0); @@ -392,19 +612,42 @@ export default class CanvasRenderingContext2D { const y1Result = Number(y1); const r1Result = Number(r1); - if (!Number.isFinite(x0Result + y0Result + r0Result + x1Result + y1Result + r1Result)) throw new TypeError('Failed to execute \'createRadialGradient\' on \'' + this.constructor.name + '\': The provided double value is non-finite.'); - if (r0Result < 0) throw new DOMException('IndexSizeError', 'Failed to execute \'createRadialGradient\' on \'' + this.constructor.name + '\': The r0 provided is less than 0.'); - if (r1Result < 0) throw new DOMException('IndexSizeError', 'Failed to execute \'createRadialGradient\' on \'' + this.constructor.name + '\': The r1 provided is less than 0.'); + if ( + !Number.isFinite( + x0Result + y0Result + r0Result + x1Result + y1Result + r1Result + ) + ) + throw new TypeError( + "Failed to execute 'createRadialGradient' on '" + + this.constructor.name + + "': The provided double value is non-finite." + ); + if (r0Result < 0) + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'createRadialGradient' on '" + + this.constructor.name + + "': The r0 provided is less than 0." + ); + if (r1Result < 0) + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'createRadialGradient' on '" + + this.constructor.name + + "': The r1 provided is less than 0." + ); const event = createCanvasEvent( 'createRadialGradient', getTransformSlice(this), - { x0: x0Result, + { + x0: x0Result, y0: y0Result, r0: r0Result, x1: x1Result, y1: y1Result, - r1: r1Result, }, + r1: r1Result, + } ); this._events.push(event); return new CanvasGradient(); @@ -421,12 +664,14 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'currentTransform', getTransformSlice(this), - { a: value.a, + { + a: value.a, b: value.b, c: value.c, d: value.d, e: value.e, - f: value.f, }, + f: value.f, + } ); this._events.push(event); } @@ -439,11 +684,9 @@ export default class CanvasRenderingContext2D { set direction(value) { if (value === 'rtl' || value === 'ltr' || value === 'inherit') { this._directionStack[this._stackIndex] = value; - const event = createCanvasEvent( - 'direction', - getTransformSlice(this), - { value }, - ); + const event = createCanvasEvent('direction', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -453,36 +696,77 @@ export default class CanvasRenderingContext2D { } drawFocusIfNeeded(path, element) { - if (arguments.length === 0) throw new TypeError('Failed to execute \'drawFocusIfNeeded\' on \'' + this.constructor.name + '\': 1 argument required, but only 0 present.'); - if (arguments.length === 2 && !(path instanceof Path2D)) throw new TypeError('Failed to execute \'drawFocusIfNeeded\' on \'' + this.constructor.name + '\': parameter 1 is not of type \'Path2D\'.'); + if (arguments.length === 0) + throw new TypeError( + "Failed to execute 'drawFocusIfNeeded' on '" + + this.constructor.name + + "': 1 argument required, but only 0 present." + ); + if (arguments.length === 2 && !(path instanceof Path2D)) + throw new TypeError( + "Failed to execute 'drawFocusIfNeeded' on '" + + this.constructor.name + + "': parameter 1 is not of type 'Path2D'." + ); if (arguments.length === 1) { element = path; path = null; } - if (!(element instanceof Element)) throw new TypeError('Failed to execute \'drawFocusIfNeeded\' on \'' + this.constructor.name + '\': parameter ' + arguments.length + ' is not of type \'Element\'.'); + if (!(element instanceof Element)) + throw new TypeError( + "Failed to execute 'drawFocusIfNeeded' on '" + + this.constructor.name + + "': parameter " + + arguments.length + + " is not of type 'Element'." + ); const event = createCanvasEvent( 'drawFocusIfNeeded', getTransformSlice(this), - { path: path ? path._path : null, element }, + { path: path ? path._path : null, element } ); this._events.push(event); } drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) { - if (arguments.length < 3) throw new TypeError('Failed to execute \'drawImage\' on \'' + this.constructor.name + '\': 3 arguments required, but only ' + arguments.length + ' present.'); - if (arguments.length === 4 || arguments.length > 5 && arguments.length < 9) throw new TypeError('Failed to execute \'drawImage\' on \'' + this.constructor.name + '\': Valid arities are: [3, 5, 9], but 4 arguments provided.'); + if (arguments.length < 3) + throw new TypeError( + "Failed to execute 'drawImage' on '" + + this.constructor.name + + "': 3 arguments required, but only " + + arguments.length + + ' present.' + ); + if ( + arguments.length === 4 || + (arguments.length > 5 && arguments.length < 9) + ) + throw new TypeError( + "Failed to execute 'drawImage' on '" + + this.constructor.name + + "': Valid arities are: [3, 5, 9], but 4 arguments provided." + ); let valid = false; - if (img instanceof HTMLImageElement) valid = true;; + if (img instanceof HTMLImageElement) valid = true; if (img instanceof ImageBitmap) { - if (img._closed) throw new DOMException('InvalidStateError', 'DOMException: Failed to execute \'drawImage\' on \'CanvasRenderingContext2D\': The image source is detached.'); + if (img._closed) + throw new DOMException( + 'InvalidStateError', + "DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image source is detached." + ); valid = true; } if (img instanceof HTMLVideoElement) valid = true; if (img instanceof HTMLCanvasElement) valid = true; - if (!valid) throw new TypeError('Failed to execute \'drawImage\' on \'' + this.constructor.name + '\': The provided value is not of type \'(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)\''); + if (!valid) + throw new TypeError( + "Failed to execute 'drawImage' on '" + + this.constructor.name + + "': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'" + ); const sxResult = Number(sx); const syResult = Number(sy); @@ -504,7 +788,8 @@ export default class CanvasRenderingContext2D { dWidth = img.width; dHeight = img.height; } else if (arguments.length === 5) { - if (!Number.isFinite(sxResult + syResult + sWidthResult + sHeightResult)) return; + if (!Number.isFinite(sxResult + syResult + sWidthResult + sHeightResult)) + return; sx = 0; sy = 0; sWidth = img.width; @@ -514,7 +799,12 @@ export default class CanvasRenderingContext2D { dWidth = sWidth; dHeight = sHeight; } else { - if (!Number.isFinite(sx + sy + sWidth + sHeight + dx + dy + dWidth + dHeight)) return; + if ( + !Number.isFinite( + sx + sy + sWidth + sHeight + dx + dy + dWidth + dHeight + ) + ) + return; sx = sxResult; sy = syResult; sWidth = sWidthResult; @@ -525,25 +815,39 @@ export default class CanvasRenderingContext2D { dHeight = dHeightResult; } - const event = createCanvasEvent( - 'drawImage', - getTransformSlice(this), - { img, - sx, - sy, - sWidth, - sHeight, - dx, - dy, - dWidth, - dHeight, }, - ); + const event = createCanvasEvent('drawImage', getTransformSlice(this), { + img, + sx, + sy, + sWidth, + sHeight, + dx, + dy, + dWidth, + dHeight, + }); this._events.push(event); this._drawCalls.push(event); } - ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise = false) { - if (arguments.length < 7) throw new TypeError('Failed to execute \'ellipse\' on \'' + this.constructor.name + '\': 6 arguments required, but only ' + arguments.length + ' present.'); + ellipse( + x, + y, + radiusX, + radiusY, + rotation, + startAngle, + endAngle, + anticlockwise = false + ) { + if (arguments.length < 7) + throw new TypeError( + "Failed to execute 'ellipse' on '" + + this.constructor.name + + "': 6 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); const radiusXResult = Number(radiusX); @@ -553,23 +857,48 @@ export default class CanvasRenderingContext2D { const endAngleResult = Number(endAngle); const anticlockwiseResult = Boolean(anticlockwise); - if (!Number.isFinite(xResult + yResult + radiusXResult + radiusYResult + rotationResult + startAngleResult + endAngleResult)) return; - - if (Number(radiusX) < 0) throw new DOMException('IndexSizeError', 'Failed to execute \'ellipse\' on \'' + this.constructor.name + '\': The major-axis radius provided (' + radiusX + ') is negative.'); - if (Number(radiusY) < 0) throw new DOMException('IndexSizeError', 'Failed to execute \'ellipse\' on \'' + this.constructor.name + '\': The minor-axis radius provided (' + radiusY + ') is negative.'); + if ( + !Number.isFinite( + xResult + + yResult + + radiusXResult + + radiusYResult + + rotationResult + + startAngleResult + + endAngleResult + ) + ) + return; + + if (Number(radiusX) < 0) + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'ellipse' on '" + + this.constructor.name + + "': The major-axis radius provided (" + + radiusX + + ') is negative.' + ); + if (Number(radiusY) < 0) + throw new DOMException( + 'IndexSizeError', + "Failed to execute 'ellipse' on '" + + this.constructor.name + + "': The minor-axis radius provided (" + + radiusY + + ') is negative.' + ); - const event = createCanvasEvent( - 'ellipse', - getTransformSlice(this), - { x: xResult, - y: yResult, - radiusX: radiusXResult, - radiusY: radiusYResult, - rotation: rotationResult, - startAngle: startAngleResult, - endAngle: endAngleResult, - anticlockwise: anticlockwiseResult, }, - ); + const event = createCanvasEvent('ellipse', getTransformSlice(this), { + x: xResult, + y: yResult, + radiusX: radiusXResult, + radiusY: radiusYResult, + rotation: rotationResult, + startAngle: startAngleResult, + endAngle: endAngleResult, + anticlockwise: anticlockwiseResult, + }); this._path.push(event); this._events.push(event); } @@ -582,27 +911,47 @@ export default class CanvasRenderingContext2D { if (arguments.length === 1) fillRule = 'nonzero'; if (path instanceof Path2D) { fillRule = String(fillRule); - if (fillRule !== 'nonzero' && fillRule !== 'evenodd') throw new TypeError('Failed to execute \'clip\' on \'' + this.constructor.name + '\': The provided value \'' + fillRule + '\' is not a valid enum value of type CanvasFillRule.'); + if (fillRule !== 'nonzero' && fillRule !== 'evenodd') + throw new TypeError( + "Failed to execute 'clip' on '" + + this.constructor.name + + "': The provided value '" + + fillRule + + "' is not a valid enum value of type CanvasFillRule." + ); path = path._path.slice(); } else { fillRule = String(path); - if (fillRule !== 'nonzero' && fillRule !== 'evenodd') throw new TypeError('Failed to execute \'clip\' on \'' + this.constructor.name + '\': The provided value \'' + fillRule + '\' is not a valid enum value of type CanvasFillRule.'); + if (fillRule !== 'nonzero' && fillRule !== 'evenodd') + throw new TypeError( + "Failed to execute 'clip' on '" + + this.constructor.name + + "': The provided value '" + + fillRule + + "' is not a valid enum value of type CanvasFillRule." + ); path = this._path.slice(); } } - const event = createCanvasEvent( - 'fill', - getTransformSlice(this), - { path, fillRule }, - ); + const event = createCanvasEvent('fill', getTransformSlice(this), { + path, + fillRule, + }); this._events.push(event); this._drawCalls.push(event); } fillRect(x, y, width, height) { - if (arguments.length < 4) throw new TypeError('Failed to execute \'fillRect\' on \'' + this.constructor.name + '\': 4 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'fillRect' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); @@ -611,14 +960,12 @@ export default class CanvasRenderingContext2D { if (!Number.isFinite(x + y + width + height)) return; - const event = createCanvasEvent( - 'fillRect', - getTransformSlice(this), - { x: xResult, - y: yResult, - width: widthResult, - height: heightResult, }, - ); + const event = createCanvasEvent('fillRect', getTransformSlice(this), { + x: xResult, + y: yResult, + width: widthResult, + height: heightResult, + }); this._events.push(event); this._drawCalls.push(event); @@ -631,20 +978,21 @@ export default class CanvasRenderingContext2D { const result = new MooColor(value); valid = true; value = this._fillStyleStack[this._stackIndex] = serializeColor(result); + } catch (e) { + return; } - catch(e) { return; } - } - else if (value instanceof CanvasGradient || value instanceof CanvasPattern) { + } else if ( + value instanceof CanvasGradient || + value instanceof CanvasPattern + ) { valid = true; this._fillStyleStack[this._stackIndex] = value; } if (valid) { - const event = createCanvasEvent( - 'fillStyle', - getTransformSlice(this), - { value }, - ); + const event = createCanvasEvent('fillStyle', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -654,33 +1002,41 @@ export default class CanvasRenderingContext2D { } fillText(text, x, y, maxWidth) { - if (arguments.length < 3) throw new TypeError('Failed to execute \'fillText\' on \'' + this.constructor.name + '\': 3 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 3) + throw new TypeError( + "Failed to execute 'fillText' on '" + + this.constructor.name + + "': 3 arguments required, but only " + + arguments.length + + ' present.' + ); const textResult = String(text); const xResult = Number(x); const yResult = Number(y); const maxWidthResult = Number(maxWidth); if (arguments.length === 3 && !Number.isFinite(xResult + yResult)) return; - if (arguments.length > 3 && !Number.isFinite(xResult + yResult + maxWidthResult)) return; - const event = createCanvasEvent( - 'fillText', - getTransformSlice(this), - { text: textResult, - x: xResult, - y: yResult, - maxWidth: arguments.length === 3 ? null : maxWidthResult, }, - ); + if ( + arguments.length > 3 && + !Number.isFinite(xResult + yResult + maxWidthResult) + ) + return; + const event = createCanvasEvent('fillText', getTransformSlice(this), { + text: textResult, + x: xResult, + y: yResult, + maxWidth: arguments.length === 3 ? null : maxWidthResult, + }); this._events.push(event); this._drawCalls.push(event); } set filter(value) { if (value === '') value = 'none'; - value = this._filterStack[this._stackIndex] = typeof value === 'string' ? value : 'none'; - const event = createCanvasEvent( - 'filter', - getTransformSlice(this), - { value, }, - ); + value = this._filterStack[this._stackIndex] = + typeof value === 'string' ? value : 'none'; + const event = createCanvasEvent('filter', getTransformSlice(this), { + value, + }); this._events.push(event); } @@ -694,11 +1050,9 @@ export default class CanvasRenderingContext2D { try { const result = cssfontparser(value); value = this._fontStack[this._stackIndex] = result.toString(); - const event = createCanvasEvent( - 'font', - getTransformSlice(this), - { value, }, - ); + const event = createCanvasEvent('font', getTransformSlice(this), { + value, + }); this._events.push(event); } catch (ex) {} } @@ -725,11 +1079,9 @@ export default class CanvasRenderingContext2D { if (value < 0) return; if (value > 1) return; this._globalAlphaStack[this._stackIndex] = value; - const event = createCanvasEvent( - 'globalAlpha', - getTransformSlice(this), - { value, }, - ); + const event = createCanvasEvent('globalAlpha', getTransformSlice(this), { + value, + }); this._events.push(event); } @@ -743,7 +1095,7 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'globalCompositeOperation', getTransformSlice(this), - { value, }, + { value } ); this._events.push(event); } @@ -758,7 +1110,7 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'imageSmoothingEnabled', getTransformSlice(this), - { value, }, + { value } ); this._events.push(event); } @@ -773,7 +1125,7 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'imageSmoothingQuality', getTransformSlice(this), - { value, }, + { value } ); this._events.push(event); } @@ -784,7 +1136,14 @@ export default class CanvasRenderingContext2D { } isPointInPath(path, x, y, fillRule = 'nonzero') { - if (arguments.length < 2) throw new TypeError('Failed to execute \'isPointInPath\' on \'' + this.constructor.name + '\': 2 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 2) + throw new TypeError( + "Failed to execute 'isPointInPath' on '" + + this.constructor.name + + "': 2 arguments required, but only " + + arguments.length + + ' present.' + ); if (!(path instanceof Path2D)) { if (arguments.length > 2) { fillRule = y; @@ -792,33 +1151,43 @@ export default class CanvasRenderingContext2D { y = x; x = path; } - if (fillRule !== 'nonzero' && fillRule !== 'evenodd') throw new TypeError('Failed to execute \'isPointInPath\' on \'' + this.constructor.name + '\': The provided value \'' + fillRule + '\' is not a valid enum value of type CanvasFillRule.'); + if (fillRule !== 'nonzero' && fillRule !== 'evenodd') + throw new TypeError( + "Failed to execute 'isPointInPath' on '" + + this.constructor.name + + "': The provided value '" + + fillRule + + "' is not a valid enum value of type CanvasFillRule." + ); - const event = createCanvasEvent( - 'isPointInPath', - getTransformSlice(this), - { x: Number(x), - y: Number(y), - fillRule, - path: path instanceof Path2D ? path._path.slice() : this._path.slice(), }, - ); + const event = createCanvasEvent('isPointInPath', getTransformSlice(this), { + x: Number(x), + y: Number(y), + fillRule, + path: path instanceof Path2D ? path._path.slice() : this._path.slice(), + }); this._events.push(event); return false; // return false in a mocking environment, unless I can verify a point is actually within the path } isPointInStroke(path, x, y) { - if (arguments.length < 2) throw new TypeError('Failed to execute \'isPointInStroke\' on \'' + this.constructor.name + '\': 2 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 2) + throw new TypeError( + "Failed to execute 'isPointInStroke' on '" + + this.constructor.name + + "': 2 arguments required, but only " + + arguments.length + + ' present.' + ); if (!(path instanceof Path2D)) { y = x; x = path; } - const event = createCanvasEvent( - 'isPointInPath', - getTransformSlice(this), - { x: Number(x), - y: Number(y), - path: path instanceof Path2D ? path._path.slice() : this._path.slice(), }, - ); + const event = createCanvasEvent('isPointInPath', getTransformSlice(this), { + x: Number(x), + y: Number(y), + path: path instanceof Path2D ? path._path.slice() : this._path.slice(), + }); this._events.push(event); return false; // return false in a mocking environment, unless I can verify a point is actually within the path } @@ -826,11 +1195,9 @@ export default class CanvasRenderingContext2D { set lineCap(value) { if (value === 'butt' || value === 'round' || value === 'square') { this._lineCapStack[this._stackIndex] = value; - const event = createCanvasEvent( - 'lineCap', - getTransformSlice(this), - { value, }, - ); + const event = createCanvasEvent('lineCap', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -847,7 +1214,7 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'lineDashOffset', getTransformSlice(this), - { value, }, + { value } ); this._events.push(event); } @@ -860,11 +1227,9 @@ export default class CanvasRenderingContext2D { set lineJoin(value) { if (value === 'round' || value === 'bevel' || value === 'miter') { this._lineJoinStack[this._stackIndex] = value; - const event = createCanvasEvent( - 'lineJoin', - getTransformSlice(this), - { value, }, - ); + const event = createCanvasEvent('lineJoin', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -874,16 +1239,22 @@ export default class CanvasRenderingContext2D { } lineTo(x, y) { - if (arguments.length < 2) throw new TypeError('Failed to execute \'lineTo\' on \'' + this.constructor.name + '\': 2 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 2) + throw new TypeError( + "Failed to execute 'lineTo' on '" + + this.constructor.name + + "': 2 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); if (!Number.isFinite(xResult + yResult)) return; - const event = createCanvasEvent( - 'lineTo', - getTransformSlice(this), - { x: xResult, y: yResult, }, - ); + const event = createCanvasEvent('lineTo', getTransformSlice(this), { + x: xResult, + y: yResult, + }); this._events.push(event); this._path.push(event); } @@ -893,11 +1264,9 @@ export default class CanvasRenderingContext2D { if (Number.isFinite(result) && result > 0) { this._lineWidthStack[this._stackIndex] = result; - const event = createCanvasEvent( - 'lineWidth', - getTransformSlice(this), - { value: result, }, - ); + const event = createCanvasEvent('lineWidth', getTransformSlice(this), { + value: result, + }); this._events.push(event); } } @@ -907,14 +1276,17 @@ export default class CanvasRenderingContext2D { } measureText(text) { - if (arguments.length < 1) throw new TypeError('Failed to execute \'measureText\' on \'' + this.constructor.name + '\': 1 argument required, but only 0 present.'); + if (arguments.length < 1) + throw new TypeError( + "Failed to execute 'measureText' on '" + + this.constructor.name + + "': 1 argument required, but only 0 present." + ); text = text == null ? '' : text; text = text.toString(); - const event = createCanvasEvent( - 'measureText', - getTransformSlice(this), - { text }, - ); + const event = createCanvasEvent('measureText', getTransformSlice(this), { + text, + }); this._events.push(event); return new TextMetrics(text); } @@ -924,11 +1296,9 @@ export default class CanvasRenderingContext2D { if (Number.isFinite(result) && result > 0) { this._miterLimitStack[this._stackIndex] = result; - const event = createCanvasEvent( - 'lineWidth', - getTransformSlice(this), - { value: result, }, - ); + const event = createCanvasEvent('lineWidth', getTransformSlice(this), { + value: result, + }); this._events.push(event); } } @@ -938,25 +1308,49 @@ export default class CanvasRenderingContext2D { } moveTo(x, y) { - if (arguments.length < 2) throw new TypeError('Failed to execute \'moveTo\' on \'' + this.constructor.name + '\': 2 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 2) + throw new TypeError( + "Failed to execute 'moveTo' on '" + + this.constructor.name + + "': 2 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); if (!Number.isFinite(x + y)) return; - const event = createCanvasEvent( - 'moveTo', - getTransformSlice(this), - { x: xResult, - y: yResult, }, - ); + const event = createCanvasEvent('moveTo', getTransformSlice(this), { + x: xResult, + y: yResult, + }); this._events.push(event); this._path.push(event); } putImageData(data, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight) { - if (arguments.length < 3) throw new TypeError('Failed to execute \'putImageData\' on \'' + this.constructor.name + '\': 3 arguments required, but only ' + arguments.length + ' present.'); - if (arguments.length > 3 && arguments.length < 7) throw new TypeError('Failed to execute \'putImageData\' on \'' + this.constructor.name + '\': Valid arities are: [3, 7], but ' + arguments.length + ' arguments provided.'); - if (!(data instanceof ImageData)) throw new TypeError('Failed to execute \'putImageData\' on \'' + this.constructor.name + '\': parameter 1 is not of type \'ImageData\'.'); + if (arguments.length < 3) + throw new TypeError( + "Failed to execute 'putImageData' on '" + + this.constructor.name + + "': 3 arguments required, but only " + + arguments.length + + ' present.' + ); + if (arguments.length > 3 && arguments.length < 7) + throw new TypeError( + "Failed to execute 'putImageData' on '" + + this.constructor.name + + "': Valid arities are: [3, 7], but " + + arguments.length + + ' arguments provided.' + ); + if (!(data instanceof ImageData)) + throw new TypeError( + "Failed to execute 'putImageData' on '" + + this.constructor.name + + "': parameter 1 is not of type 'ImageData'." + ); const xResult = Number(x); const yResult = Number(y); @@ -968,24 +1362,39 @@ export default class CanvasRenderingContext2D { if (arguments.length === 3) { if (!Number.isFinite(xResult + yResult)) return; } else { - if (!Number.isFinite(xResult + yResult + dirtyXResult + dirtyYResult + dirtyWidthResult + dirtyHeightResult)) return; + if ( + !Number.isFinite( + xResult + + yResult + + dirtyXResult + + dirtyYResult + + dirtyWidthResult + + dirtyHeightResult + ) + ) + return; } - const event = createCanvasEvent( - 'putImageData', - getTransformSlice(this), - { x: xResult, - y: yResult, - dirtyX: dirtyXResult, - dirtyY: dirtyYResult, - dirtyWidth: dirtyWidthResult, - dirtyHeight: dirtyHeightResult, }, - ); + const event = createCanvasEvent('putImageData', getTransformSlice(this), { + x: xResult, + y: yResult, + dirtyX: dirtyXResult, + dirtyY: dirtyYResult, + dirtyWidth: dirtyWidthResult, + dirtyHeight: dirtyHeightResult, + }); this._events.push(event); } quadraticCurveTo(cpx, cpy, x, y) { - if (arguments.length < 4) throw new TypeError('Failed to execute \'quadraticCurveTo\' on \'' + this.constructor.name + '\': 4 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'quadraticCurveTo' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); const cpxResult = Number(cpx); const cpyResult = Number(cpy); const xResult = Number(x); @@ -996,41 +1405,50 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'quadraticCurveTo', getTransformSlice(this), - { cpx: cpxResult, - cpy: cpyResult, - x: xResult, - y: yResult, }, + { cpx: cpxResult, cpy: cpyResult, x: xResult, y: yResult } ); this._events.push(event); } rect(x, y, width, height) { - if (arguments.length < 4) throw new TypeError('Failed to execute \'rect\' on \'' + this.constructor.name + '\': 4 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'rect' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); if (!Number.isFinite(x + y + width + height)) return; const xResult = Number(x); const yResult = Number(y); const widthResult = Number(width); const heightResult = Number(height); - const event = createCanvasEvent( - 'rect', - getTransformSlice(this), - { x: xResult, - y: yResult, - width: widthResult, - height: heightResult, }, - ); + const event = createCanvasEvent('rect', getTransformSlice(this), { + x: xResult, + y: yResult, + width: widthResult, + height: heightResult, + }); this._events.push(event); this._path.push(event); } removeHitRegion(id) { - if (arguments.length < 1) throw new TypeError('Failed to execute \'removeHitRegion\' on \'' + this.constructor.name + '\': 1 argument required, but only ' + arguments.length + ' present.'); + if (arguments.length < 1) + throw new TypeError( + "Failed to execute 'removeHitRegion' on '" + + this.constructor.name + + "': 1 argument required, but only " + + arguments.length + + ' present.' + ); const event = createCanvasEvent( 'removeHitRegion', getTransformSlice(this), - { id }, + { id } ); this._events.push(event); @@ -1044,16 +1462,14 @@ export default class CanvasRenderingContext2D { this._transformStack[this._stackIndex][4] = 0; this._transformStack[this._stackIndex][5] = 0; - const event = createCanvasEvent( - 'resetTransform', - getTransformSlice(this), - { a: 1, - b: 0, - c: 0, - d: 1, - e: 0, - f: 0, }, - ); + const event = createCanvasEvent('resetTransform', getTransformSlice(this), { + a: 1, + b: 0, + c: 0, + d: 1, + e: 0, + f: 0, + }); this._events.push(event); } @@ -1084,16 +1500,17 @@ export default class CanvasRenderingContext2D { this._textBaselineStack.pop(); this._stackIndex -= 1; - const event = createCanvasEvent( - 'restore', - getTransformSlice(this), - { }, - ); + const event = createCanvasEvent('restore', getTransformSlice(this), {}); this._events.push(event); } rotate(angle) { - if (arguments.length < 1) throw new TypeError('Failed to execute \'rotate\' on \'' + this.constructor.name + '\': 1 argument required, but only 0 present.'); + if (arguments.length < 1) + throw new TypeError( + "Failed to execute 'rotate' on '" + + this.constructor.name + + "': 1 argument required, but only 0 present." + ); angle = Number(angle); if (!Number.isFinite(angle)) return; const a = this._transformStack[this._stackIndex][0]; @@ -1107,11 +1524,9 @@ export default class CanvasRenderingContext2D { this._transformStack[this._stackIndex][2] = c * cos - a * sin; this._transformStack[this._stackIndex][3] = d * cos - b * sin; - const event = createCanvasEvent( - 'rotate', - getTransformSlice(this), - { angle }, - ); + const event = createCanvasEvent('rotate', getTransformSlice(this), { + angle, + }); this._events.push(event); } @@ -1123,9 +1538,15 @@ export default class CanvasRenderingContext2D { this._filterStack.push(this._filterStack[stackIndex]); this._fontStack.push(this._fontStack[stackIndex]); this._globalAlphaStack.push(this._globalAlphaStack[stackIndex]); - this._globalCompositeOperationStack.push(this._globalCompositeOperationStack[stackIndex]); - this._imageSmoothingEnabledStack.push(this._imageSmoothingEnabledStack[stackIndex]); - this._imageSmoothingQualityStack.push(this._imageSmoothingQualityStack[stackIndex]); + this._globalCompositeOperationStack.push( + this._globalCompositeOperationStack[stackIndex] + ); + this._imageSmoothingEnabledStack.push( + this._imageSmoothingEnabledStack[stackIndex] + ); + this._imageSmoothingQualityStack.push( + this._imageSmoothingQualityStack[stackIndex] + ); this._lineCapStack.push(this._lineCapStack[stackIndex]); this._lineDashStack.push(this._lineDashStack[stackIndex]); this._lineDashOffsetStack.push(this._lineDashOffsetStack[stackIndex]); @@ -1142,16 +1563,19 @@ export default class CanvasRenderingContext2D { this._clipStack.push(this._clipStack[stackIndex].slice()); this._stackIndex = stackIndex + 1; - const event = createCanvasEvent( - 'save', - getTransformSlice(this), - { }, - ); + const event = createCanvasEvent('save', getTransformSlice(this), {}); this._events.push(event); } scale(x, y) { - if (arguments.length < 2) throw new TypeError('Failed to execute \'scale\' on \'' + this.constructor.name + '\': 2 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 2) + throw new TypeError( + "Failed to execute 'scale' on '" + + this.constructor.name + + "': 2 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); @@ -1161,31 +1585,44 @@ export default class CanvasRenderingContext2D { this._transformStack[this._stackIndex][2] *= yResult; this._transformStack[this._stackIndex][3] *= yResult; - const event = createCanvasEvent( - 'scale', - getTransformSlice(this), - { x: xResult, - y: yResult, }, - ); + const event = createCanvasEvent('scale', getTransformSlice(this), { + x: xResult, + y: yResult, + }); this._events.push(event); } } scrollPathIntoView(path) { - if (arguments.length > 0 && path instanceof Path2D) path = path._path.slice(); + if (arguments.length > 0 && path instanceof Path2D) + path = path._path.slice(); else path = this._path.slice(); const event = createCanvasEvent( 'scrollPathIntoView', getTransformSlice(this), - { path }, + { path } ); this._events.push(event); } setLineDash(lineDash) { - const isSequence = [Array, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array] - .reduce((left, right) => left || lineDash instanceof right, false); - if (!isSequence) throw new TypeError('Failed to execute \'setLineDash\' on \'' + this.constructor.name + '\': The provided value cannot be converted to a sequence.'); + const isSequence = [ + Array, + Int8Array, + Uint8Array, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + ].reduce((left, right) => left || lineDash instanceof right, false); + if (!isSequence) + throw new TypeError( + "Failed to execute 'setLineDash' on '" + + this.constructor.name + + "': The provided value cannot be converted to a sequence." + ); let result = []; for (let i = 0; i < lineDash.length; i++) { @@ -1198,17 +1635,15 @@ export default class CanvasRenderingContext2D { } } - result = this._lineDashStack[this._stackIndex] = result.length % 2 === 1 ? result.concat(result) : result; - const event = createCanvasEvent( - 'setLineDash', - getTransformSlice(this), - { value: result.slice() }, - ); + result = this._lineDashStack[this._stackIndex] = + result.length % 2 === 1 ? result.concat(result) : result; + const event = createCanvasEvent('setLineDash', getTransformSlice(this), { + value: result.slice(), + }); this._events.push(event); } setTransform(a, b, c, d, e, f) { - if (arguments.length === 0) { a = 1; b = 0; @@ -1226,10 +1661,22 @@ export default class CanvasRenderingContext2D { e = transform.e; f = transform.f; } else { - throw new TypeError('Failed to execute \'setTransform\' on \'' + this.constructor.name + '\': parameter ' + a + ' (\'transform\') is not an object.'); + throw new TypeError( + "Failed to execute 'setTransform' on '" + + this.constructor.name + + "': parameter " + + a + + " ('transform') is not an object." + ); } } else if (arguments.length < 6) { - throw new TypeError('Failed to execute \'setTransform\' on \'' + this.constructor.name + '\': Valid arities are: [0, 1, 6], but ' + arguments.length + ' arguments provided.'); + throw new TypeError( + "Failed to execute 'setTransform' on '" + + this.constructor.name + + "': Valid arities are: [0, 1, 6], but " + + arguments.length + + ' arguments provided.' + ); } a = Number(a); b = Number(b); @@ -1245,11 +1692,14 @@ export default class CanvasRenderingContext2D { this._transformStack[this._stackIndex][4] = e; this._transformStack[this._stackIndex][5] = f; - const event = createCanvasEvent( - 'setTransform', - getTransformSlice(this), - { a, b, c, d, e, f }, - ); + const event = createCanvasEvent('setTransform', getTransformSlice(this), { + a, + b, + c, + d, + e, + f, + }); this._events.push(event); } @@ -1258,11 +1708,9 @@ export default class CanvasRenderingContext2D { if (Number.isFinite(result) && result > 0) { this._shadowBlurStack[this._stackIndex] = result; - const event = createCanvasEvent( - 'shadowBlur', - getTransformSlice(this), - { value: result }, - ); + const event = createCanvasEvent('shadowBlur', getTransformSlice(this), { + value: result, + }); this._events.push(event); } } @@ -1273,16 +1721,18 @@ export default class CanvasRenderingContext2D { set shadowColor(value) { if (typeof value === 'string') { - try { + try { const result = new MooColor(value); - value = this._shadowColorStack[this._stackIndex] = serializeColor(result); - } catch (e) { return; } + value = this._shadowColorStack[this._stackIndex] = serializeColor( + result + ); + } catch (e) { + return; + } - const event = createCanvasEvent( - 'shadowColor', - getTransformSlice(this), - { value }, - ); + const event = createCanvasEvent('shadowColor', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -1299,7 +1749,7 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'shadowOffsetX', getTransformSlice(this), - { value: result }, + { value: result } ); this._events.push(event); } @@ -1317,7 +1767,7 @@ export default class CanvasRenderingContext2D { const event = createCanvasEvent( 'shadowOffsetY', getTransformSlice(this), - { value: result }, + { value: result } ); this._events.push(event); } @@ -1331,32 +1781,43 @@ export default class CanvasRenderingContext2D { if (arguments.length === 0) { path = this._path.slice(); } else { - if (!(path instanceof Path2D)) throw new TypeError('Failed to execute \'stroke\' on \'' + this.constructor.name + '\': parameter 1 is not of type \'Path2D\'.'); + if (!(path instanceof Path2D)) + throw new TypeError( + "Failed to execute 'stroke' on '" + + this.constructor.name + + "': parameter 1 is not of type 'Path2D'." + ); path = path._path.slice(); } - const event = createCanvasEvent( - 'stroke', - getTransformSlice(this), - { path }, - ); + const event = createCanvasEvent('stroke', getTransformSlice(this), { + path, + }); this._events.push(event); this._drawCalls.push(event); } strokeRect(x, y, width, height) { - if (arguments.length < 4) throw new TypeError('Failed to execute \'strokeRect\' on \'' + this.constructor.name + '\': 4 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'strokeRect' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); x = Number(x); y = Number(y); width = Number(width); height = Number(height); if (!Number.isFinite(x + y + width + height)) return; - const event = createCanvasEvent( - 'strokeRect', - getTransformSlice(this), - { x, y, width, height }, - ); + const event = createCanvasEvent('strokeRect', getTransformSlice(this), { + x, + y, + width, + height, + }); this._events.push(event); this._drawCalls.push(event); } @@ -1367,21 +1828,24 @@ export default class CanvasRenderingContext2D { try { const result = new MooColor(value); valid = true; - value = this._strokeStyleStack[this._stackIndex] = serializeColor(result); + value = this._strokeStyleStack[this._stackIndex] = serializeColor( + result + ); + } catch (e) { + return; } - catch(e) { return; } - } - else if (value instanceof CanvasGradient || value instanceof CanvasPattern) { + } else if ( + value instanceof CanvasGradient || + value instanceof CanvasPattern + ) { valid = true; this._strokeStyleStack[this._stackIndex] = value; } if (valid) { - const event = createCanvasEvent( - 'strokeStyle', - getTransformSlice(this), - { value }, - ); + const event = createCanvasEvent('strokeStyle', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -1391,33 +1855,46 @@ export default class CanvasRenderingContext2D { } strokeText(text, x, y, maxWidth) { - if (arguments.length < 3) throw new TypeError('Failed to execute \'strokeText\' on \'' + this.constructor.name + '\': 3 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 3) + throw new TypeError( + "Failed to execute 'strokeText' on '" + + this.constructor.name + + "': 3 arguments required, but only " + + arguments.length + + ' present.' + ); const textResult = String(text); const xResult = Number(x); const yResult = Number(y); const maxWidthResult = Number(maxWidth); if (arguments.length === 3 && !Number.isFinite(xResult + yResult)) return; - if (arguments.length > 3 && !Number.isFinite(xResult + yResult + maxWidthResult)) return; - const event = createCanvasEvent( - 'strokeText', - getTransformSlice(this), - { text: textResult, - x: xResult, - y: yResult, - maxWidth: arguments.length === 3 ? null : maxWidthResult, }, - ); + if ( + arguments.length > 3 && + !Number.isFinite(xResult + yResult + maxWidthResult) + ) + return; + const event = createCanvasEvent('strokeText', getTransformSlice(this), { + text: textResult, + x: xResult, + y: yResult, + maxWidth: arguments.length === 3 ? null : maxWidthResult, + }); this._events.push(event); this._drawCalls.push(event); } set textAlign(value) { - if (value === 'left' || value === 'right' || value === 'center' || value === 'start' || value === 'end') { + if ( + value === 'left' || + value === 'right' || + value === 'center' || + value === 'start' || + value === 'end' + ) { this._textAlignStack[this._stackIndex] = value; - const event = createCanvasEvent( - 'textAlign', - getTransformSlice(this), - { value, }, - ); + const event = createCanvasEvent('textAlign', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -1427,13 +1904,18 @@ export default class CanvasRenderingContext2D { } set textBaseline(value) { - if (value === 'top' || value === 'hanging' || value === 'middle' || value === 'alphabetic' || value === 'ideographic' || value === 'bottom') { + if ( + value === 'top' || + value === 'hanging' || + value === 'middle' || + value === 'alphabetic' || + value === 'ideographic' || + value === 'bottom' + ) { this._textBaselineStack[this._stackIndex] = value; - const event = createCanvasEvent( - 'textBaseline', - getTransformSlice(this), - { value, }, - ); + const event = createCanvasEvent('textBaseline', getTransformSlice(this), { + value, + }); this._events.push(event); } } @@ -1443,7 +1925,14 @@ export default class CanvasRenderingContext2D { } transform(a, b, c, d, e, f) { - if (arguments.length < 6) throw new TypeError('Failed to execute \'transform\' on \'' + this.constructor.name + '\': 6 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 6) + throw new TypeError( + "Failed to execute 'transform' on '" + + this.constructor.name + + "': 6 arguments required, but only " + + arguments.length + + ' present.' + ); a = Number(a); b = Number(b); @@ -1467,16 +1956,26 @@ export default class CanvasRenderingContext2D { this._transformStack[this._stackIndex][4] = sa * e + sc * f + se; this._transformStack[this._stackIndex][5] = sb * e + sd * f + sf; - const event = createCanvasEvent( - 'transform', - getTransformSlice(this), - { a, b, c, d, e, f }, - ); + const event = createCanvasEvent('transform', getTransformSlice(this), { + a, + b, + c, + d, + e, + f, + }); this._events.push(event); } translate(x, y) { - if (arguments.length < 2) throw new TypeError('Failed to execute \'translate\' on \'' + this.constructor.name + '\': 2 arguments required, but only ' + arguments.length + ' present.'); + if (arguments.length < 2) + throw new TypeError( + "Failed to execute 'translate' on '" + + this.constructor.name + + "': 2 arguments required, but only " + + arguments.length + + ' present.' + ); const xResult = Number(x); const yResult = Number(y); const a = this._transformStack[this._stackIndex][0]; @@ -1488,11 +1987,10 @@ export default class CanvasRenderingContext2D { this._transformStack[this._stackIndex][4] += a * xResult + c * yResult; this._transformStack[this._stackIndex][5] += b * xResult + d * yResult; - const event = createCanvasEvent( - 'translate', - getTransformSlice(this), - { x: xResult, y: yResult }, - ); + const event = createCanvasEvent('translate', getTransformSlice(this), { + x: xResult, + y: yResult, + }); this._events.push(event); } } diff --git a/src/classes/DOMMatrix.js b/src/classes/DOMMatrix.js index 5fac963..356227f 100644 --- a/src/classes/DOMMatrix.js +++ b/src/classes/DOMMatrix.js @@ -51,21 +51,26 @@ export default class DOMMatrix { } if (transform) { - throw new TypeError('Failed to construct \'DOMMatrix\': The sequence must contain 6 elements for a 2D matrix or 16 elements for a 3D matrix.'); + throw new TypeError( + "Failed to construct 'DOMMatrix': The sequence must contain 6 elements for a 2D matrix or 16 elements for a 3D matrix." + ); } this._is2D = false; } get isIdentity() { if (this._is2D) { - 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; + 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 + ); } else { - return this.m11 = 1.0 && + return (this.m11 = + 1.0 && this.m12 === 0.0 && this.m13 === 0.0 && this.m14 === 0.0 && @@ -80,7 +85,7 @@ export default class DOMMatrix { this.m41 === 0.0 && this.m42 === 0.0 && this.m43 === 0.0 && - this.m44 === 1.0; + this.m44 === 1.0); } } diff --git a/src/classes/ImageData.js b/src/classes/ImageData.js index 6541425..accf058 100644 --- a/src/classes/ImageData.js +++ b/src/classes/ImageData.js @@ -18,46 +18,64 @@ export default class ImageData { constructor(arr, w, h) { if (arguments.length === 2) { if (arr instanceof Uint8ClampedArray) { - if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.'); - if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.'); + if (arr.length === 0) + throw new RangeError( + 'Source length must be a positive multiple of 4.' + ); + if (arr.length % 4 !== 0) + throw new RangeError( + 'Source length must be a positive multiple of 4.' + ); - if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.'); + if (!Number.isFinite(w)) + throw new RangeError('The width is zero or not a number.'); if (w === 0) throw new RangeError('The width is zero or not a number.'); this._width = w; - this._height = (arr.length / 4) / w; + this._height = arr.length / 4 / w; this._data = arr; - } else { const width = arr; const height = w; - if (!Number.isFinite(height)) throw new RangeError('The height is zero or not a number.'); - if (height === 0) throw new RangeError('The height is zero or not a number.'); - if (!Number.isFinite(width)) throw new RangeError('The width is zero or not a number.'); - if (width === 0) throw new RangeError('The width is zero or not a number.'); + if (!Number.isFinite(height)) + throw new RangeError('The height is zero or not a number.'); + if (height === 0) + throw new RangeError('The height is zero or not a number.'); + if (!Number.isFinite(width)) + throw new RangeError('The width is zero or not a number.'); + if (width === 0) + throw new RangeError('The width is zero or not a number.'); this._width = width; this._height = height; this._data = new Uint8ClampedArray(width * height * 4); } - } else if (arguments.length === 3) { - if (!(arr instanceof Uint8ClampedArray)) throw new TypeError('First argument must be a Uint8ClampedArray when using 3 arguments.'); - if (arr.length === 0) throw new RangeError('Source length must be a positive multiple of 4.'); - if (arr.length % 4 !== 0) throw new RangeError('Source length must be a positive multiple of 4.'); + if (!(arr instanceof Uint8ClampedArray)) + throw new TypeError( + 'First argument must be a Uint8ClampedArray when using 3 arguments.' + ); + if (arr.length === 0) + throw new RangeError('Source length must be a positive multiple of 4.'); + if (arr.length % 4 !== 0) + throw new RangeError('Source length must be a positive multiple of 4.'); - if (!Number.isFinite(h)) throw new RangeError('The height is zero or not a number.'); + if (!Number.isFinite(h)) + throw new RangeError('The height is zero or not a number.'); if (h === 0) throw new RangeError('The height is zero or not a number.'); - if (!Number.isFinite(w)) throw new RangeError('The width is zero or not a number.'); + if (!Number.isFinite(w)) + throw new RangeError('The width is zero or not a number.'); if (w === 0) throw new RangeError('The width is zero or not a number.'); - if (arr.length !== w * h * 4) throw new RangeError('Source doesn\'n contain the exact number of pixels needed.'); + if (arr.length !== w * h * 4) + throw new RangeError( + "Source doesn'n contain the exact number of pixels needed." + ); this._width = w; this._height = h; this._data = arr; - } else { throw new TypeError('Wrong number of arguments provided.'); } diff --git a/src/classes/Path2D.js b/src/classes/Path2D.js index 467e2d9..2d984e8 100644 --- a/src/classes/Path2D.js +++ b/src/classes/Path2D.js @@ -1,9 +1,7 @@ import CanvasRenderingContext2D from './CanvasRenderingContext2D'; // Path2D.prototype -const Path2DFunc = [ - 'addPath', -]; +const Path2DFunc = ['addPath']; const borrowedFromCanvas = [ 'closePath', @@ -33,8 +31,14 @@ export default class Path2D { } addPath(path) { - if (arguments.length < 1) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': 1 argument required, but only 0 present.'); - if (!(path instanceof Path2D)) throw new TypeError('Failed to execute \'addPath\' on \'Path2D\': parameter 1 is not of type \'Path2D\'.'); + if (arguments.length < 1) + throw new TypeError( + "Failed to execute 'addPath' on 'Path2D': 1 argument required, but only 0 present." + ); + if (!(path instanceof Path2D)) + throw new TypeError( + "Failed to execute 'addPath' on 'Path2D': parameter 1 is not of type 'Path2D'." + ); this._path = this._path.concat(path._path); } } diff --git a/src/index.js b/src/index.js index 3b6dd16..73bbcd7 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ * Contract: i@hust.cc */ -import mockWindow from './window'; +import mockWindow from './window'; // mock global window // TODO: Force coverage to ignore this branch diff --git a/src/mock/createImageBitmap.js b/src/mock/createImageBitmap.js index 2afecdc..0555c3a 100644 --- a/src/mock/createImageBitmap.js +++ b/src/mock/createImageBitmap.js @@ -1,19 +1,47 @@ import ImageBitmap from '../classes/ImageBitmap'; -export default jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, options) { +export default jest.fn(function createImageBitmap( + img, + sx, + sy, + sWidth, + sHeight, + options +) { var length = arguments.length; return new Promise((resolve, reject) => { - if (length === 0) return reject(new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': 1 argument required, but only 0 present.')); - 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.')); + if (length === 0) + return reject( + new TypeError( + "Failed to execute 'createImageBitmap' on 'Window': 1 argument required, but only 0 present." + ) + ); + 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.' + ) + ); let validImage = false; if (img instanceof HTMLImageElement) validImage = true; if (img instanceof HTMLVideoElement) validImage = true; if (img instanceof HTMLCanvasElement) validImage = true; // checking constructor name is the only reliable way to verify the object's constructing class is "blob-like" - if (img instanceof Blob || (img && img.constructor && img.constructor.name === "Blob")) validImage = true; + if ( + img instanceof Blob || + (img && img.constructor && img.constructor.name === 'Blob') + ) + validImage = true; if (img instanceof ImageBitmap) validImage = true; if (img instanceof ImageData) validImage = true; - 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)\'')); + 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)'" + ) + ); if (length >= 2) { let index = 6; if (length === 2) { @@ -22,15 +50,22 @@ export default jest.fn(function createImageBitmap(img, sx, sy, sWidth, sHeight, } if (length === 5) options = null; if (options !== null && options !== void 0) { - if (typeof options !== 'object') throw new TypeError('Failed to execute \'createImageBitmap\' on \'Window\': parameter ' + index + ' (\'options\') is not an object.'); + if (typeof options !== 'object') + throw new TypeError( + "Failed to execute 'createImageBitmap' on 'Window': parameter " + + index + + " ('options') is not an object." + ); } } if (length >= 5) { sWidth = Number(sWidth); sHeight = Number(sHeight); - if (sWidth === 0 || !Number.isFinite(sWidth)) return reject(new RangeError('The crop rect width is 0.')); - if (sHeight === 0 || !Number.isFinite(sHeight)) return reject(new RangeError('The crop rect height is 0.')); + if (sWidth === 0 || !Number.isFinite(sWidth)) + return reject(new RangeError('The crop rect width is 0.')); + if (sHeight === 0 || !Number.isFinite(sHeight)) + return reject(new RangeError('The crop rect height is 0.')); sWidth = Math.abs(sWidth); sHeight = Math.abs(sHeight); } else { diff --git a/src/mock/prototype.js b/src/mock/prototype.js index 4120251..2b10695 100644 --- a/src/mock/prototype.js +++ b/src/mock/prototype.js @@ -41,17 +41,26 @@ export default function mockPrototype() { * we don't know if the canvas is tainted. These kinds of errors will be silent. */ const toBlobOverride = jest.fn(function toBlobOverride(callback, mimetype) { - if (arguments.length < 1) throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': 1 argument required, but only 0 present.'); - if (typeof callback !== 'function') throw new TypeError('Failed to execute \'toBlob\' on \'HTMLCanvasElement\': The callback provided as parameter 1 is not a function.'); + if (arguments.length < 1) + throw new TypeError( + "Failed to execute 'toBlob' on 'HTMLCanvasElement': 1 argument required, but only 0 present." + ); + if (typeof callback !== 'function') + throw new TypeError( + "Failed to execute 'toBlob' on 'HTMLCanvasElement': The callback provided as parameter 1 is not a function." + ); /** * Mime type must be image/jpeg or image/webp exactly for the browser to accept it, otherwise * it's image/png. */ switch (mimetype) { - case 'image/webp': break; - case 'image/jpeg': break; - default: mimetype = 'image/png'; + case 'image/webp': + break; + case 'image/jpeg': + break; + default: + mimetype = 'image/png'; } /** @@ -75,11 +84,17 @@ export default function mockPrototype() { * This section creates a dataurl with a validated mime type. This is not actually valid, because * jpeg size is variable, and so is png. TODO: Is there a better way to do this? */ - const toDataURLOverride = jest.fn(function toDataURLOverride(type, encoderOptions) { - switch(type) { - case 'image/jpeg': break; - case 'image/webp': break; - default: type = 'image/png'; + const toDataURLOverride = jest.fn(function toDataURLOverride( + type, + encoderOptions + ) { + switch (type) { + case 'image/jpeg': + break; + case 'image/webp': + break; + default: + type = 'image/png'; } /** diff --git a/src/window.js b/src/window.js index 8524acf..7ddc242 100644 --- a/src/window.js +++ b/src/window.js @@ -14,7 +14,7 @@ import ImageBitmap from './classes/ImageBitmap'; import mockPrototype from './mock/prototype'; import createImageBitmap from './mock/createImageBitmap'; -export default win => { +export default (win) => { const d = win.document; const f = win.document.createElement; @@ -47,7 +47,8 @@ export default win => { if (!win.Path2D) win.Path2D = Path2D; if (!win.CanvasGradient) win.CanvasGradient = CanvasGradient; if (!win.CanvasPattern) win.CanvasPattern = CanvasPattern; - if (!win.CanvasRenderingContext2D) win.CanvasRenderingContext2D = CanvasRenderingContext2D; + if (!win.CanvasRenderingContext2D) + win.CanvasRenderingContext2D = CanvasRenderingContext2D; if (!win.DOMMatrix) win.DOMMatrix = DOMMatrix; if (!win.ImageData) win.ImageData = ImageData; if (!win.TextMetrics) win.TextMetrics = TextMetrics; @@ -57,4 +58,4 @@ export default win => { mockPrototype(); return win; -} +};