From 0ddeb7cf8e97aff54f2fb315da8a3cc34a19ba48 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 2 Jun 2022 14:29:49 +0800 Subject: [PATCH] fix: do nothing if lineTo parameters invalid --- __test__/draw.spec.ts | 11 +++++++++++ __test__/snapshots/lineTo-with-invalid-point.png | Bin 0 -> 1488 bytes src/ctx.rs | 14 ++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 __test__/snapshots/lineTo-with-invalid-point.png diff --git a/__test__/draw.spec.ts b/__test__/draw.spec.ts index 0eab7ab8..5b1749bc 100644 --- a/__test__/draw.spec.ts +++ b/__test__/draw.spec.ts @@ -570,6 +570,17 @@ test('lineTo', async (t) => { await snapshotImage(t) }) +test('lineTo-with-invalid-point', async (t) => { + const { ctx } = t.context + ctx.beginPath() // Start a new path + ctx.lineTo(NaN, 100) + ctx.lineTo(50, 50) + ctx.lineTo(100, NaN) + ctx.lineTo(250, 100) + ctx.stroke() + await snapshotImage(t) +}) + test('measureText', (t) => { const { ctx } = t.context ctx.font = '50px Iosevka Slab' diff --git a/__test__/snapshots/lineTo-with-invalid-point.png b/__test__/snapshots/lineTo-with-invalid-point.png new file mode 100644 index 0000000000000000000000000000000000000000..7de175b6d4ca099643638cbf31542530a947fe64 GIT binary patch literal 1488 zcmeIw-%C?r7zgn0PDedQGLC0mgpE?RP6#q8(tZ%Djnz6AZ6b;TFLn?iVj~uo&06rf zZW6LU38_}mjnT#CE(%8k9l@Yb2a!}4T?`CHwAIRfSZMUT`yads-J11{(1V@*h+jVR1+82{p4;qTN8jQ*i@OY=cz3iZ*arx70r_) zjmUox&2!v7(Jb+t>Qye)i}WR%mRYHy#8$bNWmd`W$n5K0sz*qk*I2Q1O0?j-ils2Q zPetLiBp|Jz@RoE+><6tk1_gFZL5mO3hji)(JCU8>+L7(DOPHX#n`=kx5v|)8K;|*m zhs+dd^CQahq940%krDCkmcyzmdckHP)sDC^AZY%Q2e2=oWf5m$D^%wZhUSMVNa8qHq`D)6xf!|C&>i4ES GM&ciB{ Result { let this = ctx.this_unchecked::(); let context_2d = ctx.env.unwrap::(&this)?; - let x = ctx.get::(0)?.get_double()? as f32; - let y = ctx.get::(1)?.get_double()? as f32; - - context_2d.path.line_to(x, y); + if let Ok((x, y)) = ctx.get::(0)?.get_double().and_then(|x| { + ctx + .get::(1)? + .get_double() + .map(|y| (x as f32, y as f32)) + }) { + if !x.is_nan() && !y.is_nan() { + context_2d.path.line_to(x, y); + } + } ctx.env.get_undefined() }