From e0d43db4f4ec4c34362dfb0f7541c3c3a957cf56 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 17 Mar 2023 10:27:04 +1300 Subject: [PATCH] Strip out extraneous whitespace. --- Graphics/Layout/CSS.hs | 13 +++++++------ Graphics/Layout/Inline/CSS.hs | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Graphics/Layout/CSS.hs b/Graphics/Layout/CSS.hs index a4886b1..489df6d 100644 --- a/Graphics/Layout/CSS.hs +++ b/Graphics/Layout/CSS.hs @@ -200,13 +200,14 @@ finalizeChilds :: PropertyParser x => Font' -> Font' -> [StyleTree (CSSBox x)] - finalizeChilds root parent (StyleTree { style = CSSBox { display = None } }:childs) = finalizeChilds root parent childs finalizeChilds root parent childs@(child:childs') - | isInlineTree childs = + | isInlineTree childs, Just self <- finalizeParagraph (flattenTree childs) parent = -- FIXME propagate display properties, how to handle the hierarchy. - [LayoutInline temp parent (finalizeParagraph (flattenTree childs) parent) - (repeat temp)] - | (inlines@(_:_), blocks) <- spanInlines childs = - LayoutInline temp parent (finalizeParagraph (flattenTree childs) parent) [] - :finalizeChilds root parent blocks + [LayoutInline temp parent self (repeat temp)] + | (inlines@(_:_), blocks) <- spanInlines childs, + Just self <- finalizeParagraph (flattenTree childs) parent = + LayoutInline temp parent self [] : finalizeChilds root parent blocks + | (StyleTree { style = CSSBox { display = Inline } }:childs') <- childs = + finalizeChilds root parent childs' -- Inline's all whitespace... | otherwise = finalizeCSS root parent child : finalizeChilds root parent childs' where isInlineTree = all isInlineTree0 diff --git a/Graphics/Layout/Inline/CSS.hs b/Graphics/Layout/Inline/CSS.hs index 0506294..1f16f35 100644 --- a/Graphics/Layout/Inline/CSS.hs +++ b/Graphics/Layout/Inline/CSS.hs @@ -10,6 +10,7 @@ import Data.Text.ParagraphLayout (Span(..), SpanOptions(..), LineHeight(..), Paragraph(..), ParagraphOptions(..)) import Graphics.Layout.CSS.Internal (Font'(..), hbScale) +import Data.Char (isSpace) data CSSInline = CSSInline Lz.Text SpanOptions @@ -38,8 +39,9 @@ concatParagraph (ParagraphBuilder aTxt aOpts) (ParagraphBuilder bTxt bOps) = ParagraphBuilder (aTxt `Lz.append` bTxt) (aOpts ++ [Span (toEnum (fromEnum $ Lz.length aTxt) + off) opts | Span off opts <- bOps]) -finalizeParagraph :: ParagraphBuilder -> Font' -> Paragraph -finalizeParagraph (ParagraphBuilder txt ops) font' = Paragraph txt' 0 ops pOps +finalizeParagraph :: ParagraphBuilder -> Font' -> Maybe Paragraph +finalizeParagraph (ParagraphBuilder txt _) _ | Lz.all isSpace txt || Lz.null txt = Nothing +finalizeParagraph (ParagraphBuilder txt ops) font' = Just $ Paragraph txt' 0 ops pOps where Text txt' _ _ = Lz.toStrict txt pOps = ParagraphOptions { -- 2.30.2