[librsvg: 10/26] Load the stylesheet directly in append_stylesheet_from_xml_processing_instruction



commit 6f7e6b82a62ef8a3142005a0d8c9bb6c2623c6a5
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Nov 7 19:16:08 2019 -0600

    Load the stylesheet directly in append_stylesheet_from_xml_processing_instruction
    
    Don't queue it for later.  This will change in the next commits.
    
    This removes the Stylesheet struct, which was just a temporary holding
    place for the xml-stylesheet's pseudo-attributes.

 rsvg_internals/src/document.rs | 41 +++++++++++++----------------------------
 rsvg_internals/src/xml.rs      | 20 +++++++++++++++-----
 2 files changed, 28 insertions(+), 33 deletions(-)
---
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index 94790d7c..c8bbd500 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -208,18 +208,11 @@ fn load_image(
     Ok(surface)
 }
 
-struct Stylesheet {
-    alternate: Option<String>,
-    type_: Option<String>,
-    href: Option<String>,
-}
-
 pub struct DocumentBuilder {
     load_options: LoadOptions,
     tree: Option<RsvgNode>,
     ids: HashMap<String, RsvgNode>,
     inline_css: String,
-    stylesheets: Vec<Stylesheet>,
     css_rules: CssRules,
 }
 
@@ -230,7 +223,6 @@ impl DocumentBuilder {
             tree: None,
             ids: HashMap::new(),
             inline_css: String::new(),
-            stylesheets: Vec::new(),
             css_rules: CssRules::default(),
         }
     }
@@ -239,13 +231,18 @@ impl DocumentBuilder {
         &mut self,
         alternate: Option<String>,
         type_: Option<String>,
-        href: Option<String>,
-    ) {
-        self.stylesheets.push(Stylesheet {
-            alternate,
-            type_,
-            href,
-        });
+        href: &str,
+    ) -> Result<(), LoadingError> {
+        if type_.as_ref().map(String::as_str) != Some("text/css")
+            || (alternate.is_some() && alternate.as_ref().map(String::as_str) != Some("no"))
+        {
+            return Err(LoadingError::BadStylesheet);
+        }
+
+        // FIXME: handle CSS errors
+        let _ = self.css_rules.load_css(&self.resolve_href(href)?);
+
+        Ok(())
     }
 
     pub fn append_element(
@@ -331,24 +328,12 @@ impl DocumentBuilder {
         chars_node.borrow().get_impl::<NodeChars>().append(text);
     }
 
-    pub fn resolve_href(&self, href: &str) -> Result<(AllowedUrl), LoadingError> {
+    pub fn resolve_href(&self, href: &str) -> Result<AllowedUrl, LoadingError> {
         AllowedUrl::from_href(href, self.load_options.base_url.as_ref())
             .map_err(|_| LoadingError::BadUrl)
     }
 
     pub fn build(mut self) -> Result<Document, LoadingError> {
-        for s in self.stylesheets.iter() {
-            if s.type_.as_ref().map(String::as_str) != Some("text/css")
-                || (s.alternate.is_some() && s.alternate.as_ref().map(String::as_str) != Some("no"))
-                || s.href.is_none()
-            {
-                return Err(LoadingError::BadStylesheet);
-            }
-
-            // FIXME: handle CSS errors
-            let _ = self.css_rules.load_css(&self.resolve_href(s.href.as_ref().unwrap())?);
-        }
-
         self.css_rules.parse(self.load_options.base_url.as_ref(), &self.inline_css);
 
         let DocumentBuilder { load_options, tree, ids, css_rules, .. } = self;
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index d6289994..3cc85972 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -230,11 +230,21 @@ impl XmlState {
             }
 
             let mut inner = self.inner.borrow_mut();
-            inner
-                .document_builder
-                .as_mut()
-                .unwrap()
-                .append_stylesheet_from_xml_processing_instruction(alternate, type_, href);
+
+            if let Some(href) = href {
+                // FIXME: https://www.w3.org/TR/xml-stylesheet/ does not seem to specify
+                // what to do if the stylesheet cannot be loaded, so here we ignore the error.
+                if let Err(_) = inner
+                    .document_builder
+                    .as_mut()
+                    .unwrap()
+                    .append_stylesheet_from_xml_processing_instruction(alternate, type_, &href)
+                {
+                    rsvg_log!("invalid xml-stylesheet {} in XML processing instruction", href);
+                }
+            } else {
+                rsvg_log!("xml-stylesheet processing instruction does not have href; ignoring");
+            }
         } else {
             self.error(ParseFromStreamError::XmlParseError(String::from(
                 "invalid processing instruction data in xml-stylesheet",


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