[librsvg: 32/45] css.rs: Use a session for logging




commit ad2b1d3c62674204a33732b35d3de183171ebca7
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Aug 23 17:26:51 2022 -0500

    css.rs: Use a session for logging
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/731>

 src/css.rs      | 46 ++++++++++++++++++++++++++++++++--------------
 src/document.rs | 21 ++++++++++++++-------
 src/handle.rs   |  2 +-
 3 files changed, 47 insertions(+), 22 deletions(-)
---
diff --git a/src/css.rs b/src/css.rs
index e9b80bbda..2f646674e 100644
--- a/src/css.rs
+++ b/src/css.rs
@@ -93,6 +93,7 @@ use crate::error::*;
 use crate::io::{self, BinaryData};
 use crate::node::{Node, NodeBorrow, NodeCascade};
 use crate::properties::{parse_value, ComputedValues, ParseAs, ParsedProperty};
+use crate::session::Session;
 use crate::url_resolver::UrlResolver;
 
 /// A parsed CSS declaration
@@ -147,9 +148,10 @@ impl<'i> AtRuleParser<'i> for DeclParser {
     type Error = ValueErrorKind;
 }
 
-/// Dummy struct to implement cssparser::QualifiedRuleParser and
-/// cssparser::AtRuleParser
-pub struct RuleParser;
+/// Struct to implement cssparser::QualifiedRuleParser and cssparser::AtRuleParser
+pub struct RuleParser {
+    session: Session,
+}
 
 /// Errors from the CSS parsing process
 #[derive(Debug)]
@@ -283,7 +285,7 @@ impl<'i> QualifiedRuleParser<'i> for RuleParser {
             .filter_map(|r| match r {
                 Ok(decl) => Some(decl),
                 Err(e) => {
-                    rsvg_log!("Invalid declaration; ignoring: {:?}", e);
+                    rsvg_log_session!(self.session, "Invalid declaration; ignoring: {:?}", e);
                     None
                 }
             })
@@ -804,9 +806,10 @@ impl Stylesheet {
         buf: &str,
         url_resolver: &UrlResolver,
         origin: Origin,
+        session: Session,
     ) -> Result<Self, LoadingError> {
         let mut stylesheet = Stylesheet::new(origin);
-        stylesheet.parse(buf, url_resolver)?;
+        stylesheet.parse(buf, url_resolver, session)?;
         Ok(stylesheet)
     }
 
@@ -814,9 +817,10 @@ impl Stylesheet {
         href: &str,
         url_resolver: &UrlResolver,
         origin: Origin,
+        session: Session,
     ) -> Result<Self, LoadingError> {
         let mut stylesheet = Stylesheet::new(origin);
-        stylesheet.load(href, url_resolver)?;
+        stylesheet.load(href, url_resolver, session)?;
         Ok(stylesheet)
     }
 
@@ -824,22 +828,30 @@ impl Stylesheet {
     ///
     /// The `base_url` is required for `@import` rules, so that librsvg
     /// can determine if the requested path is allowed.
-    pub fn parse(&mut self, buf: &str, url_resolver: &UrlResolver) -> Result<(), LoadingError> {
+    pub fn parse(
+        &mut self,
+        buf: &str,
+        url_resolver: &UrlResolver,
+        session: Session,
+    ) -> Result<(), LoadingError> {
         let mut input = ParserInput::new(buf);
         let mut parser = Parser::new(&mut input);
+        let rule_parser = RuleParser {
+            session: session.clone(),
+        };
 
-        RuleListParser::new_for_stylesheet(&mut parser, RuleParser)
+        RuleListParser::new_for_stylesheet(&mut parser, rule_parser)
             .filter_map(|r| match r {
                 Ok(rule) => Some(rule),
                 Err(e) => {
-                    rsvg_log!("Invalid rule; ignoring: {:?}", e);
+                    rsvg_log_session!(session, "Invalid rule; ignoring: {:?}", e);
                     None
                 }
             })
             .for_each(|rule| match rule {
                 Rule::AtRule(AtRule::Import(url)) => {
                     // ignore invalid imports
-                    let _ = self.load(&url, url_resolver);
+                    let _ = self.load(&url, url_resolver, session.clone());
                 }
                 Rule::QualifiedRule(qr) => self.qualified_rules.push(qr),
             });
@@ -848,7 +860,12 @@ impl Stylesheet {
     }
 
     /// Parses a stylesheet referenced by an URL
-    fn load(&mut self, href: &str, url_resolver: &UrlResolver) -> Result<(), LoadingError> {
+    fn load(
+        &mut self,
+        href: &str,
+        url_resolver: &UrlResolver,
+        session: Session,
+    ) -> Result<(), LoadingError> {
         let aurl = url_resolver
             .resolve_href(href)
             .map_err(|_| LoadingError::BadUrl)?;
@@ -864,20 +881,21 @@ impl Stylesheet {
                 if is_text_css(&mime_type) {
                     Ok(bytes)
                 } else {
-                    rsvg_log!("\"{}\" is not of type text/css; ignoring", aurl);
+                    rsvg_log_session!(session, "\"{}\" is not of type text/css; ignoring", aurl);
                     Err(LoadingError::BadCss)
                 }
             })
             .and_then(|bytes| {
                 String::from_utf8(bytes).map_err(|_| {
-                    rsvg_log!(
+                    rsvg_log_session!(
+                        session,
                         "\"{}\" does not contain valid UTF-8 CSS data; ignoring",
                         aurl
                     );
                     LoadingError::BadCss
                 })
             })
-            .and_then(|utf8| self.parse(&utf8, &UrlResolver::new(Some((*aurl).clone()))))
+            .and_then(|utf8| self.parse(&utf8, &UrlResolver::new(Some((*aurl).clone())), session))
     }
 
     /// Appends the style declarations that match a specified node to a given vector
diff --git a/src/document.rs b/src/document.rs
index fb9071ef2..1a7650c11 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -28,8 +28,9 @@ static UA_STYLESHEETS: Lazy<Vec<Stylesheet>> = Lazy::new(|| {
         include_str!("ua.css"),
         &UrlResolver::new(None),
         Origin::UserAgent,
+        Session::default(),
     )
-    .unwrap()]
+    .expect("could not parse user agent stylesheet for librsvg, there's a bug!")]
 });
 
 /// Identifier of a node
@@ -523,9 +524,12 @@ impl DocumentBuilder {
         }
 
         // FIXME: handle CSS errors
-        if let Ok(stylesheet) =
-            Stylesheet::from_href(href, &self.load_options.url_resolver, Origin::Author)
-        {
+        if let Ok(stylesheet) = Stylesheet::from_href(
+            href,
+            &self.load_options.url_resolver,
+            Origin::Author,
+            self.session.clone(),
+        ) {
             self.stylesheets.push(stylesheet);
         }
 
@@ -560,9 +564,12 @@ impl DocumentBuilder {
 
     pub fn append_stylesheet_from_text(&mut self, text: &str) {
         // FIXME: handle CSS errors
-        if let Ok(stylesheet) =
-            Stylesheet::from_data(text, &self.load_options.url_resolver, Origin::Author)
-        {
+        if let Ok(stylesheet) = Stylesheet::from_data(
+            text,
+            &self.load_options.url_resolver,
+            Origin::Author,
+            self.session.clone(),
+        ) {
             self.stylesheets.push(stylesheet);
         }
     }
diff --git a/src/handle.rs b/src/handle.rs
index 8dec2a880..6062d3f13 100644
--- a/src/handle.rs
+++ b/src/handle.rs
@@ -377,7 +377,7 @@ impl Handle {
 
     pub fn set_stylesheet(&mut self, css: &str) -> Result<(), LoadingError> {
         let mut stylesheet = Stylesheet::new(Origin::User);
-        stylesheet.parse(css, &UrlResolver::new(None))?;
+        stylesheet.parse(css, &UrlResolver::new(None), self.session.clone())?;
         self.document.cascade(&[stylesheet]);
         Ok(())
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]