Skip to content

Commit d9513c3

Browse files
SnipxiText-CI
authored andcommitted
Fix CssFontFaceRule#toString() to produce valid CSS
DEVSIX-3321
1 parent 422c996 commit d9513c3

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.styledxmlparser.css;
4444

45-
import com.itextpdf.io.util.MessageFormatUtil;
46-
4745
import java.util.ArrayList;
4846
import java.util.List;
4947

@@ -89,13 +87,11 @@ public void addBodyCssDeclarations(List<CssDeclaration> cssDeclarations) {
8987
@Override
9088
public String toString() {
9189
StringBuilder sb = new StringBuilder();
92-
sb.append(MessageFormatUtil.format("@{0} ", ruleName));
93-
sb.append("{");
94-
sb.append("\n");
90+
sb.append("@").append(getRuleName()).append(" {").append("\n");
9591
for (CssDeclaration declaration : properties) {
9692
sb.append(" ");
9793
sb.append(declaration);
98-
sb.append("\n");
94+
sb.append(";\n");
9995
}
10096
sb.append("}");
10197
return sb.toString();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.itextpdf.styledxmlparser.css;
2+
3+
import com.itextpdf.test.ExtendedITextTest;
4+
import com.itextpdf.test.annotations.type.UnitTest;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.experimental.categories.Category;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
@Category(UnitTest.class)
13+
public class CssFontFaceRuleTest extends ExtendedITextTest {
14+
15+
@Test
16+
public void verifyThatToStringProducesValidCss() {
17+
CssFontFaceRule fontFaceRule = new CssFontFaceRule("");
18+
List<CssDeclaration> declarations = new ArrayList<>();
19+
declarations.add(new CssDeclaration(CommonCssConstants.FONT_FAMILY, "test-font-family"));
20+
declarations.add(new CssDeclaration(CommonCssConstants.FONT_WEIGHT, CommonCssConstants.BOLD));
21+
fontFaceRule.addBodyCssDeclarations(declarations);
22+
23+
String expectedCss = "@font-face {\n" +
24+
" font-family: test-font-family;\n" +
25+
" font-weight: bold;\n" +
26+
"}";
27+
Assert.assertEquals(expectedCss, fontFaceRule.toString());
28+
}
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.itextpdf.styledxmlparser.css;
2+
3+
import com.itextpdf.styledxmlparser.css.page.CssMarginRule;
4+
import com.itextpdf.styledxmlparser.css.page.CssPageRule;
5+
import com.itextpdf.test.ExtendedITextTest;
6+
import com.itextpdf.test.annotations.type.UnitTest;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.experimental.categories.Category;
10+
11+
@Category(UnitTest.class)
12+
public class CssNestedAtRuleFactoryTest extends ExtendedITextTest {
13+
14+
@Test
15+
public void testCreatingNestedRule() {
16+
CssNestedAtRule pageRule = CssNestedAtRuleFactory.createNestedRule("page:first");
17+
Assert.assertTrue(pageRule instanceof CssPageRule);
18+
Assert.assertEquals(CssRuleName.PAGE, pageRule.getRuleName());
19+
Assert.assertEquals(":first", pageRule.getRuleParameters());
20+
21+
CssNestedAtRule rightBottomMarginRule = CssNestedAtRuleFactory.createNestedRule("bottom-right");
22+
Assert.assertTrue(rightBottomMarginRule instanceof CssMarginRule);
23+
Assert.assertEquals(CssRuleName.BOTTOM_RIGHT, rightBottomMarginRule.getRuleName());
24+
25+
CssNestedAtRule fontFaceRule = CssNestedAtRuleFactory.createNestedRule("font-face");
26+
Assert.assertTrue(fontFaceRule instanceof CssFontFaceRule);
27+
Assert.assertEquals(CssRuleName.FONT_FACE, fontFaceRule.getRuleName());
28+
}
29+
30+
}

0 commit comments

Comments
 (0)