Skip to content

Commit c1be07c

Browse files
author
Eugene Bochilo
committed
Fix dummy log messages when we are processing xlink href with data
DEVSIX-4840 Autoported commit. Original commit hash: [194d087a2] Manual files: styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/resolver/resource/ResourceResolverTest.java svg/src/test/java/com/itextpdf/svg/css/XLinkTest.java
1 parent 7c971d5 commit c1be07c

File tree

11 files changed

+86
-8
lines changed

11 files changed

+86
-8
lines changed

itext.tests/itext.styledxmlparser.tests/itext/styledxmlparser/resolver/resource/ResourceResolverTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ public virtual void RetrieveImageExtendedBase64Test() {
186186
}
187187

188188
[Test]
189-
[LogMessage(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI)]
189+
[LogMessage(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI)]
190190
public virtual void RetrieveImageExtendedIncorrectBase64Test() {
191191
ResourceResolver resourceResolver = new ResourceResolver(baseUri);
192192
PdfXObject image = resourceResolver.RetrieveImageExtended(bLogoCorruptedData);
193193
Assert.Null(image);
194194
}
195195

196196
[Test]
197-
[LogMessage(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI,
197+
[LogMessage(LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI,
198198
LogLevel = LogLevelConstants.ERROR)]
199199
public virtual void RetrieveImageExtendedCorruptedDataBase64Test() {
200200
ResourceResolver resourceResolver = new ResourceResolver(baseUri);

itext.tests/itext.svg.tests/itext/svg/css/XLinkTest.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,41 @@ public virtual void SvgCssResolveMalformedXlinkTest() {
5656
iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
5757
.ValueOf("image"), "");
5858
iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
59-
String value = "http:://are";
59+
String value = "http://are::";
6060
imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", value));
6161
JsoupElementNode node = new JsoupElementNode(jsoupImage);
6262
SvgStyleResolver sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
6363
IDictionary<String, String> attr = sr.ResolveStyles(node, new SvgCssContext());
6464
NUnit.Framework.Assert.AreEqual(value, attr.Get("xlink:href"));
6565
}
66+
67+
[NUnit.Framework.Test]
68+
public virtual void SvgCssResolveDataXlinkTest() {
69+
iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
70+
.ValueOf("image"), "");
71+
iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
72+
JsoupElementNode node = new JsoupElementNode(jsoupImage);
73+
74+
String value1 = "data:image/png;base64,iVBORw0KGgoAAAANSU";
75+
imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", value1));
76+
77+
SvgStyleResolver sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
78+
IDictionary<String, String> attr = sr.ResolveStyles(node, new SvgCssContext());
79+
NUnit.Framework.Assert.AreEqual(value1, attr.Get("xlink:href"));
80+
81+
String value2 = "data:...,.";
82+
imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", value2));
83+
84+
sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
85+
attr = sr.ResolveStyles(node, new SvgCssContext());
86+
NUnit.Framework.Assert.AreEqual(value2, attr.Get("xlink:href"));
87+
88+
String value3 = "dAtA:...,.";
89+
imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", value3));
90+
91+
sr = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));
92+
attr = sr.ResolveStyles(node, new SvgCssContext());
93+
NUnit.Framework.Assert.AreEqual(value3, attr.Get("xlink:href"));
94+
}
6695
}
6796
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using iText.Svg.Renderers;
3+
using iText.Test;
4+
using iText.Test.Attributes;
5+
6+
namespace iText.Svg.Renderers.Impl {
7+
public class XLinkDataTest : SvgIntegrationTest {
8+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
9+
.CurrentContext.TestDirectory) + "/resources/itext/svg/renderers/impl/XLinkDataTest/";
10+
11+
private static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
12+
+ "/test/itext/svg/renderers/impl/XLinkDataTest/";
13+
14+
[NUnit.Framework.OneTimeSetUp]
15+
public static void BeforeClass() {
16+
ITextTest.CreateDestinationFolder(DESTINATION_FOLDER);
17+
}
18+
19+
[NUnit.Framework.Test]
20+
public virtual void CorrectImageWithDataTest() {
21+
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "correctImageWithData");
22+
}
23+
24+
[NUnit.Framework.Test]
25+
[LogMessage(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI)]
26+
public virtual void IncorrectImageWithDataTest() {
27+
ConvertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "incorrectImageWithData");
28+
}
29+
}
30+
}
Lines changed: 5 additions & 0 deletions
Loading
Lines changed: 5 additions & 0 deletions
Loading

itext/itext.styledxmlparser/itext/styledxmlparser/LogMessageConstant.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ public sealed class LogMessageConstant {
103103
/// <summary>The Constant RESOURCE_WITH_GIVEN_URL_WAS_FILTERED_OUT.</summary>
104104
public const String RESOURCE_WITH_GIVEN_URL_WAS_FILTERED_OUT = "Resource with given URL ({0}) was filtered out.";
105105

106+
/// <summary>The Constant UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI.</summary>
107+
public const String UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI = "Unable to retrieve image with data URI {0}";
108+
106109
/// <summary>The Constant UNABLE_TO_RETRIEVE_RESOURCE_WITH_GIVEN_RESOURCE_SIZE_BYTE_LIMIT.</summary>
107110
public const String UNABLE_TO_RETRIEVE_RESOURCE_WITH_GIVEN_RESOURCE_SIZE_BYTE_LIMIT = "Unable to retrieve resource with given URL ({0}) and resource size byte limit ({1}).";
108111

itext/itext.styledxmlparser/itext/styledxmlparser/resolver/resource/ResourceResolver.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,14 @@ public virtual PdfXObject RetrieveImageExtended(String src) {
201201
return imageXObject_1;
202202
}
203203
}
204-
logger.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
205-
, uriResolver.GetBaseUri(), src));
204+
if (IsDataSrc(src)) {
205+
logger.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_DATA_URI
206+
, src));
207+
}
208+
else {
209+
logger.Error(MessageFormatUtil.Format(iText.StyledXmlParser.LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI
210+
, uriResolver.GetBaseUri(), src));
211+
}
206212
return null;
207213
}
208214

@@ -290,7 +296,7 @@ public virtual Stream RetrieveResourceAsInputStream(String src) {
290296
/// <param name="src">string to test</param>
291297
/// <returns>true if source is under data URI scheme</returns>
292298
public virtual bool IsDataSrc(String src) {
293-
return src.StartsWith(DATA_SCHEMA_PREFIX) && src.Contains(",");
299+
return src != null && src.ToLowerInvariant().StartsWith(DATA_SCHEMA_PREFIX) && src.Contains(",");
294300
}
295301

296302
/// <summary>Resolves a given URI against the base URI.</summary>

itext/itext.svg/itext/svg/css/impl/SvgStyleResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private IDictionary<String, String> ResolveStyles(INode element, SvgCssContext c
297297
/// <param name="attributesMap">the element styles map</param>
298298
private void ProcessXLink(IAttribute attr, IDictionary<String, String> attributesMap) {
299299
String xlinkValue = attr.GetValue();
300-
if (!IsStartedWithHash(xlinkValue)) {
300+
if (!IsStartedWithHash(xlinkValue) && !new ResourceResolver("").IsDataSrc(xlinkValue)) {
301301
try {
302302
xlinkValue = this.resourceResolver.ResolveAgainstBaseUri(attr.GetValue()).ToExternalForm();
303303
}

0 commit comments

Comments
 (0)