[librsvg: 14/26] DocumentBuilder: store a list of stylesheets, not a single one where we accumulate rules



commit 6e10007689a7aba165139b6ccfc88bf30a86f406
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Nov 8 14:12:02 2019 -0600

    DocumentBuilder: store a list of stylesheets, not a single one where we accumulate rules
    
    Each stylesheet in the list is applied/matched in turn to each
    element, and finally the element's own style attribute is set.  This
    requires splitting NodeData.set_style() into its components
    set_css_styles() and set_style_attribute().

 rsvg_internals/src/document.rs | 29 ++++++++++++++++++-----------
 rsvg_internals/src/node.rs     | 11 ++---------
 2 files changed, 20 insertions(+), 20 deletions(-)
---
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index 81af4708..3d80f3b5 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -211,8 +211,7 @@ pub struct DocumentBuilder {
     load_options: LoadOptions,
     tree: Option<RsvgNode>,
     ids: HashMap<String, RsvgNode>,
-    inline_css: String,
-    stylesheet: Stylesheet,
+    stylesheets: Vec<Stylesheet>,
 }
 
 impl DocumentBuilder {
@@ -221,8 +220,7 @@ impl DocumentBuilder {
             load_options: load_options.clone(),
             tree: None,
             ids: HashMap::new(),
-            inline_css: String::new(),
-            stylesheet: Stylesheet::default(),
+            stylesheets: Vec::new(),
         }
     }
 
@@ -239,7 +237,10 @@ impl DocumentBuilder {
         }
 
         // FIXME: handle CSS errors
-        let _ = self.stylesheet.load_css(&self.resolve_href(href)?);
+        let mut stylesheet = Stylesheet::default();
+        let _ = stylesheet.load_css(&self.resolve_href(href)?);
+
+        self.stylesheets.push(stylesheet);
 
         Ok(())
     }
@@ -274,7 +275,9 @@ impl DocumentBuilder {
     }
 
     pub fn append_stylesheet_from_text(&mut self, text: &str) {
-        self.inline_css.push_str(text);
+        let mut stylesheet = Stylesheet::default();
+        stylesheet.parse(self.load_options.base_url.as_ref(), text);
+        self.stylesheets.push(stylesheet);
     }
 
     pub fn append_characters(&mut self, text: &str, parent: &mut RsvgNode) {
@@ -317,17 +320,21 @@ impl DocumentBuilder {
             .map_err(|_| LoadingError::BadUrl)
     }
 
-    pub fn build(mut self) -> Result<Document, LoadingError> {
-        self.stylesheet.parse(self.load_options.base_url.as_ref(), &self.inline_css);
-
-        let DocumentBuilder { load_options, tree, ids, stylesheet, .. } = self;
+    pub fn build(self) -> Result<Document, LoadingError> {
+        let DocumentBuilder { load_options, tree, ids, stylesheets, .. } = self;
 
         match tree {
             None => Err(LoadingError::SvgHasNoElements),
             Some(mut root) => {
                 if root.borrow().get_type() == NodeType::Svg {
                     for mut node in root.descendants() {
-                        node.borrow_mut().set_style(&stylesheet);
+                        let mut node_borrow = node.borrow_mut();
+
+                        for stylesheet in &stylesheets {
+                            node_borrow.set_css_styles(stylesheet);
+                        }
+
+                        node_borrow.set_style_attribute();
                     }
 
                     let values = ComputedValues::default();
diff --git a/rsvg_internals/src/node.rs b/rsvg_internals/src/node.rs
index a2727f62..be6c8502 100644
--- a/rsvg_internals/src/node.rs
+++ b/rsvg_internals/src/node.rs
@@ -210,7 +210,7 @@ impl NodeData {
     }
 
     /// Applies the CSS rules that match into the node's specified_values
-    fn set_css_styles(&mut self, stylesheet: &Stylesheet) {
+    pub fn set_css_styles(&mut self, stylesheet: &Stylesheet) {
         for selector in &stylesheet.get_matches(self) {
             if let Some(decl_list) = stylesheet.get_declarations(selector) {
                 for declaration in decl_list.iter() {
@@ -222,7 +222,7 @@ impl NodeData {
     }
 
     /// Applies CSS styles from the saved value of the "style" attribute
-    fn set_style_attribute(&mut self) {
+    pub fn set_style_attribute(&mut self) {
         if !self.style_attr.is_empty() {
             if let Err(e) = self
                 .specified_values
@@ -236,13 +236,6 @@ impl NodeData {
         }
     }
 
-    // Sets the node's specified values from the style-related attributes in the pbag.
-    // Also applies CSS rules in our limited way based on the node's tag/class/id.
-    pub fn set_style(&mut self, stylesheet: &Stylesheet) {
-        self.set_css_styles(stylesheet);
-        self.set_style_attribute();
-    }
-
     fn set_error(&mut self, error: NodeError) {
         self.result = Err(error);
     }


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