module Text.Seonbi.Html.Wrapper
    ( isWrappedBy
    , isWrappedBy'
    , wrap
    ) where

import Text.Seonbi.Html
import Text.Seonbi.Html.TagStack

-- | Wraps given entities with an element.
wrap :: HtmlTagStack -> HtmlTag -> HtmlRawAttrs -> [HtmlEntity] -> [HtmlEntity]
wrap :: HtmlTagStack
-> HtmlTag -> HtmlRawAttrs -> [HtmlEntity] -> [HtmlEntity]
wrap HtmlTagStack
baseStack HtmlTag
tag' HtmlRawAttrs
attributes [HtmlEntity]
entities = (:)
    (HtmlTagStack -> HtmlTag -> HtmlRawAttrs -> HtmlEntity
HtmlStartTag HtmlTagStack
baseStack HtmlTag
tag' HtmlRawAttrs
attributes)
    [ HtmlEntity
e { tagStack :: HtmlTagStack
tagStack = HtmlTagStack -> HtmlTagStack
rebase' (HtmlEntity -> HtmlTagStack
tagStack HtmlEntity
e) }
    | HtmlEntity
e <- [HtmlEntity]
entities
    ] forall a. [a] -> [a] -> [a]
++ [HtmlTagStack -> HtmlTag -> HtmlEntity
HtmlEndTag HtmlTagStack
baseStack HtmlTag
tag']
  where
    newBaseStack :: HtmlTagStack
    newBaseStack :: HtmlTagStack
newBaseStack = HtmlTag -> HtmlTagStack -> HtmlTagStack
push HtmlTag
tag' HtmlTagStack
baseStack
    rebase' :: HtmlTagStack -> HtmlTagStack
    rebase' :: HtmlTagStack -> HtmlTagStack
rebase' = HtmlTagStack -> HtmlTagStack -> HtmlTagStack -> HtmlTagStack
rebase HtmlTagStack
baseStack HtmlTagStack
newBaseStack

-- | A shortcut to 'isWrappedBy'' of wildcard attributes match.
isWrappedBy :: [HtmlEntity] -> HtmlTag -> Bool
isWrappedBy :: [HtmlEntity] -> HtmlTag -> Bool
isWrappedBy [HtmlEntity]
entities HtmlTag
tag' =
    [HtmlEntity] -> HtmlTag -> Maybe HtmlRawAttrs -> Bool
isWrappedBy' [HtmlEntity]
entities HtmlTag
tag' forall a. Maybe a
Nothing

-- | 'True' if the given @['HtmlEntity']@ is wrapped by a tag and attributes.
-- E.g.:
--
-- >>> :set -XOverloadedLists
-- >>> :set -XOverloadedStrings
-- >>> :{
-- let entities = 
--         [ HtmlStartTag [] Em " id=foo"
--         , HtmlText [Em] "Hello"
--         , HtmlEndTag [] Em
--         ] :: [HtmlEntity]
-- :}
--
-- >>> isWrappedBy' entities Em $ Just " id=foo"
-- True
-- >>> isWrappedBy' entities Div $ Just " id=foo"
-- False
-- >>> isWrappedBy' entities Em $ Just " id=wrong"
-- False
--
-- In order to match to any attributes (wildcard match), give 'Nothing' to
-- the third argument:
--
-- >>> isWrappedBy' entities Em Nothing
-- True
-- >>> isWrappedBy' entities Span Nothing
-- False
--
-- Or you can use 'isWrappedBy' function which is a shortcut for that.
isWrappedBy' :: [HtmlEntity] -> HtmlTag -> Maybe HtmlRawAttrs -> Bool
isWrappedBy' :: [HtmlEntity] -> HtmlTag -> Maybe HtmlRawAttrs -> Bool
isWrappedBy' entities :: [HtmlEntity]
entities@(HtmlStartTag HtmlTagStack
s HtmlTag
t HtmlRawAttrs
a : [HtmlEntity]
_) HtmlTag
tag' Maybe HtmlRawAttrs
attributes =
    case forall a. [a] -> a
Prelude.last [HtmlEntity]
entities of
        HtmlEndTag HtmlTagStack
s' HtmlTag
t' ->
            HtmlTag
t forall a. Eq a => a -> a -> Bool
== HtmlTag
tag' Bool -> Bool -> Bool
&& HtmlTag
t' forall a. Eq a => a -> a -> Bool
== HtmlTag
tag' Bool -> Bool -> Bool
&& HtmlTagStack
s forall a. Eq a => a -> a -> Bool
== HtmlTagStack
s' Bool -> Bool -> Bool
&& forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (forall a. Eq a => a -> a -> Bool
== HtmlRawAttrs
a) Maybe HtmlRawAttrs
attributes
        HtmlEntity
_ ->
            Bool
False
isWrappedBy' [HtmlEntity]
_ HtmlTag
_ Maybe HtmlRawAttrs
_ = Bool
False