Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Feb 28, 2024
1 parent a378fc0 commit 8cb6d72
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
4 changes: 3 additions & 1 deletion crates/gosub_css3/src/convert/ast_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions crates/gosub_styling/src/calculator.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -438,38 +437,42 @@ impl ValueEntry {

/// Map of all declared values for a single node
pub struct CssMapEntry {
properties: HashMap<String, ValueEntry>
properties: HashMap<String, ValueEntry>,
}

impl CssMapEntry {
pub fn new() -> Self {
Self {
properties: HashMap::new()
properties: HashMap::new(),
}
}

// @todo: This should not be here. When we resolve a property, we should also resolve the value so we can
// use entry.actual or something
pub fn get_color_value(&self, prop_name: &str) -> Option<RgbColor> {
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<NodeId, CssMapEntry>
nodes: HashMap<NodeId, CssMapEntry>,
}

impl CssMap {
fn new() -> Self {
Self {
nodes: HashMap::new()
nodes: HashMap::new(),
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions crates/gosub_styling/src/css_colors.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(") {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -697,7 +697,6 @@ lazy_static! {
];
}


#[cfg(test)]
mod tests {
#[test]
Expand Down Expand Up @@ -798,4 +797,4 @@ mod tests {
assert_eq!(color.b, 0);
assert_eq!(color.a, 255);
}
}
}

0 comments on commit 8cb6d72

Please sign in to comment.