🐛 Fix case-sensitivity and multi word italic in font weight parsing

This commit is contained in:
Florian Schroedl 2025-08-27 10:33:20 +02:00 committed by Andrés Moya
parent 6da5bbf33a
commit 112fa46896
2 changed files with 55 additions and 4 deletions

View File

@ -415,14 +415,19 @@
(reduce (fn [acc [k vs]]
(into acc (zipmap vs (repeat k)))) {})))
(defn parse-font-weight [font-weight]
(let [[_ variant italic] (->> (str/lower font-weight)
(re-find #"^(.+?)\s*(italic)?$"))]
{:variant variant
:italic? (some? italic)}))
(defn valid-font-weight-variant
"Converts font-weight token value to a map like `{:weight \"100\" :style \"italic\"}`.
Converts a weight alias like `regular` to a number, needs to be a regular number.
Adds `italic` style when found in the `value` string."
[value]
(let [[weight style] (->> (str/split value #"\s+")
(map str/lower))
weight (get font-weight-map weight weight)]
(let [{:keys [variant italic?]} (parse-font-weight value)
weight (get font-weight-map variant variant)]
(when (font-weight-values weight)
(cond-> {:weight weight}
(= style "italic") (assoc :style "italic")))))
italic? (assoc :style "italic")))))

View File

@ -0,0 +1,46 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns common-tests.token-test
(:require
[app.common.types.token :as token]
[clojure.test :as t]))
(t/deftest valid-font-weight-variant
(t/testing "numeric weights"
(t/is (= {:weight "400"} (token/valid-font-weight-variant "400")))
(t/is (= {:weight "700"} (token/valid-font-weight-variant "700")))
(t/is (= {:weight "100"} (token/valid-font-weight-variant "100")))
(t/is (= {:weight "900"} (token/valid-font-weight-variant "900"))))
(t/testing "weight aliases"
(t/is (= {:weight "400"} (token/valid-font-weight-variant "normal")))
(t/is (= {:weight "400"} (token/valid-font-weight-variant "regular")))
(t/is (= {:weight "700"} (token/valid-font-weight-variant "bold")))
(t/is (= {:weight "300"} (token/valid-font-weight-variant "light")))
(t/is (= {:weight "500"} (token/valid-font-weight-variant "medium")))
(t/is (= {:weight "600"} (token/valid-font-weight-variant "semibold")))
(t/is (= {:weight "600"} (token/valid-font-weight-variant "semi-bold")))
(t/is (= {:weight "800"} (token/valid-font-weight-variant "extrabold")))
(t/is (= {:weight "900"} (token/valid-font-weight-variant "black")))
(t/is (= {:weight "950"} (token/valid-font-weight-variant "extra black"))))
(t/testing "italic style"
(t/is (= {:weight "400" :style "italic"} (token/valid-font-weight-variant "normal italic")))
(t/is (= {:weight "700" :style "italic"} (token/valid-font-weight-variant "bold italic")))
(t/is (= {:weight "400" :style "italic"} (token/valid-font-weight-variant "400 italic")))
(t/is (= {:weight "950" :style "italic"} (token/valid-font-weight-variant "extra black italic"))))
(t/testing "case-insensitivity"
(t/is (= {:weight "700"} (token/valid-font-weight-variant "BOLD")))
(t/is (= {:weight "400"} (token/valid-font-weight-variant "Normal")))
(t/is (= {:weight "400" :style "italic"} (token/valid-font-weight-variant "NORMAL ITALIC"))))
(t/testing "invalid values"
(t/is (nil? (token/valid-font-weight-variant "invalid")))
(t/is (nil? (token/valid-font-weight-variant "invalid italic")))
(t/is (nil? (token/valid-font-weight-variant "999")))
(t/is (nil? (token/valid-font-weight-variant "")))))