Skip to content

Commit

Permalink
add string tags for ImageData and CanvasGradient
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Mar 20, 2023
1 parent fdf709a commit f6e112a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ bindings.ImageData.prototype.toString = function () {
return '[object ImageData]'
}

Object.defineProperty(bindings.ImageData.prototype, Symbol.toStringTag, {
value: 'ImageData'
})

bindings.CanvasGradient.prototype.toString = function () {
return '[object CanvasGradient]'
}

Object.defineProperty(bindings.CanvasGradient.prototype, Symbol.toStringTag, {
value: 'CanvasGradient'
})
9 changes: 8 additions & 1 deletion test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,13 @@ describe('Canvas', function () {
assert.strictEqual(gradient.toString(), '[object CanvasGradient]')
})

it('CanvasGradient has class string of `CanvasGradient`', function () {
const canvas = createCanvas(20, 1)
const ctx = canvas.getContext('2d')
const gradient = ctx.createLinearGradient(1, 1, 19, 1)
assert.strictEqual(Object.prototype.toString.call(gradient), '[object CanvasGradient]')
})

describe('Context2d#putImageData()', function () {
it('throws for invalid arguments', function () {
const canvas = createCanvas(2, 1)
Expand Down Expand Up @@ -1943,7 +1950,7 @@ describe('Canvas', function () {
ctx[k] = v
ctx.restore()
assert.strictEqual(ctx[k], old)

// save() doesn't modify the value:
ctx[k] = v
old = ctx[k]
Expand Down
5 changes: 5 additions & 0 deletions test/imageData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ describe('ImageData', function () {
assert.strictEqual(imageData.toString(), '[object ImageData]')
})

it('gives class string as `ImageData`', function () {
const imageData = createImageData(2, 3)
assert.strictEqual(Object.prototype.toString.call(imageData), '[object ImageData]')
})

it('should throw with invalid numeric arguments', function () {
assert.throws(() => { createImageData(0, 0) }, /width is zero/)
assert.throws(() => { createImageData(1, 0) }, /height is zero/)
Expand Down

0 comments on commit f6e112a

Please sign in to comment.