[librsvg: 5/17] Stylesheet::from_href - take an AllowedUrl instead of validating it here




commit b1d6ddccebb36359ae01461a6bb9af464f9b6069
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Aug 29 14:05:20 2022 -0500

    Stylesheet::from_href - take an AllowedUrl instead of validating it here
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/738>

 src/css.rs      | 28 ++++++++++++++++------------
 src/document.rs | 14 +++++++-------
 2 files changed, 23 insertions(+), 19 deletions(-)
---
diff --git a/src/css.rs b/src/css.rs
index 8ef48e237..e02acbe47 100644
--- a/src/css.rs
+++ b/src/css.rs
@@ -750,7 +750,7 @@ pub enum Origin {
     Author,
 }
 
-/// A parsed CSS stylesheet
+/// A parsed CSS stylesheet.
 pub struct Stylesheet {
     origin: Origin,
     qualified_rules: Vec<QualifiedRule>,
@@ -802,6 +802,10 @@ impl Stylesheet {
         }
     }
 
+    /// Parses a new stylesheet from CSS data in a string.
+    ///
+    /// The `url_resolver_url` is required for `@import` rules, so that librsvg can determine if
+    /// the requested path is allowed.
     pub fn from_data(
         buf: &str,
         url_resolver: &UrlResolver,
@@ -809,29 +813,29 @@ impl Stylesheet {
         session: Session,
     ) -> Result<Self, LoadingError> {
         let mut stylesheet = Stylesheet::empty(origin);
-        stylesheet.parse(buf, url_resolver, session)?;
+        stylesheet.add_rules_from_string(buf, url_resolver, session)?;
         Ok(stylesheet)
     }
 
+    /// Parses a new stylesheet by loading CSS data from a URL.
     pub fn from_href(
-        href: &str,
-        url_resolver: &UrlResolver,
+        aurl: &AllowedUrl,
         origin: Origin,
         session: Session,
     ) -> Result<Self, LoadingError> {
         let mut stylesheet = Stylesheet::empty(origin);
-        let aurl = url_resolver
-            .resolve_href(href)
-            .map_err(|_| LoadingError::BadUrl)?;
         stylesheet.load(&aurl, session)?;
         Ok(stylesheet)
     }
 
-    /// Parses a CSS stylesheet from a string
+    /// Parses the CSS rules in `buf` and appends them to the stylesheet.
+    ///
+    /// The `url_resolver_url` is required for `@import` rules, so that librsvg can determine if
+    /// the requested path is allowed.
     ///
-    /// The `base_url` is required for `@import` rules, so that librsvg
-    /// can determine if the requested path is allowed.
-    fn parse(
+    /// If there is an `@import` rule, its rules will be recursively added into the
+    /// stylesheet, in the order in which they appear.
+    fn add_rules_from_string(
         &mut self,
         buf: &str,
         url_resolver: &UrlResolver,
@@ -898,7 +902,7 @@ impl Stylesheet {
             })
             .and_then(|utf8| {
                 let url = (**aurl).clone();
-                self.parse(&utf8, &UrlResolver::new(Some(url)), session)
+                self.add_rules_from_string(&utf8, &UrlResolver::new(Some(url)), session)
             })
     }
 
diff --git a/src/document.rs b/src/document.rs
index a297477be..d392adef5 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -549,13 +549,13 @@ impl DocumentBuilder {
             )));
         }
 
-        // FIXME: handle CSS errors
-        if let Ok(stylesheet) = Stylesheet::from_href(
-            href,
-            &self.load_options.url_resolver,
-            Origin::Author,
-            self.session.clone(),
-        ) {
+        let aurl = self
+            .load_options
+            .url_resolver
+            .resolve_href(href)
+            .map_err(|_| LoadingError::BadUrl)?;
+
+        if let Ok(stylesheet) = Stylesheet::from_href(&aurl, Origin::Author, self.session.clone()) {
             self.stylesheets.push(stylesheet);
         }
 


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