module Hasmin.Types.Selector (
Selector(..)
, SimpleSelector(..)
, CompoundSelector
, Combinator(..)
, Sign(..)
, AnPlusB(..)
, Att(..)
, specialPseudoElements
) where
import Control.Applicative (liftA2)
import Control.Monad.Reader (ask)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Lazy.Builder (fromText, singleton, Builder)
import Data.Monoid ((<>))
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Data.List.NonEmpty as N
import Hasmin.Config
import Hasmin.Types.Class
import Hasmin.Types.String
import Hasmin.Utils
data Combinator = Descendant
| Child
| AdjacentSibling
| GeneralSibling
deriving (Eq, Show)
instance ToText Combinator where
toBuilder Descendant = " "
toBuilder Child = ">"
toBuilder AdjacentSibling = "+"
toBuilder GeneralSibling = "~"
data Selector = Selector CompoundSelector [(Combinator, CompoundSelector)]
deriving (Eq, Show)
instance Ord Selector where
s1 <= s2 = toText s1 <= toText s2
instance ToText Selector where
toBuilder (Selector cs ccss) = toBuilder cs
<> mconcat (fmap build ccss)
where build (comb, compSel) = toBuilder comb <> toBuilder compSel
instance Minifiable Selector where
minifyWith (Selector c xs) = do
newC <- minifyWith c
newCs <- (mapM . mapM) minifyWith xs
pure $ Selector newC newCs
type CompoundSelector = NonEmpty SimpleSelector
instance ToText CompoundSelector where
toBuilder ns@(Universal{} :| xs)
| length ns > 1 = mconcat $ fmap toBuilder xs
toBuilder ns = mconcat $ N.toList (fmap toBuilder ns)
instance Minifiable CompoundSelector where
minifyWith (a :| xs) = liftA2 (:|) (minifyWith a) (mapM minifyWith xs)
type Namespace = Text
type Element = Text
type Identifier = Text
data SimpleSelector = Type Namespace Element
| Universal Namespace
| AttributeSel Att
| ClassSel Identifier
| IdSel Identifier
| PseudoElem Identifier
| PseudoClass Identifier
| Lang (Either Text StringType)
| FunctionalPseudoClass Identifier Text
| FunctionalPseudoClass1 Identifier [CompoundSelector]
| FunctionalPseudoClass2 Identifier AnPlusB
| FunctionalPseudoClass3 Identifier AnPlusB [CompoundSelector]
deriving (Eq, Show)
instance ToText SimpleSelector where
toBuilder (Type n e)
| T.null n = fromText e
| otherwise = fromText n <> singleton '|' <> fromText e
toBuilder (Universal n)
| T.null n = singleton '*'
| otherwise = fromText n <> fromText "|*"
toBuilder (AttributeSel att) = singleton '[' <> toBuilder att <> singleton ']'
toBuilder (ClassSel t) = singleton '.' <> fromText t
toBuilder (IdSel t) = singleton '#' <> fromText t
toBuilder (PseudoClass t) = singleton ':' <> fromText t
toBuilder (PseudoElem t)
| T.toCaseFold t `elem` specialPseudoElements = fromText ":" <> fromText t
| otherwise = fromText "::" <> fromText t
toBuilder (Lang x) = ":lang" <> singleton '(' <> toBuilder x <> singleton ')'
toBuilder (FunctionalPseudoClass t x) = fromText t <> singleton '(' <> fromText x <> singleton ')'
toBuilder (FunctionalPseudoClass1 t ss) = singleton ':' <> fromText t <> singleton '('
<> mconcatIntersperse toBuilder (singleton ',') ss
<> singleton ')'
toBuilder (FunctionalPseudoClass2 t x) = singleton ':' <> fromText t
<> singleton '(' <> toBuilder x <> singleton ')'
toBuilder (FunctionalPseudoClass3 t a xs) = singleton ':' <> fromText t
<> singleton '(' <> toBuilder a <> f xs <> singleton ')'
where f [] = mempty
f (y:ys) = " of " <> toBuilder y
<> mconcat (fmap (\z -> singleton ',' <> toBuilder z) ys)
specialPseudoElements :: [Text]
specialPseudoElements = fmap T.toCaseFold
["after", "before", "first-line", "first-letter"]
instance Minifiable SimpleSelector where
minifyWith a@(AttributeSel att) = do
conf <- ask
pure $ if shouldRemoveQuotes conf
then AttributeSel (removeAttributeQuotes att)
else a
minifyWith a@(Lang x) = do
conf <- ask
pure $ if shouldRemoveQuotes conf
then case x of
Left _ -> a
Right s -> Lang (removeQuotes s)
else a
minifyWith (FunctionalPseudoClass1 i cs) = FunctionalPseudoClass1 i <$> mapM minifyWith cs
minifyWith (FunctionalPseudoClass2 i n) = FunctionalPseudoClass2 i <$> minifyWith n
minifyWith (FunctionalPseudoClass3 i n cs) = FunctionalPseudoClass3 i <$> minifyWith n <*> pure cs
minifyWith x = pure x
data Sign = Plus | Minus
deriving (Eq, Show)
isPositive :: Maybe Sign -> Bool
isPositive Nothing = True
isPositive (Just Plus) = True
isPositive (Just Minus) = False
instance ToText Sign where
toBuilder Plus = singleton '+'
toBuilder Minus = singleton '-'
data AnPlusB = Even
| Odd
| A (Maybe Sign) (Maybe Int)
| B Int
| AB (Maybe Sign) (Maybe Int) Int
deriving (Eq, Show)
instance ToText AnPlusB where
toBuilder Even = "even"
toBuilder Odd = "odd"
toBuilder (B b) = toBuilder b
toBuilder (A ms mi) = an2Builder ms mi
toBuilder (AB ms mi b) = an2Builder ms mi <> bSign b <> toBuilder b
where bSign x
| x < 0 = mempty
| otherwise = singleton '+'
an2Builder :: Maybe Sign -> Maybe Int -> Builder
an2Builder ms mi = maybeToBuilder ms <> maybeToBuilder mi <> singleton 'n'
where maybeToBuilder :: ToText a => Maybe a -> Builder
maybeToBuilder = maybe mempty toBuilder
instance Minifiable AnPlusB where
minifyWith x = do
conf <- ask
pure $ if shouldMinifyMicrosyntax conf
then minifyAnPlusB x
else x
minifyAnPlusB :: AnPlusB -> AnPlusB
minifyAnPlusB Even = A Nothing (Just 2)
minifyAnPlusB (A ms mi) =
case mi of
Just 0 -> B 0
_ -> uncurry A (minifyAN ms mi)
minifyAnPlusB (AB _ (Just 0) b) = B b
minifyAnPlusB (AB ms mi b)
| isPositive ms && mi == Just 2 =
if b == 1 || b < 0 && odd b
then Odd
else if even b && b <= 0
then minifyAnPlusB Even
else AB ms' mi' b
| otherwise = if b == 0
then A ms' mi'
else AB ms' mi' b
where (ms', mi') = minifyAN ms mi
minifyAnPlusB x = x
minifyAN :: Maybe Sign -> Maybe Int -> (Maybe Sign, Maybe Int)
minifyAN (Just Plus) i = minifyAN Nothing i
minifyAN s (Just 1) = minifyAN s Nothing
minifyAN s i = (s, i)
type AttId = Text
type AttValue = Either Text StringType
data Att = Attribute AttId
| AttId :=: AttValue
| AttId :~=: AttValue
| AttId :|=: AttValue
| AttId :^=: AttValue
| AttId :$=: AttValue
| AttId :*=: AttValue
deriving (Eq, Show)
instance ToText Att where
toBuilder (Attribute t) = fromText t
toBuilder (attid :=: attval) = fromText attid <> singleton '=' <> toBuilder attval
toBuilder (attid :~=: attval) = fromText attid <> fromText "~=" <> toBuilder attval
toBuilder (attid :|=: attval) = fromText attid <> fromText "|=" <> toBuilder attval
toBuilder (attid :^=: attval) = fromText attid <> fromText "^=" <> toBuilder attval
toBuilder (attid :$=: attval) = fromText attid <> fromText "$=" <> toBuilder attval
toBuilder (attid :*=: attval) = fromText attid <> fromText "*=" <> toBuilder attval
removeAttributeQuotes :: Att -> Att
removeAttributeQuotes (attId :=: val) = attId :=: either Left removeQuotes val
removeAttributeQuotes (attId :~=: val) = attId :~=: either Left removeQuotes val
removeAttributeQuotes (attId :|=: val) = attId :|=: either Left removeQuotes val
removeAttributeQuotes (attId :^=: val) = attId :^=: either Left removeQuotes val
removeAttributeQuotes (attId :$=: val) = attId :$=: either Left removeQuotes val
removeAttributeQuotes (attId :*=: val) = attId :*=: either Left removeQuotes val
removeAttributeQuotes a@Attribute{} = a