[librsvg: 5/14] Consider specificity during matching



commit b0885e410339e5a0b38a4b9126deb6be5f5d1c9a
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Nov 11 15:02:18 2019 -0600

    Consider specificity during matching

 rsvg_internals/src/css.rs                   |  13 ++++++++-----
 tests/fixtures/reftests/specificity-ref.png | Bin 0 -> 89 bytes
 tests/fixtures/reftests/specificity.svg     |  25 +++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 5 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index a7617a5e..ac3eaa59 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -651,12 +651,15 @@ struct Match<'a> {
 /// Runs the CSS cascade on the specified tree from all the stylesheets
 pub fn cascade(root: &mut RsvgNode, stylesheets: &[Stylesheet]) {
     for mut node in root.descendants() {
+        let mut matches = Vec::new();
         for stylesheet in stylesheets {
-            let mut decls = Vec::new();
-            stylesheet.get_matches(&node, &mut decls);
-            for decl in decls {
-                node.borrow_mut().apply_style_declaration(decl.declaration);
-            }
+            stylesheet.get_matches(&node, &mut matches);
+        }
+
+        matches.as_mut_slice().sort_by(|a, b| a.specificity.cmp(&b.specificity));
+
+        for m in matches {
+            node.borrow_mut().apply_style_declaration(m.declaration);
         }
 
         node.borrow_mut().set_style_attribute();
diff --git a/tests/fixtures/reftests/specificity-ref.png b/tests/fixtures/reftests/specificity-ref.png
new file mode 100644
index 00000000..c7119981
Binary files /dev/null and b/tests/fixtures/reftests/specificity-ref.png differ
diff --git a/tests/fixtures/reftests/specificity.svg b/tests/fixtures/reftests/specificity.svg
new file mode 100644
index 00000000..c9623ce2
--- /dev/null
+++ b/tests/fixtures/reftests/specificity.svg
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<svg version="1.1" xmlns="http://www.w3.org/2000/svg"; width="32" height="16">
+  <!-- We have two stylesheets - test that they are both considered.
+       Also, test, that specificity and ordering are handled by having
+       multiple rules that match each element.
+  -->
+  <style type="text/css">
+    .foo {
+      fill: limegreen;
+    }
+    .bar {
+      fill: red;
+    }
+  </style>
+  <rect x="0" y="0" width="16" height="16" class="foo"/>
+  <rect x="16" y="0" width="16" height="16" class="bar"/>
+  <style type="text/css">
+    rect {
+      fill: red;
+    }
+    .bar {
+      fill: limegreen;
+    }
+  </style>
+</svg>


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