Skip to content

Commit c976374

Browse files
committed
Add font handler
1 parent 03994f2 commit c976374

File tree

6 files changed

+805
-5
lines changed

6 files changed

+805
-5
lines changed

src/lib.rs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ mod tests {
337337
.foo {
338338
border: none;
339339
}
340-
"#, indoc! {".foo{border:0;}"
340+
"#, indoc! {".foo{border:0}"
341341
});
342342
}
343343

@@ -676,7 +676,7 @@ mod tests {
676676
.foo {
677677
background-position: center center;
678678
}
679-
"#, indoc! {".foo{background-position:50% 50%;}"
679+
"#, indoc! {".foo{background-position:50% 50%}"
680680
});
681681
}
682682

@@ -948,4 +948,76 @@ mod tests {
948948
"#
949949
});
950950
}
951+
952+
#[test]
953+
fn test_font() {
954+
test(r#"
955+
.foo {
956+
font-family: "Helvetica", "Times New Roman", sans-serif;
957+
font-size: 12px;
958+
font-weight: bold;
959+
font-style: italic;
960+
font-stretch: expanded;
961+
font-variant-caps: small-caps;
962+
line-height: 1.2em;
963+
}
964+
"#, indoc! {r#"
965+
.foo {
966+
font: italic small-caps bold expanded 12px / 1.2em Helvetica, Times New Roman, sans-serif;
967+
}
968+
"#
969+
});
970+
971+
minify_test(r#"
972+
.foo {
973+
font-family: "Helvetica", "Times New Roman", sans-serif;
974+
font-size: 12px;
975+
font-weight: bold;
976+
font-style: italic;
977+
font-stretch: expanded;
978+
font-variant-caps: small-caps;
979+
line-height: 1.2em;
980+
}
981+
"#, indoc! {".foo{font:italic small-caps 700 50% 12px/1.2em Helvetica,Times New Roman,sans-serif}"
982+
});
983+
984+
test(r#"
985+
.foo {
986+
font: 12px "Helvetica", "Times New Roman", sans-serif;
987+
line-height: 1.2em;
988+
}
989+
"#, indoc! {r#"
990+
.foo {
991+
font: 12px / 1.2em Helvetica, Times New Roman, sans-serif;
992+
}
993+
"#
994+
});
995+
996+
minify_test(r#"
997+
.foo {
998+
font-family: "Helvetica", "Times New Roman", sans-serif;
999+
font-size: 12px;
1000+
font-stretch: expanded;
1001+
}
1002+
"#, indoc! {".foo{font-family:Helvetica,Times New Roman,sans-serif;font-size:12px;font-stretch:50%}"
1003+
});
1004+
1005+
test(r#"
1006+
.foo {
1007+
font-family: "Helvetica", "Times New Roman", sans-serif;
1008+
font-size: 12px;
1009+
font-weight: bold;
1010+
font-style: italic;
1011+
font-stretch: expanded;
1012+
font-variant-caps: all-small-caps;
1013+
line-height: 1.2em;
1014+
}
1015+
"#, indoc! {r#"
1016+
.foo {
1017+
font: italic bold expanded 12px / 1.2em Helvetica, Times New Roman, sans-serif;
1018+
font-variant-caps: all-small-caps;
1019+
}
1020+
"#
1021+
});
1022+
}
9511023
}

src/parser.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl ToCss for StyleRule {
440440
dest.write_str("\n ")?;
441441
}
442442
prop.to_css(dest)?;
443-
if i != len - 1 {
443+
if i != len - 1 || !dest.minify {
444444
dest.write_char(';')?;
445445
}
446446
}
@@ -452,7 +452,7 @@ impl ToCss for StyleRule {
452452
impl StyleRule {
453453
pub fn minify(&mut self) {
454454
use crate::values::border::*;
455-
use crate::properties::{margin_padding::*, outline::*, flex::*, align::*};
455+
use crate::properties::{margin_padding::*, outline::*, flex::*, align::*, font::*};
456456
use crate::properties::background::BackgroundHandler;
457457

458458
// TODO: somehow macro-ify this
@@ -465,6 +465,7 @@ impl StyleRule {
465465
let mut padding_handler = PaddingHandler::default();
466466
let mut scroll_margin_handler = MarginHandler::default();
467467
let mut scroll_padding_handler = PaddingHandler::default();
468+
let mut font_handler = FontHandler::default();
468469

469470
let mut decls = vec![];
470471
for decl in self.declarations.iter() {
@@ -476,7 +477,8 @@ impl StyleRule {
476477
!margin_handler.handle_property(decl) &&
477478
!padding_handler.handle_property(decl) &&
478479
!scroll_margin_handler.handle_property(decl) &&
479-
!scroll_padding_handler.handle_property(decl)
480+
!scroll_padding_handler.handle_property(decl) &&
481+
!font_handler.handle_property(decl)
480482
{
481483
decls.push(decl.clone());
482484
}
@@ -491,6 +493,7 @@ impl StyleRule {
491493
decls.extend(padding_handler.finalize());
492494
decls.extend(scroll_margin_handler.finalize());
493495
decls.extend(scroll_padding_handler.finalize());
496+
decls.extend(font_handler.finalize());
494497
self.declarations = decls;
495498
}
496499
}

0 commit comments

Comments
 (0)