Skip to content

Commit 23f7aea

Browse files
Kate IvanovaiText-CI
authored andcommitted
Replace html2pdf-private tests: CssStyleAttributeParseTest
DEVSIX-5346, DEVSIX-872 Autoported commit. Original commit hash: [980ec02f7]
1 parent 11feb75 commit 23f7aea

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using iText.StyledXmlParser.Css;
4+
using iText.Test;
5+
6+
namespace iText.StyledXmlParser.Css.Parse {
7+
public class CssRuleSetParserTest : ExtendedITextTest {
8+
[NUnit.Framework.Test]
9+
public virtual void ParsePropertyDeclarationsTest() {
10+
String src = "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; "
11+
+ "line-height:1.4em; font-size:88%;";
12+
String[] expected = new String[] { "float: right", "clear: right", "width: 22.0em", "margin: 0 0 1.0em 1.0em"
13+
, "background: #f9f9f9", "border: 1px solid #aaa", "padding: 0.2em", "border-spacing: 0.4em 0", "text-align: center"
14+
, "line-height: 1.4em", "font-size: 88%" };
15+
IList<CssDeclaration> declarations = CssRuleSetParser.ParsePropertyDeclarations(src);
16+
NUnit.Framework.Assert.AreEqual(expected.Length, declarations.Count);
17+
for (int i = 0; i < expected.Length; i++) {
18+
NUnit.Framework.Assert.AreEqual(expected[i], declarations[i].ToString());
19+
}
20+
}
21+
}
22+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using iText.StyledXmlParser;
5+
using iText.StyledXmlParser.Node;
6+
using iText.StyledXmlParser.Node.Impl.Jsoup;
7+
using iText.Test;
8+
9+
namespace iText.StyledXmlParser.Css.Parse {
10+
public class CssStyleAttributeParseTest : ExtendedITextTest {
11+
private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
12+
.CurrentContext.TestDirectory) + "/resources/itext/styledxmlparser/css/parse/CssStyleAttributeParseTest/";
13+
14+
[NUnit.Framework.Test]
15+
public virtual void StyleAttributeParseTest() {
16+
String fileName = SOURCE_FOLDER + "cssStyleAttributeParse.html";
17+
IXmlParser parser = new JsoupHtmlParser();
18+
IDocumentNode document = parser.Parse(new FileStream(fileName, FileMode.Open, FileAccess.Read), "UTF-8");
19+
IList<String> styleDeclarations = new List<String>();
20+
IList<String> expectStyleDeclarations = new List<String>();
21+
expectStyleDeclarations.Add("display:none;");
22+
expectStyleDeclarations.Add("position:relative;");
23+
expectStyleDeclarations.Add("display:none");
24+
expectStyleDeclarations.Add("text-align:center;");
25+
expectStyleDeclarations.Add("white-space:nowrap;");
26+
expectStyleDeclarations.Add("float:right; clear:right; width:22.0em; margin:0 0 1.0em 1.0em; background:#f9f9f9;"
27+
+ " border:1px solid #aaa; padding:0.2em; border-spacing:0.4em 0; text-align:center;" + " line-height:1.4em; font-size:88%;"
28+
);
29+
expectStyleDeclarations.Add("padding:0.2em 0.4em 0.2em; font-size:145%; line-height:1.15em; font-weight:bold;"
30+
+ " display:block; margin-bottom:0.25em;");
31+
ParseStyleAttrForSubtree(document, styleDeclarations);
32+
NUnit.Framework.Assert.AreEqual(styleDeclarations.Count, expectStyleDeclarations.Count);
33+
for (int i = 0; i < expectStyleDeclarations.Count; i++) {
34+
NUnit.Framework.Assert.AreEqual(expectStyleDeclarations[i], styleDeclarations[i]);
35+
}
36+
}
37+
38+
private void ParseOwnStyleAttr(IElementNode element, IList<String> styleDeclarations) {
39+
IAttributes attributes = element.GetAttributes();
40+
String styleAttr = attributes.GetAttribute("style");
41+
if (styleAttr != null && styleAttr.Length > 0) {
42+
styleDeclarations.Add(styleAttr);
43+
}
44+
}
45+
46+
private void ParseStyleAttrForSubtree(INode node, IList<String> styleDeclarations) {
47+
if (node is IElementNode) {
48+
ParseOwnStyleAttr((IElementNode)node, styleDeclarations);
49+
}
50+
foreach (INode child in node.ChildNodes()) {
51+
ParseStyleAttrForSubtree(child, styleDeclarations);
52+
}
53+
}
54+
}
55+
}
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>

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e9ce2ffdb4ae355b60312c7f66239cb4ff0d7b67
1+
980ec02f729694209fd364e21e5cd09b18454de6

0 commit comments

Comments
 (0)