From 3b5de8d58df221f24a6532dde0152660677f65fa Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Thu, 21 Dec 2023 21:17:22 +0000 Subject: [PATCH 1/2] Ignore CSS parsing errors rather than panicking. --- src/css.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/css.rs b/src/css.rs index 1bcee59..4345f9b 100644 --- a/src/css.rs +++ b/src/css.rs @@ -180,8 +180,13 @@ impl StyleData { /// Add some CSS source to be included. The source will be parsed /// and the relevant and supported features extracted. pub fn add_css(&mut self, css: &str) { - let ss = StyleSheet::parse(css, ParserOptions::default()).unwrap(); - html_trace!("add css [[{}]]", css); + let ss = match StyleSheet::parse(css, ParserOptions::default()) { + Ok(ss) => ss, + Err(e) => { + html_trace!("failed to parse CSS: {}, [[{}]]", e, css); + return; + } + }; for rule in &ss.rules.0 { match rule { From afaa96ff629482e1e2db99d63a88e4dee4ac6efd Mon Sep 17 00:00:00 2001 From: Chris Emerson Date: Fri, 22 Dec 2023 09:59:58 +0000 Subject: [PATCH 2/2] Take colours from fonts. --- src/css.rs | 4 ++-- src/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/css.rs b/src/css.rs index 4345f9b..d0e505c 100644 --- a/src/css.rs +++ b/src/css.rs @@ -182,8 +182,8 @@ impl StyleData { pub fn add_css(&mut self, css: &str) { let ss = match StyleSheet::parse(css, ParserOptions::default()) { Ok(ss) => ss, - Err(e) => { - html_trace!("failed to parse CSS: {}, [[{}]]", e, css); + Err(_e) => { + html_trace!("failed to parse CSS: {}, [[{}]]", _e, css); return; } }; diff --git a/src/lib.rs b/src/lib.rs index 3d8298c..215cca7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1101,6 +1101,27 @@ fn process_dom_node<'a, 'b, 'c, T: Write>( } } } + #[cfg(feature = "css")] + expanded_name!(html "font") => { + let mut colour = None; + if context.use_doc_css { + use lightningcss::traits::Parse; + let borrowed = attrs.borrow(); + for attr in borrowed.iter() { + if &attr.name.local == "color" { + colour = CssColor::parse_string(&*attr.value) + .ok() + .and_then(|c| TryFrom::try_from(&c).ok()); + break; + } + } + } + if let Some(colour) = colour { + pending(handle, move |_, cs| Ok(Some(RenderNode::new(Coloured(colour, vec![RenderNode::new(Container(cs))]))))) + } else { + pending(handle, |_, cs| Ok(Some(RenderNode::new(Container(cs))))) + } + } expanded_name!(html "a") => { let borrowed = attrs.borrow(); let mut target = None;