Skip to content

Commit 980ec02

Browse files
author
Kate Ivanova
committed
Replace html2pdf-private tests: CssStyleAttributeParseTest
DEVSIX-5346, DEVSIX-872
1 parent e9ce2ff commit 980ec02

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.itextpdf.styledxmlparser.css.parse;
2+
3+
import com.itextpdf.io.IOException;
4+
import com.itextpdf.styledxmlparser.css.CssDeclaration;
5+
import com.itextpdf.test.ExtendedITextTest;
6+
import com.itextpdf.test.annotations.type.UnitTest;
7+
8+
import java.util.List;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
import org.junit.experimental.categories.Category;
12+
13+
@Category(UnitTest.class)
14+
public class CssRuleSetParserTest extends ExtendedITextTest {
15+
16+
@Test
17+
public void parsePropertyDeclarationsTest() throws IOException {
18+
String src = "float:right; clear:right;width:22.0em; margin:0 0 1.0em 1.0em; background:#f9f9f9; "
19+
+ "border:1px solid #aaa;padding:0.2em;border-spacing:0.4em 0; text-align:center; "
20+
+ "line-height:1.4em; font-size:88%;";
21+
22+
String[] expected = new String[] {
23+
"float: right",
24+
"clear: right",
25+
"width: 22.0em",
26+
"margin: 0 0 1.0em 1.0em",
27+
"background: #f9f9f9",
28+
"border: 1px solid #aaa",
29+
"padding: 0.2em",
30+
"border-spacing: 0.4em 0",
31+
"text-align: center",
32+
"line-height: 1.4em",
33+
"font-size: 88%"
34+
};
35+
36+
List<CssDeclaration> declarations = CssRuleSetParser.parsePropertyDeclarations(src);
37+
Assert.assertEquals(expected.length, declarations.size());
38+
for (int i = 0; i < expected.length; i++) {
39+
Assert.assertEquals(expected[i], declarations.get(i).toString());
40+
}
41+
}
42+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.itextpdf.styledxmlparser.css.parse;
2+
3+
import com.itextpdf.styledxmlparser.IXmlParser;
4+
import com.itextpdf.styledxmlparser.node.IAttributes;
5+
import com.itextpdf.styledxmlparser.node.IDocumentNode;
6+
import com.itextpdf.styledxmlparser.node.IElementNode;
7+
import com.itextpdf.styledxmlparser.node.INode;
8+
import com.itextpdf.styledxmlparser.node.impl.jsoup.JsoupHtmlParser;
9+
import com.itextpdf.test.ExtendedITextTest;
10+
import com.itextpdf.test.annotations.type.IntegrationTest;
11+
12+
import java.io.FileInputStream;
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import org.junit.experimental.categories.Category;
19+
20+
@Category(IntegrationTest.class)
21+
public class CssStyleAttributeParseTest extends ExtendedITextTest {
22+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/styledxmlparser/css/parse/CssStyleAttributeParseTest/";
23+
24+
@Test
25+
public void styleAttributeParseTest() throws IOException {
26+
String fileName = SOURCE_FOLDER + "cssStyleAttributeParse.html";
27+
28+
IXmlParser parser = new JsoupHtmlParser();
29+
IDocumentNode document = parser.parse(new FileInputStream(fileName), "UTF-8");
30+
31+
List<String> styleDeclarations = new ArrayList<>();
32+
33+
List<String> expectStyleDeclarations = new ArrayList<>();
34+
expectStyleDeclarations.add("display:none;");
35+
expectStyleDeclarations.add("position:relative;");
36+
expectStyleDeclarations.add("display:none");
37+
expectStyleDeclarations.add("text-align:center;");
38+
expectStyleDeclarations.add("white-space:nowrap;");
39+
expectStyleDeclarations
40+
.add("float:right; clear:right; width:22.0em; margin:0 0 1.0em 1.0em; background:#f9f9f9;"
41+
+ " border:1px solid #aaa; padding:0.2em; border-spacing:0.4em 0; text-align:center;"
42+
+ " line-height:1.4em; font-size:88%;");
43+
expectStyleDeclarations.add("padding:0.2em 0.4em 0.2em; font-size:145%; line-height:1.15em; font-weight:bold;"
44+
+ " display:block; margin-bottom:0.25em;");
45+
46+
parseStyleAttrForSubtree(document, styleDeclarations);
47+
48+
Assert.assertEquals(styleDeclarations.size(), expectStyleDeclarations.size());
49+
50+
for (int i = 0; i < expectStyleDeclarations.size(); i++) {
51+
Assert.assertEquals(expectStyleDeclarations.get(i), styleDeclarations.get(i));
52+
}
53+
}
54+
55+
private void parseOwnStyleAttr(IElementNode element, List<String> styleDeclarations) {
56+
IAttributes attributes = element.getAttributes();
57+
String styleAttr = attributes.getAttribute("style");
58+
59+
if (styleAttr != null && styleAttr.length() > 0) {
60+
styleDeclarations.add(styleAttr);
61+
}
62+
}
63+
64+
private void parseStyleAttrForSubtree(INode node, List<String> styleDeclarations) {
65+
if (node instanceof IElementNode) {
66+
parseOwnStyleAttr((IElementNode) node, styleDeclarations);
67+
}
68+
for (INode child : node.childNodes()) {
69+
parseStyleAttrForSubtree(child, styleDeclarations);
70+
}
71+
}
72+
}
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE html>
2+
<html lang="ru" dir="ltr">
3+
4+
<body>
5+
<h1 lang="ru">Hello</h1>
6+
<div>
7+
<div>Good morning</div>
8+
<div>
9+
<div>Have a nice day</div>
10+
<div>
11+
<div>
12+
<p style="display:none;">today</p></div>
13+
<div style="position:relative;">
14+
<div style="display:none">Hello!!!</div>
15+
</div>
16+
</div>
17+
</div>
18+
<div lang="ru" dir="ltr">
19+
<table style="">
20+
<caption class="" style="">Hello, iText!</caption>
21+
<tr><td colspan="2" class="" style="text-align:center;"></td></tr>
22+
<tr><td style=""></td></tr>
23+
<tr><td class="" style=""><p><span>text/css</span></td></tr>
24+
<tr>
25+
<th style="">Monday</th>
26+
<td class="" style=""></td>
27+
</tr>
28+
<tr>
29+
<th style="">Tuesday</th>
30+
<td class="" style=""><p><span style="white-space:nowrap;">morning</span></p></td>
31+
</tr>
32+
<tr>
33+
<th style="">Wednesday</th>
34+
<td class="" style="">
35+
<p>evening</p>
36+
</td>
37+
</tr>
38+
<tr>
39+
<th style="">Thursday</th>
40+
<td class="" style="">
41+
<p>afternoon</a><br /></p>
42+
</td>
43+
</tr>
44+
<tr>
45+
<th style=""></th>
46+
<td class="" style="">
47+
<p></p>
48+
</td>
49+
</tr>
50+
</table>
51+
<table style="float:right; clear:right; width:22.0em; margin:0 0 1.0em 1.0em; background:#f9f9f9; border:1px solid #aaa; padding:0.2em; border-spacing:0.4em 0; text-align:center; line-height:1.4em; font-size:88%;" cellspacing="0" cellpadding="0">
52+
<tr>
53+
<td style="padding:0.2em 0.4em 0.2em; font-size:145%; line-height:1.15em; font-weight:bold; display:block; margin-bottom:0.25em;">Good bye</td>
54+
</tr>
55+
</table>
56+
</body>
57+
</html>

0 commit comments

Comments
 (0)