Skip to content

Commit

Permalink
feat: px2rem support selectorDoubleRemList (#1336)
Browse files Browse the repository at this point in the history
* feat: px2rem support selectorDoubleRemList

* chore: rename

* chore: remove test case

---------

Co-authored-by: sorrycc <sorrycc@gmail.com>
  • Loading branch information
xiaohuoni and sorrycc authored Jul 9, 2024
1 parent cc673d3 commit 62e1227
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub struct BuildParams {
propWhiteList?: string[];
selectorBlackList?: string[];
selectorWhiteList?: string[];
selectorDoubleList?: string[];
};
stats?: boolean;
hash?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions crates/mako/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ pub struct Px2RemConfig {
pub selector_blacklist: Vec<String>,
#[serde(rename = "selectorWhiteList", default)]
pub selector_whitelist: Vec<String>,
#[serde(rename = "selectorDoubleList", default)]
pub selector_doublelist: Vec<String>,
#[serde(rename = "minPixelValue", default)]
pub min_pixel_value: f64,
}
Expand All @@ -257,6 +259,7 @@ impl Default for Px2RemConfig {
prop_whitelist: vec![],
selector_blacklist: vec![],
selector_whitelist: vec![],
selector_doublelist: vec![],
min_pixel_value: 0.0,
}
}
Expand Down
47 changes: 45 additions & 2 deletions crates/mako/src/visitors/css_px2rem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,32 @@ pub struct Px2Rem {
current_selectors: Vec<String>,
selector_blacklist: Vec<Regex>,
selector_whitelist: Vec<Regex>,
selector_doublelist: Vec<Regex>,
}

impl Px2Rem {
pub fn new(config: Px2RemConfig) -> Self {
let selector_blacklist = parse_patterns(&config.selector_blacklist);
let selector_whitelist = parse_patterns(&config.selector_whitelist);
let selector_doublelist = parse_patterns(&config.selector_doublelist);
Self {
config,
current_decl: None,
current_selectors: vec![],
selector_blacklist,
selector_whitelist,
selector_doublelist,
}
}

fn is_any_in_doublelist(&self) -> bool {
let is_any_in_doublelist = self.current_selectors.iter().any(|selector| {
self.selector_doublelist
.iter()
.any(|regx| regx.is_match(selector))
});
is_any_in_doublelist
}
fn should_transform(&self, val: f64) -> bool {
if val.abs() < self.config.min_pixel_value {
return false;
Expand All @@ -55,12 +66,14 @@ impl Px2Rem {
.iter()
.any(|regx| regx.is_match(selector))
});

let is_any_in_blacklist = self.current_selectors.iter().any(|selector| {
self.selector_blacklist
.iter()
.any(|regx| regx.is_match(selector))
});
(is_whitelist_empty || is_all_in_whitelist) && !is_any_in_blacklist
(is_whitelist_empty || is_all_in_whitelist || self.is_any_in_doublelist())
&& !is_any_in_blacklist
};
is_prop_valid && is_selector_valid
}
Expand Down Expand Up @@ -92,6 +105,9 @@ impl VisitMut for Px2Rem {
fn visit_mut_length(&mut self, n: &mut Length) {
if n.unit.value.to_string() == "px" && self.should_transform(n.value.value) {
n.value.value /= self.config.root;
if self.is_any_in_doublelist() {
n.value.value *= 2.0;
}
n.value.raw = None;
n.unit.value = "rem".into();
}
Expand All @@ -104,8 +120,11 @@ impl VisitMut for Px2Rem {
fn visit_mut_token(&mut self, t: &mut Token) {
if let Token::Dimension(dimension) = t {
if dimension.unit.to_string() == "px" && self.should_transform(dimension.value) {
let rem_val = dimension.value / self.config.root;
let mut rem_val = dimension.value / self.config.root;
dimension.raw_value = rem_val.to_string().into();
if self.is_any_in_doublelist() {
rem_val *= 2.0;
}
dimension.value = rem_val;
dimension.raw_unit = "rem".into();
dimension.unit = "rem".into();
Expand Down Expand Up @@ -577,6 +596,30 @@ mod tests {
);
}

#[test]
fn test_multi_selector_doublelist() {
assert_eq!(
run(
r#".a{width:100px;}"#,
Px2RemConfig {
selector_doublelist: vec![],
..Default::default()
}
),
r#".a{width:1rem}"#
);
assert_eq!(
run(
r#".a{width:100px;}"#,
Px2RemConfig {
selector_doublelist: vec![".a".to_string()],
..Default::default()
}
),
r#".a{width:2rem}"#
);
}

fn run_with_default(css_code: &str) -> String {
run(css_code, Px2RemConfig::default())
}
Expand Down
3 changes: 2 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ publicPath configuration. Note: There is a special value `"runtime"`, which mean

### px2rem

- Type: `false | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[], minPixelValue?: number }`
- Type: `false | { root?: number, propBlackList?: string[], propWhiteList?: string[], selectorBlackList?: string[], selectorWhiteList?: string[], selectorDoubleList?: string[], minPixelValue?: number }`
- Default: `false`

Whether to enable px2rem conversion.
Expand All @@ -505,6 +505,7 @@ Whether to enable px2rem conversion.
- `propWhiteList`, property white list
- `selectorBlackList`, selector black list
- `selectorWhiteList`, selector white list
- `selectorDoubleList`, selector double rem list
- `minPixelValue`,minimum pixel value, default is `0`

### react
Expand Down
1 change: 1 addition & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface BuildParams {
propWhiteList?: string[];
selectorBlackList?: string[];
selectorWhiteList?: string[];
selectorDoubleList?: string[];
};
stats?: boolean;
hash?: boolean;
Expand Down

0 comments on commit 62e1227

Please sign in to comment.