Skip to content

Commit 2a5e3d8

Browse files
committed
support lch color declaration
1 parent 17ef10c commit 2a5e3d8

13 files changed

Lines changed: 638 additions & 31 deletions

File tree

src/main/java/org/htmlunit/cssparser/dom/CSSValueImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ else if (value.getLexicalUnitType() == LexicalUnitType.LABCOLOR) {
207207
// LABColor
208208
value_ = new LABColorImpl(value.getFunctionName(), value.getParameters());
209209
}
210+
else if (value.getLexicalUnitType() == LexicalUnitType.LCHCOLOR) {
211+
// LCHColor
212+
value_ = new LCHColorImpl(value.getFunctionName(), value.getParameters());
213+
}
210214
else if (value.getLexicalUnitType() == LexicalUnitType.COUNTER_FUNCTION) {
211215
// Counter
212216
value_ = new CounterImpl(false, value.getParameters());

src/main/java/org/htmlunit/cssparser/dom/HSLColorImpl.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5656
if (next == null) {
5757
throw new DOMException(DOMException.SYNTAX_ERR, function_ + " requires at least three values.");
5858
}
59-
hue_ = new CSSValueImpl(next, true);
59+
hue_ = getHuePart(next);
6060

6161
next = next.getNextLexicalUnit();
6262
if (next == null) {
@@ -79,10 +79,8 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
7979
throw new DOMException(DOMException.SYNTAX_ERR,
8080
function_ + " has to use blank as separator if none is used.");
8181
}
82-
else {
83-
if (LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
84-
throw new DOMException(DOMException.SYNTAX_ERR, "Saturation part has to be percentage.");
85-
}
82+
if (LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
83+
throw new DOMException(DOMException.SYNTAX_ERR, "Saturation part has to be percentage.");
8684
}
8785
saturation_ = new CSSValueImpl(next, true);
8886

@@ -127,12 +125,7 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
127125
throw new DOMException(DOMException.SYNTAX_ERR,
128126
function_ + " has to use blank as separator if none is used.");
129127
}
130-
if (LexicalUnitType.INTEGER != next.getLexicalUnitType()
131-
&& LexicalUnitType.REAL != next.getLexicalUnitType()
132-
&& LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
133-
throw new DOMException(DOMException.SYNTAX_ERR, "Alpha part has to be numeric or percentage.");
134-
}
135-
alpha_ = new CSSValueImpl(next, true);
128+
alpha_ = getAlphaPart(next);
136129
next = next.getNextLexicalUnit();
137130
if (next != null) {
138131
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for " + function_ + " function.");
@@ -173,19 +166,43 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
173166
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
174167
}
175168

176-
if (LexicalUnitType.INTEGER != next.getLexicalUnitType()
177-
&& LexicalUnitType.REAL != next.getLexicalUnitType()
178-
&& LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
179-
throw new DOMException(DOMException.SYNTAX_ERR, "Alpha part has to be numeric or percentage.");
180-
}
181-
alpha_ = new CSSValueImpl(next, true);
169+
alpha_ = getAlphaPart(next);
182170

183171
next = next.getNextLexicalUnit();
184172
if (next != null) {
185173
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for " + function_ + " function.");
186174
}
187175
}
188176

177+
private static CSSValueImpl getHuePart(final LexicalUnit next) {
178+
if (LexicalUnitType.DEGREE == next.getLexicalUnitType()
179+
|| LexicalUnitType.RADIAN == next.getLexicalUnitType()
180+
|| LexicalUnitType.GRADIAN == next.getLexicalUnitType()
181+
|| LexicalUnitType.TURN == next.getLexicalUnitType()
182+
183+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
184+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
185+
186+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
187+
return new CSSValueImpl(next, true);
188+
}
189+
190+
throw new DOMException(DOMException.SYNTAX_ERR, "Color hue part has to be numeric or an angle.");
191+
}
192+
193+
private static CSSValueImpl getAlphaPart(final LexicalUnit next) {
194+
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
195+
196+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
197+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
198+
199+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
200+
return new CSSValueImpl(next, true);
201+
}
202+
203+
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
204+
}
205+
189206
/**
190207
* @return the hue part.
191208
*/

src/main/java/org/htmlunit/cssparser/dom/HWBColorImpl.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5252
if (next == null) {
5353
throw new DOMException(DOMException.SYNTAX_ERR, "hwb requires at least three values.");
5454
}
55-
hue_ = new CSSValueImpl(next, true);
55+
hue_ = getHuePart(next);
5656

5757
next = next.getNextLexicalUnit();
5858
if (next == null) {
@@ -88,19 +88,43 @@ public HWBColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8888
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
8989
}
9090

91-
if (LexicalUnitType.INTEGER != next.getLexicalUnitType()
92-
&& LexicalUnitType.REAL != next.getLexicalUnitType()
93-
&& LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
94-
throw new DOMException(DOMException.SYNTAX_ERR, "Alpha part has to be numeric or percentage.");
95-
}
96-
alpha_ = new CSSValueImpl(next, true);
91+
alpha_ = getAlphaPart(next);
9792

9893
next = next.getNextLexicalUnit();
9994
if (next != null) {
10095
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for hwb function.");
10196
}
10297
}
10398

99+
private static CSSValueImpl getHuePart(final LexicalUnit next) {
100+
if (LexicalUnitType.DEGREE == next.getLexicalUnitType()
101+
|| LexicalUnitType.RADIAN == next.getLexicalUnitType()
102+
|| LexicalUnitType.GRADIAN == next.getLexicalUnitType()
103+
|| LexicalUnitType.TURN == next.getLexicalUnitType()
104+
105+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
106+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
107+
108+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
109+
return new CSSValueImpl(next, true);
110+
}
111+
112+
throw new DOMException(DOMException.SYNTAX_ERR, "Color hue part has to be numeric or an angle.");
113+
}
114+
115+
private static CSSValueImpl getAlphaPart(final LexicalUnit next) {
116+
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
117+
118+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
119+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
120+
121+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
122+
return new CSSValueImpl(next, true);
123+
}
124+
125+
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
126+
}
127+
104128
/**
105129
* @return the hue part.
106130
*/

src/main/java/org/htmlunit/cssparser/dom/LABColorImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8080
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
8181
}
8282

83-
alpha_ = getPart(next);
83+
alpha_ = getAlphaPart(next);
8484
next = next.getNextLexicalUnit();
8585
if (next != null) {
8686
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for lab function.");
@@ -89,15 +89,30 @@ public LABColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
8989

9090
private static CSSValueImpl getPart(final LexicalUnit next) {
9191
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
92+
9293
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
9394
|| LexicalUnitType.REAL == next.getLexicalUnitType()
95+
9496
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
9597
return new CSSValueImpl(next, true);
9698
}
9799

98100
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");
99101
}
100102

103+
private static CSSValueImpl getAlphaPart(final LexicalUnit next) {
104+
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
105+
106+
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
107+
|| LexicalUnitType.REAL == next.getLexicalUnitType()
108+
109+
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
110+
return new CSSValueImpl(next, true);
111+
}
112+
113+
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
114+
}
115+
101116
/**
102117
* @return the lightness part.
103118
*/

0 commit comments

Comments
 (0)