Skip to content

Commit

Permalink
Dedupe font family
Browse files Browse the repository at this point in the history
Fixes #246
  • Loading branch information
devongovett committed Aug 21, 2022
1 parent 57a1e37 commit 41dfaa4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4984,6 +4984,24 @@ mod tests {
..Browsers::default()
},
);

prefix_test(
r#"
.foo {
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}
"#,
indoc! {r#"
.foo {
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Noto Sans, Ubuntu, Cantarell, Helvetica Neue, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}
"#
},
Browsers {
firefox: Some(91 << 16),
..Browsers::default()
},
);
}

#[test]
Expand Down
15 changes: 13 additions & 2 deletions src/properties/font.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! CSS properties related to fonts.

use std::collections::HashSet;

use super::{Property, PropertyId};
use crate::compat::Feature;
use crate::context::PropertyHandlerContext;
Expand Down Expand Up @@ -312,6 +314,7 @@ enum_property! {
///
/// See [FontFamily](FontFamily).
#[allow(missing_docs)]
#[derive(Eq, Hash)]
pub enum GenericFontFamily {
"serif": Serif,
"sans-serif": SansSerif,
Expand Down Expand Up @@ -345,7 +348,7 @@ enum_property! {
}

/// A value for the [font-family](https://www.w3.org/TR/css-fonts-4/#font-family-prop) property.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -841,7 +844,7 @@ impl<'i> PropertyHandler<'i> for FontHandler<'i> {

self.has_any = false;

let family = compatible_font_family(
let mut family = compatible_font_family(
std::mem::take(&mut self.family),
context.is_supported(Feature::FontFamilySystemUi),
);
Expand All @@ -852,6 +855,14 @@ impl<'i> PropertyHandler<'i> for FontHandler<'i> {
let line_height = std::mem::take(&mut self.line_height);
let variant_caps = std::mem::take(&mut self.variant_caps);

if let Some(family) = &mut family {
if family.len() > 1 {
// Dedupe.
let mut seen = HashSet::new();
family.retain(|f| seen.insert(f.clone()));
}
}

if family.is_some()
&& size.is_some()
&& style.is_some()
Expand Down

0 comments on commit 41dfaa4

Please sign in to comment.