Skip to content

Commit edc2458

Browse files
authored
Merge pull request #95 from vyorkin-forks/feature/font-style-type
Refactor FontStyle type
2 parents 57070c8 + 33c790f commit edc2458

File tree

4 files changed

+82
-31
lines changed

4 files changed

+82
-31
lines changed

src/CSS.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import CSS.Display (ClearFloat(..), Display(..), Float(..), Position(..), absolu
99
import CSS.Elements (a, abbr, address, area, article, aside, audio, b, bdi, bdo, blockquote, body, br, button, canvas, caption, cite, code, col, colgroup, datalist, dd, del, details, dfn, div, dl, dt, embed, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hr, html, i, iframe, img, input, ins, kbd, label, legend, li, main, map, mark, meter, nav, object, ol, optgroup, output, p, pre, progress, q, s, samp, section, small, span, strong, sub, summary, sup, tbody, td, textarea, tfoot, th, thead, tr, u, ul, var) as X
1010
import CSS.Flexbox (class FlexEnd, class FlexStart, class SpaceAround, class SpaceBetween, class Stretch, AlignContentValue(..), AlignItemsValue(..), AlignSelfValue(..), FlexDirection(..), FlexWrap(..), JustifyContentValue(..), alignContent, alignItems, alignSelf, column, columnReverse, flexBasis, flexDirection, flexEnd, flexFlow, flexGrow, flexShrink, flexStart, flexWrap, justifyContent, nowrap, order, row, rowReverse, spaceAround, spaceBetween, stretch, wrap, wrapReverse) as X
1111
import CSS.FontFace (FontFaceFormat(..), FontFaceSrc(..), fontFaceFamily, fontFaceSrc, formatName) as X
12-
import CSS.Font (FontStyle(..), FontWeight(..), GenericFontFamily(..), bold, bolder, color, fontFamily, fontSize, fontStyle, fontWeight, italic, lighter, oblique, sansSerif, weight) as X
12+
import CSS.Font (FontWeight(..), GenericFontFamily(..), bold, bolder, color, fontFamily, fontSize, fontWeight, lighter, sansSerif, weight) as X
13+
import CSS.FontStyle (FontStyle, fontStyle) as X
1314
import CSS.Geometry (bottom, height, left, lineHeight, margin, marginBottom, marginLeft, marginRight, marginTop, maxHeight, maxWidth, minHeight, minWidth, padding, paddingBottom, paddingLeft, paddingRight, paddingTop, right, top, width) as X
1415
import CSS.Gradient (Extend, Radial, Ramp, circle, circular, closestCorner, closestSide, ellipse, elliptical, farthestCorner, farthestSide, hGradient, hRepeatingGradient, linearGradient, radialGradient, repeatingLinearGradient, repeatingRadialGradient, vGradient, vRepeatingGradient) as X
1516
import CSS.Property (class Val, Key(..), Literal(..), Prefixed(..), Value(..), cast, noCommas, plain, quote, value, (!)) as X

src/CSS/Font.purs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,3 @@ weight i = FontWeight $ value i
6363

6464
fontWeight :: FontWeight -> CSS
6565
fontWeight = key $ fromString "font-weight"
66-
67-
newtype FontStyle = FontStyle Value
68-
69-
derive instance eqFontStyle :: Eq FontStyle
70-
derive instance ordFontStyle :: Ord FontStyle
71-
72-
instance valFontStyle :: Val FontStyle where
73-
value (FontStyle v) = v
74-
75-
instance normalFontStyle :: Normal FontStyle where
76-
normal = FontStyle (fromString "normal")
77-
78-
instance initialFontStyle :: Initial FontStyle where
79-
initial = FontStyle (fromString "initial")
80-
81-
instance inheritFontStyle :: Inherit FontStyle where
82-
inherit = FontStyle (fromString "inherit")
83-
84-
instance unsetFontStyle :: Unset FontStyle where
85-
unset = FontStyle (fromString "unset")
86-
87-
italic :: FontStyle
88-
italic = FontStyle $ fromString "italic"
89-
90-
oblique :: FontStyle
91-
oblique = FontStyle $ fromString "oblique"
92-
93-
fontStyle :: FontStyle -> CSS
94-
fontStyle = key $ fromString "font-style"

src/CSS/FontStyle.purs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module CSS.FontStyle where
2+
3+
import Prelude
4+
import CSS.Common (class Inherit, class Initial, class Normal, class Unset)
5+
import CSS.Property (class Val, Value, value)
6+
import CSS.Size (Angle)
7+
import CSS.String (fromString)
8+
import CSS.Stylesheet (CSS, key)
9+
10+
data ObliqueValue
11+
= ObliqueDefault
12+
| ObliqueAngle Value
13+
14+
derive instance eqObliqueValue :: Eq ObliqueValue
15+
derive instance ordObliqueValue :: Ord ObliqueValue
16+
17+
instance valObliqueValue :: Val ObliqueValue where
18+
value ObliqueDefault = fromString "oblique"
19+
value (ObliqueAngle v) = fromString "oblique " <> v
20+
21+
data FontStyle
22+
= Normal
23+
| Initial
24+
| Inherit
25+
| Unset
26+
| Italic
27+
| Oblique ObliqueValue
28+
29+
derive instance eqFontStyle :: Eq FontStyle
30+
derive instance ordFontStyle :: Ord FontStyle
31+
32+
instance valFontStyle :: Val FontStyle where
33+
value Normal = fromString "normal"
34+
value Initial = fromString "initial"
35+
value Inherit = fromString "inherit"
36+
value Unset = fromString "unset"
37+
value Italic = fromString "italic"
38+
value (Oblique v) = value v
39+
40+
instance normalFontStyle :: Normal FontStyle where
41+
normal = Normal
42+
43+
instance initialFontStyle :: Initial FontStyle where
44+
initial = Initial
45+
46+
instance inheritFontStyle :: Inherit FontStyle where
47+
inherit = Inherit
48+
49+
instance unsetFontStyle :: Unset FontStyle where
50+
unset = Unset
51+
52+
italic :: FontStyle
53+
italic = Italic
54+
55+
oblique :: FontStyle
56+
oblique = Oblique ObliqueDefault
57+
58+
obliqueAngle :: forall a. Angle a -> FontStyle
59+
obliqueAngle = Oblique <<< ObliqueAngle <<< value
60+
61+
fontStyle :: FontStyle -> CSS
62+
fontStyle = key $ fromString "font-style"

test/Main.purs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import Prelude
44

55
import Effect (Effect)
66
import Effect.Exception (error, throwException)
7-
import CSS (Rendered, Path(..), Predicate(..), Refinement(..), Selector(..), FontFaceSrc(..), FontFaceFormat(..), renderedSheet, renderedInline, fromString, selector, block, display, render, borderBox, boxSizing, contentBox, blue, color, body, a, p, px, dashed, border, inlineBlock, red, (?), (##), (|>), (**), hover, fontFaceSrc, zIndex)
7+
import CSS (Rendered, Path(..), Predicate(..), Refinement(..), Selector(..), FontFaceSrc(..), FontFaceFormat(..), renderedSheet, renderedInline, fromString, selector, block, display, render, borderBox, boxSizing, contentBox, blue, color, body, a, p, px, dashed, border, inlineBlock, red, (?), (##), (|>), (**), hover, fontFaceSrc, fontStyle, deg, zIndex)
8+
import CSS.FontStyle as FontStyle
89
import Data.Maybe (Maybe(..))
910
import Data.NonEmpty (singleton)
1011

@@ -58,6 +59,18 @@ deepSelector = render do
5859
p ** a ? do
5960
display block
6061

62+
exampleFontStyle1 :: Rendered
63+
exampleFontStyle1 = render do
64+
fontStyle FontStyle.italic
65+
66+
exampleFontStyle2 :: Rendered
67+
exampleFontStyle2 = render do
68+
fontStyle FontStyle.oblique
69+
70+
exampleFontStyle3 :: Rendered
71+
exampleFontStyle3 = render do
72+
fontStyle $ FontStyle.obliqueAngle (deg 45.0)
73+
6174
nestedNodes :: Rendered
6275
nestedNodes = render do
6376
fromString "#parent" ? do
@@ -97,3 +110,7 @@ main = do
97110
renderedInline example6 `assertEqual` Just "src: url(\"font.woff\") format(\"woff\")"
98111

99112
renderedInline example7 `assertEqual` Just "z-index: 11"
113+
114+
renderedInline exampleFontStyle1 `assertEqual` Just "font-style: italic"
115+
renderedInline exampleFontStyle2 `assertEqual` Just "font-style: oblique"
116+
renderedInline exampleFontStyle3 `assertEqual` Just "font-style: oblique 45.0deg"

0 commit comments

Comments
 (0)