From 8cb6d726c38e2c4ba7bd5a1f58c6580e4e63c0dc Mon Sep 17 00:00:00 2001 From: Joshua Thijssen Date: Wed, 28 Feb 2024 10:23:21 +0100 Subject: [PATCH] fixed --- .../gosub_css3/src/convert/ast_converter.rs | 4 ++- crates/gosub_styling/src/calculator.rs | 27 ++++++++++--------- crates/gosub_styling/src/css_colors.rs | 11 ++++---- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/crates/gosub_css3/src/convert/ast_converter.rs b/crates/gosub_css3/src/convert/ast_converter.rs index 486b312a8..1116776b5 100644 --- a/crates/gosub_css3/src/convert/ast_converter.rs +++ b/crates/gosub_css3/src/convert/ast_converter.rs @@ -140,7 +140,9 @@ pub fn convert_ast_to_stylesheet( value: value.clone(), ..Default::default() }, - NodeType::AttributeSelector { name, value, flags, .. } => CssSelectorPart { + NodeType::AttributeSelector { + name, value, flags, .. + } => CssSelectorPart { type_: CssSelectorType::Attribute, name: name.clone(), matcher: MatcherType::Equals, // @todo: this needs to be parsed diff --git a/crates/gosub_styling/src/calculator.rs b/crates/gosub_styling/src/calculator.rs index 25b65fe8c..b30e04ed1 100644 --- a/crates/gosub_styling/src/calculator.rs +++ b/crates/gosub_styling/src/calculator.rs @@ -1,3 +1,4 @@ +use crate::css_colors::RgbColor; use core::fmt::Debug; use gosub_css3::convert::ast_converter::convert_ast_to_stylesheet; use gosub_css3::parser_config::ParserConfig; @@ -11,7 +12,6 @@ use gosub_html5::parser::document::{Document, DocumentHandle, TreeIterator}; use std::cmp::Ordering; use std::collections::HashMap; use std::fs; -use crate::css_colors::RgbColor; /// Style calculator will generate a declared values map for all nodes in the document based on the stylesheets given pub struct StyleCalculator { @@ -129,7 +129,6 @@ impl StyleCalculator { pub fn find_specified_values(&mut self) { for (_, css_map_entry) in self.css_map.nodes.iter_mut() { for (_, entry) in css_map_entry.properties.iter_mut() { - match entry.cascaded { Some(ref cascaded) => { entry.specified = cascaded.clone(); @@ -438,13 +437,13 @@ impl ValueEntry { /// Map of all declared values for a single node pub struct CssMapEntry { - properties: HashMap + properties: HashMap, } impl CssMapEntry { pub fn new() -> Self { Self { - properties: HashMap::new() + properties: HashMap::new(), } } @@ -452,24 +451,28 @@ impl CssMapEntry { // use entry.actual or something pub fn get_color_value(&self, prop_name: &str) -> Option { let prop_name = prop_name.to_lowercase(); - match self.properties.get(&prop_name) { - Some(entry) => { - Some(RgbColor::from(entry.specified.as_ref())) - } - None => None - } + + self.properties + .get(&prop_name) + .map(|entry| RgbColor::from(entry.specified.as_ref())) + } +} + +impl Default for CssMapEntry { + fn default() -> Self { + Self::new() } } /// Map of all declared values for all nodes in the document pub struct CssMap { - nodes: HashMap + nodes: HashMap, } impl CssMap { fn new() -> Self { Self { - nodes: HashMap::new() + nodes: HashMap::new(), } } } diff --git a/crates/gosub_styling/src/css_colors.rs b/crates/gosub_styling/src/css_colors.rs index 1f4fd9e03..5ce94d9a3 100644 --- a/crates/gosub_styling/src/css_colors.rs +++ b/crates/gosub_styling/src/css_colors.rs @@ -1,6 +1,6 @@ use lazy_static::lazy_static; -use std::convert::From; use regex::Regex; +use std::convert::From; // Values for this table is taken from https://www.w3.org/TR/CSS21/propidx.html // Probably not the complete list, but it will do for now @@ -40,10 +40,10 @@ impl Default for RgbColor { impl From<&str> for RgbColor { fn from(value: &str) -> Self { - if value.len() == 0 { + if value.is_empty() { return RgbColor::default(); } - if value.starts_with("#") { + if value.starts_with('#') { return parse_hex(value); } if value.starts_with("rgb(") { @@ -56,7 +56,7 @@ impl From<&str> for RgbColor { // HSL function } - return get_hex_color_from_name(value).map_or(RgbColor::default(), |hex| parse_hex(hex)); + return get_hex_color_from_name(value).map_or(RgbColor::default(), parse_hex); } } @@ -697,7 +697,6 @@ lazy_static! { ]; } - #[cfg(test)] mod tests { #[test] @@ -798,4 +797,4 @@ mod tests { assert_eq!(color.b, 0); assert_eq!(color.a, 255); } -} \ No newline at end of file +}