Skip to content

Commit 191a7df

Browse files
committed
Improve processing of block elements / images inside of a paragraph.
The getElementResult should return the same instance on each call: otherwise the assosiated css properties will not get considered. DEVSIX-2746
1 parent f1d8afa commit 191a7df

File tree

11 files changed

+33
-20
lines changed

11 files changed

+33
-20
lines changed

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.html2pdf.attach.util.WaitingInlineElementsHelper;
4848
import com.itextpdf.html2pdf.css.CssConstants;
4949
import com.itextpdf.layout.IPropertyContainer;
50+
import com.itextpdf.layout.element.AbstractElement;
5051
import com.itextpdf.layout.element.Div;
5152
import com.itextpdf.layout.element.IBlockElement;
53+
import com.itextpdf.layout.element.IElement;
5254
import com.itextpdf.layout.element.ILeafElement;
55+
import com.itextpdf.layout.element.Image;
5356
import com.itextpdf.layout.element.Paragraph;
5457
import com.itextpdf.styledxmlparser.node.IElementNode;
5558

@@ -66,16 +69,17 @@ This file is part of the iText (R) project.
6669
*
6770
* <li> if the worker meets a block element without inline displaying or
6871
* an inline element with the {@code display: block} style, it wraps all the content which hasn't been handled yet
69-
* into a {@code com.itextpdf.layout.element.Paragraph} object and adds this paragraph to the {@link #elements list of elements},
70-
* which are going to be wrapped into a {@code com.itextpdf.layout.element.Div} object on {@link #processEnd}</li>
72+
* into a {@code com.itextpdf.layout.element.Paragraph} object and adds this paragraph to the resultant {@code com.itextpdf.layout.element.Div} object
73+
* </li>
7174
* </ul>
7275
*/
7376
public class PTagWorker implements ITagWorker, IDisplayAware {
7477

7578
/** The latest paragraph object inside tag. */
7679
private Paragraph lastParagraph;
77-
/** List of block elements that contained in the &lt;p&gt; tag. */
78-
private List<IBlockElement> elements;
80+
81+
/** The container which handles the elements that are present in the &lt;p&gt; tag. */
82+
private Div elementsContainer;
7983

8084
/** Helper class for waiting inline elements. */
8185
private WaitingInlineElementsHelper inlineHelper;
@@ -119,7 +123,11 @@ public boolean processContent(String content, ProcessorContext context) {
119123
public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext context) {
120124
// TODO child might be inline, however still have display:block; it behaves like a block, however p includes it in own occupied area
121125
IPropertyContainer element = childTagWorker.getElementResult();
122-
if (element instanceof ILeafElement) {
126+
if (childTagWorker instanceof ImgTagWorker && CssConstants.BLOCK.equals(((ImgTagWorker) childTagWorker).getDisplay())) {
127+
IPropertyContainer propertyContainer = childTagWorker.getElementResult();
128+
processBlockElement((Image) propertyContainer);
129+
return true;
130+
} else if (element instanceof ILeafElement) {
123131
inlineHelper.add((ILeafElement) element);
124132
return true;
125133
} else if (isBlockWithDisplay(childTagWorker, element, CssConstants.INLINE_BLOCK, false)) {
@@ -153,33 +161,29 @@ public boolean processTagChild(ITagWorker childTagWorker, ProcessorContext conte
153161
*/
154162
@Override
155163
public IPropertyContainer getElementResult() {
156-
if (elements == null) {
157-
return lastParagraph;
158-
}
159-
160-
Div div = new Div();
161-
for (IBlockElement element : elements) {
162-
div.add(element);
163-
}
164-
return div;
164+
return null == elementsContainer ? (IPropertyContainer) lastParagraph : (IPropertyContainer) elementsContainer;
165165
}
166166

167167
@Override
168168
public String getDisplay() {
169169
return display;
170170
}
171171

172-
private void processBlockElement(IBlockElement propertyContainer) {
173-
if (elements == null) {
174-
elements = new ArrayList<>();
175-
elements.add(lastParagraph);
172+
private void processBlockElement(IElement propertyContainer) {
173+
if (elementsContainer == null) {
174+
elementsContainer = new Div();
175+
elementsContainer.add(lastParagraph);
176176
}
177177
inlineHelper.flushHangingLeaves(lastParagraph);
178178

179-
elements.add(propertyContainer);
179+
if (propertyContainer instanceof Image) {
180+
elementsContainer.add((Image) propertyContainer);
181+
} else {
182+
elementsContainer.add((IBlockElement) propertyContainer);
183+
}
180184

181185
lastParagraph = new Paragraph();
182-
elements.add(lastParagraph);
186+
elementsContainer.add(lastParagraph);
183187
}
184188

185189
private boolean isBlockWithDisplay(ITagWorker childTagWorker, IPropertyContainer element,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,9 @@ public void paragraphWithLabelSpanDisplayBlockTest() throws IOException, Interru
185185
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "paragraphWithLabelSpanDisplayBlockTest.pdf", sourceFolder + "cmp_paragraphWithLabelSpanDisplayBlockTest.pdf", destinationFolder, "diff15_"));
186186
}
187187

188+
@Test
189+
public void paragraphWithImageTest01() throws IOException, InterruptedException {
190+
HtmlConverter.convertToPdf(new File(sourceFolder + "paragraphWithImageTest01.html"), new File(destinationFolder + "paragraphWithImageTest01.pdf"));
191+
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "paragraphWithImageTest01.pdf", sourceFolder + "cmp_paragraphWithImageTest01.pdf", destinationFolder, "diff_paragraphWithImageTest01_"));
192+
}
188193
}
Binary file not shown.
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p><img
2+
style='display: block; margin-left: auto; margin-right: auto;' title='placeholder-170x70.jpg'
3+
src='image.png' width='170'
4+
height='70'/></p>

0 commit comments

Comments
 (0)