From b02c25b2cafb56f9f5bbcb0edf9f957b93b4afd0 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sun, 7 Oct 2018 18:57:25 -0700
Subject: Docx writer: handle tables in table cells.

Although this is not documented in the spec, some versions of
Word require a 'w:p' element inside every table cell. Thus, we
add one when the contents of a cell do not already include one
(e.g. when a table cell contains a table).

Closes #4953.
---
 src/Text/Pandoc/Writers/Docx.hs | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

(limited to 'src/Text')

diff --git a/src/Text/Pandoc/Writers/Docx.hs b/src/Text/Pandoc/Writers/Docx.hs
index 524d20fd1..6ff38535b 100644
--- a/src/Text/Pandoc/Writers/Docx.hs
+++ b/src/Text/Pandoc/Writers/Docx.hs
@@ -945,8 +945,13 @@ blockToOpenXML' opts (Table caption aligns widths headers rows) = do
                  else withParaProp (pCustomStyle "TableCaption")
                       $ blockToOpenXML opts (Para caption)
   let alignmentFor al = mknode "w:jc" [("w:val",alignmentToString al)] ()
-  let cellToOpenXML (al, cell) = withParaProp (alignmentFor al)
-                                    $ blocksToOpenXML opts cell
+  -- Table cells require a <w:p> element, even an empty one!
+  -- Not in the spec but in Word 2007, 2010. See #4953.
+  let cellToOpenXML (al, cell) = do
+        es <- withParaProp (alignmentFor al) $ blocksToOpenXML opts cell
+        if any (\e -> qName (elName e) == "p") es
+           then return es
+           else return $ es ++ [mknode "w:p" [] ()]
   headers' <- mapM cellToOpenXML $ zip aligns headers
   rows' <- mapM (mapM cellToOpenXML . zip aligns) rows
   let borderProps = mknode "w:tcPr" []
-- 
cgit v1.2.3