Skip to content

Commit 1094128

Browse files
committed
Support column-rule shorthand
DEVSIX-7564
1 parent a52e878 commit 1094128

File tree

5 files changed

+387
-2
lines changed

5 files changed

+387
-2
lines changed

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/CommonCssConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ public class CommonCssConstants {
323323
*/
324324
public static final String COLUMNS = "columns";
325325

326+
/**
327+
* The Constant COLUMNS
328+
*/
329+
public static final String COLUMN_RULE = "column-rule";
330+
331+
326332
/**
327333
* The Constant DARKEN.
328334
*/

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/ShorthandResolverFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This file is part of the iText (R) project.
3535
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.BorderStyleShorthandResolver;
3636
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.BorderTopShorthandResolver;
3737
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.BorderWidthShorthandResolver;
38+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.ColumnRuleShortHandResolver;
3839
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.ColumnsShorthandResolver;
3940
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.FlexFlowShorthandResolver;
4041
import com.itextpdf.styledxmlparser.css.resolve.shorthand.impl.FlexShorthandResolver;
@@ -81,6 +82,7 @@ public class ShorthandResolverFactory {
8182
shorthandResolvers.put(CommonCssConstants.GAP, new GapShorthandResolver());
8283
shorthandResolvers.put(CommonCssConstants.PLACE_ITEMS, new PlaceItemsShorthandResolver());
8384
shorthandResolvers.put(CommonCssConstants.COLUMNS, new ColumnsShorthandResolver());
85+
shorthandResolvers.put(CommonCssConstants.COLUMN_RULE, new ColumnRuleShortHandResolver());
8486
}
8587

8688
/**
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2023 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl;
24+
25+
import com.itextpdf.commons.utils.MessageFormatUtil;
26+
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
27+
import com.itextpdf.styledxmlparser.css.CssDeclaration;
28+
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
29+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
30+
import com.itextpdf.styledxmlparser.css.util.CssUtils;
31+
import com.itextpdf.styledxmlparser.css.validate.impl.datatype.CssEnumValidator;
32+
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
33+
34+
import java.util.ArrayList;
35+
import java.util.Arrays;
36+
import java.util.Collections;
37+
import java.util.List;
38+
import org.slf4j.Logger;
39+
import org.slf4j.LoggerFactory;
40+
41+
/**
42+
* Shorthand resolver for the column-rule property.
43+
* This property is a shorthand for the column-rule-width, column-rule-style, and column-rule-color properties.
44+
*/
45+
public class ColumnRuleShortHandResolver implements IShorthandResolver {
46+
47+
private static final Logger LOGGER = LoggerFactory.getLogger(ColumnRuleShortHandResolver.class);
48+
private final CssEnumValidator borderStyleValidators = new CssEnumValidator(CommonCssConstants.BORDER_STYLE_VALUES);
49+
private final CssEnumValidator borderWithValidators = new CssEnumValidator(CommonCssConstants.BORDER_WIDTH_VALUES);
50+
51+
/**
52+
* Creates a new {@link ColumnsShorthandResolver} instance.
53+
*/
54+
public ColumnRuleShortHandResolver() {
55+
//empty constructor
56+
}
57+
58+
/**
59+
* Resolves a shorthand expression.
60+
*
61+
* @param shorthandExpression the shorthand expression
62+
*
63+
* @return a list of CSS declaration
64+
*/
65+
@Override
66+
public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
67+
shorthandExpression = shorthandExpression.trim();
68+
if (CssTypesValidationUtils.isInitialOrInheritOrUnset(shorthandExpression)) {
69+
return Arrays.asList(
70+
new CssDeclaration(CommonCssConstants.COLUMN_RULE_COLOR, shorthandExpression),
71+
new CssDeclaration(CommonCssConstants.COLUMN_RULE_WIDTH, shorthandExpression),
72+
new CssDeclaration(CommonCssConstants.COLUMN_RULE_STYLE, shorthandExpression)
73+
);
74+
}
75+
if (CssTypesValidationUtils.containsInitialOrInheritOrUnset(shorthandExpression)) {
76+
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION,
77+
CommonCssConstants.COLUMN_RULE, shorthandExpression);
78+
}
79+
if (shorthandExpression.isEmpty()) {
80+
return handleExpressionError(StyledXmlParserLogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY,
81+
CommonCssConstants.COLUMN_RULE, shorthandExpression);
82+
}
83+
84+
final int maxProperties = 3;
85+
List<String> properties = CssUtils.extractShorthandProperties(shorthandExpression).get(0);
86+
if (properties.size() > maxProperties) {
87+
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION,
88+
CommonCssConstants.COLUMN_RULE, shorthandExpression);
89+
}
90+
List<CssDeclaration> result = new ArrayList<>(maxProperties);
91+
for (String property : properties) {
92+
String cleanProperty = property.trim();
93+
CssDeclaration declaration = processProperty(cleanProperty);
94+
if (declaration != null) {
95+
result.add(declaration);
96+
}
97+
if (declaration == null) {
98+
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION,
99+
CommonCssConstants.COLUMN_RULE_STYLE, shorthandExpression);
100+
}
101+
}
102+
103+
return result;
104+
}
105+
106+
private CssDeclaration processProperty(String value) {
107+
if (CssTypesValidationUtils.isMetricValue(value) || CssTypesValidationUtils.isRelativeValue(value)
108+
|| borderWithValidators.isValid(value)) {
109+
return new CssDeclaration(CommonCssConstants.COLUMN_RULE_WIDTH, value);
110+
}
111+
if (CssTypesValidationUtils.isColorProperty(value)) {
112+
return new CssDeclaration(CommonCssConstants.COLUMN_RULE_COLOR, value);
113+
}
114+
if (borderStyleValidators.isValid(value)) {
115+
return new CssDeclaration(CommonCssConstants.COLUMN_RULE_STYLE, value);
116+
}
117+
return null;
118+
}
119+
120+
private static List<CssDeclaration> handleExpressionError(String logMessage, String attribute,
121+
String shorthandExpression) {
122+
LOGGER.warn(MessageFormatUtil.format(logMessage, attribute, shorthandExpression));
123+
return Collections.<CssDeclaration>emptyList();
124+
}
125+
}

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/impl/ColumnsShorthandResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.styledxmlparser.css.CssDeclaration;
2828
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
2929
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
30+
import com.itextpdf.styledxmlparser.css.util.CssUtils;
3031
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
3132

3233
import java.util.ArrayList;
@@ -72,8 +73,8 @@ public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
7273
CommonCssConstants.COLUMNS, shorthandExpression);
7374
}
7475

75-
final String[] properties = shorthandExpression.split(" ");
76-
if (properties.length > 2) {
76+
final List<String> properties = CssUtils.extractShorthandProperties(shorthandExpression).get(0);
77+
if (properties.size() > 2) {
7778
return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION,
7879
CommonCssConstants.COLUMNS, shorthandExpression);
7980
}

0 commit comments

Comments
 (0)