Skip to content

Commit

Permalink
Implement Property::set_prefix
Browse files Browse the repository at this point in the history
Closes #396
  • Loading branch information
devongovett committed Jan 13, 2023
1 parent aa0c8d1 commit a19228d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod tests {
use crate::targets::Browsers;
use crate::traits::{Parse, ToCss};
use crate::values::color::CssColor;
use crate::vendor_prefix::VendorPrefix;
use cssparser::SourceLocation;
use indoc::indoc;
use std::collections::HashMap;
Expand Down Expand Up @@ -22434,6 +22435,33 @@ mod tests {
);
}
}

let mut property = Property::Transform(Default::default(), VendorPrefix::WebKit);
property.set_prefix(VendorPrefix::None);
assert_eq!(property, Property::Transform(Default::default(), VendorPrefix::None));
property.set_prefix(VendorPrefix::Moz);
assert_eq!(property, Property::Transform(Default::default(), VendorPrefix::Moz));
property.set_prefix(VendorPrefix::WebKit | VendorPrefix::Moz);
assert_eq!(
property,
Property::Transform(Default::default(), VendorPrefix::WebKit | VendorPrefix::Moz)
);

let mut property = Property::TextDecorationLine(Default::default(), VendorPrefix::None);
property.set_prefix(VendorPrefix::Ms);
assert_eq!(
property,
Property::TextDecorationLine(Default::default(), VendorPrefix::None)
);
property.set_prefix(VendorPrefix::WebKit | VendorPrefix::Moz | VendorPrefix::Ms);
assert_eq!(
property,
Property::TextDecorationLine(Default::default(), VendorPrefix::WebKit | VendorPrefix::Moz)
);

let mut property = Property::AccentColor(Default::default());
property.set_prefix(VendorPrefix::WebKit);
assert_eq!(property, Property::AccentColor(Default::default()));
}

#[cfg(feature = "substitute_variables")]
Expand Down
43 changes: 34 additions & 9 deletions src/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ macro_rules! define_properties {
};
}

macro_rules! get_allowed_prefixes {
($v: literal) => {
VendorPrefix::empty()
};
() => {
VendorPrefix::None
};
}

impl<'i> From<CowArcStr<'i>> for PropertyId<'i> {
fn from(name: CowArcStr<'i>) -> PropertyId<'i> {
let name_ref = name.as_ref();
Expand Down Expand Up @@ -261,15 +270,6 @@ macro_rules! define_properties {

impl<'i> PropertyId<'i> {
fn from_name_and_prefix(name: &str, prefix: VendorPrefix) -> Result<Self, ()> {
macro_rules! get_allowed_prefixes {
($v: literal) => {
VendorPrefix::empty()
};
() => {
VendorPrefix::None
};
}

match_ignore_ascii_case! { name.as_ref(),
$(
$(#[$meta])*
Expand Down Expand Up @@ -720,6 +720,31 @@ macro_rules! define_properties {
Self::parse(property_id, &mut parser, &options)
}

/// Sets the vendor prefixes for this property.
///
/// If the property doesn't support vendor prefixes, this function does nothing.
/// If vendor prefixes are set which do not exist for the property, they are ignored
/// and only the valid prefixes are set.
pub fn set_prefix(&mut self, prefix: VendorPrefix) {
use Property::*;
match self {
$(
$(#[$meta])*
$property(_, $(vp_name!($vp, p))?) => {
macro_rules! set {
($v: ty) => {
*p = (prefix & (get_allowed_prefixes!($($unprefixed)?) $(| VendorPrefix::$prefix)*)).or(*p);
};
() => {};
}

set!($($vp)?);
},
)+
_ => {}
}
}

/// Serializes the value of a CSS property without its name or `!important` flag.
pub fn value_to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
use Property::*;
Expand Down
2 changes: 1 addition & 1 deletion src/properties/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use cssparser::*;
use std::f32::consts::PI;

/// A value for the [transform](https://www.w3.org/TR/2019/CR-css-transforms-1-20190214/#propdef-transform) property.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
Expand Down
8 changes: 7 additions & 1 deletion src/vendor_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ impl VendorPrefix {
/// Returns VendorPrefix::None if empty.
#[inline]
pub fn or_none(self) -> Self {
self.or(VendorPrefix::None)
}

/// Returns `other` if `self` is empty
#[inline]
pub fn or(self, other: Self) -> Self {
if self.is_empty() {
VendorPrefix::None
other
} else {
self
}
Expand Down

0 comments on commit a19228d

Please sign in to comment.