Skip to content

Commit 1fbbcc0

Browse files
committed
relative colors (wip)
1 parent fc1a866 commit 1fbbcc0

File tree

8 files changed

+3168
-3003
lines changed

8 files changed

+3168
-3003
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
public class AbstractColor implements Serializable {
3030

3131
private CSSValueImpl alpha_;
32+
private CSSValueImpl relative_;
3233

3334
protected void getNumberPercentagePart(final LexicalUnit next, final Consumer<CSSValueImpl> setter) {
3435
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
@@ -44,14 +45,14 @@ protected void getNumberPercentagePart(final LexicalUnit next, final Consumer<CS
4445
throw new DOMException(DOMException.SYNTAX_ERR, "Color part has to be numeric or percentage.");
4546
}
4647

47-
protected void getAlphaPart(final LexicalUnit next) {
48-
if (LexicalUnitType.PERCENTAGE == next.getLexicalUnitType()
48+
protected void getAlphaPart(final LexicalUnit lu) {
49+
if (LexicalUnitType.PERCENTAGE == lu.getLexicalUnitType()
4950

50-
|| LexicalUnitType.INTEGER == next.getLexicalUnitType()
51-
|| LexicalUnitType.REAL == next.getLexicalUnitType()
51+
|| LexicalUnitType.INTEGER == lu.getLexicalUnitType()
52+
|| LexicalUnitType.REAL == lu.getLexicalUnitType()
5253

53-
|| LexicalUnitType.NONE == next.getLexicalUnitType()) {
54-
setAlpha(new CSSValueImpl(next, true));
54+
|| LexicalUnitType.NONE == lu.getLexicalUnitType()) {
55+
setAlpha(new CSSValueImpl(lu, true));
5556
return;
5657
}
5758

@@ -72,4 +73,19 @@ public CSSValueImpl getAlpha() {
7273
public void setAlpha(final CSSValueImpl alpha) {
7374
alpha_ = alpha;
7475
}
76+
77+
/**
78+
* @return the relative value or null.
79+
*/
80+
public CSSValueImpl getRelative() {
81+
return relative_;
82+
}
83+
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;
90+
}
7591
}

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMExcep
5050
}
5151
function_ = functionLC;
5252

53-
LexicalUnit next = lu;
54-
if (next == null) {
53+
if (lu == null) {
5554
throw new DOMException(DOMException.SYNTAX_ERR, "'" + function_ + "' requires at least three values.");
5655
}
56+
57+
if (handleRelativeColors(lu)) {
58+
commaSeparated_ = false;
59+
return;
60+
}
61+
62+
LexicalUnit next = lu;
5763
hue_ = getHuePart(next);
5864

5965
next = next.getNextLexicalUnit();
@@ -248,26 +254,36 @@ public String toString() {
248254

249255
sb
250256
.append(function_)
251-
.append("(")
252-
.append(hue_);
253-
if (commaSeparated_) {
257+
.append("(");
258+
259+
final CSSValueImpl rel = getRelative();
260+
if (rel != null) {
254261
sb
255-
.append(", ")
256-
.append(saturation_)
257-
.append(", ")
258-
.append(lightness_);
259-
if (null != getAlpha()) {
260-
sb.append(", ").append(getAlpha());
261-
}
262+
.append("from ")
263+
.append(rel);
262264
}
263265
else {
264-
sb
265-
.append(" ")
266-
.append(saturation_)
267-
.append(" ")
268-
.append(lightness_);
269-
if (null != getAlpha()) {
270-
sb.append(" / ").append(getAlpha());
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+
}
271287
}
272288
}
273289

src/main/java/org/htmlunit/cssparser/parser/LexicalUnit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ enum LexicalUnitType {
106106
LCHCOLOR,
107107
/** NONE. */
108108
NONE,
109+
/** FROM. */
110+
FROM,
109111

110112
/** DEGREE. */
111113
DEGREE,

src/main/java/org/htmlunit/cssparser/parser/LexicalUnitImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ public String getCssText() {
427427
case NONE:
428428
sb.append("none");
429429
break;
430+
case FROM:
431+
sb.append("from");
432+
break;
430433
case IDENT:
431434
sb.append(getStringValue());
432435
break;
@@ -1148,6 +1151,14 @@ public static LexicalUnit createNone(final LexicalUnit prev) {
11481151
return new LexicalUnitImpl(prev, LexicalUnitType.NONE);
11491152
}
11501153

1154+
/**
1155+
* @param prev the previous LexicalUnit
1156+
* @return lexical unit with type rgb color
1157+
*/
1158+
public static LexicalUnit createFrom(final LexicalUnit prev) {
1159+
return new LexicalUnitImpl(prev, LexicalUnitType.FROM);
1160+
}
1161+
11511162
/**
11521163
* @param prev the previous LexicalUnit
11531164
* @param params the params

0 commit comments

Comments
 (0)