From d7ce78638b82ba58283b9c197ace8be368f6e5dc Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 8 May 2020 21:34:09 +1200 Subject: [PATCH] XML Conduit Stylist functions upstream. --- xml-conduit-stylist/src/Data/HTML2CSS.hs | 26 +++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/xml-conduit-stylist/src/Data/HTML2CSS.hs b/xml-conduit-stylist/src/Data/HTML2CSS.hs index 1102e17..c9ec8d3 100644 --- a/xml-conduit-stylist/src/Data/HTML2CSS.hs +++ b/xml-conduit-stylist/src/Data/HTML2CSS.hs @@ -2,7 +2,8 @@ -- | Bindings from `xml-conduit` to `haskell-stylist`. module Data.HTML2CSS( html2css, cssPriorityAgent, cssPriorityUser, cssPriorityAuthor, -- parsing - preorder, el2styletree, els2stylist, el2stylist, stylize, stylize', stylizeEl -- application + preorder, el2styletree, els2stylist, el2stylist, stylize, stylize', stylizeEl, -- application + inlinePseudos, stylizeNoPseudos, stylizeElNoPseudos ) where import qualified Data.List as L @@ -65,15 +66,6 @@ strContent [] = "" ---- Styling -preorder :: (Maybe b -> Maybe b -> a -> b) -> StyleTree a -> StyleTree b -preorder cb self = head $ preorder' cb Nothing Nothing [self] -preorder' :: (Maybe b -> Maybe b -> a -> b) -> - Maybe b -> Maybe b -> [StyleTree a] -> [StyleTree b] -preorder' cb parent previous (self:sibs) = let self' = cb parent previous $ style self - in StyleTree self' (preorder' cb (Just self') Nothing $ children self) : - preorder' cb parent (Just self') sibs -preorder' _ _ _ [] = [] - el2styletree el = StyleTree (Left el) $ mapMaybe node2styletree $ XML.elementNodes el node2styletree (XML.NodeElement el) = Just $ el2styletree el node2styletree (XML.NodeContent txt) = Just $ StyleTree (Right [("content", [String txt])]) [] @@ -110,3 +102,17 @@ stylize' stylesheet parent _ el = ("", base) : [ overrides = concat [fst $ parseProperties' $ tokenize $ Txt.pack val | Attribute "style" _ val <- attributes el] stylizeEl stylesheet = stylize stylesheet . el2stylist + +inlinePseudos :: PropertyParser s => StyleTree [(Txt.Text, VarParser s)] -> StyleTree s +inlinePseudos (StyleTree self childs) = StyleTree { + style = fromMaybe temp $ innerParser <$> lookup "" self, + children = pseudo "before" ++ map inlinePseudos childs ++ pseudo "after" + } where + pseudo n + | Just style <- innerParser <$> lookup n self, + Just style' <- longhand style style "::" [Ident n] = [StyleTree style' []] + | Just style <- innerParser <$> lookup n self = [StyleTree style []] + | otherwise = [] + +stylizeNoPseudos css = inlinePseudos . stylize css +stylizeElNoPseudos css = inlinePseudos . stylizeEl css -- 2.30.2