[librsvg: 8/14] Store a Stylesheet's origin (UserAgent, User, Author)



commit d4599a4fe96b615600117b407e3462308a93918a
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Nov 11 15:26:14 2019 -0600

    Store a Stylesheet's origin (UserAgent, User, Author)
    
    This should make it possible to later add an API for setting
    application-side stylesheets (with Origin::User).

 rsvg_internals/src/css.rs      | 34 +++++++++++++++++++++++++++++-----
 rsvg_internals/src/document.rs | 10 +++++++---
 2 files changed, 36 insertions(+), 8 deletions(-)
---
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index 8e5268c5..ccae1c8e 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -539,21 +539,45 @@ impl selectors::Element for RsvgElement {
     }
 }
 
+/// Origin for a stylesheet, per https://www.w3.org/TR/CSS22/cascade.html#cascading-order
+///
+/// This is used when sorting selector matches according to their origin and specificity.
+pub enum Origin {
+    UserAgent,
+    User,
+    Author,
+}
+
 /// A parsed CSS stylesheet
-#[derive(Default)]
 pub struct Stylesheet {
+    origin: Origin,
     qualified_rules: Vec<QualifiedRule>,
 }
 
 impl Stylesheet {
-    pub fn from_data(buf: &str, base_url: Option<&Url>) -> Result<Self, LoadingError> {
-        let mut stylesheet = Stylesheet::default();
+    pub fn new(origin: Origin) -> Stylesheet {
+        Stylesheet {
+            origin,
+            qualified_rules: Vec::new(),
+        }
+    }
+
+    pub fn from_data(
+        buf: &str,
+        base_url: Option<&Url>,
+        origin: Origin,
+    ) -> Result<Self, LoadingError> {
+        let mut stylesheet = Stylesheet::new(origin);
         stylesheet.parse(buf, base_url)?;
         Ok(stylesheet)
     }
 
-    pub fn from_href(href: &str, base_url: Option<&Url>) -> Result<Self, LoadingError> {
-        let mut stylesheet = Stylesheet::default();
+    pub fn from_href(
+        href: &str,
+        base_url: Option<&Url>,
+        origin: Origin,
+    ) -> Result<Self, LoadingError> {
+        let mut stylesheet = Stylesheet::new(origin);
         stylesheet.load(href, base_url)?;
         Ok(stylesheet)
     }
diff --git a/rsvg_internals/src/document.rs b/rsvg_internals/src/document.rs
index 3921f457..d709f9b7 100644
--- a/rsvg_internals/src/document.rs
+++ b/rsvg_internals/src/document.rs
@@ -8,7 +8,7 @@ use std::rc::Rc;
 
 use crate::allowed_url::{AllowedUrl, AllowedUrlError, Fragment};
 use crate::create_node::create_node;
-use crate::css::{cascade, Stylesheet};
+use crate::css::{cascade, Origin, Stylesheet};
 use crate::error::LoadingError;
 use crate::handle::LoadOptions;
 use crate::io::{self, BinaryData};
@@ -236,7 +236,9 @@ impl DocumentBuilder {
         }
 
         // FIXME: handle CSS errors
-        if let Ok(stylesheet) = Stylesheet::from_href(href, self.load_options.base_url.as_ref()) {
+        if let Ok(stylesheet) =
+            Stylesheet::from_href(href, self.load_options.base_url.as_ref(), Origin::Author)
+        {
             self.stylesheets.push(stylesheet);
         }
 
@@ -274,7 +276,9 @@ impl DocumentBuilder {
 
     pub fn append_stylesheet_from_text(&mut self, text: &str) {
         // FIXME: handle CSS errors
-        if let Ok(stylesheet) = Stylesheet::from_data(text, self.load_options.base_url.as_ref()) {
+        if let Ok(stylesheet) =
+            Stylesheet::from_data(text, self.load_options.base_url.as_ref(), Origin::Author)
+        {
             self.stylesheets.push(stylesheet);
         }
     }


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