packages feed

mangrove-0.1.0.0: src/Web/Mangrove/Parse/Tokenize/ScriptDataEscaped.hs

{-|
Description:    Tokenization rules for characters within @\<!--@ ... @--\>@
                sections in script data.

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

Stability:      stable
Portability:    portable
-}
module Web.Mangrove.Parse.Tokenize.ScriptDataEscaped
    ( tokenScriptDataEscaped
    , tokenScriptDataEscapeStart
    ) where


import Web.Mangrove.Parse.Common.Error
import Web.Mangrove.Parse.Tokenize.Common
import Web.Mangrove.Parse.Tokenize.ScriptDataDoubleEscaped
import Web.Mangrove.Parse.Tokenize.Tag
import Web.Willow.Common.Encoding.Character


-- | __HTML:__
--      @[script data escape start state]
--      (https://html.spec.whatwg.org/multipage/parsing.html#script-data-escape-start-state)@
-- 
-- The parsing instructions for after reading @"\<!"@ in the 'ScriptDataState'
-- section of the state machine.
tokenScriptDataEscapeStart :: Tokenizer [TokenizerOutput Token]
tokenScriptDataEscapeStart = tokenizers (Just [([], EndOfStream)])
    [ ifs_ (== '-') $ consEmit ([], Character '-') tokenScriptDataEscapeStartDash
    , elsePush_ $ return []
    ]

-- | __HTML:__
--      @[script data escape start dash state]
--      (https://html.spec.whatwg.org/multipage/parsing.html#script-data-escape-start-state)@
-- 
-- The parsing instructions for after reading @"\<!-"@ in the 'ScriptDataState'
-- section of the state machine.
tokenScriptDataEscapeStartDash :: Tokenizer [TokenizerOutput Token]
tokenScriptDataEscapeStartDash = tokenizers (Just [([], EndOfStream)])
    [ ifs_ (== '-') $ changeState ScriptDataEscapedState *> emit' ([], Character '-')
    , elsePush_ $ return []
    ]

-- | __HTML:__
--      @[script data escaped state]
--      (https://html.spec.whatwg.org/multipage/parsing.html#script-data-escaped-state)@
-- 
-- The parsing instructions rooted in the 'ScriptDataEscapedState' section of
-- the state machine.
tokenScriptDataEscaped :: Tokenizer [TokenizerOutput Token]
tokenScriptDataEscaped = tokenizers (Just [([EOFInScriptHtmlCommentLikeText], EndOfStream)])
    [ ifs_ (== '-') $ consEmit ([], Character '-') tokenScriptDataEscapedDash
    , ifs_ (== '<') tokenScriptDataEscapedLessThanSign
    , ifs_ (== '\NUL') $ emit' ([UnexpectedNullCharacter], Character replacementChar)
    , elsesChar $ \c -> emit' ([], Character c)
    ]

-- | __HTML:__
--      @[script data escaped dash state]
--      (https://html.spec.whatwg.org/multipage/parsing.html#script-data-escaped-dash-state)@
-- 
-- The parsing instructions for after reading @"-"@ in the
-- 'ScriptDataEscapedState' section of the state machine.
tokenScriptDataEscapedDash :: Tokenizer [TokenizerOutput Token]
tokenScriptDataEscapedDash = tokenizers (Just [([EOFInScriptHtmlCommentLikeText], EndOfStream)])
    [ ifs_ (== '-') $ consEmit ([], Character '-') tokenScriptDataEscapedDashDash
    , ifPush_ (== '<') $ return []
    , ifPush_ (== '\NUL') $ return []
    , elsePush_ $ return []
    ]

-- | __HTML:__
--      @[script data escaped dash dash state]
--      (https://html.spec.whatwg.org/multipage/parsing.html#script-data-escaped-dash-dash-state)@
-- 
-- The parsing instructions for after reading @"--"@ in the
-- 'ScriptDataEscapedState' section of the state machine.
tokenScriptDataEscapedDashDash :: Tokenizer [TokenizerOutput Token]
tokenScriptDataEscapedDashDash = tokenizers (Just [([EOFInScriptHtmlCommentLikeText], EndOfStream)])
    [ ifs_ (== '-') $ consEmit ([], Character '-') tokenScriptDataEscapedDashDash
    , ifPush_ (== '<') $ return []
    , ifs_ (== '>') $ changeState ScriptDataState *> emit' ([], Character '>')
    , ifPush_ (== '\NUL') $ return []
    , elsePush_ $ return []
    ]

-- | __HTML:__
--      @[script data escaped less-than state]
--      (https://html.spec.whatwg.org/multipage/parsing.html#script-data-escaped-less-than-state)@
-- 
-- The parsing instructions for after reading @"\<"@ in the
-- 'ScriptDataEscapedState' section of the state machine.
tokenScriptDataEscapedLessThanSign :: Tokenizer [TokenizerOutput Token]
tokenScriptDataEscapedLessThanSign = tokenizers (Just [([], Character '<')])
    [ ifs_ (== '/') tokenAppropriateEndTagOpen
    , ifPush_ isAsciiAlpha $ consEmit ([], Character '<') tokenScriptDataDoubleEscapeStart
    , elsePush_ $ emit' ([], Character '<')
    ]