Skip to content

Commit d9f2474

Browse files
committed
fix: remove special handling of align attribute in HTML paragraphs
- Treat align attribute like any other attribute - Always wrap paragraphs with attributes in divs (including align-only) - Remove validation logic for align values - Update tests to reflect consistent wrapper behavior
1 parent 3b33de2 commit d9f2474

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/Text/Pandoc/Readers/HTML.hs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -632,40 +632,29 @@ pParaWithWrapper (ident, classes, kvs) = do
632632
guard (null pInhalt)
633633
return mempty) <|> do
634634
let wrapperAttr = ("wrapper", "1")
635-
let alignValue = lookup "align" kvs
636-
let otherKVs = filter (\(k,_) -> k /= "align") kvs
637-
let validAlignKV = case alignValue of
638-
Just algn | algn `elem` ["left","right","center","justify"] -> [("align", algn)]
639-
_ -> []
640-
let finalKVs = wrapperAttr : (validAlignKV ++ otherKVs)
635+
let finalKVs = wrapperAttr : kvs
641636
let finalAttrs = (ident, classes, finalKVs)
642637
return $ B.divWith finalAttrs (B.para pInhalt)
643638

644-
-- Helper function for pPara when only align or no attributes are present
645-
pParaSimple :: PandocMonad m => Maybe Text -> TagParser m Blocks
646-
pParaSimple alignValue = do
639+
-- Helper function for pPara when no significant attributes are present
640+
pParaSimple :: PandocMonad m => TagParser m Blocks
641+
pParaSimple = do
647642
contents <- trimInlines <$> pInTags "p" inline
648-
let paraBlock = B.para contents
649643
(do guardDisabled Ext_empty_paragraphs
650644
guard (null contents)
651645
return mempty) <|>
652-
return (case alignValue of
653-
Just algn | algn `elem` ["left","right","center","justify"] ->
654-
B.divWith ("", [], [("align", algn)]) paraBlock
655-
_ -> paraBlock)
646+
return (B.para contents)
656647

657648
pPara :: PandocMonad m => TagParser m Blocks
658649
pPara = do
659650
TagOpen _ attr' <- lookAhead $ pSatisfy (matchTagOpen "p" [])
660651
let attr@(ident, classes, kvs) = toAttr attr'
661-
let alignValue = lookup "align" kvs
662-
-- "Significant" attributes are any id, class, or key-value pair other than 'align'.
663-
let significantKVs = filter (\(k,_) -> k /= "align") kvs
664-
let hasSignificantAttributes = not (T.null ident) || not (null classes) || not (null significantKVs)
652+
-- "Significant" attributes are any id, class, or key-value pair.
653+
let hasSignificantAttributes = not (T.null ident) || not (null classes) || not (null kvs)
665654

666655
if hasSignificantAttributes
667656
then pParaWithWrapper attr
668-
else pParaSimple alignValue
657+
else pParaSimple
669658

670659
pFigure :: PandocMonad m => TagParser m Blocks
671660
pFigure = do

test/Tests/Readers/HTML.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ tests = [ testGroup "base tag"
158158
doc (para (text "This is a normal paragraph."))
159159
, test htmlNativeDivs "paragraph with align only (center)" $
160160
"<p align=\"center\">Aligned paragraph.</p>" =?>
161-
doc (divWith ("", [], [("align", "center")]) (para (text "Aligned paragraph.")))
161+
doc (divWith ("", [], [("wrapper", "1"), ("align", "center")]) (para (text "Aligned paragraph.")))
162162
, test htmlNativeDivs "paragraph with align only (right)" $
163163
"<p align=\"right\">Aligned paragraph.</p>" =?>
164-
doc (divWith ("", [], [("align", "right")]) (para (text "Aligned paragraph.")))
164+
doc (divWith ("", [], [("wrapper", "1"), ("align", "right")]) (para (text "Aligned paragraph.")))
165165
, test htmlNativeDivs "paragraph with align and id" $
166166
"<p id=\"foo\" align=\"left\">Aligned paragraph with id.</p>" =?>
167167
doc (divWith ("foo", [], [("wrapper", "1"), ("align", "left")]) (para (text "Aligned paragraph with id.")))
@@ -170,10 +170,10 @@ tests = [ testGroup "base tag"
170170
doc (divWith ("", ["bar"], [("wrapper", "1"), ("align", "justify")]) (para (text "Aligned paragraph with class.")))
171171
, test htmlNativeDivs "paragraph with invalid align" $
172172
"<p align=\"invalid\">Invalid align.</p>" =?>
173-
doc (para (text "Invalid align."))
173+
doc (divWith ("", [], [("wrapper", "1"), ("align", "invalid")]) (para (text "Invalid align.")))
174174
, test htmlNativeDivs "paragraph with invalid align and id" $
175175
"<p id=\"baz\" align=\"invalid\">Invalid align with id.</p>" =?>
176-
doc (divWith ("baz", [], [("wrapper", "1")]) (para (text "Invalid align with id.")))
176+
doc (divWith ("baz", [], [("wrapper", "1"), ("align", "invalid")]) (para (text "Invalid align with id.")))
177177
]
178178
, askOption $ \(QuickCheckTests numtests) ->
179179
testProperty "Round trip" $

0 commit comments

Comments
 (0)