Skip to content

Commit

Permalink
fix: should not throw if fill/stroke style is invalid
Browse files Browse the repository at this point in the history
Close #438
  • Loading branch information
Brooooooklyn committed Mar 10, 2022
1 parent 75c7238 commit 0d12337
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
14 changes: 14 additions & 0 deletions __test__/draw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
50 changes: 25 additions & 25 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1931,27 +1931,26 @@ fn set_fill_style(ctx: CallContext) -> Result<JsUndefined> {
let context_2d = ctx.env.unwrap::<Context>(&this)?;
let js_fill_style = ctx.get::<JsUnknown>(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::<JsString>() }.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::<JsObject>() };
let pattern = ctx.env.unwrap::<Pattern>(&fill_object)?;
pattern.clone()
}
_ => {
return Err(Error::new(
Status::InvalidArg,
"Invalid fillStyle".to_string(),
))
ctx
.env
.unwrap::<Pattern>(&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()
}
Expand Down Expand Up @@ -2029,26 +2028,27 @@ fn set_stroke_style(ctx: CallContext) -> Result<JsUndefined> {
let js_stroke_style = ctx.get::<JsUnknown>(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::<JsObject>() };
let pattern = ctx.env.unwrap::<Pattern>(&stroke_object)?;
last_state.stroke_style = pattern.clone();
ctx
.env
.unwrap::<Pattern>(&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()
}
Expand Down

0 comments on commit 0d12337

Please sign in to comment.