From 0d12337e8ed6590a4788fd7758e30a77db312577 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 10 Mar 2022 12:14:30 +0800 Subject: [PATCH] fix: should not throw if fill/stroke style is invalid Close https://github.com/Brooooooklyn/canvas/issues/438 --- __test__/draw.spec.ts | 14 ++++++++++++ src/ctx.rs | 50 +++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/__test__/draw.spec.ts b/__test__/draw.spec.ts index d807289c..b43fd953 100644 --- a/__test__/draw.spec.ts +++ b/__test__/draw.spec.ts @@ -963,6 +963,20 @@ test('shadowOffsetX', async (t) => { await snapshotImage(t) }) +test('should not throw while fill/stroke style is invalid', (t) => { + const { ctx } = t.context + t.notThrows(() => { + ctx.fillStyle = '#' + ctx.fillStyle = '#123' + // @ts-expect-error + ctx.fillStyle = {} + ctx.strokeStyle = '#' + ctx.strokeStyle = '#123' + // @ts-expect-error + ctx.strokeStyle = {} + }) +}) + test('shadowOffsetY', async (t) => { const { ctx } = t.context ctx.shadowColor = 'red' diff --git a/src/ctx.rs b/src/ctx.rs index 6d6d097f..094f56de 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -1931,27 +1931,26 @@ fn set_fill_style(ctx: CallContext) -> Result { let context_2d = ctx.env.unwrap::(&this)?; let js_fill_style = ctx.get::(0)?; - let p = match js_fill_style.get_type()? { + let pattern = match js_fill_style.get_type()? { ValueType::String => { let js_color = unsafe { js_fill_style.cast::() }.into_utf8()?; - Pattern::from_color(js_color.as_str()?)? + Pattern::from_color(js_color.as_str()?).ok() } ValueType::Object => { let fill_object = unsafe { js_fill_style.cast::() }; - let pattern = ctx.env.unwrap::(&fill_object)?; - pattern.clone() - } - _ => { - return Err(Error::new( - Status::InvalidArg, - "Invalid fillStyle".to_string(), - )) + ctx + .env + .unwrap::(&fill_object) + .ok() + .map(|p| p.clone()) } + _ => None, }; - context_2d.state.fill_style = p; - - this.set_named_property("_fillStyle", js_fill_style)?; + if let Some(p) = pattern { + context_2d.state.fill_style = p; + this.set_named_property("_fillStyle", js_fill_style)?; + } ctx.env.get_undefined() } @@ -2029,26 +2028,27 @@ fn set_stroke_style(ctx: CallContext) -> Result { let js_stroke_style = ctx.get::(0)?; let last_state = &mut context_2d.state; - match js_stroke_style.get_type()? { + let pattern = match js_stroke_style.get_type()? { ValueType::String => { let js_color = unsafe { JsString::from_raw_unchecked(ctx.env.raw(), js_stroke_style.raw()) } .into_utf8()?; - last_state.stroke_style = Pattern::from_color(js_color.as_str()?)?; + Pattern::from_color(js_color.as_str()?).ok() } ValueType::Object => { let stroke_object = unsafe { js_stroke_style.cast::() }; - let pattern = ctx.env.unwrap::(&stroke_object)?; - last_state.stroke_style = pattern.clone(); + ctx + .env + .unwrap::(&stroke_object) + .ok() + .map(|p| p.clone()) } - _ => { - return Err(Error::new( - Status::InvalidArg, - "Invalid strokeStyle".to_string(), - )) - } - } + _ => None, + }; - this.set_named_property("_strokeStyle", js_stroke_style)?; + if let Some(p) = pattern { + last_state.stroke_style = p; + this.set_named_property("_strokeStyle", js_stroke_style)?; + } ctx.env.get_undefined() }