Skip to content

Commit

Permalink
fix(typography): Font @import statements need to come before `@font…
Browse files Browse the repository at this point in the history
…-face` declarations (#39)
  • Loading branch information
gadenbuie authored Oct 24, 2024
2 parents 9e583be + 9e8d26d commit 4225ccb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pkg-py/src/brand_yml/typography.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,9 @@ def fonts_css_include(self) -> str:
if len(self.fonts) == 0:
return ""

includes = [font.to_css() for font in self.fonts]
fonts = sorted([*self.fonts], key=lambda x: x.source == "file")

includes = [font.to_css() for font in fonts]

return "\n".join([i for i in includes if i])

Expand Down
6 changes: 3 additions & 3 deletions pkg-py/tests/__snapshots__/test_typography.ambr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# serializer version: 1
# name: test_brand_typography_css_fonts
'''
@import url('https://fonts.googleapis.com/css2?family=Roboto+Slab%3Aital%2Cwght%400%2C600..900&display=block');
@import url('https://fonts.bunny.net/css?family=Fira+Code%3A100%2C100i%2C200%2C200i%2C300%2C300i%2C400%2C400i%2C500%2C500i%2C600%2C600i%2C700%2C700i%2C800%2C800i%2C900%2C900i&display=auto');
@font-face {
font-family: 'Open Sans';
font-weight: auto;
Expand All @@ -25,18 +27,16 @@
font-style: italic;
src: url('https://example.com/Closed-Sans-Italic.woff2') format('woff2');
}
@import url('https://fonts.googleapis.com/css2?family=Roboto+Slab%3Aital%2Cwght%400%2C600..900&display=block');
@import url('https://fonts.bunny.net/css?family=Fira+Code%3A100%2C100i%2C200%2C200i%2C300%2C300i%2C400%2C400i%2C500%2C500i%2C600%2C600i%2C700%2C700i%2C800%2C800i%2C900%2C900i&display=auto');
'''
# ---
# name: test_brand_typography_css_fonts_local
'''
@import url('https://fonts.googleapis.com/css2?family=Roboto%3Aital%2Cwght%400%2C200..500%3B1%2C200..500&display=auto');
@font-face {
font-family: 'Open Sans';
font-weight: 400 800;
font-style: italic;
src: url('OpenSans-Variable.ttf') format('truetype');
}
@import url('https://fonts.googleapis.com/css2?family=Roboto%3Aital%2Cwght%400%2C200..500%3B1%2C200..500&display=auto');
'''
# ---
8 changes: 7 additions & 1 deletion pkg-py/tests/test_typography.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import re
import tempfile
from pathlib import Path
from urllib.parse import unquote
Expand Down Expand Up @@ -706,7 +707,12 @@ def test_brand_typography_css_fonts_local(snapshot):
""")

assert isinstance(brand.typography, BrandTypography)
assert snapshot == brand.typography.fonts_css_include()
fonts_css = brand.typography.fonts_css_include()
at_rules = re.findall(r"@(import|font-face)", fonts_css)

# The google @import rule must come before @font-face rules
assert at_rules == ["import", "font-face"]
assert snapshot == fonts_css


def test_brand_typography_google_fonts_weight_range():
Expand Down

0 comments on commit 4225ccb

Please sign in to comment.