Skip to content

Commit 2b98bbd

Browse files
Samuel Huylebroeckitext-teamcity
authored andcommitted
Guard against NPE when converting to Elements
Guard when using OutlineHandler Add test DEVSIX-1679 Autoported commit. Original commit hash: [8774a15b]
1 parent acc41ea commit 2b98bbd

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/Html2ElementsTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ source product.
4242
*/
4343
using System;
4444
using System.Collections.Generic;
45+
using iText.Html2pdf.Attach.Impl;
4546
using iText.Layout.Element;
4647
using iText.Layout.Properties;
4748
using iText.Test;
@@ -147,5 +148,25 @@ public virtual void HtmlToElementsTest08() {
147148
String html = "<html><p>Hello world!</p><meta name=\"author\" content=\"Bruno\"><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p></html>";
148149
HtmlConverter.ConvertToElements(html);
149150
}
151+
152+
/// <exception cref="System.IO.IOException"/>
153+
[NUnit.Framework.Test]
154+
public virtual void HtmlToElementsTest09() {
155+
//Test OutlineHandler exception throwing
156+
/*
157+
Outlines require a PdfDocument, and OutlineHandler is based around its availability
158+
Any convert to elements workflow of course doesn't have a PdfDocument.
159+
Instead of throwing an NPE when trying it, the OutlineHandler will check for the existence of a pdfDocument
160+
If no PdfDocument is found, the handler will do nothing silently
161+
*/
162+
String html = "<html><p>Hello world!</p><meta name=\"author\" content=\"Bruno\"><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p></html>";
163+
ConverterProperties props = new ConverterProperties();
164+
OutlineHandler outlineHandler = new OutlineHandler();
165+
outlineHandler.PutTagPriorityMapping("h1", 1);
166+
outlineHandler.PutTagPriorityMapping("h3", 2);
167+
outlineHandler.PutTagPriorityMapping("p", 3);
168+
props.SetOutlineHandler(outlineHandler);
169+
HtmlConverter.ConvertToElements(html);
170+
}
150171
}
151172
}

itext/itext.html2pdf/itext/html2pdf/attach/impl/OutlineHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public virtual void Reset() {
143143
internal virtual OutlineHandler AddOutline(ITagWorker tagWorker, IElementNode element, ProcessorContext context
144144
) {
145145
String tagName = element.Name();
146-
if (null != tagWorker && HasTagPriorityMapping(tagName)) {
146+
if (null != tagWorker && HasTagPriorityMapping(tagName) && context.GetPdfDocument() != null) {
147147
int level = (int)GetTagPriorityMapping(tagName);
148148
if (null == currentOutline) {
149149
currentOutline = context.GetPdfDocument().GetOutlines(false);
@@ -173,7 +173,7 @@ internal virtual OutlineHandler AddOutline(ITagWorker tagWorker, IElementNode el
173173
/// <returns>the outline handler</returns>
174174
internal virtual OutlineHandler AddDestination(ITagWorker tagWorker, IElementNode element) {
175175
String tagName = element.Name();
176-
if (null != tagWorker && HasTagPriorityMapping(tagName)) {
176+
if (null != tagWorker && HasTagPriorityMapping(tagName) && destinationsInProcess.Count > 0) {
177177
String content = destinationsInProcess.JRemoveFirst();
178178
if (tagWorker.GetElementResult() is IElement) {
179179
tagWorker.GetElementResult().SetProperty(Property.DESTINATION, content);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e1b550952c9e49af7b1fdf6c8a0d17aa7bea0f23
1+
8774a15bbac67df1924b0a3a26a8aa63d593f18c

0 commit comments

Comments
 (0)