@@ -5,12 +5,14 @@ module Text.HTML.Tree
55 ( -- * Constructing forests
66 tokensToForest
77 , ParseTokenForestError(..), PStack(..)
8+ , nonClosing
89 -- * Deconstructing forests
910 , tokensFromForest
1011 , tokensFromTree
1112 ) where
1213
1314import Data.Monoid
15+import Data.Text (Text)
1416import Data.Tree
1517import Text.HTML.Parser
1618
@@ -21,16 +23,18 @@ tokensToForest = f (PStack [] [])
2123 f (PStack ss []) [] = Right (reverse ss)
2224 f pstack [] = Left $ ParseTokenForestErrorBracketMismatch pstack Nothing
2325 f pstack (t : ts) = case t of
24− TagOpen "br" _ -> f (pushFlatSibling t pstack) ts
25− TagOpen "hr" _ -> f (pushFlatSibling t pstack) ts
26− TagOpen "img" _ -> f (pushFlatSibling t pstack) ts
27− TagOpen _ _ -> f (pushParent t pstack) ts
26+ TagOpen n _ -> if n `elem` nonClosing
27+ then f (pushFlatSibling t pstack) ts
28+ else f (pushParent t pstack) ts
2829 TagClose n -> (`f` ts) =<< popParent n pstack
2930 ContentChar _ -> f (pushFlatSibling t pstack) ts
3031 ContentText _ -> f (pushFlatSibling t pstack) ts
3132 Comment _ -> f (pushFlatSibling t pstack) ts
3233 Doctype _ -> f (pushFlatSibling t pstack) ts
3334
35+nonClosing :: [Text]
36+nonClosing = ["br", "hr", "img"]
37+
3438data ParseTokenForestError =
3539 ParseTokenForestErrorBracketMismatch PStack (Maybe Token)
3640 deriving (Eq, Show)
@@ -58,7 +62,7 @@ tokensFromForest :: Forest Token -> [Token]
5862tokensFromForest = mconcat . fmap tokensFromTree
5963
6064tokensFromTree :: Tree Token -> [Token]
61−tokensFromTree (Node o@(TagOpen n _) ts)
65+tokensFromTree (Node o@(TagOpen n _) ts) | n `notElem` nonClosing
6266 = [o] <> tokensFromForest ts <> [TagClose n]
6367tokensFromTree (Node t [])
6468 = [t]