Skip to content

Commit d16c1ad

Browse files
committed
Support grid-gap, grid-column-gap and grid-row-gap and log unsupported properties
DEVSIX-8376 Fix per code review, part 1
1 parent cae4c3d commit d16c1ad

29 files changed

+357
-25
lines changed

src/main/java/com/itextpdf/html2pdf/css/CssConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ public class CssConstants extends CommonCssConstants {
232232
/** The Constant SUB. */
233233
public static final String SUB = "sub";
234234

235+
/** The Constant SUBGRID. */
236+
public static final String SUBGRID = "subgrid";
237+
235238
/** The Constant SUPER. */
236239
public static final String SUPER = "super";
237240

src/main/java/com/itextpdf/html2pdf/css/apply/util/GridApplierUtil.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@ This file is part of the iText (R) project.
2424

2525
import com.itextpdf.commons.datastructures.Tuple2;
2626
import com.itextpdf.commons.utils.MapUtil;
27+
import com.itextpdf.commons.utils.MessageFormatUtil;
2728
import com.itextpdf.html2pdf.attach.ProcessorContext;
2829
import com.itextpdf.html2pdf.css.CssConstants;
2930
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
3031
import com.itextpdf.layout.IPropertyContainer;
3132
import com.itextpdf.layout.element.IAbstractElement;
32-
import com.itextpdf.layout.properties.grid.AutoRepeatValue;
33-
import com.itextpdf.layout.properties.grid.BreadthValue;
34-
import com.itextpdf.layout.properties.grid.FixedRepeatValue;
35-
import com.itextpdf.layout.properties.grid.GridFlow;
3633
import com.itextpdf.layout.element.IElement;
3734
import com.itextpdf.layout.properties.Property;
3835
import com.itextpdf.layout.properties.UnitValue;
36+
import com.itextpdf.layout.properties.grid.AutoRepeatValue;
3937
import com.itextpdf.layout.properties.grid.AutoValue;
38+
import com.itextpdf.layout.properties.grid.BreadthValue;
4039
import com.itextpdf.layout.properties.grid.FitContentValue;
40+
import com.itextpdf.layout.properties.grid.FixedRepeatValue;
4141
import com.itextpdf.layout.properties.grid.FlexValue;
42+
import com.itextpdf.layout.properties.grid.GridFlow;
4243
import com.itextpdf.layout.properties.grid.GridValue;
4344
import com.itextpdf.layout.properties.grid.MaxContentValue;
4445
import com.itextpdf.layout.properties.grid.MinContentValue;
@@ -148,15 +149,16 @@ public static void applyGridContainerProperties(Map<String, String> cssProps, IP
148149
applyAuto(cssProps.get(CssConstants.GRID_AUTO_COLUMNS), container, Property.GRID_AUTO_COLUMNS, emValue, remValue);
149150
applyFlow(cssProps.get(CssConstants.GRID_AUTO_FLOW), container);
150151

151-
final UnitValue columnGap = CssDimensionParsingUtils.parseLengthValueToPt(cssProps.get(CssConstants.COLUMN_GAP),
152-
emValue, remValue);
153-
if (columnGap != null) {
154-
container.setProperty(Property.COLUMN_GAP, columnGap.getValue());
155-
}
156-
final UnitValue rowGap = CssDimensionParsingUtils.parseLengthValueToPt(cssProps.get(CssConstants.ROW_GAP),
157-
emValue, remValue);
158-
if (rowGap != null) {
159-
container.setProperty(Property.ROW_GAP, rowGap.getValue());
152+
applyGap(container, emValue, remValue, cssProps.get(CssConstants.COLUMN_GAP), Property.COLUMN_GAP);
153+
applyGap(container, emValue, remValue, cssProps.get(CssConstants.GRID_COLUMN_GAP), Property.COLUMN_GAP);
154+
applyGap(container, emValue, remValue, cssProps.get(CssConstants.ROW_GAP), Property.ROW_GAP);
155+
applyGap(container, emValue, remValue, cssProps.get(CssConstants.GRID_ROW_GAP), Property.ROW_GAP);
156+
}
157+
158+
private static void applyGap(IPropertyContainer container, float emValue, float remValue, String gap, int property) {
159+
final UnitValue gapValue = CssDimensionParsingUtils.parseLengthValueToPt(gap, emValue, remValue);
160+
if (gapValue != null) {
161+
container.setProperty(property, gapValue.getValue());
160162
}
161163
}
162164

@@ -206,10 +208,15 @@ private static NamedAreas applyNamedAreas(String gridTemplateAreas, IPropertyCon
206208

207209
private static void applyTemplate(String templateStr, IPropertyContainer container, int property,
208210
float emValue, float remValue, NamedAreas namedAreas) {
211+
if (templateStr != null && templateStr.contains(CssConstants.SUBGRID)) {
212+
LOGGER.warn(Html2PdfLogMessageConstant.SUBGRID_VALUE_IS_NOT_SUPPORTED);
213+
}
214+
209215
Map<String, List<Integer>> lineNumbersPerName = new HashMap<>();
210216
int namedAreaLength = 0;
217+
final boolean applyColumns = property == Property.GRID_TEMPLATE_COLUMNS;
211218
if (namedAreas != null) {
212-
if (property == Property.GRID_TEMPLATE_COLUMNS) {
219+
if (applyColumns) {
213220
lineNumbersPerName = namedAreas.getNamedColumnNumbers();
214221
namedAreaLength = namedAreas.getColumnsCount();
215222
} else {
@@ -233,7 +240,10 @@ private static void applyTemplate(String templateStr, IPropertyContainer contain
233240
}
234241
}
235242
}
236-
if (!templateResult.isEmpty()) {
243+
if (templateResult.isEmpty()) {
244+
LOGGER.warn(MessageFormatUtil.format(Html2PdfLogMessageConstant.GRID_TEMPLATE_WAS_NOT_RECOGNISED,
245+
applyColumns ? "columns" : "rows"));
246+
} else {
237247
container.setProperty(property, templateResult);
238248
}
239249
}
@@ -242,7 +252,7 @@ private static void applyTemplate(String templateStr, IPropertyContainer contain
242252
int startProperty;
243253
int endProperty;
244254
int spanProperty;
245-
if (property == Property.GRID_TEMPLATE_COLUMNS) {
255+
if (applyColumns) {
246256
startProperty = Property.GRID_COLUMN_START;
247257
endProperty = Property.GRID_COLUMN_END;
248258
spanProperty = Property.GRID_COLUMN_SPAN;
@@ -424,8 +434,8 @@ private static TemplateValue parseTemplateValue(String str, float emValue, float
424434
}
425435

426436
private static TemplateValue parseTemplateValue(String str, float emValue, float remValue,
427-
Map<String, List<Integer>> lineNumbersPerName,
428-
int currentLine) {
437+
Map<String, List<Integer>> lineNumbersPerName, int currentLine) {
438+
429439
if (str == null) {
430440
return null;
431441
}

src/main/java/com/itextpdf/html2pdf/logs/Html2PdfLogMessageConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ public final class Html2PdfLogMessageConstant {
179179
+ "with auto-repeat as a track-size";
180180
public static final String ADDING_GRID_LINES_TO_THE_LEFT_OR_TOP_IS_NOT_SUPPORTED = "Adding grid lines to the left "
181181
+ "or to the top is not supported";
182+
public static final String SUBGRID_VALUE_IS_NOT_SUPPORTED = "Subgrid value for grid-template-row\\columns isn't supported";
183+
public static final String GRID_TEMPLATE_WAS_NOT_RECOGNISED = "Grid template {0} value was not recognised";
182184

183185
private Html2PdfLogMessageConstant() {
184186
//Private constructor will prevent the instantiation of this class directly

src/test/java/com/itextpdf/html2pdf/css/apply/util/GridApplierUtilTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ This file is part of the iText (R) project.
2929
import com.itextpdf.layout.element.Div;
3030
import com.itextpdf.layout.element.GridContainer;
3131
import com.itextpdf.layout.element.IElement;
32+
import com.itextpdf.layout.properties.Property;
3233
import com.itextpdf.layout.properties.grid.AutoRepeatValue;
3334
import com.itextpdf.layout.properties.grid.FitContentValue;
3435
import com.itextpdf.layout.properties.grid.FixedRepeatValue;
3536
import com.itextpdf.layout.properties.grid.GridFlow;
3637
import com.itextpdf.layout.properties.grid.GridValue;
37-
import com.itextpdf.layout.properties.Property;
3838
import com.itextpdf.layout.properties.grid.LengthValue;
3939
import com.itextpdf.layout.properties.grid.MinMaxValue;
4040
import com.itextpdf.layout.properties.grid.PercentValue;
@@ -445,6 +445,17 @@ public void containerGapValuesTest() {
445445
Assertions.assertEquals(30, element.<Float>getProperty(Property.ROW_GAP));
446446
}
447447

448+
@Test
449+
public void containerGridGapValuesTest() {
450+
Map<String, String> cssProps = new HashMap<>();
451+
cssProps.put(CssConstants.GRID_COLUMN_GAP, "11px");
452+
cssProps.put(CssConstants.GRID_ROW_GAP, "30%");
453+
IElement element = new Div();
454+
GridApplierUtil.applyGridContainerProperties(cssProps, element, new ProcessorContext(new ConverterProperties()));
455+
Assertions.assertEquals(8.25f, element.<Float>getProperty(Property.COLUMN_GAP));
456+
Assertions.assertEquals(30, element.<Float>getProperty(Property.ROW_GAP));
457+
}
458+
448459
@Test
449460
public void columnFlowTest() {
450461
Map<String, String> cssProps = new HashMap<>();

src/test/java/com/itextpdf/html2pdf/css/grid/GridGapTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,31 @@ public void smallValuesGapTest() throws IOException, InterruptedException {
177177
runTest("small_values_gap");
178178
}
179179

180+
@Test
181+
public void differentRowsColsGapTest() throws IOException, InterruptedException {
182+
runTest("different_rows_cols_gap");
183+
}
184+
185+
@Test
186+
public void gridGapTest1() throws IOException, InterruptedException {
187+
runTest("gridGapTest1");
188+
}
189+
190+
@Test
191+
public void gridGapTest2() throws IOException, InterruptedException {
192+
runTest("gridGapTest2");
193+
}
194+
195+
@Test
196+
public void gridColumnGapTest() throws IOException, InterruptedException {
197+
runTest("gridColumnGapTest");
198+
}
199+
200+
@Test
201+
public void gridRowGapTest() throws IOException, InterruptedException {
202+
runTest("gridRowGapTest");
203+
}
204+
180205
private void runTest(String testName) throws IOException, InterruptedException {
181206
convertToPdfAndCompare(testName, SOURCE_FOLDER, DESTINATION_FOLDER, false,
182207
new ConverterProperties().setBaseUri(SOURCE_FOLDER));

src/test/java/com/itextpdf/html2pdf/css/grid/GridTemplatesTest.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ public void autoFitRepeatWithFlexMinMaxTest() throws IOException, InterruptedExc
553553
runTest("autoFitRepeatWithFlexMinMaxTest");
554554
}
555555

556+
@LogMessages(messages = {
557+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.GRID_TEMPLATE_WAS_NOT_RECOGNISED, logLevel = LogLevelConstants.WARN)
558+
})
556559
@Test
557560
public void repeatInsideMinMaxTest() throws IOException, InterruptedException {
558561
runTest("repeatInsideMinMaxTest");
@@ -633,15 +636,40 @@ public void maxHeightFlexRowsTest() throws IOException, InterruptedException {
633636
public void maxHeightFlexRowsTest2() throws IOException, InterruptedException {
634637
runTest("maxHeightFlexRowsTest2");
635638
}
636-
637-
@Test
639+
638640
@LogMessages(messages = {
639641
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, logLevel =
640642
LogLevelConstants.WARN)})
643+
@Test
641644
public void divNestingTest() throws IOException, InterruptedException {
642645
runTest("divNestingTest");
643646
}
644647

648+
@LogMessages(messages = {
649+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.SUBGRID_VALUE_IS_NOT_SUPPORTED, logLevel = LogLevelConstants.WARN),
650+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.GRID_TEMPLATE_WAS_NOT_RECOGNISED, logLevel = LogLevelConstants.WARN)
651+
})
652+
@Test
653+
public void subgridTest() throws IOException, InterruptedException {
654+
runTest("subgridTest");
655+
}
656+
657+
@LogMessages(messages = {
658+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.GRID_TEMPLATE_WAS_NOT_RECOGNISED, logLevel = LogLevelConstants.WARN)
659+
})
660+
@Test
661+
public void invalidTemplateColumns() throws IOException, InterruptedException {
662+
runTest("invalidTemplateColumns");
663+
}
664+
665+
@LogMessages(messages = {
666+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.GRID_TEMPLATE_WAS_NOT_RECOGNISED, logLevel = LogLevelConstants.WARN)
667+
})
668+
@Test
669+
public void invalidTemplateRows() throws IOException, InterruptedException {
670+
runTest("invalidTemplateRows");
671+
}
672+
645673
private void runTest(String testName) throws IOException, InterruptedException {
646674
convertToPdfAndCompare(testName,
647675
SOURCE_FOLDER, DESTINATION_FOLDER, false,

src/test/java/com/itextpdf/html2pdf/css/w3c/css_flexbox/FlexboxColumnRowGap001Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.test.annotations.LogMessage;
2828
import com.itextpdf.test.annotations.LogMessages;
2929

30-
//TODO DEVSIX-7554 change after column-gap is supported
3130
//TODO DEVSIX-7616 change after row-gap is supported
3231
//TODO DEVSIX-5164 change after align-content: space-around is supported
3332
//TODO DEVSIX-5163 change after more complex justify-content values are supported

src/test/java/com/itextpdf/html2pdf/css/w3c/css_flexbox/FlexboxColumnRowGap002Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.test.annotations.LogMessage;
2828
import com.itextpdf.test.annotations.LogMessages;
2929

30-
//TODO DEVSIX-7554 change after column-gap is supported
3130
//TODO DEVSIX-7616 change after row-gap is supported
3231
//TODO DEVSIX-5164 change after align-content: flex-start is supported
3332
@LogMessages(messages = @LogMessage(messageTemplate = Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET, count = 6))

src/test/java/com/itextpdf/html2pdf/css/w3c/css_flexbox/FlexboxColumnRowGap003Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.test.annotations.LogMessage;
2828
import com.itextpdf.test.annotations.LogMessages;
2929

30-
//TODO DEVSIX-7554 change after column-gap is supported
3130
//TODO DEVSIX-7616 change after row-gap is supported
3231
//TODO DEVSIX-5164 change after align-content: space-around is supported
3332
//TODO DEVSIX-5163 change after more complex justify-content values are supported

src/test/java/com/itextpdf/html2pdf/css/w3c/css_flexbox/FlexboxColumnRowGap004Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.test.annotations.LogMessage;
2828
import com.itextpdf.test.annotations.LogMessages;
2929

30-
//TODO DEVSIX-7554 change after column-gap is supported
3130
//TODO DEVSIX-7616 change after row-gap is supported
3231
//TODO DEVSIX-5164 change after align-content: start is supported
3332
@LogMessages(messages = @LogMessage(messageTemplate = Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET, count = 12))

0 commit comments

Comments
 (0)