From 824601bbf986343ebac862c2e85693bb13238eda Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 28 Jun 2024 17:10:20 +1200 Subject: [PATCH] Test unicode-range parsing. --- lib/Graphics/Text/Font/Choose/CharSet.hs | 3 ++- test/Main.hs | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/Graphics/Text/Font/Choose/CharSet.hs b/lib/Graphics/Text/Font/Choose/CharSet.hs index 1a1ad69..f315d91 100644 --- a/lib/Graphics/Text/Font/Choose/CharSet.hs +++ b/lib/Graphics/Text/Font/Choose/CharSet.hs @@ -5,7 +5,7 @@ module Graphics.Text.Font.Choose.CharSet( import Data.IntSet as IntSet -import Data.Char (isHexDigit, ord, chr) +import Data.Char (isHexDigit, isSpace, ord, chr) import Numeric (readHex) import Data.MessagePack (MessagePack(..), Object(..)) @@ -25,6 +25,7 @@ parseWild :: Char -> String -> Int parseWild ch str = parseChar $ replaceWild ch str -- | Utility for parsing "unicode-range" @font-face property. parseCharSet :: String -> Maybe CharSet +parseCharSet (c:rest) | isSpace c = parseCharSet rest -- Irrelevant in Stylist integration. parseCharSet ('U':rest) = parseCharSet ('u':rest) -- lowercase initial "u" parseCharSet ('u':'+':cs) | (start@(_:_), '-':ends) <- span isHexDigit cs, diff --git a/test/Main.hs b/test/Main.hs index a8b3725..d308909 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -10,7 +10,8 @@ import Data.MessagePack as MP import qualified Data.Map as M import qualified Data.Set as S import qualified Data.Text as Txt -import Data.Maybe (isJust) +import qualified Data.IntSet as IS +import Data.Maybe (isJust, fromMaybe) import GHC.Real (infinity) import Graphics.Text.Font.Choose @@ -131,3 +132,12 @@ main = hspec $ do nameParse ":weight=[medium bold]" `shouldBe` M.fromList [ ("weight", [(Strong, ValueRange $ Range 100 200)]) ] + describe "CSS Parsing" $ do + it "unicode-range" $ do + let parseCharSet' = IS.toList . fromMaybe IS.empty . parseCharSet + parseCharSet' "U+26" `shouldBe` [0x26] + parseCharSet' "U+26, U+42" `shouldBe` [0x26, 0x42] + parseCharSet' "U+0-7F" `shouldBe` [0x0..0x7f] + parseCharSet' "U+0025-00FF" `shouldBe` [0x0025..0x00ff] + parseCharSet' "U+4??" `shouldBe` [0x400..0x4ff] + parseCharSet' "U+0025-00FF, U+4??" `shouldBe` [0x0025..0x00ff] ++ [0x400..0x4ff] -- 2.30.2