Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change px2rem regex config #1469

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions crates/mako/src/visitors/css_px2rem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ fn parse_patterns(patterns: &[String]) -> Vec<Regex> {
patterns
.iter()
.map(|pattern| {
let pattern = if contains_magic_chars(pattern) {
pattern.to_string()
let pattern = if pattern.starts_with('/') && pattern.ends_with('/') {
pattern[1..pattern.len() - 1].to_string()
} else {
format!("^{}$", pattern)
};
Expand All @@ -156,15 +156,6 @@ fn parse_patterns(patterns: &[String]) -> Vec<Regex> {
.collect()
}

fn contains_magic_chars(pattern: &str) -> bool {
pattern.contains('*')
|| pattern.contains('\\')
|| pattern.contains('(')
|| pattern.contains(')')
|| pattern.contains('^')
|| pattern.contains('$')
}

fn parse_combinator(combinator: &Combinator) -> String {
match combinator.value {
CombinatorValue::Descendant => " ".to_string(),
Expand Down Expand Up @@ -415,7 +406,7 @@ mod tests {
run(
r#".a{width:100px;}.ac{width:100px;}.b{width:100px;}"#,
Px2RemConfig {
selector_blacklist: vec!["^.a$".to_string()],
selector_blacklist: vec!["/^.a$/".to_string()],
..Default::default()
}
),
Expand Down Expand Up @@ -457,7 +448,7 @@ mod tests {
run(
r#"div *{width:100px;}"#,
Px2RemConfig {
selector_blacklist: vec!["div *".to_string()],
selector_blacklist: vec!["/div */".to_string()],
..Default::default()
}
),
Expand Down Expand Up @@ -499,7 +490,7 @@ mod tests {
run(
r#"[class*="button"]{width:100px;}"#,
Px2RemConfig {
selector_blacklist: vec!["[class*=\"button\"]".to_string()],
selector_blacklist: vec!["/[class*=\"button\"]/".to_string()],
..Default::default()
}
),
Expand All @@ -513,7 +504,7 @@ mod tests {
run(
r#"[class*="button"]{width:100px;}"#,
Px2RemConfig {
selector_whitelist: vec!["[class*=\"button\"]".to_string()],
selector_whitelist: vec!["/[class*=\"button\"]/".to_string()],
..Default::default()
}
),
Expand Down Expand Up @@ -649,7 +640,7 @@ mod tests {
run(
r#".a-x{width:100px;}"#,
Px2RemConfig {
selector_doublelist: vec!["^.a-".to_string()],
selector_doublelist: vec!["/^.a-/".to_string()],
..Default::default()
}
),
Expand All @@ -665,6 +656,17 @@ mod tests {
),
r#":root{width:2rem}"#
);
assert_eq!(
run(
r#".a{width:100px;}.b{width:100px;}.b.a{width:100px;}"#,
Px2RemConfig {
selector_whitelist: vec!["/.a/".to_string()],
selector_blacklist: vec![],
..Default::default()
}
),
r#".a{width:1rem}.b{width:100px}.b.a{width:1rem}"#
);
}

fn run_with_default(css_code: &str) -> String {
Expand Down
8 changes: 8 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ Whether to enable px2rem conversion.
- `minPixelValue`,minimum pixel value, default is `0`
- `mediaQuery`,allow px to be converted in media queries, default is `false`

Among them, `selectorBlackList`, `selectorWhiteList` and `selectorDoubleList` all support passing regular expressions or strings, such as

```json
"selectorBlackList": [".a", "/.__CustomClass_/"]
```

> The string wrapped by the characters `/` will be parsed as a regular expression.

### react

- Type: `{ runtime: "automatic" | "classic", pragma: string, import_source: string, pragma_frag: string }`
Expand Down
8 changes: 8 additions & 0 deletions docs/config.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ __mako_public_path__ = '/foo/';
- `minPixelValue`,最小像素值,默认为 `0`
- `mediaQuery`,是否转换媒体查询中的 px, 默认 `false`

其中 `selectorBlackList`、`selectorWhiteList`、`selectorDoubleList` 均支持传递正则表达式或者字符串,如

```json
"selectorBlackList": [".a", "/.__CustomClass_/"]
```

> 被字符 `/` 包裹的字符串会被当作正则表达式解析。

### react

- 类型:`{ runtime: "automatic" | "classic", pragma: string, import_source: string, pragma_frag: string }`
Expand Down