Skip to content

Commit f4279a7

Browse files
committed
Support row-gap, column-gap, gap properties for flex container
DEVSIX-7616
1 parent d1f563d commit f4279a7

File tree

301 files changed

+5575
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+5575
-224
lines changed

src/main/java/com/itextpdf/html2pdf/css/apply/impl/DisplayFlexTagCssApplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void apply(ProcessorContext context, IStylesContainer stylesContainer, IT
3939
super.apply(context, stylesContainer, tagWorker);
4040
final IPropertyContainer container = tagWorker.getElementResult();
4141
if (container != null) {
42-
FlexApplierUtil.applyFlexContainerProperties(stylesContainer.getStyles(), container);
42+
FlexApplierUtil.applyFlexContainerProperties(stylesContainer.getStyles(), container, context);
4343
//TODO DEVSIX-5087 remove these lines when working on a ticket
4444
container.deleteOwnProperty(Property.FLOAT);
4545
container.deleteOwnProperty(Property.CLEAR);

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

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.html2pdf.css.apply.util;
2424

2525
import com.itextpdf.commons.utils.MessageFormatUtil;
26+
import com.itextpdf.html2pdf.ConverterProperties;
2627
import com.itextpdf.html2pdf.attach.ProcessorContext;
2728
import com.itextpdf.html2pdf.css.CssConstants;
2829
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
@@ -36,13 +37,13 @@ This file is part of the iText (R) project.
3637
import com.itextpdf.layout.properties.UnitValue;
3738
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
3839
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
3942

4043
import java.util.HashMap;
4144
import java.util.HashSet;
4245
import java.util.Map;
4346
import java.util.Set;
44-
import org.slf4j.Logger;
45-
import org.slf4j.LoggerFactory;
4647

4748
/**
4849
* Utilities class to apply flex properties.
@@ -58,11 +59,11 @@ private FlexApplierUtil() {
5859
* Applies properties to a flex item.
5960
*
6061
* @param cssProps the map of the CSS properties
61-
* @param context the context of the converter processor
62-
* @param element the element to set the properties
62+
* @param context the context of the converter processor
63+
* @param element the element to set the properties
6364
*/
6465
public static void applyFlexItemProperties(Map<String, String> cssProps, ProcessorContext context,
65-
IPropertyContainer element) {
66+
IPropertyContainer element) {
6667

6768
logWarningIfThereAreNotSupportedPropertyValues(createSupportedFlexItemPropertiesAndValuesMap(), cssProps);
6869

@@ -101,15 +102,31 @@ public static void applyFlexItemProperties(Map<String, String> cssProps, Process
101102
* Applies properties to a flex container.
102103
*
103104
* @param cssProps the CSS properties
104-
* @param element the element
105+
* @param element the element
106+
*
107+
* @deprecated in favour of {@link #applyFlexContainerProperties(Map, IPropertyContainer, ProcessorContext)}
105108
*/
109+
@Deprecated
106110
public static void applyFlexContainerProperties(Map<String, String> cssProps, IPropertyContainer element) {
111+
applyFlexContainerProperties(cssProps, element, new ProcessorContext(new ConverterProperties()));
112+
}
113+
114+
/**
115+
* Applies properties to a flex container.
116+
*
117+
* @param cssProps the CSS properties
118+
* @param element the element
119+
* @param context the context of the converter processor
120+
*/
121+
public static void applyFlexContainerProperties(Map<String, String> cssProps, IPropertyContainer element,
122+
ProcessorContext context) {
107123
logWarningIfThereAreNotSupportedPropertyValues(createSupportedFlexContainerPropertiesAndValuesMap(), cssProps);
108124
applyAlignItems(cssProps, element);
109125
applyJustifyContent(cssProps, element);
110126
applyAlignContent(cssProps, element);
111127
applyWrap(cssProps, element);
112128
applyDirection(cssProps, element);
129+
applyGap(cssProps, element, context);
113130
}
114131

115132
private static void applyAlignSelf(Map<String, String> cssProps, IPropertyContainer element) {
@@ -277,7 +294,7 @@ private static void applyJustifyContent(Map<String, String> cssProps, IPropertyC
277294
justifyContent = JustifyContent.RIGHT;
278295
break;
279296
case CommonCssConstants.SPACE_BETWEEN:
280-
justifyContent =JustifyContent.SPACE_BETWEEN;
297+
justifyContent = JustifyContent.SPACE_BETWEEN;
281298
break;
282299
case CommonCssConstants.SPACE_AROUND:
283300
justifyContent = JustifyContent.SPACE_AROUND;
@@ -290,7 +307,7 @@ private static void applyJustifyContent(Map<String, String> cssProps, IPropertyC
290307
break;
291308
default:
292309
LOGGER.warn(MessageFormatUtil.format(Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET,
293-
CommonCssConstants.JUSTIFY_CONTENT, justifyContentString));
310+
CommonCssConstants.JUSTIFY_CONTENT, justifyContentString));
294311
justifyContent = JustifyContent.FLEX_START;
295312
break;
296313
}
@@ -338,6 +355,21 @@ private static void applyAlignContent(Map<String, String> cssProps, IPropertyCon
338355
}
339356
}
340357

358+
private static void applyGap(Map<String, String> cssProps, IPropertyContainer element, ProcessorContext context) {
359+
final float emValue = CssDimensionParsingUtils.parseAbsoluteFontSize(cssProps.get(CssConstants.FONT_SIZE));
360+
final float remValue = context.getCssContext().getRootFontSize();
361+
applyGap(element, emValue, remValue, cssProps.get(CssConstants.COLUMN_GAP), Property.COLUMN_GAP);
362+
applyGap(element, emValue, remValue, cssProps.get(CssConstants.ROW_GAP), Property.ROW_GAP);
363+
}
364+
365+
private static void applyGap(IPropertyContainer container, float em, float rem, String gap, int property) {
366+
String gapLength = CommonCssConstants.NORMAL.equals(gap) ? "0px" : gap;
367+
final UnitValue gapValue = CssDimensionParsingUtils.parseLengthValueToPt(gapLength, em, rem);
368+
if (gapValue != null) {
369+
container.setProperty(property, gapValue.getValue());
370+
}
371+
}
372+
341373
private static void logWarningIfThereAreNotSupportedPropertyValues(Map<String, Set<String>> supportedPairs,
342374
Map<String, String> cssProps) {
343375
for (Map.Entry<String, Set<String>> entry : supportedPairs.entrySet()) {
@@ -383,16 +415,6 @@ private static Map<String, Set<String>> createSupportedFlexContainerPropertiesAn
383415

384416
supportedPairs.put(CommonCssConstants.ALIGN_CONTENT, supportedAlignContentValues);
385417

386-
final Set<String> supportedRowGapValues = new HashSet<>();
387-
supportedRowGapValues.add(CommonCssConstants.NORMAL);
388-
389-
supportedPairs.put(CommonCssConstants.ROW_GAP, supportedRowGapValues);
390-
391-
final Set<String> supportedColumnGapValues = new HashSet<>();
392-
supportedColumnGapValues.add(CommonCssConstants.NORMAL);
393-
394-
supportedPairs.put(CommonCssConstants.COLUMN_GAP, supportedColumnGapValues);
395-
396418
return supportedPairs;
397419
}
398420
}

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public void applyFlexBasisAbsoluteValueTest() {
120120
@Test
121121
@LogMessages(messages = @LogMessage(messageTemplate = Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET))
122122
public void applyAlignItemsTest() {
123+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
123124
String[] alignItemsStrings = {
124125
CssConstants.START,
125126
CssConstants.END,
@@ -148,13 +149,14 @@ public void applyAlignItemsTest() {
148149
Map<String, String> cssProps = new HashMap<>();
149150
cssProps.put(CssConstants.ALIGN_ITEMS, alignItemsStrings[i]);
150151
IElement element = new Div();
151-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
152+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
152153
Assertions.assertEquals(alignItemsValues[i], (AlignmentPropertyValue) element.<AlignmentPropertyValue>getProperty(Property.ALIGN_ITEMS));
153154
}
154155
}
155156

156157
@Test
157158
public void applyJustifyContentTest() {
159+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
158160
String[] justifyContentStrings = {
159161
CssConstants.START,
160162
CssConstants.END,
@@ -181,13 +183,14 @@ public void applyJustifyContentTest() {
181183
Map<String, String> cssProps = new HashMap<>();
182184
cssProps.put(CssConstants.JUSTIFY_CONTENT, justifyContentStrings[i]);
183185
IElement element = new Div();
184-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
186+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
185187
Assertions.assertEquals(justifyContentValues[i], (JustifyContent) element.<JustifyContent>getProperty(Property.JUSTIFY_CONTENT));
186188
}
187189
}
188190

189191
@Test
190192
public void applyFlexWrapTest() {
193+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
191194
String[] wrapStrings = {
192195
CssConstants.NOWRAP,
193196
CssConstants.WRAP,
@@ -202,7 +205,7 @@ public void applyFlexWrapTest() {
202205
Map<String, String> cssProps = new HashMap<>();
203206
cssProps.put(CssConstants.FLEX_WRAP, wrapStrings[i]);
204207
IElement element = new Div();
205-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
208+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
206209
Assertions.assertEquals(wrapValues[i], (FlexWrapPropertyValue) element.<FlexWrapPropertyValue>getProperty(Property.FLEX_WRAP));
207210
}
208211
}
@@ -213,7 +216,8 @@ public void applyAlignItemsUnsupportedValuesTest() {
213216
Map<String, String> cssProps = new HashMap<>();
214217
cssProps.put(CommonCssConstants.ALIGN_ITEMS, CssConstants.SAFE + " " + CommonCssConstants.FLEX_END);
215218
IElement element = new Div();
216-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
219+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
220+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
217221
Assertions.assertEquals(AlignmentPropertyValue.STRETCH, (AlignmentPropertyValue) element.<AlignmentPropertyValue>getProperty(Property.ALIGN_ITEMS));
218222
}
219223

@@ -223,30 +227,29 @@ public void applyJustifyContentUnsupportedValuesTest() {
223227
Map<String, String> cssProps = new HashMap<>();
224228
cssProps.put(CssConstants.JUSTIFY_CONTENT, "safe center");
225229
IElement element = new Div();
226-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
230+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
231+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
227232
Assertions.assertEquals(JustifyContent.FLEX_START, (JustifyContent) element.<JustifyContent>getProperty(Property.JUSTIFY_CONTENT));
228233
}
229234

230235
@Test
231236
@LogMessages(messages = @LogMessage(messageTemplate = Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET, count = 2))
232237
public void applyFlexContainerUnsupportedPropertiesUnsupportedValuesTest() {
238+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
233239
String[] unsupportedProperties = {
234240
CssConstants.FLEX_DIRECTION,
235-
CssConstants.ROW_GAP,
236-
CssConstants.COLUMN_GAP,
237241
CssConstants.ALIGN_CONTENT
238242
};
243+
// Invalid values are used for testing.
239244
String[] unsupportedValues = {
240-
CssConstants.COLUMN,
241-
"20px",
242-
"10em",
243-
CssConstants.SPACE_AROUND
245+
CssConstants.NORMAL,
246+
CssConstants.RIGHT
244247
};
245248
for (int i = 0; i < unsupportedValues.length; ++i) {
246249
Map<String, String> cssProps = new HashMap<>();
247250
cssProps.put(unsupportedProperties[i], unsupportedValues[i]);
248251
IElement element = new Div();
249-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
252+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
250253
}
251254

252255
// This test checks that there are log messages so assertions are not required
@@ -270,6 +273,7 @@ public void applyFlexItemUnsupportedPropertiesUnsupportedValuesTest() {
270273

271274
@Test
272275
public void applyFlexContainerUnsupportedPropertiesSupportedValuesTest() {
276+
ProcessorContext context = new ProcessorContext(new ConverterProperties());
273277
String[] unsupportedProperties = {
274278
CssConstants.FLEX_DIRECTION,
275279
CssConstants.ALIGN_CONTENT
@@ -282,7 +286,7 @@ public void applyFlexContainerUnsupportedPropertiesSupportedValuesTest() {
282286
Map<String, String> cssProps = new HashMap<>();
283287
cssProps.put(unsupportedProperties[i], supportedValues[i]);
284288
IElement element = new Div();
285-
FlexApplierUtil.applyFlexContainerProperties(cssProps, element);
289+
FlexApplierUtil.applyFlexContainerProperties(cssProps, element, context);
286290
}
287291

288292
// This test checks that there are no log messages so assertions are not required

src/test/java/com/itextpdf/html2pdf/css/flex/FlexAlignSelfTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ public void baselineFlexDirRowReverseFlexWrapTest() throws IOException, Interrup
197197
}
198198

199199
// TODO DEVSIX-5167 Support baseline value for align-items and align-self
200-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
201200
@Test
202201
@LogMessages(messages = {
203202
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET, count = 12)
@@ -207,7 +206,6 @@ public void baselineFlexDirColumnFlexWrapTest() throws IOException, InterruptedE
207206
}
208207

209208
// TODO DEVSIX-5167 Support baseline value for align-items and align-self
210-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
211209
@Test
212210
@LogMessages(messages = {
213211
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.FLEX_PROPERTY_IS_NOT_SUPPORTED_YET, count = 12)
@@ -221,7 +219,6 @@ public void autoFlexDirRowFlexWrapTest() throws IOException, InterruptedExceptio
221219
convertToPdfAndCompare("autoFlexDirRowFlexWrapTest", SOURCE_FOLDER, DESTINATION_FOLDER);
222220
}
223221

224-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
225222
@Test
226223
public void normalFlexDirRowFlexWrapTest() throws IOException, InterruptedException {
227224
convertToPdfAndCompare("normalFlexDirRowFlexWrapTest", SOURCE_FOLDER, DESTINATION_FOLDER);
@@ -263,25 +260,21 @@ public void selfEndFlexDirColumnFlexWrapDirTest() throws IOException, Interrupte
263260
convertToPdfAndCompare("selfEndFlexDirColumnFlexWrapDirTest", SOURCE_FOLDER, DESTINATION_FOLDER);
264261
}
265262

266-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
267263
@Test
268264
public void stretchFlexDirRowFlexWrapTest() throws IOException, InterruptedException {
269265
convertToPdfAndCompare("stretchFlexDirRowFlexWrapTest", SOURCE_FOLDER, DESTINATION_FOLDER);
270266
}
271267

272-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
273268
@Test
274269
public void stretchFlexDirRowReverseFlexWrapTest() throws IOException, InterruptedException {
275270
convertToPdfAndCompare("stretchFlexDirRowReverseFlexWrapTest", SOURCE_FOLDER, DESTINATION_FOLDER);
276271
}
277272

278-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
279273
@Test
280274
public void stretchFlexDirColumnFlexWrapTest() throws IOException, InterruptedException {
281275
convertToPdfAndCompare("stretchFlexDirColumnFlexWrapTest", SOURCE_FOLDER, DESTINATION_FOLDER);
282276
}
283277

284-
// TODO DEVSIX-9435 Flex: fix items align-self in case of flex-wrap: wrap-reverse
285278
@Test
286279
public void stretchFlexDirColumnReverseFlexWrapTest() throws IOException, InterruptedException {
287280
convertToPdfAndCompare("stretchFlexDirColumnReverseFlexWrapTest", SOURCE_FOLDER, DESTINATION_FOLDER);

0 commit comments

Comments
 (0)