diff --git a/src/css_gradient.rs b/src/css_gradient.rs index 90d6edb..fea93ec 100644 --- a/src/css_gradient.rs +++ b/src/css_gradient.rs @@ -1,5 +1,6 @@ use crate::{BlendMode, Color}; +#[allow(clippy::question_mark)] pub(crate) fn parse(s: &str, mode: BlendMode) -> Option<(Vec, Vec)> { let mut stops = Vec::new(); @@ -13,7 +14,9 @@ pub(crate) fn parse(s: &str, mode: BlendMode) -> Option<(Vec, Vec)> return None; } - stops[0].0.as_ref()?; + if stops[0].0.is_none() { + return None; + } for i in 0..stops.len() { if i == 0 && stops[i].1.is_none() { @@ -25,10 +28,13 @@ pub(crate) fn parse(s: &str, mode: BlendMode) -> Option<(Vec, Vec)> if stops[i].1.is_none() { stops[i].1 = Some(1.0); } - continue; + break; } if stops[i].0.is_none() { + if stops[i + 1].0.is_none() { + return None; + } let col1 = stops[i - 1].0.as_ref().unwrap(); let col2 = stops[i + 1].0.as_ref().unwrap(); let col = match mode { @@ -66,6 +72,12 @@ pub(crate) fn parse(s: &str, mode: BlendMode) -> Option<(Vec, Vec)> } } + for (col, pos) in &stops { + if col.is_none() || pos.is_none() { + return None; + } + } + let colors = stops .iter() .map(|(c, _)| c.clone().unwrap()) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 8bfe5f3..b76e069 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -224,6 +224,19 @@ fn css_gradient() { assert_eq!(g.at(pos).to_rgba8(), rgba8); } } + + let invalid_css = [ + "", + " ", + "0, red, lime", + "red, lime, 100%", + "deeppink, 0.4, 0.9, pink", + "0%, 100%", + ]; + for s in invalid_css { + let g = GradientBuilder::new().css(s).build::(); + assert!(g.is_err()); + } } #[test]