@@ -47,9 +47,12 @@ This file is part of the iText (R) project.
4747import com .itextpdf .html2pdf .attach .util .WaitingInlineElementsHelper ;
4848import com .itextpdf .html2pdf .css .CssConstants ;
4949import com .itextpdf .layout .IPropertyContainer ;
50+ import com .itextpdf .layout .element .AbstractElement ;
5051import com .itextpdf .layout .element .Div ;
5152import com .itextpdf .layout .element .IBlockElement ;
53+ import com .itextpdf .layout .element .IElement ;
5254import com .itextpdf .layout .element .ILeafElement ;
55+ import com .itextpdf .layout .element .Image ;
5356import com .itextpdf .layout .element .Paragraph ;
5457import 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 */
7376public 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 <p> tag. */
78- private List <IBlockElement > elements ;
80+
81+ /** The container which handles the elements that are present in the <p> 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 ,
0 commit comments