[librsvg/librsvg-2.44] (#320) - Parse XmlLang as an identifier, not a CSS string



commit 3acf5fef49c27493fc568e605f6e33944dc174a7
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Oct 8 18:24:00 2018 -0500

    (#320) - Parse XmlLang as an identifier, not a CSS string
    
    CSS strings are quoted, like "foo".  Our type for the xml:lang
    attribute was being parsed as a CSS string, so an attribute like
    
      xml:lang="es"
    
    would be parsed incorrectly, as the inner string would have had to be
    
      xml:lang="'es'"
    
    So, we parse this attribute as an identifier now.
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/issues/320

 rsvg_internals/src/parsers.rs         | 14 --------------
 rsvg_internals/src/property_macros.rs | 17 +++++++++++++++++
 rsvg_internals/src/state.rs           | 26 +++++++++++++++++++++++++-
 3 files changed, 42 insertions(+), 15 deletions(-)
---
diff --git a/rsvg_internals/src/parsers.rs b/rsvg_internals/src/parsers.rs
index 5bb1f6a0..5eae2591 100644
--- a/rsvg_internals/src/parsers.rs
+++ b/rsvg_internals/src/parsers.rs
@@ -73,20 +73,6 @@ impl Parse for f64 {
     }
 }
 
-impl Parse for String {
-    type Data = ();
-    type Err = ValueErrorKind;
-
-    fn parse(parser: &mut Parser<'_, '_>, _: ()) -> Result<String, ValueErrorKind> {
-        Ok(String::from(
-            parser
-                .expect_string()
-                .map_err(|_| ValueErrorKind::Parse(ParseError::new("expected string")))?
-                .as_ref(),
-        ))
-    }
-}
-
 /// Parses a `value` string into a type `T`.
 ///
 /// Some value types need some extra `data` to be parsed.  This
diff --git a/rsvg_internals/src/property_macros.rs b/rsvg_internals/src/property_macros.rs
index 2335e3c9..86cce6d3 100644
--- a/rsvg_internals/src/property_macros.rs
+++ b/rsvg_internals/src/property_macros.rs
@@ -144,6 +144,23 @@ macro_rules! make_property {
         $parse
     };
 
+    ($computed_values_type: ty,
+     $name: ident,
+     default: $default: expr,
+     inherits_automatically: $inherits_automatically: expr,
+     newtype: $type: ty,
+     parse_impl: { $parse: item },
+     parse_data_type: $parse_data_type: ty
+    ) => {
+        #[derive(Debug, Clone, PartialEq)]
+        pub struct $name(pub $type);
+
+        impl_default!($name, $name($default));
+        impl_property!($computed_values_type, $name, $inherits_automatically);
+
+        $parse
+    };
+
     ($computed_values_type: ty,
      $name: ident,
      inherits_automatically: $inherits_automatically: expr,
diff --git a/rsvg_internals/src/state.rs b/rsvg_internals/src/state.rs
index 9faa5bc1..753f21c7 100644
--- a/rsvg_internals/src/state.rs
+++ b/rsvg_internals/src/state.rs
@@ -1365,10 +1365,34 @@ make_property!(
     XmlLang,
     default: "".to_string(), // see create_pango_layout()
     inherits_automatically: true,
-    newtype_parse: String,
+    newtype: String,
+    parse_impl: {
+        impl Parse for XmlLang {
+            type Data = ();
+            type Err = ValueErrorKind;
+
+            fn parse(
+                parser: &mut Parser<'_, '_>,
+                _: Self::Data
+            ) -> Result<XmlLang, ValueErrorKind> {
+                Ok(XmlLang(parser.expect_ident()?.to_string()))
+            }
+        }
+    },
     parse_data_type: ()
 );
 
+#[cfg(test)]
+#[test]
+fn parses_xml_lang() {
+    assert_eq!(
+        XmlLang::parse_str("es-MX", ()).unwrap(),
+        XmlLang("es-MX".to_string())
+    );
+
+    assert!(XmlLang::parse_str("", ()).is_err());
+}
+
 make_property!(
     ComputedValues,
     XmlSpace,


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