Skip to content

Commit 0ddd5dc

Browse files
committed
Correct insert paragraphs into table cells
1 parent e236270 commit 0ddd5dc

File tree

1 file changed

+94
-90
lines changed

1 file changed

+94
-90
lines changed

Diff for: src/helpers/xml-builder.ts

+94-90
Original file line numberDiff line numberDiff line change
@@ -337,105 +337,107 @@ function fixupMargin(marginString: string) {
337337

338338
function modifiedStyleAttributesBuilder(
339339
docxDocumentInstance: DocxDocument,
340-
vNode: VNode | null,
340+
vNode: VTree | null,
341341
attributes: Attributes,
342342
options = { isParagraph: false },
343343
) {
344344
const modifiedAttributes = { ...attributes }
345345

346346
// styles
347-
if (vNode && isVNode(vNode) && vNode.properties && vNode.properties.style) {
348-
if (
349-
vNode.properties.style.color &&
350-
!colorlessColors.includes(vNode.properties.style.color)
351-
) {
352-
modifiedAttributes.color = fixupColorCode(vNode.properties.style.color)
353-
}
347+
if (vNode && isVNode(vNode)) {
348+
const properties = (vNode as VNode).properties
349+
if (properties && properties.style) {
350+
if (
351+
properties.style.color &&
352+
!colorlessColors.includes(properties.style.color)
353+
) {
354+
modifiedAttributes.color = fixupColorCode(properties.style.color)
355+
}
354356

355-
if (
356-
vNode.properties.style["background-color"] &&
357-
!colorlessColors.includes(vNode.properties.style["background-color"])
358-
) {
359-
modifiedAttributes.backgroundColor = fixupColorCode(
360-
vNode.properties.style["background-color"],
361-
)
362-
}
357+
if (
358+
properties.style["background-color"] &&
359+
!colorlessColors.includes(properties.style["background-color"])
360+
) {
361+
modifiedAttributes.backgroundColor = fixupColorCode(
362+
properties.style["background-color"],
363+
)
364+
}
363365

364-
if (
365-
vNode.properties.style["vertical-align"] &&
366-
verticalAlignValues.includes(vNode.properties.style["vertical-align"])
367-
) {
368-
modifiedAttributes.verticalAlign =
369-
vNode.properties.style["vertical-align"]
370-
}
366+
if (
367+
properties.style["vertical-align"] &&
368+
verticalAlignValues.includes(properties.style["vertical-align"])
369+
) {
370+
modifiedAttributes.verticalAlign = properties.style["vertical-align"]
371+
}
371372

372-
if (
373-
vNode.properties.style["text-align"] &&
374-
["left", "right", "center", "justify"].includes(
375-
vNode.properties.style["text-align"],
376-
)
377-
) {
378-
modifiedAttributes.textAlign = vNode.properties.style["text-align"]
379-
}
373+
if (
374+
properties.style["text-align"] &&
375+
["left", "right", "center", "justify"].includes(
376+
properties.style["text-align"],
377+
)
378+
) {
379+
modifiedAttributes.textAlign = properties.style["text-align"]
380+
}
380381

381-
// FIXME: remove bold check when other font weights are handled.
382-
if (
383-
vNode.properties.style["font-weight"] &&
384-
vNode.properties.style["font-weight"] === "bold"
385-
) {
386-
modifiedAttributes.strong = vNode.properties.style["font-weight"]
387-
}
388-
if (vNode.properties.style["font-family"]) {
389-
modifiedAttributes.font = docxDocumentInstance.createFont(
390-
vNode.properties.style["font-family"],
391-
)
392-
}
393-
if (vNode.properties.style["font-size"]) {
394-
modifiedAttributes.fontSize = fixupFontSize(
395-
vNode.properties.style["font-size"],
396-
)
397-
}
398-
if (vNode.properties.style["line-height"]) {
399-
modifiedAttributes.lineHeight = fixupLineHeight(
400-
vNode.properties.style["line-height"],
401-
vNode.properties.style["font-size"]
402-
? fixupFontSize(vNode.properties.style["font-size"])
403-
: undefined,
404-
)
405-
}
406-
if (
407-
vNode.properties.style["margin-left"] ||
408-
vNode.properties.style["margin-right"]
409-
) {
410-
const leftMargin = fixupMargin(vNode.properties.style["margin-left"])
411-
const rightMargin = fixupMargin(vNode.properties.style["margin-right"])
382+
// FIXME: remove bold check when other font weights are handled.
383+
if (
384+
properties.style["font-weight"] &&
385+
properties.style["font-weight"] === "bold"
386+
) {
387+
modifiedAttributes.strong = properties.style["font-weight"]
388+
}
389+
if (properties.style["font-family"]) {
390+
modifiedAttributes.font = docxDocumentInstance.createFont(
391+
properties.style["font-family"],
392+
)
393+
}
394+
if (properties.style["font-size"]) {
395+
modifiedAttributes.fontSize = fixupFontSize(
396+
properties.style["font-size"],
397+
)
398+
}
399+
if (properties.style["line-height"]) {
400+
modifiedAttributes.lineHeight = fixupLineHeight(
401+
properties.style["line-height"],
402+
properties.style["font-size"]
403+
? fixupFontSize(properties.style["font-size"])
404+
: undefined,
405+
)
406+
}
407+
if (
408+
properties.style["margin-left"] ||
409+
properties.style["margin-right"]
410+
) {
411+
const leftMargin = fixupMargin(properties.style["margin-left"])
412+
const rightMargin = fixupMargin(properties.style["margin-right"])
412413

413-
if (leftMargin || rightMargin) {
414-
modifiedAttributes.indentation = {
415-
left: leftMargin,
416-
right: rightMargin,
414+
if (leftMargin || rightMargin) {
415+
modifiedAttributes.indentation = {
416+
left: leftMargin,
417+
right: rightMargin,
418+
}
417419
}
418420
}
419-
}
420-
if (vNode.properties.style.display) {
421-
modifiedAttributes.display = vNode.properties.style.display
422-
}
421+
if (properties.style.display) {
422+
modifiedAttributes.display = properties.style.display
423+
}
423424

424-
if (vNode.properties.style.width) {
425-
modifiedAttributes.width = vNode.properties.style.width
425+
if (properties.style.width) {
426+
modifiedAttributes.width = properties.style.width
427+
}
426428
}
427429
}
428430

429431
// paragraph only
430432
if (options?.isParagraph) {
431-
if (vNode && isVNode(vNode) && vNode.tagName === "blockquote") {
433+
if (vNode && isVNode(vNode) && (vNode as VNode).tagName === "blockquote") {
432434
modifiedAttributes.indentation = { left: 284, right: 0 }
433435
modifiedAttributes.textAlign = "justify"
434436
}
435-
else if (vNode && isVNode(vNode) && vNode.tagName === "code") {
437+
else if (vNode && isVNode(vNode) && (vNode as VNode).tagName === "code") {
436438
modifiedAttributes.highlightColor = "lightGray"
437439
}
438-
else if (vNode && isVNode(vNode) && vNode.tagName === "pre") {
440+
else if (vNode && isVNode(vNode) && (vNode as VNode).tagName === "pre") {
439441
modifiedAttributes.font = "Courier"
440442
}
441443
}
@@ -1165,7 +1167,7 @@ function computeImageDimensions(vNode: VNode, attributes: Attributes) {
11651167
}
11661168

11671169
async function buildParagraph(
1168-
vNode: VNode | null,
1170+
vNode: VTree | null,
11691171
attributes: Attributes,
11701172
docxDocumentInstance: DocxDocument,
11711173
) {
@@ -1183,7 +1185,7 @@ async function buildParagraph(
11831185
modifiedAttributes,
11841186
)
11851187
paragraphFragment.import(paragraphPropertiesFragment)
1186-
if (vNode && isVNode(vNode) && vNodeHasChildren(vNode)) {
1188+
if (vNode && isVNode(vNode) && vNodeHasChildren(vNode as VNode)) {
11871189
if (
11881190
[
11891191
"span",
@@ -1202,10 +1204,10 @@ async function buildParagraph(
12021204
"a",
12031205
"code",
12041206
"pre",
1205-
].includes(vNode.tagName)
1207+
].includes((vNode as VNode).tagName)
12061208
) {
12071209
const runOrHyperlinkFragments = await buildRunOrHyperLink(
1208-
vNode,
1210+
vNode as VNode,
12091211
modifiedAttributes,
12101212
docxDocumentInstance,
12111213
)
@@ -1224,7 +1226,7 @@ async function buildParagraph(
12241226
paragraphFragment.import(runOrHyperlinkFragments)
12251227
}
12261228
}
1227-
else if (vNode.tagName === "blockquote") {
1229+
else if ((vNode as VNode).tagName === "blockquote") {
12281230
const runFragmentOrFragments = await buildRun(
12291231
vNode,
12301232
attributes,
@@ -1240,8 +1242,8 @@ async function buildParagraph(
12401242
}
12411243
}
12421244
else {
1243-
for (let index = 0; index < vNode.children.length; index++) {
1244-
const childVNode = vNode.children[index]
1245+
for (let index = 0; index < (vNode as VNode).children.length; index++) {
1246+
const childVNode = (vNode as VNode).children[index]
12451247
if (isVNode(childVNode) && (childVNode as VNode).tagName === "img") {
12461248
if (isValidUrl((childVNode as VNode).properties.src)) {
12471249
;(childVNode as VNode).properties.src = await fetchImageToDataUrl(
@@ -1298,10 +1300,12 @@ async function buildParagraph(
12981300
// In case paragraphs has to be rendered
12991301
// where vText is present. Eg. table-cell
13001302
// Or in case the vNode is something like img
1301-
if (vNode && isVNode(vNode) && vNode.tagName === "img") {
1302-
const imageSource = vNode.properties.src
1303-
if (isValidUrl(vNode.properties.src)) {
1304-
vNode.properties.src = await fetchImageToDataUrl(vNode.properties.src)
1303+
if (vNode && isVNode(vNode) && (vNode as VNode).tagName === "img") {
1304+
const imageSource = (vNode as VNode).properties.src
1305+
if (isValidUrl((vNode as VNode).properties.src)) {
1306+
;(vNode as VNode).properties.src = await fetchImageToDataUrl(
1307+
(vNode as VNode).properties.src,
1308+
)
13051309
}
13061310
const base64String = extractBase64Data(imageSource)?.base64Content
13071311
const imageBuffer = Buffer.from(
@@ -1315,10 +1319,10 @@ async function buildParagraph(
13151319
modifiedAttributes.originalWidth = imageProperties.width
13161320
modifiedAttributes.originalHeight = imageProperties.height
13171321

1318-
computeImageDimensions(vNode, modifiedAttributes)
1322+
computeImageDimensions(vNode as VNode, modifiedAttributes)
13191323
}
13201324
const runFragments = await buildRunOrRuns(
1321-
vNode,
1325+
vNode as VNode,
13221326
modifiedAttributes,
13231327
docxDocumentInstance,
13241328
)
@@ -1724,9 +1728,9 @@ async function buildTableCell(
17241728
await buildList(childVNode, docxDocumentInstance, tableCellFragment)
17251729
}
17261730
}
1727-
else if (isVNode(childVNode)) {
1731+
else {
17281732
const paragraphFragment = await buildParagraph(
1729-
childVNode as VNode,
1733+
childVNode,
17301734
modifiedAttributes,
17311735
docxDocumentInstance,
17321736
)

0 commit comments

Comments
 (0)