Skip to content

Commit 630e306

Browse files
committed
accept more color values, refactor tests (wip)
1 parent 1fbbcc0 commit 630e306

19 files changed

+814
-2123
lines changed

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

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
package org.htmlunit.cssparser.dom;
1616

1717
import java.io.Serializable;
18-
import java.util.function.Consumer;
18+
import java.util.Locale;
1919

2020
import org.htmlunit.cssparser.parser.LexicalUnit;
21-
import org.htmlunit.cssparser.parser.LexicalUnit.LexicalUnitType;
2221
import org.w3c.dom.DOMException;
2322

2423
/**
@@ -28,64 +27,34 @@
2827
*/
2928
public class AbstractColor implements Serializable {
3029

31-
private CSSValueImpl alpha_;
32-
private CSSValueImpl relative_;
33-
34-
protected void getNumberPercentagePart(final LexicalUnit next, final Consumer<CSSValueImpl> setter) {
35-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
36-
37-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
38-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
39-
40-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
41-
setter.accept(new CSSValueImpl(next, true));
42-
return;
43-
}
44-
45-
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");
46-
}
47-
48-
protected void getAlphaPart(final LexicalUnit lu) {
49-
if (LexicalUnitType.PERCENTAGE == lu.getLexicalUnitType()
50-
51-
|| LexicalUnitType.INTEGER == lu.getLexicalUnitType()
52-
|| LexicalUnitType.REAL == lu.getLexicalUnitType()
53-
54-
|| LexicalUnitType.NONE == lu.getLexicalUnitType()) {
55-
setAlpha(new CSSValueImpl(lu, true));
56-
return;
57-
}
58-
59-
throw new DOMException(DOMException.SYNTAX_ERR, "Color alpha part has to be numeric or percentage.");
60-
}
30+
private String function_;
31+
private CSSValueImpl cssValue_;
6132

6233
/**
63-
* @return the alpha part.
34+
* Constructor that reads the values from the given
35+
* chain of LexicalUnits.
36+
* @param function the name of the function; rgb or rgba
37+
* @param lu the values
38+
* @throws DOMException in case of error
6439
*/
65-
public CSSValueImpl getAlpha() {
66-
return alpha_;
40+
public AbstractColor(final String function, final LexicalUnit lu) throws DOMException {
41+
function_ = function.toLowerCase(Locale.ROOT);
42+
cssValue_ = new CSSValueImpl(lu);
6743
}
6844

6945
/**
70-
* Sets the alpha part to a new value.
71-
* @param alpha the new CSSValueImpl
46+
* {@inheritDoc}
7247
*/
73-
public void setAlpha(final CSSValueImpl alpha) {
74-
alpha_ = alpha;
75-
}
48+
@Override
49+
public String toString() {
50+
final StringBuilder sb = new StringBuilder();
7651

77-
/**
78-
* @return the relative value or null.
79-
*/
80-
public CSSValueImpl getRelative() {
81-
return relative_;
82-
}
52+
sb
53+
.append(function_)
54+
.append("(")
55+
.append(cssValue_)
56+
.append(")");
8357

84-
public boolean handleRelativeColors(final LexicalUnit lu) {
85-
if (LexicalUnitType.FROM == lu.getLexicalUnitType()) {
86-
relative_ = new CSSValueImpl(lu.getNextLexicalUnit());
87-
return true;
88-
}
89-
return false;
58+
return sb.toString();
9059
}
9160
}

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

Lines changed: 1 addition & 258 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
*/
1515
package org.htmlunit.cssparser.dom;
1616

17-
import java.util.Locale;
18-
1917
import org.htmlunit.cssparser.parser.LexicalUnit;
20-
import org.htmlunit.cssparser.parser.LexicalUnit.LexicalUnitType;
2118
import org.w3c.dom.DOMException;
2219

2320
/**
@@ -26,12 +23,6 @@
2623
* @author Ronald Brill
2724
*/
2825
public class HSLColorImpl extends AbstractColor {
29-
private final String function_;
30-
31-
private CSSValueImpl hue_;
32-
private CSSValueImpl saturation_;
33-
private CSSValueImpl lightness_;
34-
private final boolean commaSeparated_;
3526

3627
/**
3728
* Constructor that reads the values from the given
@@ -41,254 +32,6 @@ public class HSLColorImpl extends AbstractColor {
4132
* @throws DOMException in case of error
4233
*/
4334
public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMException {
44-
if (function == null) {
45-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space 'hsl' or 'hsla' is required.");
46-
}
47-
final String functionLC = function.toLowerCase(Locale.ROOT);
48-
if (!"hsl".equals(functionLC) && !"hsla".equals(functionLC)) {
49-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space '" + functionLC + "' not supported.");
50-
}
51-
function_ = functionLC;
52-
53-
if (lu == null) {
54-
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
55-
}
56-
57-
if (handleRelativeColors(lu)) {
58-
commaSeparated_ = false;
59-
return;
60-
}
61-
62-
LexicalUnit next = lu;
63-
hue_ = getHuePart(next);
64-
65-
next = next.getNextLexicalUnit();
66-
if (next == null) {
67-
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
68-
}
69-
70-
commaSeparated_ = LexicalUnitType.OPERATOR_COMMA == next.getLexicalUnitType();
71-
if (commaSeparated_) {
72-
if (hue_.getLexicalUnitType() == LexicalUnitType.NONE) {
73-
throw new DOMException(DOMException.SYNTAX_ERR,
74-
"'" + function_ + "' has to use blank as separator if none is used.");
75-
}
76-
77-
next = next.getNextLexicalUnit();
78-
if (next == null) {
79-
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
80-
}
81-
82-
if (LexicalUnitType.NONE == next.getLexicalUnitType()) {
83-
throw new DOMException(DOMException.SYNTAX_ERR,
84-
"'" + function_ + "' has to use blank as separator if none is used.");
85-
}
86-
if (LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
87-
throw new DOMException(DOMException.SYNTAX_ERR, "Saturation part has to be percentage.");
88-
}
89-
saturation_ = new CSSValueImpl(next, true);
90-
91-
next = next.getNextLexicalUnit();
92-
if (next == null) {
93-
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
94-
}
95-
96-
if (LexicalUnitType.OPERATOR_COMMA != next.getLexicalUnitType()) {
97-
throw new DOMException(DOMException.SYNTAX_ERR,
98-
"'" + function_ + "' parameters must be separated by ','.");
99-
}
100-
101-
next = next.getNextLexicalUnit();
102-
if (next == null) {
103-
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
104-
}
105-
106-
if (LexicalUnitType.NONE == next.getLexicalUnitType()) {
107-
throw new DOMException(DOMException.SYNTAX_ERR,
108-
"'" + function_ + "' has to use blank as separator if none is used.");
109-
}
110-
if (LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
111-
throw new DOMException(DOMException.SYNTAX_ERR, "Lightness part has to be percentage.");
112-
}
113-
lightness_ = new CSSValueImpl(next, true);
114-
115-
next = next.getNextLexicalUnit();
116-
if (next == null) {
117-
return;
118-
}
119-
120-
if (LexicalUnitType.OPERATOR_COMMA != next.getLexicalUnitType()) {
121-
throw new DOMException(DOMException.SYNTAX_ERR,
122-
"'" + function_ + "' parameters must be separated by ','.");
123-
}
124-
125-
next = next.getNextLexicalUnit();
126-
if (next == null) {
127-
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
128-
}
129-
130-
if (LexicalUnitType.NONE == next.getLexicalUnitType()) {
131-
throw new DOMException(DOMException.SYNTAX_ERR,
132-
"'" + function_ + "' has to use blank as separator if none is used.");
133-
}
134-
135-
getAlphaPart(next);
136-
137-
next = next.getNextLexicalUnit();
138-
if (next != null) {
139-
throw new DOMException(DOMException.SYNTAX_ERR,
140-
"Too many parameters for '" + function_ + "' function.");
141-
}
142-
return;
143-
}
144-
145-
if (LexicalUnitType.NONE != next.getLexicalUnitType()
146-
&& LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
147-
throw new DOMException(DOMException.SYNTAX_ERR, "Saturation part has to be percentage.");
148-
}
149-
saturation_ = new CSSValueImpl(next, true);
150-
151-
next = next.getNextLexicalUnit();
152-
if (next == null) {
153-
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
154-
}
155-
if (next.getLexicalUnitType() == LexicalUnitType.OPERATOR_COMMA) {
156-
throw new DOMException(DOMException.SYNTAX_ERR,
157-
"'" + function_ + "' requires consitent separators (blank or comma).");
158-
}
159-
160-
if (LexicalUnitType.NONE != next.getLexicalUnitType()
161-
&& LexicalUnitType.PERCENTAGE != next.getLexicalUnitType()) {
162-
throw new DOMException(DOMException.SYNTAX_ERR, "Lightness part has to be percentage.");
163-
}
164-
lightness_ = new CSSValueImpl(next, true);
165-
next = next.getNextLexicalUnit();
166-
if (next == null) {
167-
return;
168-
}
169-
170-
if (next.getLexicalUnitType() != LexicalUnitType.OPERATOR_SLASH) {
171-
throw new DOMException(DOMException.SYNTAX_ERR,
172-
"'" + function_ + "' alpha value must be separated by '/'.");
173-
}
174-
next = next.getNextLexicalUnit();
175-
if (next == null) {
176-
throw new DOMException(DOMException.SYNTAX_ERR, "Missing alpha value.");
177-
}
178-
179-
getAlphaPart(next);
180-
181-
next = next.getNextLexicalUnit();
182-
if (next != null) {
183-
throw new DOMException(DOMException.SYNTAX_ERR, "Too many parameters for '" + function_ + "' function.");
184-
}
185-
}
186-
187-
private static CSSValueImpl getHuePart(final LexicalUnit next) {
188-
if (LexicalUnitType.DEGREE == next.getLexicalUnitType()
189-
|| LexicalUnitType.RADIAN == next.getLexicalUnitType()
190-
|| LexicalUnitType.GRADIAN == next.getLexicalUnitType()
191-
|| LexicalUnitType.TURN == next.getLexicalUnitType()
192-
193-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
194-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
195-
196-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
197-
return new CSSValueImpl(next, true);
198-
}
199-
200-
throw new DOMException(DOMException.SYNTAX_ERR, "Color hue part has to be numeric or an angle.");
201-
}
202-
203-
/**
204-
* @return the hue part.
205-
*/
206-
public CSSValueImpl getHue() {
207-
return hue_;
208-
}
209-
210-
/**
211-
* Sets the hue part to a new value.
212-
* @param hue the new CSSValueImpl
213-
*/
214-
public void setHue(final CSSValueImpl hue) {
215-
hue_ = hue;
216-
}
217-
218-
/**
219-
* @return the saturation part.
220-
*/
221-
public CSSValueImpl getSaturation() {
222-
return saturation_;
223-
}
224-
225-
/**
226-
* Sets the saturation part to a new value.
227-
* @param saturation the new CSSValueImpl
228-
*/
229-
public void setSaturation(final CSSValueImpl saturation) {
230-
saturation_ = saturation;
231-
}
232-
233-
/**
234-
* @return the lightness part.
235-
*/
236-
public CSSValueImpl getLightness() {
237-
return lightness_;
238-
}
239-
240-
/**
241-
* Sets the lightness part to a new value.
242-
* @param lightness the new CSSValueImpl
243-
*/
244-
public void setLightness(final CSSValueImpl lightness) {
245-
lightness_ = lightness;
246-
}
247-
248-
/**
249-
* {@inheritDoc}
250-
*/
251-
@Override
252-
public String toString() {
253-
final StringBuilder sb = new StringBuilder();
254-
255-
sb
256-
.append(function_)
257-
.append("(");
258-
259-
final CSSValueImpl rel = getRelative();
260-
if (rel != null) {
261-
sb
262-
.append("from ")
263-
.append(rel);
264-
}
265-
else {
266-
if (commaSeparated_) {
267-
sb
268-
.append(hue_)
269-
.append(", ")
270-
.append(saturation_)
271-
.append(", ")
272-
.append(lightness_);
273-
if (null != getAlpha()) {
274-
sb.append(", ").append(getAlpha());
275-
}
276-
}
277-
else {
278-
sb
279-
.append(hue_)
280-
.append(" ")
281-
.append(saturation_)
282-
.append(" ")
283-
.append(lightness_);
284-
if (null != getAlpha()) {
285-
sb.append(" / ").append(getAlpha());
286-
}
287-
}
288-
}
289-
290-
sb.append(")");
291-
292-
return sb.toString();
35+
super(function, lu);
29336
}
29437
}

0 commit comments

Comments
 (0)