Skip to content

Commit 123a98d

Browse files
BlackEgoistiText-CI
authored andcommitted
Add shorthand text decoration support
DEVSIX-3933 Autoported commit. Original commit hash: [d97d18245]
1 parent a38e784 commit 123a98d

File tree

11 files changed

+440
-14
lines changed

11 files changed

+440
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using iText.StyledXmlParser.Css;
4+
using iText.StyledXmlParser.Css.Resolve.Shorthand.Impl;
5+
using iText.Test;
6+
7+
namespace iText.StyledXmlParser.Css.Resolve.Shorthand {
8+
public class TextDecorationUnitTest : ExtendedITextTest {
9+
[NUnit.Framework.Test]
10+
public virtual void ResolveShorthandLineEmptyTest() {
11+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
12+
IList<CssDeclaration> result = resolver.ResolveShorthand("");
13+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
14+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
15+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-line"));
16+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
17+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
18+
}
19+
20+
[NUnit.Framework.Test]
21+
public virtual void ResolveShorthandLineNoneTest() {
22+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
23+
IList<CssDeclaration> result = resolver.ResolveShorthand("none");
24+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
25+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
26+
NUnit.Framework.Assert.AreEqual("none", resultMap.Get("text-decoration-line"));
27+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
28+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
29+
}
30+
31+
[NUnit.Framework.Test]
32+
public virtual void ResolveShorthandLineNoneAndUnderlineTogetherTest() {
33+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
34+
IList<CssDeclaration> result = resolver.ResolveShorthand("none underline");
35+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
36+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
37+
String line = resultMap.Get("text-decoration-line");
38+
NUnit.Framework.Assert.IsTrue(line != null && line.Contains("underline") && line.Contains("none"));
39+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
40+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
41+
}
42+
43+
[NUnit.Framework.Test]
44+
public virtual void ResolveShorthandLineOnePropertyTest() {
45+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
46+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline");
47+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
48+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
49+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
50+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
51+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
52+
}
53+
54+
[NUnit.Framework.Test]
55+
public virtual void ResolveShorthandLineTwoPropertiesTest() {
56+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
57+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline overline");
58+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
59+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
60+
String line = resultMap.Get("text-decoration-line");
61+
NUnit.Framework.Assert.IsTrue(line != null && line.Contains("underline") && line.Contains("overline"));
62+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
63+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
64+
}
65+
66+
[NUnit.Framework.Test]
67+
public virtual void ResolveShorthandColorNamedTest() {
68+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
69+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline red");
70+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
71+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
72+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
73+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
74+
NUnit.Framework.Assert.AreEqual("red", resultMap.Get("text-decoration-color"));
75+
}
76+
77+
[NUnit.Framework.Test]
78+
public virtual void ResolveShorthandColorRgbTest() {
79+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
80+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline rgb(255, 255, 0)");
81+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
82+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
83+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
84+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
85+
NUnit.Framework.Assert.AreEqual("rgb(255,255,0)", resultMap.Get("text-decoration-color"));
86+
}
87+
88+
[NUnit.Framework.Test]
89+
public virtual void ResolveShorthandColorRgbWithOpacityTest() {
90+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
91+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline rgb(255, 255, 0, 0.5)");
92+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
93+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
94+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
95+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
96+
NUnit.Framework.Assert.AreEqual("rgb(255,255,0,0.5)", resultMap.Get("text-decoration-color"));
97+
}
98+
99+
[NUnit.Framework.Test]
100+
public virtual void ResolveShorthandColorHslTest() {
101+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
102+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline hsl(300, 76%, 72%)");
103+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
104+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
105+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
106+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
107+
NUnit.Framework.Assert.AreEqual("hsl(300,76%,72%)", resultMap.Get("text-decoration-color"));
108+
}
109+
110+
[NUnit.Framework.Test]
111+
public virtual void ResolveShorthandColorHexTest() {
112+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
113+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline #DDAA55");
114+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
115+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
116+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
117+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-style"));
118+
NUnit.Framework.Assert.AreEqual("#ddaa55", resultMap.Get("text-decoration-color"));
119+
}
120+
121+
[NUnit.Framework.Test]
122+
public virtual void ResolveShorthandStyleOnePropertyTest() {
123+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
124+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline wavy");
125+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
126+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
127+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
128+
NUnit.Framework.Assert.AreEqual("wavy", resultMap.Get("text-decoration-style"));
129+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
130+
}
131+
132+
[NUnit.Framework.Test]
133+
public virtual void ResolveShorthandStyleTwoPropertiesTest() {
134+
TextDecorationShorthandResolver resolver = new TextDecorationShorthandResolver();
135+
IList<CssDeclaration> result = resolver.ResolveShorthand("underline wavy dotted");
136+
IDictionary<String, String> resultMap = ConvertCssDeclarationsToMap(result);
137+
NUnit.Framework.Assert.AreEqual(3, resultMap.Count);
138+
NUnit.Framework.Assert.AreEqual("underline", resultMap.Get("text-decoration-line"));
139+
NUnit.Framework.Assert.AreEqual("dotted", resultMap.Get("text-decoration-style"));
140+
NUnit.Framework.Assert.AreEqual("initial", resultMap.Get("text-decoration-color"));
141+
}
142+
143+
private IDictionary<String, String> ConvertCssDeclarationsToMap(IList<CssDeclaration> declarations) {
144+
IDictionary<String, String> result = new Dictionary<String, String>();
145+
foreach (CssDeclaration decl in declarations) {
146+
result.Put(decl.GetProperty(), decl.GetExpression());
147+
}
148+
return result;
149+
}
150+
}
151+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using iText.StyledXmlParser.Css.Resolve;
4+
using iText.Test;
5+
6+
namespace iText.StyledXmlParser.Util {
7+
public class StyleUtilUnitTest : ExtendedITextTest {
8+
private static ICollection<IStyleInheritance> inheritanceRules;
9+
10+
[NUnit.Framework.OneTimeSetUp]
11+
public static void Before() {
12+
inheritanceRules = new HashSet<IStyleInheritance>();
13+
inheritanceRules.Add(new CssInheritance());
14+
}
15+
16+
[NUnit.Framework.Test]
17+
public virtual void MergeParentDeclarationsMeasurementDoNotInheritTest() {
18+
IDictionary<String, String> styles = new Dictionary<String, String>();
19+
String styleProperty = "font-size";
20+
styles.Put(styleProperty, "12px");
21+
String parentPropValue = "16cm";
22+
String parentFontSize = "0";
23+
IDictionary<String, String> expectedStyles = new Dictionary<String, String>();
24+
expectedStyles.Put(styleProperty, "12px");
25+
styles = StyleUtil.MergeParentStyleDeclaration(styles, styleProperty, parentPropValue, parentFontSize, inheritanceRules
26+
);
27+
bool equal = styles.Count == expectedStyles.Count;
28+
foreach (KeyValuePair<String, String> kvp in expectedStyles) {
29+
equal &= kvp.Value.Equals(styles.Get(kvp.Key));
30+
}
31+
NUnit.Framework.Assert.IsTrue(equal);
32+
}
33+
34+
[NUnit.Framework.Test]
35+
public virtual void MergeParentDeclarationsMeasurementInheritTest() {
36+
IDictionary<String, String> styles = new Dictionary<String, String>();
37+
String styleProperty = "font-size";
38+
String parentPropValue = "16cm";
39+
String parentFontSize = "0";
40+
IDictionary<String, String> expectedStyles = new Dictionary<String, String>();
41+
expectedStyles.Put(styleProperty, parentPropValue);
42+
styles = StyleUtil.MergeParentStyleDeclaration(styles, styleProperty, parentPropValue, parentFontSize, inheritanceRules
43+
);
44+
bool equal = styles.Count == expectedStyles.Count;
45+
foreach (KeyValuePair<String, String> kvp in expectedStyles) {
46+
equal &= kvp.Value.Equals(styles.Get(kvp.Key));
47+
}
48+
NUnit.Framework.Assert.IsTrue(equal);
49+
}
50+
51+
[NUnit.Framework.Test]
52+
public virtual void MergeParentDeclarationsRelativeMeasurementInheritTest() {
53+
IDictionary<String, String> styles = new Dictionary<String, String>();
54+
String styleProperty = "font-size";
55+
String parentPropValue = "80%";
56+
String parentFontSize = "16";
57+
IDictionary<String, String> expectedStyles = new Dictionary<String, String>();
58+
expectedStyles.Put(styleProperty, "9.6pt");
59+
styles = StyleUtil.MergeParentStyleDeclaration(styles, styleProperty, parentPropValue, parentFontSize, inheritanceRules
60+
);
61+
bool equal = styles.Count == expectedStyles.Count;
62+
foreach (KeyValuePair<String, String> kvp in expectedStyles) {
63+
equal &= kvp.Value.Equals(styles.Get(kvp.Key));
64+
}
65+
NUnit.Framework.Assert.IsTrue(equal);
66+
}
67+
68+
[NUnit.Framework.Test]
69+
public virtual void MergeParentDeclarationsTextDecorationsTest() {
70+
IDictionary<String, String> styles = new Dictionary<String, String>();
71+
String styleProperty = "text-decoration-line";
72+
styles.Put(styleProperty, "line-through");
73+
String parentPropValue = "underline";
74+
String parentFontSize = "0";
75+
IDictionary<String, String> expectedStyles = new Dictionary<String, String>();
76+
expectedStyles.Put(styleProperty, "line-through underline");
77+
styles = StyleUtil.MergeParentStyleDeclaration(styles, styleProperty, parentPropValue, parentFontSize, inheritanceRules
78+
);
79+
bool equal = styles.Count == expectedStyles.Count;
80+
foreach (KeyValuePair<String, String> kvp in expectedStyles) {
81+
equal &= kvp.Value.Equals(styles.Get(kvp.Key));
82+
}
83+
NUnit.Framework.Assert.IsTrue(equal);
84+
}
85+
}
86+
}

itext/itext.styledxmlparser/itext/styledxmlparser/css/CommonCssConstants.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ static CommonCssConstants() {
366366
/// <summary>The Constant TEXT_DECORATION.</summary>
367367
public const String TEXT_DECORATION = "text-decoration";
368368

369+
/// <summary>The Constant TEXT_DECORATION_LINE.</summary>
370+
public const String TEXT_DECORATION_LINE = "text-decoration-line";
371+
372+
/// <summary>The Constant TEXT_DECORATION_STYLE.</summary>
373+
public const String TEXT_DECORATION_STYLE = "text-decoration-style";
374+
375+
/// <summary>The Constant TEXT_DECORATION_COLOR.</summary>
376+
public const String TEXT_DECORATION_COLOR = "text-decoration-color";
377+
369378
/// <summary>The Constant TEXT_INDENT.</summary>
370379
public const String TEXT_INDENT = "text-indent";
371380

@@ -427,6 +436,9 @@ static CommonCssConstants() {
427436
/// <summary>The Constant AUTO.</summary>
428437
public const String AUTO = "auto";
429438

439+
/// <summary>The Constant BLINK.</summary>
440+
public const String BLINK = "blink";
441+
430442
/// <summary>The Constant BOLD.</summary>
431443
public const String BOLD = "bold";
432444

@@ -541,6 +553,9 @@ static CommonCssConstants() {
541553
/// <summary>The Constant LIGHTER.</summary>
542554
public const String LIGHTER = "lighter";
543555

556+
/// <summary>The Constant value LINE_THROUGH.</summary>
557+
public const String LINE_THROUGH = "line-through";
558+
544559
/// <summary>The Constant LOCAL.</summary>
545560
public const String LOCAL = "local";
546561

@@ -598,6 +613,9 @@ static CommonCssConstants() {
598613
/// <summary>The Constant OUTSET.</summary>
599614
public const String OUTSET = "outset";
600615

616+
/// <summary>The Constant value OVERLINE.</summary>
617+
public const String OVERLINE = "overline";
618+
601619
/// <summary>The Constant PADDING_BOX.</summary>
602620
public const String PADDING_BOX = "padding-box";
603621

@@ -691,6 +709,9 @@ static CommonCssConstants() {
691709
/// <summary>The Constant TRANSPARENT.</summary>
692710
public const String TRANSPARENT = "transparent";
693711

712+
/// <summary>The Constant value UNDERLINE</summary>
713+
public const String UNDERLINE = "underline";
714+
694715
/// <summary>The Constant UPPER_ALPHA.</summary>
695716
public const String UPPER_ALPHA = "upper-alpha";
696717

@@ -703,6 +724,9 @@ static CommonCssConstants() {
703724
/// <summary>The Constant value VISIBLE.</summary>
704725
public const String VISIBLE = "visible";
705726

727+
/// <summary>The Constant value WAVY.</summary>
728+
public const String WAVY = "wavy";
729+
706730
/// <summary>The Constant X_LARGE.</summary>
707731
public const String X_LARGE = "x-large";
708732

itext/itext.styledxmlparser/itext/styledxmlparser/css/resolve/CssDefaults.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ static CssDefaults() {
115115
defaultValues.Put(CommonCssConstants.QUOTES, "\"\\00ab\" \"\\00bb\"");
116116
defaultValues.Put(CommonCssConstants.TEXT_ALIGN, CommonCssConstants.START);
117117
defaultValues.Put(CommonCssConstants.TEXT_DECORATION, CommonCssConstants.NONE);
118+
defaultValues.Put(CommonCssConstants.TEXT_DECORATION_LINE, CommonCssConstants.NONE);
119+
defaultValues.Put(CommonCssConstants.TEXT_DECORATION_STYLE, CommonCssConstants.SOLID);
120+
defaultValues.Put(CommonCssConstants.TEXT_DECORATION_COLOR, CommonCssConstants.CURRENTCOLOR);
118121
defaultValues.Put(CommonCssConstants.TEXT_TRANSFORM, CommonCssConstants.NONE);
119-
defaultValues.Put(CommonCssConstants.TEXT_DECORATION, CommonCssConstants.NONE);
120122
defaultValues.Put(CommonCssConstants.WHITE_SPACE, CommonCssConstants.NORMAL);
121123
defaultValues.Put(CommonCssConstants.WIDTH, CommonCssConstants.AUTO);
122124
defaultValues.Put(CommonCssConstants.ORPHANS, "2");

itext/itext.styledxmlparser/itext/styledxmlparser/css/resolve/shorthand/ShorthandResolverFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ static ShorthandResolverFactory() {
6868
shorthandResolvers.Put(CommonCssConstants.MARGIN, new MarginShorthandResolver());
6969
shorthandResolvers.Put(CommonCssConstants.OUTLINE, new OutlineShorthandResolver());
7070
shorthandResolvers.Put(CommonCssConstants.PADDING, new PaddingShorthandResolver());
71+
shorthandResolvers.Put(CommonCssConstants.TEXT_DECORATION, new TextDecorationShorthandResolver());
7172
}
7273

73-
// TODO text-decoration is a shorthand in CSS3, however it is not yet supported in any major browsers
7474
/// <summary>Gets a shorthand resolver.</summary>
7575
/// <param name="shorthandProperty">the property</param>
7676
/// <returns>the shorthand resolver</returns>

0 commit comments

Comments
 (0)