Skip to content

Commit 4c7964a

Browse files
author
Dmitry Radchuk
committed
Make fill opaque if it is parsed from invalid url
DEVSIX-3365
1 parent 73a56ae commit 4c7964a

File tree

17 files changed

+46
-17
lines changed

17 files changed

+46
-17
lines changed

svg/src/main/java/com/itextpdf/svg/renderers/impl/AbstractSvgNodeRenderer.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -470,22 +470,38 @@ private TransparentColor getColorFromAttributeValue(SvgDrawContext context, Stri
470470
return null;
471471
}
472472
String tokenValue = token.getValue();
473-
if (tokenValue.startsWith("url(#") && tokenValue.endsWith(")")) {
474-
Color resolvedColor = null;
475-
float resolvedOpacity = 1;
476-
final String normalizedName = tokenValue.substring(5, tokenValue.length() - 1).trim();
477-
final ISvgNodeRenderer colorRenderer = context.getNamedObject(normalizedName);
478-
if (colorRenderer instanceof ISvgPaintServer) {
479-
if (colorRenderer.getParent() == null) {
480-
colorRenderer.setParent(this);
473+
boolean isUrlInvalid = false;
474+
if (tokenValue.startsWith("url(") && tokenValue.endsWith(")")) {
475+
String normalizedName = tokenValue.substring(4, tokenValue.length() - 1).trim();
476+
normalizedName = CssUtils.extractUnquotedString(normalizedName);
477+
if (normalizedName.startsWith("#")) {
478+
Color resolvedColor = null;
479+
float resolvedOpacity = 1;
480+
normalizedName = normalizedName.substring(1);
481+
final ISvgNodeRenderer colorRenderer = context.getNamedObject(normalizedName);
482+
if (colorRenderer instanceof ISvgPaintServer) {
483+
if (colorRenderer.getParent() == null) {
484+
colorRenderer.setParent(this);
485+
}
486+
resolvedColor = ((ISvgPaintServer) colorRenderer).createColor(
487+
context, getObjectBoundingBox(context), objectBoundingBoxMargin, parentOpacity);
481488
}
482-
resolvedColor = ((ISvgPaintServer) colorRenderer).createColor(
483-
context, getObjectBoundingBox(context), objectBoundingBoxMargin, parentOpacity);
484-
}
485-
if (resolvedColor != null) {
486-
return new TransparentColor(resolvedColor, resolvedOpacity);
489+
if (resolvedColor != null) {
490+
return new TransparentColor(resolvedColor, resolvedOpacity);
491+
}
492+
if (colorRenderer == null) {
493+
isUrlInvalid = true;
494+
}
495+
} else {
496+
//we don't support those, but we need to make fill transparent anyway in such a case
497+
isUrlInvalid = true;
498+
//try to get next token to work the same as for the local url values
487499
}
488500
token = tokenizer.getNextValidToken();
501+
} else if (tokenValue.startsWith("url(") && !tokenValue.contains(" ")) {
502+
//for cases like url\([\w\d]+ browser treats them as urls, but url\([\w\d]+\s[\w\d]+ are not
503+
isUrlInvalid = true;
504+
token = tokenizer.getNextValidToken();
489505
}
490506
// may become null after function parsing and reading the 2nd token
491507
if (token != null) {
@@ -498,6 +514,9 @@ private TransparentColor getColorFromAttributeValue(SvgDrawContext context, Stri
498514
return new TransparentColor(result.getColor(), result.getOpacity() * parentOpacity);
499515
}
500516
}
517+
if (isUrlInvalid) {
518+
return new TransparentColor(ColorConstants.BLACK, 0.0F);
519+
}
501520
return null;
502521
}
503522

svg/src/test/java/com/itextpdf/svg/renderers/FillTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.svg.renderers;
2424

25+
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
2526
import com.itextpdf.svg.exceptions.SvgProcessingException;
2627
import com.itextpdf.svg.logs.SvgLogMessageConstant;
2728
import com.itextpdf.test.ITextTest;
@@ -119,7 +120,9 @@ public void pathHorizontalLineFillTest() throws IOException, InterruptedExceptio
119120
}
120121

121122
@Test
122-
//TODO update cmp file after DEVSIX-3365 will be fixed
123+
@LogMessages(messages = {
124+
@LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.URL_IS_NOT_CLOSED_IN_CSS_EXPRESSION)
125+
})
123126
public void invalidUrlFillTest() throws IOException, InterruptedException {
124127
convertAndCompare(SOURCE_FOLDER, DESTINATION_FOLDER, "invalidUrlFillTest");
125128
}
@@ -134,7 +137,6 @@ public void textFillFallbackTest() throws IOException, InterruptedException {
134137
}
135138

136139
@Test
137-
//TODO DEVSIX-8821: update after supported
138140
public void fillLinkToNonExistingGradientTest() throws IOException, InterruptedException {
139141
convertAndCompareSinglePage(SOURCE_FOLDER, DESTINATION_FOLDER, "fillLinkToNonExistingGradient");
140142
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)