@@ 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