[librsvg: 4/5] (#525): Consider specificity when applying CSS selector matches



commit c0f28bebc737481ed9bd49b11824e5b8e0373ca3
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Nov 6 18:03:27 2019 -0600

    (#525): Consider specificity when applying CSS selector matches
    
    We still don't consider ordering of rules in stylesheets, but at least
    we handle specificity for conflicting matches with this.
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/issues/525

 rsvg_internals/src/css.rs                       |   8 +++++--
 tests/fixtures/reftests/525-specificity-ref.png | Bin 0 -> 302 bytes
 tests/fixtures/reftests/525-specificity.svg     |  27 ++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index bfadeb6c..a14142a1 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -282,7 +282,7 @@ impl CssRules {
     }
 
     pub fn get_matches(&self, node_data: &NodeData) -> Vec<Selector> {
-        self.selectors_to_declarations
+        let mut matches: Vec<_> = self.selectors_to_declarations
             .iter()
             .filter_map(|(selector, _)| {
                 if self.selector_matches_node(selector, node_data) {
@@ -292,7 +292,11 @@ impl CssRules {
                 }
             })
             .map(Selector::clone)
-            .collect()
+            .collect();
+
+        matches.as_mut_slice().sort_by(|sel_a, sel_b| sel_a.specificity.cmp(&sel_b.specificity));
+
+        matches
     }
 }
 
diff --git a/tests/fixtures/reftests/525-specificity-ref.png b/tests/fixtures/reftests/525-specificity-ref.png
new file mode 100644
index 00000000..2eaa94d7
Binary files /dev/null and b/tests/fixtures/reftests/525-specificity-ref.png differ
diff --git a/tests/fixtures/reftests/525-specificity.svg b/tests/fixtures/reftests/525-specificity.svg
new file mode 100644
index 00000000..2fb1c525
--- /dev/null
+++ b/tests/fixtures/reftests/525-specificity.svg
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg version="1.1"
+     xmlns="http://www.w3.org/2000/svg";
+     xmlns:xi="http://www.w3.org/2001/XInclude";
+     width="16"
+     height="16">
+  <style type="text/css">
+    rect,circle,path {
+      fill: rgb(0,255,0) !important;
+    }
+    .warning {
+      fill: rgb(255,0,0) !important;
+    }
+    .error {
+      fill: rgb(0,255,0) !important;
+    }
+    .success {
+      fill: rgb(0,255,0) !important;
+    }
+  </style>
+  <svg xmlns="http://www.w3.org/2000/svg"; width="16" height="16">
+    <path d="M6.5 0C2.922 0 0 2.922 0 6.5S2.922 13 6.5 13c.17 0 
.333-.018.5-.031v-2.031c-.167.018-.327.062-.5.062A4.485 4.485 0 0 1 2 6.5C2 4.003 4.003 2 6.5 2S11 4.003 11 
6.5c0 .173-.044.333-.063.5h2.032c.013-.167.031-.33.031-.5C13 2.922 10.078 0 6.5 0z" 
style="line-height:normal;-inkscape-font-specification:Sans;text-indent:0;text-align:start;text-decoration-line:none;text-transform:none;marker:none"
 color="#000" font-weight="400" font-size="xx-small" font-family="Sans" overflow="visible" fill="#2e3436"/>
+    <path d="M6.492 2.992A.5.5 0 0 0 6 3.5V6H4.5a.5.5 0 1 0 0 1h2a.5.5 0 0 0 .5-.5v-3a.5.5 0 0 0-.508-.508z" 
style="line-height:normal;font-variant-ligatures:normal;font-variant-position:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-feature-settings:normal;text-indent:0;text-align:start;text-decoration-line:none;text-decoration-style:solid;text-decoration-color:#000;text-transform:none;text-orientation:mixed;shape-padding:0;isolation:auto;mix-blend-mode:normal"
 color="#000" font-weight="400" font-family="sans-serif" white-space="normal" overflow="visible" 
fill="#2e3436"/>
+    <path class="warning" d="M8.875 8.068a.861.861 0 0 0-.875.87v6.195a.86.86 0 0 0 .875.867h6.25a.86.86 0 0 
0 .875-.867V8.938a.861.861 0 0 0-.875-.87zM11 9h2v2.5s0 .5-.5.5h-1c-.5 0-.5-.5-.5-.5zm.5 4h1c.277 0 
.5.223.5.5v1c0 .277-.223.5-.5.5h-1a.499.499 0 0 1-.5-.5v-1c0-.277.223-.5.5-.5z" style="marker:none" 
color="#bebebe" overflow="visible" fill="#f57900"/>
+  </svg>
+
+</svg>
\ No newline at end of file


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