Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Not type class, Fixes #239 #240

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading