git.iko.soy/typeable/html-parse
on master help sign in
remove obsolete command and keygen elements from nonClosing list. Add haddocks comments about void and optional tags.
a381746Jeremy Shaw
Text/HTML/Parser.hs+1−1
@@ -55,7 +55,7 @@ type AttrValue = Text
55
55
56-- | An HTML token
56-- | An HTML token
57data Token
57data Token
58− -- | An opening tag. Attribute ordering is arbitrary.
58+ -- | An opening tag. Attribute ordering is arbitrary. Void elements have a 'TagOpen' but no corresponding 'TagClose'. See 'Text.HTML.Tree.nonClosing'.
59 = TagOpen !TagName [Attr]
59 = TagOpen !TagName [Attr]
60 -- | A self-closing tag.
60 -- | A self-closing tag.
61 | TagSelfClose !TagName [Attr]
61 | TagSelfClose !TagName [Attr]
Text/HTML/Tree.hs+21−3
@@ -16,7 +16,13 @@ import Data.Text (Text)
16import Data.Tree
16import Data.Tree
17import Text.HTML.Parser
17import Text.HTML.Parser
18
18
19−
19+-- | construct a 'Forest' from a 'Token' list.
20+--
21+-- This code correctly handles void elements. Void elements are required to have a start tag and must not have an end tag. See 'nonClosing'.
22+--
23+-- This code does __not__ correctly handle optional tags. It assumes all optional start and end tags are present.
24+--
25+-- <https:\/\/www.w3.org\/TR\/html52\/syntax.html#optional-tags>
20tokensToForest :: [Token] -> Either ParseTokenForestError (Forest Token)
26tokensToForest :: [Token] -> Either ParseTokenForestError (Forest Token)
21tokensToForest = f (PStack [] [])
27tokensToForest = f (PStack [] [])
22 where
28 where
@@ -33,8 +39,15 @@ tokensToForest = f (PStack [] [])
33 Comment _ -> f (pushFlatSibling t pstack) ts
39 Comment _ -> f (pushFlatSibling t pstack) ts
34 Doctype _ -> f (pushFlatSibling t pstack) ts
40 Doctype _ -> f (pushFlatSibling t pstack) ts
35
41
42+-- | void elements which must not have an end tag
43+--
44+-- This list does not include the obsolete @\<command\>@ and @\<keygen\>@ elements.
45+--
46+-- @ nonClosing = ["br", "hr", "img", "meta", "area", "base", "col", "embed", "input", "link", "param", "source", "track", "wbr"] @
47+--
48+-- <https:\/\/www.w3.org\/TR\/html52\/syntax.html#void-elements>
36nonClosing :: [Text]
49nonClosing :: [Text]
37−nonClosing = ["br", "hr", "img", "meta", "area", "base", "col", "command", "embed", "input", "keygen", "link", "param", "source", "track", "wbr"]
50+nonClosing = ["br", "hr", "img", "meta", "area", "base", "col", "embed", "input", "link", "param", "source", "track", "wbr"]
38
51
39data ParseTokenForestError =
52data ParseTokenForestError =
40 ParseTokenForestErrorBracketMismatch PStack (Maybe Token)
53 ParseTokenForestErrorBracketMismatch PStack (Maybe Token)
@@ -58,10 +71,15 @@ popParent n pstack
58pushFlatSibling :: Token -> PStack -> PStack
71pushFlatSibling :: Token -> PStack -> PStack
59pushFlatSibling t (PStack ss ps) = PStack (Node t [] : ss) ps
72pushFlatSibling t (PStack ss ps) = PStack (Node t [] : ss) ps
60
73
61−
74+-- | convert a 'Forest' of 'Token' into a list of 'Token'.
75+--
76+-- This code correctly handles void elements. Void elements are required to have a start tag and must not have an end tag. See 'nonClosing'.
62tokensFromForest :: Forest Token -> [Token]
77tokensFromForest :: Forest Token -> [Token]
63tokensFromForest = mconcat . fmap tokensFromTree
78tokensFromForest = mconcat . fmap tokensFromTree
64
79
80+-- | convert a 'Tree' of 'Token' into a list of 'Token'.
81+--
82+-- This code correctly handles void elements. Void elements are required to have a start tag and must not have an end tag. See 'nonClosing'.
65tokensFromTree :: Tree Token -> [Token]
83tokensFromTree :: Tree Token -> [Token]
66tokensFromTree (Node o@(TagOpen n _) ts) | n `notElem` nonClosing
84tokensFromTree (Node o@(TagOpen n _) ts) | n `notElem` nonClosing
67 = [o] <> tokensFromForest ts <> [TagClose n]
85 = [o] <> tokensFromForest ts <> [TagClose n]