Skip to content

Commit a19228d

Browse files
committed
Implement Property::set_prefix
Closes parcel-bundler#396
1 parent aa0c8d1 commit a19228d

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod tests {
5757
use crate::targets::Browsers;
5858
use crate::traits::{Parse, ToCss};
5959
use crate::values::color::CssColor;
60+
use crate::vendor_prefix::VendorPrefix;
6061
use cssparser::SourceLocation;
6162
use indoc::indoc;
6263
use std::collections::HashMap;
@@ -22434,6 +22435,33 @@ mod tests {
2243422435
);
2243522436
}
2243622437
}
22438+
22439+
let mut property = Property::Transform(Default::default(), VendorPrefix::WebKit);
22440+
property.set_prefix(VendorPrefix::None);
22441+
assert_eq!(property, Property::Transform(Default::default(), VendorPrefix::None));
22442+
property.set_prefix(VendorPrefix::Moz);
22443+
assert_eq!(property, Property::Transform(Default::default(), VendorPrefix::Moz));
22444+
property.set_prefix(VendorPrefix::WebKit | VendorPrefix::Moz);
22445+
assert_eq!(
22446+
property,
22447+
Property::Transform(Default::default(), VendorPrefix::WebKit | VendorPrefix::Moz)
22448+
);
22449+
22450+
let mut property = Property::TextDecorationLine(Default::default(), VendorPrefix::None);
22451+
property.set_prefix(VendorPrefix::Ms);
22452+
assert_eq!(
22453+
property,
22454+
Property::TextDecorationLine(Default::default(), VendorPrefix::None)
22455+
);
22456+
property.set_prefix(VendorPrefix::WebKit | VendorPrefix::Moz | VendorPrefix::Ms);
22457+
assert_eq!(
22458+
property,
22459+
Property::TextDecorationLine(Default::default(), VendorPrefix::WebKit | VendorPrefix::Moz)
22460+
);
22461+
22462+
let mut property = Property::AccentColor(Default::default());
22463+
property.set_prefix(VendorPrefix::WebKit);
22464+
assert_eq!(property, Property::AccentColor(Default::default()));
2243722465
}
2243822466

2243922467
#[cfg(feature = "substitute_variables")]

src/properties/mod.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ macro_rules! define_properties {
200200
};
201201
}
202202

203+
macro_rules! get_allowed_prefixes {
204+
($v: literal) => {
205+
VendorPrefix::empty()
206+
};
207+
() => {
208+
VendorPrefix::None
209+
};
210+
}
211+
203212
impl<'i> From<CowArcStr<'i>> for PropertyId<'i> {
204213
fn from(name: CowArcStr<'i>) -> PropertyId<'i> {
205214
let name_ref = name.as_ref();
@@ -261,15 +270,6 @@ macro_rules! define_properties {
261270

262271
impl<'i> PropertyId<'i> {
263272
fn from_name_and_prefix(name: &str, prefix: VendorPrefix) -> Result<Self, ()> {
264-
macro_rules! get_allowed_prefixes {
265-
($v: literal) => {
266-
VendorPrefix::empty()
267-
};
268-
() => {
269-
VendorPrefix::None
270-
};
271-
}
272-
273273
match_ignore_ascii_case! { name.as_ref(),
274274
$(
275275
$(#[$meta])*
@@ -720,6 +720,31 @@ macro_rules! define_properties {
720720
Self::parse(property_id, &mut parser, &options)
721721
}
722722

723+
/// Sets the vendor prefixes for this property.
724+
///
725+
/// If the property doesn't support vendor prefixes, this function does nothing.
726+
/// If vendor prefixes are set which do not exist for the property, they are ignored
727+
/// and only the valid prefixes are set.
728+
pub fn set_prefix(&mut self, prefix: VendorPrefix) {
729+
use Property::*;
730+
match self {
731+
$(
732+
$(#[$meta])*
733+
$property(_, $(vp_name!($vp, p))?) => {
734+
macro_rules! set {
735+
($v: ty) => {
736+
*p = (prefix & (get_allowed_prefixes!($($unprefixed)?) $(| VendorPrefix::$prefix)*)).or(*p);
737+
};
738+
() => {};
739+
}
740+
741+
set!($($vp)?);
742+
},
743+
)+
744+
_ => {}
745+
}
746+
}
747+
723748
/// Serializes the value of a CSS property without its name or `!important` flag.
724749
pub fn value_to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
725750
use Property::*;

src/properties/transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use cssparser::*;
2222
use std::f32::consts::PI;
2323

2424
/// A value for the [transform](https://www.w3.org/TR/2019/CR-css-transforms-1-20190214/#propdef-transform) property.
25-
#[derive(Debug, Clone, PartialEq)]
25+
#[derive(Debug, Clone, PartialEq, Default)]
2626
#[cfg_attr(feature = "visitor", derive(Visit))]
2727
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
2828
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]

src/vendor_prefix.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ impl VendorPrefix {
5252
/// Returns VendorPrefix::None if empty.
5353
#[inline]
5454
pub fn or_none(self) -> Self {
55+
self.or(VendorPrefix::None)
56+
}
57+
58+
/// Returns `other` if `self` is empty
59+
#[inline]
60+
pub fn or(self, other: Self) -> Self {
5561
if self.is_empty() {
56-
VendorPrefix::None
62+
other
5763
} else {
5864
self
5965
}

0 commit comments

Comments
 (0)