packages feed

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

{-# LANGUAGE OverloadedStrings #-}

{-|
Description:    Token processing rules for a @\<tr\>@ data segment in a table.

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

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


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


-- | __HTML:__
--      @[the "in row" insertion mode]
--      (https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intr)@
-- 
-- The parsing instructions corresponding to the 'InRow' section of the state
-- machine.
treeInRow :: TreeBuilder TreeOutput
treeInRow = next >>= switch
    [ If (isStartTag ["th", "td"]) $ \t' -> do
        switchMode InCell
        clear <- clearToContext tableRowContext
        insertFormattingMarker
        insert <- insertElement t'
        return $ clear ++| insert
    , If (isEndTag ["tr"]) $ \t' -> do
        hasTr <- hasInTableScope ["tr"]
        if hasTr
            then do
                switchMode InTableBody
                close <- closeTableRow
                packTree t' close
            else packTreeErrors [UnmatchedEndTag $ tokenElement t'] t'
    , If (isStartTag
        [ "caption"
        , "col"
        , "colgroup"
        , "tbody"
        , "tfoot"
        , "thead"
        , "tr"
        ]) $ \t' -> do
            hasTr <- hasInTableScope ["tr"]
            if hasTr
                then do
                    push t'
                    switchMode InTableBody
                    close <- closeTableRow
                    packTree_ close
                else packTreeErrors [MalformedTableStructure $ tokenElement t'] t'
    , If (isEndTag ["table"]) $ \t' -> do
        hasTr <- hasInTableScope ["tr"]
        if hasTr
            then do
                push t'
                switchMode InTableBody
                close <- closeTableRow
                packTree_ close
            else packTreeErrors [MalformedTableStructure $ tokenElement t'] t'
    , If (isEndTag ["tbody", "tfoot", "thead"]) $ \t' -> do
        hasMatch <- hasInTableScope [tagName $ tokenTag t']
        if hasMatch
            then do
                push t'
                switchMode InTableBody
                close <- closeTableRow
                packTree_ close
            else packTreeErrors [UnmatchedEndTag $ tokenElement t'] t'
    , If (isEndTag
        [ "body"
        , "caption"
        , "col"
        , "colgroup"
        , "html"
        , "td"
        , "th"
        ]) $ \t' ->
            packTreeErrors [UnexpectedEndTag $ tokenElement t'] t'
    , Else $ \t' -> do
        push t'
        treeInTable
    ]
  where closeTableRow = do
            clear <- clearToContext tableRowContext
            close <- closeCurrentNode_
            return $ clear ++ close