[librsvg: 4/8] (#792): Distinguish between properties and presentation attributes when parsing




commit b1c8a8ed778922848932f9618bd20d1193e23bb8
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Sep 17 18:32:09 2021 -0500

    (#792): Distinguish between properties and presentation attributes when parsing
    
    Not all properties have presentation attributes, and now we can
    distinguish between them.
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/-/issues/792
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/591>

 src/css.rs        |  4 ++--
 src/properties.rs | 26 +++++++++++++++++++-------
 2 files changed, 21 insertions(+), 9 deletions(-)
---
diff --git a/src/css.rs b/src/css.rs
index bcffd335..07d56c66 100644
--- a/src/css.rs
+++ b/src/css.rs
@@ -90,7 +90,7 @@ use std::str;
 use crate::error::*;
 use crate::io::{self, BinaryData};
 use crate::node::{Node, NodeBorrow, NodeCascade};
-use crate::properties::{parse_property, ComputedValues, ParsedProperty};
+use crate::properties::{parse_property, ComputedValues, ParseAs, ParsedProperty};
 use crate::url_resolver::UrlResolver;
 
 /// A parsed CSS declaration
@@ -122,7 +122,7 @@ impl<'i> DeclarationParser<'i> for DeclParser {
         input: &mut Parser<'i, 't>,
     ) -> Result<Declaration, ParseError<'i>> {
         let prop_name = QualName::new(None, ns!(), LocalName::from(name.as_ref()));
-        let property = parse_property(&prop_name, input, true)?;
+        let property = parse_property(&prop_name, input, ParseAs::Property)?;
 
         let important = input.try_parse(parse_important).is_ok();
 
diff --git a/src/properties.rs b/src/properties.rs
index b91af177..e593bba1 100644
--- a/src/properties.rs
+++ b/src/properties.rs
@@ -85,6 +85,13 @@ enum PresentationAttr {
     Yes,
 }
 
+/// How to parse a value, whether it comes from a property or from a presentation attribute
+#[derive(PartialEq)]
+pub enum ParseAs {
+    Property,
+    PresentationAttr,
+}
+
 impl PropertyId {
     fn as_u8(&self) -> u8 {
         *self as u8
@@ -344,25 +351,30 @@ macro_rules! make_properties {
         pub fn parse_property<'i>(
             prop_name: &QualName,
             input: &mut Parser<'i, '_>,
-            accept_shorthands: bool
+            parse_as: ParseAs,
         ) -> Result<ParsedProperty, ParseError<'i>> {
             match prop_name.expanded() {
                 $(
-                    expanded_name!("", $long_str) =>
-                        Ok(ParsedProperty::$long_name(parse_input(input)?)),
+                    expanded_name!("", $long_str) if !(parse_as == ParseAs::PresentationAttr && 
$long_presentation_attr == PresentationAttr::No) => {
+                        Ok(ParsedProperty::$long_name(parse_input(input)?))
+                    }
                 )+
 
                 $(
                     e if e == ExpandedName {
                         ns: &ns!(),
                         local: &LocalName::from($long_m5e_str),
-                    } =>
-                        Ok(ParsedProperty::$long_m5e_name(parse_input(input)?)),
+                    } && !(parse_as == ParseAs::PresentationAttr && $long_m5e_presentation_attr == 
PresentationAttr::No) => {
+                        Ok(ParsedProperty::$long_m5e_name(parse_input(input)?))
+                    }
                 )+
 
                 $(
                     expanded_name!("", $short_str) => {
-                        if accept_shorthands {
+                        // No shorthand has a presentation attribute.
+                        assert!($short_presentation_attr == PresentationAttr::No);
+
+                        if parse_as == ParseAs::Property {
                             Ok(ParsedProperty::$short_name(parse_input(input)?))
                         } else {
                             let loc = input.current_source_location();
@@ -713,7 +725,7 @@ impl SpecifiedValues {
         // Presentation attributes don't accept shorthands, e.g. there is no
         // attribute like marker="#foo" and it needs to be set in the style attribute
         // like style="marker: #foo;".  So, pass false for accept_shorthands here.
-        match parse_property(&attr, &mut parser, false) {
+        match parse_property(&attr, &mut parser, ParseAs::PresentationAttr) {
             Ok(prop) => {
                 if parser.expect_exhausted().is_ok() {
                     self.set_parsed_property(&prop);


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