Skip to content

Commit c42d636

Browse files
committed
Impl fontFaces to globalCss
1 parent a532f26 commit c42d636

25 files changed

+595
-54
lines changed

.changeset/wet-fans-add.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
"@devup-ui/react": patch
4+
---
5+
6+
Impl font-faces to global

Cargo.lock

Lines changed: 32 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/devup-ui-wasm/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ impl Output {
128128
ExtractStyleValue::Import(st) => {
129129
sheet.add_import(&st.file, &st.url);
130130
}
131+
ExtractStyleValue::FontFace(font) => {
132+
sheet.add_font_face(&font.file, &font.properties);
133+
}
131134
}
132135
}
133136

libs/css/src/constant.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ pub(super) static GLOBAL_STYLE_PROPERTY: phf::Map<&str, &[&str]> = phf_map! {
6969
"objectPos" => &["object-position"],
7070
"offsetPos" => &["offset-position"],
7171
};
72+
pub(super) static OPTIMIZE_MULTI_CSS_VALUE_PROPERTY: phf::Set<&str> = phf_set! {
73+
"font-family",
74+
"src",
75+
"content",
76+
"animation-name",
77+
};
7278

7379
pub(super) static DOUBLE_SEPARATOR: phf::Set<&str> = phf_set! {
7480
"after",

libs/css/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod class_map;
22
mod constant;
33
pub mod debug;
44
pub mod is_special_property;
5+
pub mod optimize_multi_css_value;
56
pub mod optimize_value;
67
pub mod rm_css_comment;
78
mod selector_separator;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
use crate::constant::OPTIMIZE_MULTI_CSS_VALUE_PROPERTY;
2+
3+
pub fn optimize_mutli_css_value(value: &str) -> String {
4+
value
5+
.split(",")
6+
.map(|s| {
7+
let s = s.trim();
8+
let s = if s.starts_with("'") && s.ends_with("'")
9+
|| s.starts_with('"') && s.ends_with('"')
10+
{
11+
s[1..s.len() - 1].to_string()
12+
} else {
13+
s.to_string()
14+
};
15+
if s.contains(" ") {
16+
format!("\"{s}\"")
17+
} else {
18+
s
19+
}
20+
})
21+
.collect::<Vec<_>>()
22+
.join(",")
23+
}
24+
25+
pub fn check_multi_css_optimize(property: &str) -> bool {
26+
OPTIMIZE_MULTI_CSS_VALUE_PROPERTY.contains(property)
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
use rstest::rstest;
34+
35+
#[rstest]
36+
#[case("Roboto, sans-serif", "Roboto,sans-serif")]
37+
#[case("'Roboto', sans-serif", "Roboto,sans-serif")]
38+
#[case("\"Roboto\", sans-serif", "Roboto,sans-serif")]
39+
#[case("'Roboto Hello', sans-serif", "\"Roboto Hello\",sans-serif")]
40+
#[case("\"Roboto Hello\", sans-serif", "\"Roboto Hello\",sans-serif")]
41+
#[case("Roboto", "Roboto")]
42+
#[case("'Roboto'", "Roboto")]
43+
#[case("\"Roboto\"", "Roboto")]
44+
#[case("url('/fonts/Roboto-Regular.ttf')", "url('/fonts/Roboto-Regular.ttf')")]
45+
#[case(
46+
"url(\"/fonts/Roboto-Regular.ttf\")",
47+
"url(\"/fonts/Roboto-Regular.ttf\")"
48+
)]
49+
#[case("'A B', 'C D', E", "\"A B\",\"C D\",E")]
50+
#[case("A,B,C", "A,B,C")]
51+
#[case("A, B, C", "A,B,C")]
52+
#[case("'A', 'B', 'C'", "A,B,C")]
53+
#[case("\"A\", \"B\", \"C\"", "A,B,C")]
54+
fn test_optimize_mutli_css_value(#[case] input: &str, #[case] expected: &str) {
55+
assert_eq!(optimize_mutli_css_value(input), expected);
56+
}
57+
58+
#[rstest]
59+
#[case("font-family", true)]
60+
#[case("src", true)]
61+
#[case("content", true)]
62+
#[case("animation-name", true)]
63+
#[case("background", false)]
64+
#[case("color", false)]
65+
#[case("margin", false)]
66+
fn test_check_multi_css_optimize(#[case] property: &str, #[case] expected: bool) {
67+
assert_eq!(check_multi_css_optimize(property), expected);
68+
}
69+
}

libs/css/src/rm_css_comment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub fn rm_css_comment(value: &str) -> String {
77
.split_ascii_whitespace()
88
.collect::<Vec<&str>>()
99
.join(" ")
10+
.replace(", ", ",")
1011
}
1112

1213
#[cfg(test)]

libs/extractor/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
oxc_parser = "0.79.1"
8-
oxc_syntax = "0.79.1"
9-
oxc_span = "0.79.1"
10-
oxc_allocator = "0.79.1"
11-
oxc_ast = "0.79.1"
12-
oxc_ast_visit = "0.79.1"
13-
oxc_codegen = "0.79.1"
7+
oxc_parser = "0.80.0"
8+
oxc_syntax = "0.80.0"
9+
oxc_span = "0.80.0"
10+
oxc_allocator = "0.80.0"
11+
oxc_ast = "0.80.0"
12+
oxc_ast_visit = "0.80.0"
13+
oxc_codegen = "0.80.0"
1414
css = { path = "../css" }
1515
phf = "0.12"
1616
strum = "0.27.2"

libs/extractor/src/css_utils.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::collections::BTreeMap;
22

3-
use css::{rm_css_comment::rm_css_comment, style_selector::StyleSelector};
3+
use css::{
4+
optimize_multi_css_value::{check_multi_css_optimize, optimize_mutli_css_value},
5+
rm_css_comment::rm_css_comment,
6+
style_selector::StyleSelector,
7+
};
48

59
use crate::{
610
ExtractStyleProp,
@@ -107,8 +111,13 @@ fn css_to_style_block<'a>(
107111
let mut iter = s.split(":").map(|s| s.trim());
108112
let property = iter.next().unwrap();
109113
let value = iter.next().unwrap();
114+
let value = if check_multi_css_optimize(property) {
115+
optimize_mutli_css_value(value)
116+
} else {
117+
value.to_string()
118+
};
110119
Some(ExtractStyleProp::Static(ExtractStyleValue::Static(
111-
ExtractStaticStyle::new(property, value, level, selector.clone()),
120+
ExtractStaticStyle::new(property, &value, level, selector.clone()),
112121
)))
113122
}
114123
})
@@ -164,6 +173,11 @@ pub fn optimize_css_block(css: &str) -> String {
164173
let mut iter = s.split(":");
165174
let property = iter.next().unwrap().trim();
166175
let value = iter.next().unwrap().trim();
176+
let value = if check_multi_css_optimize(property.split("{").last().unwrap()) {
177+
optimize_mutli_css_value(value)
178+
} else {
179+
value.to_string()
180+
};
167181
format!("{property}:{value}")
168182
}
169183
})
@@ -219,6 +233,14 @@ mod tests {
219233
"ul { list-style : none ; margin : 0 ; padding : 0 ; }",
220234
"ul{list-style:none;margin:0;padding:0}"
221235
)]
236+
#[case(
237+
"ul { font-family: 'Roboto', sans-serif; }",
238+
"ul{font-family:Roboto,sans-serif}"
239+
)]
240+
#[case(
241+
"ul { font-family: \"Roboto Hello\", sans-serif; }",
242+
"ul{font-family:\"Roboto Hello\",sans-serif}"
243+
)]
222244
#[case("section{ }", "section{}")]
223245
fn test_optimize_css_block(#[case] input: &str, #[case] expected: &str) {
224246
assert_eq!(optimize_css_block(input), expected);
@@ -456,6 +478,12 @@ mod tests {
456478
}",
457479
vec![]
458480
)]
481+
#[case(
482+
"ul { font-family: 'Roboto Hello', sans-serif; }",
483+
vec![
484+
("font-family", "\"Roboto Hello\",sans-serif", Some(StyleSelector::Selector("ul".to_string()))),
485+
]
486+
)]
459487
fn test_css_to_style(
460488
#[case] input: &str,
461489
#[case] expected: Vec<(&str, &str, Option<StyleSelector>)>,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::collections::BTreeMap;
2+
3+
#[derive(Debug, PartialEq, Clone, Eq, Hash, Ord, PartialOrd)]
4+
pub struct ExtractFontFace {
5+
pub file: String,
6+
pub properties: BTreeMap<String, String>,
7+
}

0 commit comments

Comments
 (0)