module Text.HTML.Scalpel.Internal.Select.Combinators (
(//)
, (@:)
, (@=)
, (@=~)
, hasClass
) where
import Text.HTML.Scalpel.Internal.Select.Types
import qualified Data.Text as T
import qualified Text.Regex.Base.RegexLike as RE
import qualified Text.StringLike as TagSoup
(@:) :: TagName tag => tag -> [AttributePredicate] -> Selector
(@:) tag attrs = MkSelector [toSelectNode tag attrs]
infixl 9 @:
(@=) :: AttributeName key => key -> String -> AttributePredicate
(@=) key value = MkAttributePredicate $ \(attrKey, attrValue) ->
matchKey key attrKey
&& TagSoup.fromString value == attrValue
infixl 6 @=
(@=~) :: (AttributeName key, RE.RegexLike re String)
=> key -> re -> AttributePredicate
(@=~) key re = MkAttributePredicate $ \(attrKey, attrValue) ->
matchKey key attrKey
&& RE.matchTest re (TagSoup.toString attrValue)
infixl 6 @=~
(//) :: (Selectable a, Selectable b) => a -> b -> Selector
(//) a b = MkSelector (as ++ bs)
where (MkSelector as) = toSelector a
(MkSelector bs) = toSelector b
infixl 5 //
hasClass :: String -> AttributePredicate
hasClass clazz = MkAttributePredicate hasClass'
where
hasClass' (attrName, classes)
| "class" == TagSoup.toString attrName = textClass `elem` classList
| otherwise = False
where textClass = TagSoup.castString clazz
textClasses = TagSoup.castString classes
classList = T.split (== ' ') textClasses