Skip to content

Commit c05cebf

Browse files
JonPReillyars18wrw
authored andcommitted
Process li's inline children as inline elements. Process br inside oi correctly.
They should be treated as an inline element and be handled inside the inlineHelper. DEVSIX-1932
1 parent 20598ae commit c05cebf

File tree

11 files changed

+232
-12
lines changed

11 files changed

+232
-12
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This file is part of the iText (R) project.
5454
import com.itextpdf.layout.element.Image;
5555
import com.itextpdf.layout.element.List;
5656
import com.itextpdf.layout.element.ListItem;
57+
import com.itextpdf.layout.element.Text;
5758
import com.itextpdf.layout.property.ListSymbolPosition;
5859
import com.itextpdf.layout.property.Property;
5960
import com.itextpdf.styledxmlparser.css.util.CssUtils;
@@ -64,13 +65,19 @@ This file is part of the iText (R) project.
6465
*/
6566
public class LiTagWorker implements ITagWorker {
6667

67-
/** The list item. */
68+
/**
69+
* The list item.
70+
*/
6871
protected ListItem listItem;
6972

70-
/** The list. */
73+
/**
74+
* The list.
75+
*/
7176
protected List list;
7277

73-
/** The inline helper. */
78+
/**
79+
* The inline helper.
80+
*/
7481
private WaitingInlineElementsHelper inlineHelper;
7582

7683
/**
@@ -117,7 +124,11 @@ public boolean processContent(String content, ProcessorContext context) {
117124
*/
118125
@Override
119126
public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext context) {
120-
if (childTagWorker instanceof SpanTagWorker) {
127+
IPropertyContainer element = childTagWorker.getElementResult();
128+
if (element instanceof ILeafElement) {
129+
inlineHelper.add((ILeafElement) element);
130+
return true;
131+
} else if (childTagWorker instanceof SpanTagWorker) {
121132
boolean allChildrenProcessed = true;
122133
for (IPropertyContainer propertyContainer : ((SpanTagWorker) childTagWorker).getAllElements()) {
123134
if (propertyContainer instanceof ILeafElement) {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.html2pdf.attach.ProcessorContext;
4747
import com.itextpdf.html2pdf.attach.util.WaitingInlineElementsHelper;
4848
import com.itextpdf.html2pdf.css.CssConstants;
49+
import com.itextpdf.html2pdf.html.AttributeConstants;
4950
import com.itextpdf.layout.IPropertyContainer;
5051
import com.itextpdf.layout.element.IBlockElement;
5152
import com.itextpdf.layout.element.ILeafElement;
5253
import com.itextpdf.layout.element.List;
5354
import com.itextpdf.layout.element.ListItem;
5455
import com.itextpdf.layout.element.Paragraph;
56+
import com.itextpdf.layout.element.Text;
5557
import com.itextpdf.layout.property.Property;
56-
import com.itextpdf.html2pdf.html.AttributeConstants;
5758
import com.itextpdf.styledxmlparser.css.util.CssUtils;
5859
import com.itextpdf.styledxmlparser.node.IElementNode;
5960

@@ -62,10 +63,14 @@ This file is part of the iText (R) project.
6263
*/
6364
public class UlOlTagWorker implements ITagWorker {
6465

65-
/** The list object. */
66+
/**
67+
* The list object.
68+
*/
6669
private List list;
6770

68-
/** Helper class for waiting inline elements. */
71+
/**
72+
* Helper class for waiting inline elements.
73+
*/
6974
private WaitingInlineElementsHelper inlineHelper;
7075

7176
/**
@@ -77,9 +82,9 @@ public class UlOlTagWorker implements ITagWorker {
7782
public UlOlTagWorker(IElementNode element, ProcessorContext context) {
7883
list = new List().setListSymbol("");
7984
//In the case of an ordered list, see if the start attribute can be found
80-
if(element.getAttribute(AttributeConstants.START) != null){
85+
if (element.getAttribute(AttributeConstants.START) != null) {
8186
Integer startValue = CssUtils.parseInteger(element.getAttribute(AttributeConstants.START));
82-
if(startValue != null) list.setItemStartIndex((int)startValue);
87+
if (startValue != null) list.setItemStartIndex((int) startValue);
8388
}
8489
inlineHelper = new WaitingInlineElementsHelper(element.getStyles().get(CssConstants.WHITE_SPACE), element.getStyles().get(CssConstants.TEXT_TRANSFORM));
8590
}
@@ -107,7 +112,10 @@ public boolean processContent(String content, ProcessorContext context) {
107112
@Override
108113
public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext context) {
109114
IPropertyContainer child = childTagWorker.getElementResult();
110-
if (childTagWorker instanceof SpanTagWorker) {
115+
if (child instanceof ILeafElement) {
116+
inlineHelper.add((ILeafElement) child);
117+
return true;
118+
} else if (childTagWorker instanceof SpanTagWorker) {
111119
boolean allChildrenProcessed = true;
112120
for (IPropertyContainer propertyContainer : ((SpanTagWorker) childTagWorker).getAllElements()) {
113121
if (propertyContainer instanceof ILeafElement) {

src/test/java/com/itextpdf/html2pdf/css/DisplayTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.html2pdf.ConverterProperties;
4646
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
4747
import com.itextpdf.html2pdf.HtmlConverter;
48-
import com.itextpdf.html2pdf.attach.impl.DefaultTagWorkerFactory;
4948
import com.itextpdf.io.LogMessageConstant;
5049
import com.itextpdf.kernel.geom.PageSize;
5150
import com.itextpdf.kernel.pdf.PdfDocument;
5251
import com.itextpdf.kernel.pdf.PdfWriter;
5352
import com.itextpdf.kernel.utils.CompareTool;
54-
import com.itextpdf.test.ExtendedITextTest;
5553
import com.itextpdf.test.annotations.LogMessage;
5654
import com.itextpdf.test.annotations.LogMessages;
5755
import com.itextpdf.test.annotations.type.IntegrationTest;

src/test/java/com/itextpdf/html2pdf/element/BrTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,10 @@ public void br03Test() throws IOException, InterruptedException {
9191
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "brTest03.html"), pdfDoc, new ConverterProperties());
9292
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "brTest03.pdf", sourceFolder + "cmp_brTest03.pdf", destinationFolder, "diff03_"));
9393
}
94+
95+
@Test
96+
public void brInsideDifferentTagsTest01() throws IOException, InterruptedException {
97+
HtmlConverter.convertToPdf(new File(sourceFolder + "brInsideDifferentTagsTest01.html"), new File(destinationFolder + "brInsideDifferentTagsTest01.pdf"));
98+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "brInsideDifferentTagsTest01.pdf", sourceFolder + "cmp_brInsideDifferentTagsTest01.pdf", destinationFolder, "diff04_"));
99+
}
94100
}

src/test/java/com/itextpdf/html2pdf/element/ListTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ public void listTest17() throws IOException, InterruptedException {
165165
runTest("listTest17");
166166
}
167167

168+
@Test
169+
public void listTest18() throws IOException, InterruptedException {
170+
runTest("listTest18");
171+
}
172+
173+
@Test
174+
public void listTest19() throws IOException, InterruptedException {
175+
runTest("listTest19");
176+
}
177+
168178
@Test
169179
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.NOT_SUPPORTED_LIST_STYLE_TYPE, count = 32)})
170180
public void listToPdfaTest() throws IOException, InterruptedException {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<br><br><br>
4+
<head>
5+
<title>Hello<br><br><br>World!</title>
6+
<style>
7+
br {
8+
background-color: blue;
9+
}
10+
td {
11+
background-color: red;
12+
}
13+
th {
14+
background-color: green;
15+
}
16+
tr {
17+
background-color: blue;
18+
}
19+
tfoot {
20+
background-color: yellow;
21+
}
22+
thead {
23+
background-color: violet;
24+
}
25+
table {
26+
background-color: orange;
27+
}
28+
</style>
29+
</head>
30+
<body>
31+
<ol>
32+
<li>0 Line breaks: Test</li>
33+
text outside li with<br>1 line break
34+
<li>1 Line break:<br>Test</li>
35+
text outside li with<br><br>2 line breaks
36+
<li>2 Line breaks:<br><br>Test</li>
37+
text outside li with<br><br><br>3 line breaks
38+
<li>3 Line breaks:<br><br><br>Test</li>
39+
</ol>
40+
<abbr title="Common Gateway Interface">CG<br><br><br>I</abbr>
41+
<br>
42+
<br>
43+
<br>
44+
<br>
45+
<a style="display: block;">Three line breaks<br><br><br>inside</a>
46+
<a>Three line breaks<br><br><br>inside</a>
47+
<div>Three line breaks<br><br><br>inside</div>
48+
<h1>Three line breaks<br><br><br>inside</h1>
49+
<link>Three line breaks<br><br><br>inside</link>
50+
<pre>Three line breaks<br><br><br>inside</pre>
51+
<p>Three line breaks<br><br><br>inside</p>
52+
<select>
53+
<option>Three line breaks<br><br><br>inside</option>
54+
<br><br><br>
55+
<option>Three line breaks<br><br><br>inside</option>
56+
</select>
57+
<hr>
58+
<span>Three line breaks<br><br><br>inside</span>
59+
<hr>
60+
<table>
61+
<tr>
62+
<th>Three line breaks<br><br><br>inside</th>
63+
Three line breaks<br><br><br>inside
64+
<th>Three line breaks<br><br><br>inside</th>
65+
</tr>
66+
Three line breaks<br><br><br>inside
67+
<tr>
68+
<td>Three line breaks<br><br><br>inside</td>
69+
Three line breaks<br><br><br>inside
70+
<td>Three line breaks<br><br><br>inside</td>
71+
</tr>
72+
</table>
73+
<hr>
74+
<table>
75+
<thead>
76+
<td>Three line breaks<br><br><br>inside</td>
77+
Three line breaks<br><br><br>inside
78+
<td>Three line breaks<br><br><br>inside</td>
79+
</thead>
80+
Three line breaks<br><br><br>inside
81+
<tr>
82+
<td>Three line breaks<br><br><br>inside</td>
83+
Three line breaks<br><br><br>inside
84+
<td>Three line breaks<br><br><br>inside</td>
85+
</tr>
86+
<tfoot>
87+
<td>Three line breaks<br><br><br>inside</td>
88+
Three line breaks<br><br><br>inside
89+
<td>Three line breaks<br><br><br>inside</td>
90+
</tfoot>
91+
</table>
92+
<button>Three line breaks<br><br><br>inside</button>
93+
<textarea>Three line breaks<br><br><br>inside</textarea>
94+
<div style="display: table;">
95+
<div style="display: table-row;">
96+
<div style="display: table-cell;">Three line breaks<br><br><br>inside</div>
97+
Three line breaks<br><br><br>inside
98+
<div style="display: table-cell;">Three line breaks<br><br><br>inside</div>
99+
</div>
100+
Three line breaks<br><br><br>inside
101+
<div style="display: table-row;">
102+
<div style="display: table-cell;">Three line breaks<br><br><br>inside</div>
103+
Three line breaks<br><br><br>inside
104+
<div style="display: table-cell;">Three line breaks<br><br><br>inside</div>
105+
</div>
106+
</div>
107+
<input placeholder="Three line breaks<br><br><br>inside">
108+
</body>
109+
</html>
110+
Binary file not shown.
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
5+
<h2>An unordered list with and without line breaks</h2>
6+
7+
<ol>
8+
<li>0 Line breaks: Test</li>
9+
<li>1 Line break:<br>Test</li>
10+
<li>2 Line breaks:<br><br>Test</li>
11+
<li>3 Line breaks:<br><br><br>Test</li>
12+
</ol>
13+
14+
<h2>An unordered list with some tags inside. The children shall be treated as inline elements.</h2>
15+
16+
<ol>
17+
<li>Before some tag<br><br>After some tag</li>
18+
<li>Before some tag<textarea>textarea1</textarea><textarea>textarea2</textarea>After some tag</li>
19+
<li>Before some tag<input type="text" value="input1"><input type="text" value="input2">After some tag</li>
20+
<li>Before some tag
21+
<button>button1</button>
22+
<button>button2</button>
23+
After some tag
24+
</li>
25+
<li>Before some tag<select>
26+
<option>1Option 1</option>
27+
<option>1Option 2</option>
28+
</select><select>
29+
<option>2Option 1</option>
30+
<option>2Option 2</option>
31+
</select>After some tag
32+
</li>
33+
</ol>
34+
35+
36+
</body>
37+
</html>
38+

0 commit comments

Comments
 (0)