[librsvg: 46/51] filters/input.rs: Implement the CSS custom-ident type



commit 113dd7911d26118ddd0131e98d8d699b55165f34
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Dec 18 11:25:09 2019 -0600

    filters/input.rs: Implement the CSS custom-ident type

 rsvg_internals/src/filters/input.rs | 51 +++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
---
diff --git a/rsvg_internals/src/filters/input.rs b/rsvg_internals/src/filters/input.rs
index 3ffbd31e..8ced997e 100644
--- a/rsvg_internals/src/filters/input.rs
+++ b/rsvg_internals/src/filters/input.rs
@@ -1,6 +1,8 @@
+use cssparser::{Parser, Token};
 use markup5ever::QualName;
 
 use crate::error::*;
+use crate::parsers::Parse;
 
 /// An enumeration of possible inputs for a filter primitive.
 #[derive(Debug, Clone, Eq, PartialEq, Hash)]
@@ -14,6 +16,10 @@ pub enum Input {
     FilterOutput(String),
 }
 
+/// https://www.w3.org/TR/css-values-4/#custom-idents
+#[derive(Debug, PartialEq)]
+pub struct CustomIdent(String);
+
 impl Input {
     pub fn parse(attr: QualName, s: &str) -> Result<Input, NodeError> {
         match s {
@@ -28,3 +34,48 @@ impl Input {
         }
     }
 }
+
+impl Parse for CustomIdent {
+    fn parse(parser: &mut Parser<'_, '_>) -> Result<Self, ValueErrorKind> {
+        let loc = parser.current_source_location();
+        let token = parser.next()?;
+
+        match token {
+            // CSS-wide keywords and "default" are errors here
+            // https://www.w3.org/TR/css-values-4/#css-wide-keywords
+            Token::Ident(ref cow) => {
+                for s in &["initial", "inherit", "unset", "default"] {
+                    if cow.eq_ignore_ascii_case(s) {
+                        Err(loc.new_basic_unexpected_token_error(token.clone()))?
+                    }
+                }
+
+                Ok(CustomIdent(cow.as_ref().to_string()))
+            }
+
+            _ => Err(loc.new_basic_unexpected_token_error(token.clone()))?,
+        }
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn parses_custom_ident() {
+        assert_eq!(
+            CustomIdent::parse_str("hello"),
+            Ok(CustomIdent("hello".to_string()))
+        );
+    }
+
+    #[test]
+    fn invalid_custom_ident_yields_error() {
+        assert!(CustomIdent::parse_str("initial").is_err());
+        assert!(CustomIdent::parse_str("inherit").is_err());
+        assert!(CustomIdent::parse_str("unset").is_err());
+        assert!(CustomIdent::parse_str("default").is_err());
+        assert!(CustomIdent::parse_str("").is_err());
+    }
+}


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