module Text.CxML.HTML where
import Text.CxML.Types
import Text.CxML.Tags(input)
hText = HText . escapeBrackets
escapeBrackets :: String -> String
escapeBrackets [] = []
escapeBrackets ('<':t) = "<"++escapeBrackets t
escapeBrackets ('>':t) = ">"++escapeBrackets t
escapeBrackets ('&':t) = "&"++escapeBrackets t
escapeBrackets ('\'':t) = "'"++escapeBrackets t
escapeBrackets ('"':t) = "&qout;"++escapeBrackets t
escapeBrackets ('(':'C':')':t) = "©"++escapeBrackets t --copyright
escapeBrackets (h:t) = h:(escapeBrackets t)
t :: String-> CxML a
t str = CxML (\_->[hText str],[],[],[])
modHElems :: (HElem->HElem)->CxML a->CxML a
modHElems f (CxML (h,ts,c,j)) = CxML (\ctx->map f $ h ctx, ts, c, j)
class CxMLChild a b where
(//) :: CxML a -> b -> CxML a
instance CxMLChild a (CxML a)where
p//c= withChildren p [c]
instance CxMLChild a [CxML a]where
p//c= withChildren p c
instance CxMLChild a [Char]where
p//c= withChildren p [t c]
instance CxMLChild a [[Char]] where
p//c= withChildren p $ map t c
infixl 5 //
setAttrOp :: String -> (CxML a) -> String -> (CxML a)
setAttrOp at= \tag-> \val->
CxML (\ctx-> map (add_attr at val) $ htm tag ctx, titleParts tag, css tag,js tag )
add_attr :: String->String->HElem->HElem
add_attr _ _ s@(HText str)= s
add_attr nm vl (HTag tn ats chs) = HTag tn (add_attr' nm vl ats) chs
where
add_attr' nm vl [] = [(nm,vl)]
add_attr' nm vl ((nm',vl'):ats)
= if (nm==nm')
then (nm,vl' ++ " " ++ vl):ats
else (nm',vl'): add_attr' nm vl ats
infixl 8 ^#
(^#)=setAttrOp "id"
infixl 8 ^.
(^.)=setAttrOp "class"
infixl 7 ^>
(^>)=setAttrOp "href"
infixl 8 !
(!) :: (CxML a)->(String, String)->(CxML a)
tag ! (at,vl) = CxML (\ctx-> map (add_attr at vl) $ htm tag ctx, titleParts tag, css tag, js tag)
widget :: String -> String -> [(String,String)] -> CxML a
widget w n markupAttrs = foldl (!) input (("type",w):("name",n):("id",n):markupAttrs)
textfield n = widget "text" n []
hidden n v = widget "hidden" n [("value",v)]
afile n = widget "file" n []