packages feed

mangrove-0.1.0.0: src/Web/Mangrove/Parse/Tree/BeforeHead.hs

{-|
Description:    Token processing rules between the opening @\<html\>@ and @\<head\>@ tags.

Copyright:      (c) 2020 Sam May
License:        MPL-2.0
Maintainer:     ag.eitilt@gmail.com

Stability:      stable
Portability:    portable
-}
module Web.Mangrove.Parse.Tree.BeforeHead
    ( treeBeforeHead
    ) where


import qualified Data.Text as T

import Web.Mangrove.Parse.Common.Error
import Web.Mangrove.Parse.Tokenize.Common
import Web.Mangrove.Parse.Tree.Common
import Web.Mangrove.Parse.Tree.InBody
import Web.Mangrove.Parse.Tree.Patch
import Web.Willow.Common.Parser
import Web.Willow.Common.Parser.Switch


-- | __HTML:__
--      @[the "before head" insertion mode]
--      (https://html.spec.whatwg.org/multipage/parsing.html#the-before-head-insertion-mode)@
-- 
-- The parsing instructions corresponding to the 'BeforeHead' section of the
-- state machine.
treeBeforeHead :: TreeBuilder TreeOutput
treeBeforeHead = next >>= switch
    [ If isWhitespace $ packTreeErrors []
    , If isComment insertComment
    , If isDoctype $ \t' ->
        packTreeErrors [UnexpectedDoctype $ tokenDocumentType t'] t'
    , If (isStartTag ["html"]) $ \t' -> do
        push t'
        treeInBody
    , If (isStartTag ["head"]) $ \t' -> do
        switchMode InHead
        insertHeadElement t'
    , If (isEndTag ["head", "body", "html", "br"]) anythingElse
    , If isAnyEndTag $ \t' ->
        packTreeErrors [UnmatchedEndTag $ tokenElement t'] t'
    , Else anythingElse
    ]
  where anythingElse t' = do
            push t'
            switchMode InHead
            ps <- insertHeadElement_ $ emptyTagParams
                { tagName = T.pack "head"
                }
            packTree_ ps