[librsvg: 1/3] Parse the "type" attribute of the style element without cssparser



commit 30ff6a5c8965d87d2a071c4ab20f37af25038356
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Feb 13 11:18:14 2020 -0600

    Parse the "type" attribute of the style element without cssparser
    
    This is not the root cause for #558, but it caught my eye in the
    logs.  The contents of the type attribute in
    
      <style type="text/css">
    
    were not being parsed correctly; apparently cssparser.expect_ident()
    gives "text" as the first token, and "text/cssparser" is not a valid
    identifier name...
    
    ... Which is okay, I guess.  We'll parse it by hand and actually kinda
    make it match SVG2's expectations.

 rsvg_internals/src/style.rs | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)
---
diff --git a/rsvg_internals/src/style.rs b/rsvg_internals/src/style.rs
index b10a49a9..2d73afba 100644
--- a/rsvg_internals/src/style.rs
+++ b/rsvg_internals/src/style.rs
@@ -1,11 +1,9 @@
 //! The `style` element.
 
-use cssparser::Parser;
 use markup5ever::{expanded_name, local_name, namespace_url, ns};
 
 use crate::error::*;
 use crate::node::{NodeResult, NodeTrait, RsvgNode};
-use crate::parsers::{Parse, ParseValue};
 use crate::property_bag::PropertyBag;
 
 /// Represents the syntax used in the <style> node.
@@ -14,15 +12,26 @@ use crate::property_bag::PropertyBag;
 ///
 /// https://www.w3.org/TR/SVG11/styling.html#StyleElementTypeAttribute
 /// https://www.w3.org/TR/SVG11/styling.html#ContentStyleTypeAttribute
-#[derive(Copy, Clone, PartialEq)]
+#[derive(Copy, Clone, PartialEq, Debug)]
 pub enum StyleType {
     TextCss,
 }
 
-impl Parse for StyleType {
-    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<StyleType, ParseError<'i>> {
-        parser.expect_ident_matching("text/css")?;
-        Ok(StyleType::TextCss)
+impl StyleType {
+    fn parse(value: &str) -> Result<StyleType, ValueErrorKind> {
+        // https://html.spec.whatwg.org/multipage/semantics.html#the-style-element
+        //
+        // 4. If element's type attribute is present and its value is
+        // neither the empty string nor an ASCII case-insensitive
+        // match for "text/css", then return.
+
+        if value.eq_ignore_ascii_case("text/css") {
+            Ok(StyleType::TextCss)
+        } else {
+            Err(ValueErrorKind::parse_error(
+                "invalid value for type attribute in style element",
+            ))
+        }
     }
 }
 
@@ -45,10 +54,26 @@ impl NodeTrait for Style {
     fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
         for (attr, value) in pbag.iter() {
             if attr.expanded() == expanded_name!("", "type") {
-                self.type_ = Some(attr.parse(value)?);
+                self.type_ = Some(StyleType::parse(value).attribute(attr)?);
             }
         }
 
         Ok(())
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn parses_style_type() {
+        assert_eq!(StyleType::parse("text/css"), Ok(StyleType::TextCss));
+    }
+
+    #[test]
+    fn invalid_style_type_yields_error() {
+        assert!(StyleType::parse("").is_err());
+        assert!(StyleType::parse("some-other-stylesheet-language").is_err());
+    }
+}


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