Skip to content

Commit

Permalink
Adds Not type class, Fixes #239 (#240)
Browse files Browse the repository at this point in the history
Adds Not type class, Fixes #239

---------

Co-authored-by: Manuel Bärenz <[email protected]>
  • Loading branch information
Skyfold and turion authored Oct 24, 2024
1 parent 21c113c commit 7f646d4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
19 changes: 19 additions & 0 deletions spec/Clay/PseudoSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{-# LANGUAGE OverloadedStrings, OverloadedLists, TypeApplications #-}
module Clay.PseudoSpec where

import Test.Hspec
import Clay
import Common
import Prelude hiding (not, div)

spec :: Spec
spec = do
describe "not pseudo selector" $ do
describe "applied to a selector" $ do
":not(p){display:none}" `shouldRenderFrom` not p & display none
describe "applied to a refinement" $ do
"input:not(:checked){display:none}" `shouldRenderFrom` input # not checked ? display none
describe "applied to both a selector and a refinement" $ do
":not(#some-input:not(:checked)){display:none}" `shouldRenderFrom` not ("#some-input" # not checked) & display none
describe "applied to a overloaded string" $ do
"#some-input:not(:checked) ~ #some-div > div:not(.test){display:none}" `shouldRenderFrom` "#some-input" # not checked |~ "#some-div" |> div # not @Clay.Selector ".test" ? display none
22 changes: 19 additions & 3 deletions src/Clay/Pseudo.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
module Clay.Pseudo where

import Data.Text (Text)

import Clay.Render (renderSelector)
import Clay.Render (renderSelector, renderRefinement)
import Clay.Selector

import qualified Data.Text.Lazy as Lazy
Expand Down Expand Up @@ -64,5 +66,19 @@ nthLastChild n = func "nth-last-child" [n]
nthLastOfType n = func "nth-last-of-type" [n]
nthOfType n = func "nth-of-type" [n]

not :: Selector -> Refinement
not r = func "not" [Lazy.toStrict (renderSelector r)]
-- | The 'not' pseudo selector can be applied to both a 'Refinement'
--
-- > input # not checked
--
-- or a 'Selector'
--
-- > not p

class Not a where
not :: a -> Refinement

instance Not Selector where
not r = func "not" [Lazy.toStrict (renderSelector r)]

instance Not Refinement where
not r = func "not" (Lazy.toStrict <$> renderRefinement r)
5 changes: 5 additions & 0 deletions src/Clay/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Clay.Render
, putCss
, renderWith
, renderSelector
, renderRefinement
, withBanner
)
where
Expand Down Expand Up @@ -120,6 +121,10 @@ renderWith cfg top
renderSelector :: Selector -> Lazy.Text
renderSelector = toLazyText . selector compact

-- | Render a CSS `Refinement`.
renderRefinement :: Refinement -> [Lazy.Text]
renderRefinement r = toLazyText . predicate <$> unFilter r

-------------------------------------------------------------------------------

renderBanner :: Config -> Lazy.Text -> Lazy.Text
Expand Down

0 comments on commit 7f646d4

Please sign in to comment.