Skip to content

Commit 7b53a50

Browse files
yulian-gaponenkoiText-CI
authored andcommitted
Fix anchor links handling with destinations on elements handled by SpanTagWorker
DEVSIX-2774 Autoported commit. Original commit hash: [55c40116]
1 parent f710aaa commit 7b53a50

File tree

7 files changed

+57
-14
lines changed

7 files changed

+57
-14
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/element/LinkTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,16 @@ public virtual void LinkTest12() {
204204
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "linkTest12.pdf", sourceFolder
205205
+ "cmp_linkTest12.pdf", destinationFolder, "diff12_"));
206206
}
207+
208+
/// <exception cref="System.IO.IOException"/>
209+
/// <exception cref="System.Exception"/>
210+
[NUnit.Framework.Test]
211+
public virtual void AnchorLinkToSpanTest01() {
212+
String fileName = "anchorLinkToSpanTest01";
213+
HtmlConverter.ConvertToPdf(new FileInfo(sourceFolder + fileName + ".html"), new FileInfo(destinationFolder
214+
+ fileName + ".pdf"));
215+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + fileName + ".pdf", sourceFolder
216+
+ "cmp_" + fileName + ".pdf", destinationFolder));
217+
}
207218
}
208219
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
<body>
3+
4+
<a id="main_q" href="#Welsh_Corgi">Who's a good boy?</a>
5+
<p style="page-break-before: always;">
6+
<span id="Welsh_Corgi">corgi</span>
7+
<a href="#main_q">Back to main question!</a>
8+
</p>
9+
10+
11+
</body>
12+
</html>
13+

itext/itext.html2pdf/itext/html2pdf/LogMessageConstant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public sealed class LogMessageConstant {
4848
/// <summary>The Constant ACROFORM_NOT_SUPPORTED_FOR_SELECT.</summary>
4949
public const String ACROFORM_NOT_SUPPORTED_FOR_SELECT = "AcroForm fields creation for select fields (ComboBoxField and ListBoxField) is not supported. They will be flattened instead.";
5050

51+
public const String ANCHOR_LINK_NOT_HANDLED = "The anchor link was not handled. Could not create a destination for element \"{0}\" with ID \"{1}\", which is processed by \"{2}\" tag worker class.";
52+
5153
/// <summary>The Constant CONTENT_PROPERTY_INVALID.</summary>
5254
public const String CONTENT_PROPERTY_INVALID = "Content property \"{0}\" is either invalid or uses unsupported function.";
5355

itext/itext.html2pdf/itext/html2pdf/attach/impl/LinkContext.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ public virtual iText.Html2pdf.Attach.Impl.LinkContext ScanForIds(INode root) {
7878
while (!stk.IsEmpty()) {
7979
INode n = stk.Pop();
8080
if (n is IElementNode) {
81-
IElementNode n2 = (IElementNode)n;
82-
if (n2.Name().Equals(AttributeConstants.a) && n2.GetAttribute(AttributeConstants.HREF) != null && n2.GetAttribute
83-
(AttributeConstants.HREF).StartsWith("#")) {
84-
linkDestinations.Add(n2.GetAttribute(AttributeConstants.HREF).Substring(1));
81+
IElementNode elem = (IElementNode)n;
82+
if (TagConstants.A.Equals(elem.Name())) {
83+
String href = elem.GetAttribute(AttributeConstants.HREF);
84+
if (href != null && href.StartsWith("#")) {
85+
linkDestinations.Add(href.Substring(1));
86+
}
8587
}
8688
}
8789
if (!n.ChildNodes().IsEmpty()) {

itext/itext.html2pdf/itext/html2pdf/attach/util/LinkHelper.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ source product.
4141
4242
*/
4343
using System;
44+
using System.Collections.Generic;
45+
using Common.Logging;
4446
using iText.Html2pdf.Attach;
47+
using iText.Html2pdf.Attach.Impl.Tags;
4548
using iText.Html2pdf.Html;
49+
using iText.IO.Util;
4650
using iText.Kernel.Geom;
4751
using iText.Kernel.Pdf;
4852
using iText.Kernel.Pdf.Action;
@@ -93,22 +97,33 @@ public static void ApplyLinkAnnotation(IPropertyContainer container, String url)
9397
/// <param name="element">the (HTML) element being converted</param>
9498
/// <param name="context">the Processor context</param>
9599
public static void CreateDestination(ITagWorker tagWorker, IElementNode element, ProcessorContext context) {
96-
if (element.GetAttribute(AttributeConstants.ID) == null) {
100+
String id = element.GetAttribute(AttributeConstants.ID);
101+
if (id == null) {
97102
return;
98103
}
99-
if (tagWorker == null) {
104+
if (!context.GetLinkContext().IsUsedLinkDestination(id)) {
100105
return;
101106
}
102-
IPropertyContainer propertyContainer = tagWorker.GetElementResult();
107+
IPropertyContainer propertyContainer = null;
108+
if (tagWorker != null) {
109+
if (tagWorker is SpanTagWorker) {
110+
IList<IPropertyContainer> spanElements = ((SpanTagWorker)tagWorker).GetAllElements();
111+
if (!spanElements.IsEmpty()) {
112+
propertyContainer = spanElements[0];
113+
}
114+
}
115+
else {
116+
propertyContainer = tagWorker.GetElementResult();
117+
}
118+
}
103119
if (propertyContainer == null) {
120+
ILog logger = LogManager.GetLogger(typeof(iText.Html2pdf.Attach.Util.LinkHelper));
121+
String tagWorkerClassName = tagWorker != null ? tagWorker.GetType().FullName : "null";
122+
logger.Warn(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.ANCHOR_LINK_NOT_HANDLED, element.Name
123+
(), id, tagWorkerClassName));
104124
return;
105125
}
106-
// get id
107-
String id = element.GetAttribute(AttributeConstants.ID);
108-
// set property
109-
if (context.GetLinkContext().IsUsedLinkDestination(id)) {
110-
propertyContainer.SetProperty(Property.DESTINATION, id);
111-
}
126+
propertyContainer.SetProperty(Property.DESTINATION, id);
112127
}
113128
}
114129
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
db4b9714d85f18a7ee4c20693455b9dbb266f27d
1+
55c40116085862a958782d9d94e070c88e27f2f6

0 commit comments

Comments
 (0)