~alcinnz/haskell-stylist

fcbba23e577176b22b66ce920fe933c38df2867f — Adrian Cochrane 4 years ago b93db8b
Implement style tree flattening utility.

This will be useful in Rhapsode for making sure voice synthesizers continue to
have full voice features information as the user jumps around the document.
And it will be useful for visual browsers in transitioning from layout to render.
1 files changed, 8 insertions(+), 1 deletions(-)

M src/Data/CSS/StyleTree.hs
M src/Data/CSS/StyleTree.hs => src/Data/CSS/StyleTree.hs +8 -1
@@ 1,7 1,7 @@
-- | Abstracts away tree traversals.
-- Mostly used by callers including (soon) XML Conduit Stylist,
-- but also used internally for generating counter text.
module Data.CSS.StyleTree(StyleTree(..), treeOrder, treeOrder', Path, treeMap) where
module Data.CSS.StyleTree(StyleTree(..), treeOrder, treeOrder', Path, treeMap, treeFlatten) where

data StyleTree p = StyleTree {
    style :: p,


@@ 26,3 26,10 @@ treeOrder' _ _ _ [] _ = error "Invalid path during tree traversal!"

treeMap :: (p -> p') -> StyleTree p -> StyleTree p'
treeMap cb = treeOrder (\_ _ _ p -> ((), cb p)) ()

treeFlatten :: StyleTree p -> [p]
treeFlatten = treeFlatten' . children
treeFlatten' :: [StyleTree p] -> [p]
treeFlatten' (StyleTree p []:ps) = p : treeFlatten' ps
treeFlatten' (StyleTree _ childs:sibs) = treeFlatten' childs ++ treeFlatten' sibs
treeFlatten' [] = []