Skip to content

Commit 0042bf6

Browse files
committed
Support multicol for th and td and add tests for the rest of commonly used block elements
DEVSIX-7592
1 parent 5f82edf commit 0042bf6

File tree

49 files changed

+898
-41
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+898
-41
lines changed

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/PTagWorker.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ This file is part of the iText (R) project.
2828
import com.itextpdf.html2pdf.attach.util.WaitingInlineElementsHelper;
2929
import com.itextpdf.html2pdf.css.CssConstants;
3030
import com.itextpdf.layout.IPropertyContainer;
31-
import com.itextpdf.layout.element.MulticolContainer;
3231
import com.itextpdf.layout.element.Div;
3332
import com.itextpdf.layout.element.IBlockElement;
3433
import com.itextpdf.layout.element.IElement;
3534
import com.itextpdf.layout.element.ILeafElement;
3635
import com.itextpdf.layout.element.Image;
36+
import com.itextpdf.layout.element.MulticolContainer;
3737
import com.itextpdf.layout.element.Paragraph;
3838
import com.itextpdf.styledxmlparser.node.IElementNode;
3939

@@ -58,6 +58,9 @@ public class PTagWorker implements ITagWorker, IDisplayAware {
5858
/** The container which handles the elements that are present in the <p> tag. */
5959
private Div elementsContainer;
6060

61+
/** Container for the result in case of multicol layouting */
62+
private MulticolContainer multicolContainer;
63+
6164
/** Helper class for waiting inline elements. */
6265
private WaitingInlineElementsHelper inlineHelper;
6366

@@ -74,8 +77,8 @@ public PTagWorker(IElementNode element, ProcessorContext context) {
7477
lastParagraph = new Paragraph();
7578

7679
if (element.getStyles().get(CssConstants.COLUMN_COUNT) != null ) {
77-
elementsContainer = new MulticolContainer();
78-
elementsContainer.add(lastParagraph);
80+
multicolContainer = new MulticolContainer();
81+
multicolContainer.add(lastParagraph);
7982
}
8083
inlineHelper = new WaitingInlineElementsHelper(element.getStyles().get(CssConstants.WHITE_SPACE),
8184
element.getStyles().get(CssConstants.TEXT_TRANSFORM));
@@ -157,19 +160,25 @@ public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext conte
157160
*/
158161
@Override
159162
public IPropertyContainer getElementResult() {
160-
return null == elementsContainer ? (IPropertyContainer) lastParagraph : (IPropertyContainer) elementsContainer;
163+
if (multicolContainer == null) {
164+
return null == elementsContainer ? (IPropertyContainer) lastParagraph : (IPropertyContainer) elementsContainer;
165+
}
166+
return multicolContainer;
161167
}
162168

163169
@Override
164170
public String getDisplay() {
165171
return display;
166172
}
167173

168-
//TODO: DEVSIX-7592 rework column count support when elements container is not empty and contains several elements
169174
private void processBlockElement(IElement propertyContainer) {
170175
if (elementsContainer == null) {
171176
elementsContainer = new Div();
172177
elementsContainer.add(lastParagraph);
178+
if (multicolContainer != null) {
179+
multicolContainer.getChildren().clear();
180+
multicolContainer.add(elementsContainer);
181+
}
173182
}
174183
inlineHelper.flushHangingLeaves(lastParagraph);
175184

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/TdTagWorker.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,35 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.html2pdf.attach.util.AccessiblePropHelper;
2828
import com.itextpdf.html2pdf.attach.util.WaitingInlineElementsHelper;
2929
import com.itextpdf.html2pdf.css.CssConstants;
30+
import com.itextpdf.html2pdf.css.apply.impl.ColumnCssApplierUtil;
31+
import com.itextpdf.html2pdf.html.AttributeConstants;
3032
import com.itextpdf.layout.IPropertyContainer;
3133
import com.itextpdf.layout.element.Cell;
34+
import com.itextpdf.layout.element.Div;
3235
import com.itextpdf.layout.element.IBlockElement;
3336
import com.itextpdf.layout.element.ILeafElement;
34-
import com.itextpdf.html2pdf.html.AttributeConstants;
37+
import com.itextpdf.layout.element.MulticolContainer;
3538
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
3639
import com.itextpdf.styledxmlparser.node.IElementNode;
3740

41+
import java.util.Map;
42+
3843
/**
3944
* TagWorker class for the {@code td} element.
4045
*/
4146
public class TdTagWorker implements ITagWorker, IDisplayAware {
4247

4348
/** The cell. */
44-
private Cell cell;
49+
private final Cell cell;
50+
51+
/** Container for cell children in case of multicol layouting */
52+
private Div childOfMulticolContainer;
4553

4654
/** The inline helper. */
47-
private WaitingInlineElementsHelper inlineHelper;
55+
private final WaitingInlineElementsHelper inlineHelper;
4856

4957
/** The display. */
50-
private String display;
58+
private final String display;
5159

5260
/**
5361
* Creates a new {@link TdTagWorker} instance.
@@ -63,8 +71,19 @@ public TdTagWorker(IElementNode element, ProcessorContext context) {
6371

6472
cell = new Cell((int)rowspan, (int)colspan);
6573
cell.setPadding(0);
66-
inlineHelper = new WaitingInlineElementsHelper(element.getStyles().get(CssConstants.WHITE_SPACE), element.getStyles().get(CssConstants.TEXT_TRANSFORM));
67-
display = element.getStyles() != null ? element.getStyles().get(CssConstants.DISPLAY) : null;
74+
75+
Map<String, String> styles = element.getStyles();
76+
if (styles.containsKey(CssConstants.COLUMN_COUNT)) {
77+
MulticolContainer multicolContainer = new MulticolContainer();
78+
childOfMulticolContainer = new Div();
79+
multicolContainer.add(childOfMulticolContainer);
80+
// TODO DEVSIX-7564, DEVSIX-7562 apply other multicol properties
81+
ColumnCssApplierUtil.applyColumnCount(styles, context, multicolContainer);
82+
cell.add(multicolContainer);
83+
}
84+
85+
inlineHelper = new WaitingInlineElementsHelper(styles.get(CssConstants.WHITE_SPACE), styles.get(CssConstants.TEXT_TRANSFORM));
86+
display = styles.get(CssConstants.DISPLAY);
6887

6988
AccessiblePropHelper.trySetLangAttribute(cell, element);
7089
}
@@ -74,7 +93,7 @@ public TdTagWorker(IElementNode element, ProcessorContext context) {
7493
*/
7594
@Override
7695
public void processEnd(IElementNode element, ProcessorContext context) {
77-
inlineHelper.flushHangingLeaves(cell);
96+
inlineHelper.flushHangingLeaves(getCellContainer());
7897
}
7998

8099
/* (non-Javadoc)
@@ -121,7 +140,7 @@ public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext conte
121140
*/
122141
@Override
123142
public IPropertyContainer getElementResult() {
124-
return cell;
143+
return (IPropertyContainer) cell;
125144
}
126145

127146
/* (non-Javadoc)
@@ -140,11 +159,19 @@ public String getDisplay() {
140159
*/
141160
private boolean processChild(IPropertyContainer propertyContainer) {
142161
boolean processed = false;
143-
inlineHelper.flushHangingLeaves(cell);
162+
inlineHelper.flushHangingLeaves(getCellContainer());
144163
if (propertyContainer instanceof IBlockElement) {
145-
cell.add((IBlockElement) propertyContainer);
164+
if (childOfMulticolContainer == null) {
165+
cell.add((IBlockElement) propertyContainer);
166+
} else {
167+
childOfMulticolContainer.add((IBlockElement) propertyContainer);
168+
}
146169
processed = true;
147170
}
148171
return processed;
149172
}
173+
174+
private IPropertyContainer getCellContainer() {
175+
return childOfMulticolContainer == null ? (IPropertyContainer) cell : (IPropertyContainer) childOfMulticolContainer;
176+
}
150177
}

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/UlOlTagWorker.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ This file is part of the iText (R) project.
2929
import com.itextpdf.html2pdf.css.CssConstants;
3030
import com.itextpdf.html2pdf.html.AttributeConstants;
3131
import com.itextpdf.layout.IPropertyContainer;
32-
import com.itextpdf.layout.element.MulticolContainer;
33-
import com.itextpdf.layout.element.Div;
3432
import com.itextpdf.layout.element.IBlockElement;
3533
import com.itextpdf.layout.element.ILeafElement;
3634
import com.itextpdf.layout.element.List;
3735
import com.itextpdf.layout.element.ListItem;
36+
import com.itextpdf.layout.element.MulticolContainer;
3837
import com.itextpdf.layout.element.Paragraph;
3938
import com.itextpdf.layout.properties.Property;
4039
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
@@ -50,7 +49,7 @@ public class UlOlTagWorker implements ITagWorker {
5049
*/
5150
private List list;
5251

53-
private Div elementsContainer;
52+
private MulticolContainer multicolContainer;
5453

5554
/**
5655
* Helper class for waiting inline elements.
@@ -67,8 +66,8 @@ public UlOlTagWorker(IElementNode element, ProcessorContext context) {
6766
list = new List().setListSymbol("");
6867

6968
if (element.getStyles().get(CssConstants.COLUMN_COUNT) != null ) {
70-
elementsContainer = new MulticolContainer();
71-
elementsContainer.add(list);
69+
multicolContainer = new MulticolContainer();
70+
multicolContainer.add(list);
7271
}
7372

7473
//In the case of an ordered list, see if the start attribute can be found
@@ -132,10 +131,7 @@ public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext conte
132131
*/
133132
@Override
134133
public IPropertyContainer getElementResult() {
135-
if (elementsContainer == null) {
136-
return list;
137-
}
138-
return elementsContainer;
134+
return multicolContainer == null ? (IPropertyContainer) list : (IPropertyContainer) multicolContainer;
139135
}
140136

141137
/**

src/test/java/com/itextpdf/html2pdf/css/multicol/ColumnCountTest.java

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ public void convertBasicPTest() throws IOException, InterruptedException {
6161
runTest("basicPTest");
6262
}
6363

64-
//TODO: DEVSIX-7592 add support for forms
64+
@Test
65+
public void diffElementsInsidePTest() throws IOException, InterruptedException {
66+
convertToPdfAndCompare("diffElementsInsidePTest", SOURCE_FOLDER, DESTINATION_FOLDER, false,
67+
new ConverterProperties().setMulticolEnabled(true).setBaseUri(SOURCE_FOLDER));
68+
}
69+
70+
//TODO: DEVSIX-7591 support nested multicol layouting
6571
@Test
6672
public void convertBasicFormTest() throws IOException, InterruptedException {
6773
runTest("basicFormTest");
@@ -72,19 +78,36 @@ public void convertBasicUlTest() throws IOException, InterruptedException {
7278
runTest("basicUlTest");
7379
}
7480

75-
//TODO: DEVSIX-7591
81+
//TODO: DEVSIX-7591 Support nested multicol layouting
7682
@Test
7783
public void convertBasicOlTest() throws IOException, InterruptedException {
7884
runTest("basicOlTest");
7985
}
8086

81-
//TODO: DEVSIX-7592
8287
@Test
8388
public void convertBasicTableTest() throws IOException, InterruptedException {
8489
runTest("basicTableTest");
8590
}
8691

87-
//TODO: DEVSIX-7584 add multipage support
92+
@Test
93+
public void tableColspanTest() throws IOException, InterruptedException {
94+
convertToPdfAndCompare("tableColspanTest",
95+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
96+
}
97+
98+
@Test
99+
public void tableRowspanTest() throws IOException, InterruptedException {
100+
convertToPdfAndCompare("tableRowspanTest",
101+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
102+
}
103+
104+
@Test
105+
public void tableColspanRowspanTest() throws IOException, InterruptedException {
106+
convertToPdfAndCompare("tableColspanRowspanTest",
107+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
108+
}
109+
110+
//TODO: DEVSIX-7591 Support nested multicol layouting
88111
@Test
89112
public void convertBasicSectionTest() throws IOException, InterruptedException {
90113
runTest("basicSectionTest");
@@ -96,7 +119,7 @@ public void convertBasicDivMultiPageDocumentsTest() throws IOException, Interrup
96119
runTest("basicDivMultiPageTest");
97120
}
98121

99-
//TODO: DEVSIX-7592 add support for forms
122+
//TODO: DEVSIX-7584 add multipage support
100123
@Test
101124
public void convertBasicFormMultiPageDocumentsTest() throws IOException, InterruptedException {
102125
runTest("basicFormMultiPageTest");
@@ -107,7 +130,7 @@ public void convertBasicDisplayPropertyTest() throws IOException, InterruptedExc
107130
runTest("basicDisplayPropertyTest");
108131
}
109132

110-
//TODO: DEVSIX-7591
133+
//TODO: DEVSIX-7591 Support nested multicol layouting
111134
@Test
112135
public void convertBasicDisplayPropertyWithNestedColumnsTest() throws IOException, InterruptedException {
113136
runTest("basicDisplayPropertyWithNestedColumnsTest");
@@ -190,12 +213,39 @@ public void splitEmptyParagraphElementsBetweenColumns() throws IOException, Inte
190213
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
191214
}
192215

193-
194216
@Test
195217
public void splitEmptyContinuousBlockElementBetweenColumns() throws IOException, InterruptedException {
196218
convertToPdfAndCompare("splitEmptyContinuousBlockElementBetweenColumns",
197219
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
198220
}
199221

222+
@Test
223+
public void basicHiTest() throws IOException, InterruptedException {
224+
convertToPdfAndCompare("basicHiTest",
225+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
226+
}
227+
228+
@Test
229+
public void basicFooterHeaderTest() throws IOException, InterruptedException {
230+
convertToPdfAndCompare("basicFooterHeaderTest",
231+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
232+
}
233+
234+
@Test
235+
public void basicDlTest() throws IOException, InterruptedException {
236+
convertToPdfAndCompare("basicDlTest",
237+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
238+
}
200239

240+
@Test
241+
public void basicInlineElementsTest() throws IOException, InterruptedException {
242+
convertToPdfAndCompare("basicInlineElementsTest",
243+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
244+
}
245+
246+
@Test
247+
public void basicBlockquoteTest() throws IOException, InterruptedException {
248+
convertToPdfAndCompare("basicBlockquoteTest",
249+
SOURCE_FOLDER, DESTINATION_FOLDER, false, new ConverterProperties().setMulticolEnabled(true));
250+
}
201251
}

src/test/java/com/itextpdf/html2pdf/css/multicol/ColumnWidthTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,51 @@ public void convertColumnWidthEqualsImagesTest() throws IOException, Interrupted
216216
runTest("columnWidthEqualsImagesTest");
217217
}
218218

219+
@Test
220+
public void diffElementsInsidePTest() throws IOException, InterruptedException {
221+
runTest("diffElementsInsidePTest");
222+
}
223+
224+
@Test
225+
public void tableColspanTest() throws IOException, InterruptedException {
226+
runTest("tableColspanTest");
227+
}
228+
229+
@Test
230+
public void tableRowspanTest() throws IOException, InterruptedException {
231+
runTest("tableRowspanTest");
232+
}
233+
234+
@Test
235+
public void tableColspanRowspanTest() throws IOException, InterruptedException {
236+
runTest("tableColspanRowspanTest");
237+
}
238+
239+
@Test
240+
public void basicHiTest() throws IOException, InterruptedException {
241+
runTest("basicHiTest");
242+
}
243+
244+
@Test
245+
public void basicFooterHeaderTest() throws IOException, InterruptedException {
246+
runTest("basicFooterHeaderTest");
247+
}
248+
249+
@Test
250+
public void basicDlTest() throws IOException, InterruptedException {
251+
runTest("basicDlTest");
252+
}
253+
254+
@Test
255+
public void basicInlineElementsTest() throws IOException, InterruptedException {
256+
runTest("basicInlineElementsTest");
257+
}
258+
259+
@Test
260+
public void basicBlockquoteTest() throws IOException, InterruptedException {
261+
runTest("basicBlockquoteTest");
262+
}
263+
219264
private void runTest(String testName) throws IOException, InterruptedException {
220265
convertToPdfAndCompare(testName,
221266
SOURCE_FOLDER, DESTINATION_FOLDER, false,
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ This file is part of the iText (R) project.
2424

2525
import com.itextpdf.html2pdf.css.w3c.W3CCssMulticolTest;
2626

27-
import org.junit.Ignore;
2827

29-
30-
public class FixedInNestedMulticolWithTransformContainerTest extends W3CCssMulticolTest {
28+
public class NestedMulticolWithTransformContainerTest extends W3CCssMulticolTest {
3129
@Override
3230
protected String getHtmlFileName() {
33-
return "fixed-in-nested-multicol-with-transform-container.html";
31+
return "nested-multicol-with-transform-container.html";
3432
}
3533
}

0 commit comments

Comments
 (0)