Skip to content

Commit e413af8

Browse files
Add basic CSS min/max + varying fixes
1 parent a62f8d9 commit e413af8

File tree

17 files changed

+347
-26
lines changed

17 files changed

+347
-26
lines changed

Spec/src/main/java/com/github/webicitybrowser/spec/css/parser/property/background/BackgroundPositionValueParser.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,13 @@ private PropertyValueParseResult<BackgroundPositionValue> parseTwoKeywordPositio
9090
PropertyValueParseResult<CSSValue> offsetResult1 = lengthPercentageValueParser.parse(tokens, offset + totalLength, length - totalLength);
9191
if (offsetResult1.getResult().isPresent()) totalLength += offsetResult1.getLength();
9292

93-
if (!(tokens[offset + totalLength] instanceof IdentToken identToken2)) return PropertyValueParseResultImp.empty();
93+
if (!(
94+
offset + totalLength < length
95+
&& tokens[offset + totalLength] instanceof IdentToken identToken2)
96+
) {
97+
return PropertyValueParseResultImp.empty();
98+
}
99+
94100
totalLength++;
95101
PropertyValueParseResult<CSSValue> offsetResult2 = lengthPercentageValueParser.parse(tokens, offset + totalLength, length - totalLength);
96102
if (offsetResult2.getResult().isPresent()) totalLength += offsetResult2.getLength();

Spec/src/main/java/com/github/webicitybrowser/spec/css/parser/property/background/BackgroundValueParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public PropertyValueParseResult<PositionSizeComposite> parse(TokenLike[] tokens,
7777
BackgroundPositionValue position = positionResult.getResult().get();
7878
int totalLength = positionResult.getLength();
7979

80-
if (!(tokens[offset + totalLength] instanceof DelimToken delimToken && delimToken.getValue() == '/')) {
80+
if (!(totalLength < length && tokens[offset + totalLength] instanceof DelimToken delimToken && delimToken.getValue() == '/')) {
8181
return PropertyValueParseResultImp.of(new PositionSizeComposite(position, null), totalLength);
8282
}
8383
totalLength++;

Spec/src/main/java/com/github/webicitybrowser/spec/css/parser/property/shared/length/LengthValueParser.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44
import com.github.webicitybrowser.spec.css.parser.property.PropertyValueParseResult;
55
import com.github.webicitybrowser.spec.css.parser.property.PropertyValueParser;
66
import com.github.webicitybrowser.spec.css.parser.property.imp.PropertyValueParseResultImp;
7+
import com.github.webicitybrowser.spec.css.parser.property.shared.math.MathValueParser;
78
import com.github.webicitybrowser.spec.css.parser.tokens.DimensionToken;
89
import com.github.webicitybrowser.spec.css.parser.tokens.NumberToken;
10+
import com.github.webicitybrowser.spec.css.property.CSSValue;
911
import com.github.webicitybrowser.spec.css.property.shared.length.AbsoluteLengthValue;
10-
import com.github.webicitybrowser.spec.css.property.shared.length.LengthValue;
1112
import com.github.webicitybrowser.spec.css.property.shared.length.RelativeLengthValue;
1213

13-
public class LengthValueParser implements PropertyValueParser<LengthValue> {
14+
public class LengthValueParser implements PropertyValueParser<CSSValue> {
15+
16+
private final MathValueParser mathValueParser = new MathValueParser(LengthValueParser::parseInternal);
1417

1518
@Override
16-
public PropertyValueParseResult<LengthValue> parse(TokenLike[] tokens, int offset, int length) {
19+
public PropertyValueParseResult<CSSValue> parse(TokenLike[] tokens, int offset, int length) {
20+
return mathValueParser.parse(tokens, offset, length);
21+
}
22+
23+
private static PropertyValueParseResult<CSSValue> parseInternal(TokenLike[] tokens, int offset, int length) {
1724
if (length > 0 && offset < tokens.length && tokens[offset] instanceof NumberToken numberToken && numberToken.getValue().floatValue() == 0) {
1825
return PropertyValueParseResultImp.of(AbsoluteLengthValue.of(0, "px"), 1);
1926
}
@@ -23,14 +30,14 @@ public PropertyValueParseResult<LengthValue> parse(TokenLike[] tokens, int offse
2330
}
2431

2532
DimensionToken token = (DimensionToken) tokens[offset];
26-
return creteParseResultFromDimensionToken(token);
33+
return createParseResultFromDimensionToken(token);
2734
}
2835

29-
private boolean checkSelectorFormat(TokenLike[] tokens, int offset, int length) {
36+
private static boolean checkSelectorFormat(TokenLike[] tokens, int offset, int length) {
3037
return length > 0 && offset < tokens.length && tokens[offset] instanceof DimensionToken;
3138
}
3239

33-
private PropertyValueParseResult<LengthValue> creteParseResultFromDimensionToken(DimensionToken token) {
40+
private static PropertyValueParseResult<CSSValue> createParseResultFromDimensionToken(DimensionToken token) {
3441
switch (token.getUnit()) {
3542
case "cm":
3643
case "mm":
@@ -62,7 +69,7 @@ private PropertyValueParseResult<LengthValue> creteParseResultFromDimensionToken
6269
}
6370
}
6471

65-
private RelativeLengthValue createRelativeLengthValue(Number value, String unit) {
72+
private static RelativeLengthValue createRelativeLengthValue(Number value, String unit) {
6673
return new RelativeLengthValue() {
6774

6875
@Override
@@ -74,6 +81,12 @@ public RelativeLengthUnit getUnit() {
7481
public float getValue() {
7582
return value.floatValue();
7683
}
84+
85+
@Override
86+
public String toString() {
87+
return value + unit;
88+
}
89+
7790
};
7891
}
7992

Spec/src/main/java/com/github/webicitybrowser/spec/css/parser/property/shared/lengthpercentage/LengthPercentageValueParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55
import com.github.webicitybrowser.spec.css.parser.property.PropertyValueParser;
66
import com.github.webicitybrowser.spec.css.parser.property.PropertyValueParserUtil;
77
import com.github.webicitybrowser.spec.css.parser.property.shared.length.LengthValueParser;
8+
import com.github.webicitybrowser.spec.css.parser.property.shared.math.MathValueParser;
89
import com.github.webicitybrowser.spec.css.parser.property.shared.percentage.PercentageValueParser;
910
import com.github.webicitybrowser.spec.css.property.CSSValue;
1011

1112
public class LengthPercentageValueParser implements PropertyValueParser<CSSValue> {
1213

13-
private final LengthValueParser lengthValueParser = new LengthValueParser();
14-
private final PercentageValueParser percentageValueParser = new PercentageValueParser();
14+
private static final LengthValueParser lengthValueParser = new LengthValueParser();
15+
private static final PercentageValueParser percentageValueParser = new PercentageValueParser();
16+
17+
private final MathValueParser mathValueParser = new MathValueParser(LengthPercentageValueParser::parseInternal);
1518

1619
@Override
1720
public PropertyValueParseResult<CSSValue> parse(TokenLike[] tokens, int offset, int length) {
21+
return mathValueParser.parse(tokens, offset, length);
22+
}
23+
24+
private static PropertyValueParseResult<CSSValue> parseInternal(TokenLike[] tokens, int offset, int length) {
1825
return PropertyValueParserUtil.takeLongestResult(
1926
lengthValueParser.parse(tokens, offset, length).as(CSSValue.class),
2027
percentageValueParser.parse(tokens, offset, length).as(CSSValue.class));
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.github.webicitybrowser.spec.css.parser.property.shared.math;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.github.webicitybrowser.spec.css.componentvalue.FunctionValue;
7+
import com.github.webicitybrowser.spec.css.parser.TokenLike;
8+
import com.github.webicitybrowser.spec.css.parser.property.PropertyValueParseResult;
9+
import com.github.webicitybrowser.spec.css.parser.property.PropertyValueParser;
10+
import com.github.webicitybrowser.spec.css.parser.property.imp.PropertyValueParseResultImp;
11+
import com.github.webicitybrowser.spec.css.parser.tokens.CommaToken;
12+
import com.github.webicitybrowser.spec.css.parser.tokens.WhitespaceToken;
13+
import com.github.webicitybrowser.spec.css.property.CSSValue;
14+
import com.github.webicitybrowser.spec.css.property.shared.math.MaxMathValue;
15+
import com.github.webicitybrowser.spec.css.property.shared.math.MinMathValue;
16+
17+
public class MathValueParser implements PropertyValueParser<CSSValue> {
18+
19+
private final PropertyValueParser<CSSValue> innerParser;
20+
21+
public MathValueParser(PropertyValueParser<CSSValue> innerParser) {
22+
this.innerParser = innerParser;
23+
}
24+
25+
@Override
26+
public PropertyValueParseResult<CSSValue> parse(TokenLike[] tokens, int offset, int length) {
27+
if (length >= 1 && tokens[offset] instanceof FunctionValue functionValue) {
28+
TokenLike[] functionTokens = stripWhitespace(functionValue.getValue());
29+
PropertyValueParseResult<CSSValue> result = switch (functionValue.getName()) {
30+
case "min" -> parseMinMax(functionTokens, 0, functionTokens.length, true);
31+
case "max" -> parseMinMax(functionTokens, 0, functionTokens.length, false);
32+
default -> PropertyValueParseResultImp.empty();
33+
};
34+
35+
if (
36+
result.getResult().isPresent()
37+
&& result.getLength() == functionTokens.length
38+
) {
39+
return PropertyValueParseResultImp.of(result.getResult().get(), 1);
40+
}
41+
}
42+
43+
return innerParser.parse(tokens, offset, length);
44+
}
45+
46+
private PropertyValueParseResult<CSSValue> parseMinMax(TokenLike[] tokens, int offset, int length, boolean isMin) {
47+
List<CSSValue> values = new ArrayList<>();
48+
49+
int priorTokens = 0;
50+
do {
51+
if (priorTokens != 0) {
52+
priorTokens++;
53+
}
54+
// TODO: Allow calc value
55+
PropertyValueParseResult<CSSValue> result = innerParser.parse(tokens, offset + priorTokens, length);
56+
if (result.getResult().isEmpty()) {
57+
return PropertyValueParseResultImp.empty();
58+
}
59+
values.add(result.getResult().get());
60+
61+
priorTokens += result.getLength();
62+
} while (offset + priorTokens < length && tokens[offset + priorTokens] instanceof CommaToken);
63+
64+
CSSValue result = isMin ? new MinMathValue(values) : new MaxMathValue(values);
65+
return PropertyValueParseResultImp.of(result, priorTokens);
66+
}
67+
68+
private TokenLike[] stripWhitespace(TokenLike[] tokens) {
69+
List<TokenLike> strippedTokens = new ArrayList<>();
70+
for (TokenLike token : tokens) {
71+
if (token instanceof WhitespaceToken) continue;
72+
strippedTokens.add(token);
73+
}
74+
return strippedTokens.toArray(TokenLike[]::new);
75+
}
76+
77+
}

Spec/src/main/java/com/github/webicitybrowser/spec/css/parser/property/text/LetterSpacingValueParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
import com.github.webicitybrowser.spec.css.parser.property.imp.PropertyValueParseResultImp;
77
import com.github.webicitybrowser.spec.css.parser.property.shared.length.LengthValueParser;
88
import com.github.webicitybrowser.spec.css.parser.tokens.IdentToken;
9+
import com.github.webicitybrowser.spec.css.property.CSSValue;
910
import com.github.webicitybrowser.spec.css.property.shared.length.AbsoluteLengthValue;
10-
import com.github.webicitybrowser.spec.css.property.shared.length.LengthValue;
1111

12-
public class LetterSpacingValueParser implements PropertyValueParser<LengthValue> {
12+
public class LetterSpacingValueParser implements PropertyValueParser<CSSValue> {
1313

1414
private final LengthValueParser lengthValueParser = new LengthValueParser();
1515

1616
@Override
17-
public PropertyValueParseResult<LengthValue> parse(TokenLike[] tokens, int offset, int length) {
18-
if (length > 0 && tokens[offset] instanceof IdentToken && ((IdentToken) tokens[offset]).equals("normal")) {
17+
public PropertyValueParseResult<CSSValue> parse(TokenLike[] tokens, int offset, int length) {
18+
if (length > 0 && tokens[offset] instanceof IdentToken && ((IdentToken) tokens[offset]).getValue().equals("normal")) {
1919
return PropertyValueParseResultImp.of(AbsoluteLengthValue.of(0, "px"), 1);
2020
}
2121

Spec/src/main/java/com/github/webicitybrowser/spec/css/property/shared/length/AbsoluteLengthValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public AbsoluteLengthUnit getUnit() {
2121
public float getValue() {
2222
return value.floatValue();
2323
}
24+
25+
@Override
26+
public String toString() {
27+
return value + unit.name().toLowerCase();
28+
}
2429
};
2530
}
2631

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.webicitybrowser.spec.css.property.shared.math;
2+
3+
import com.github.webicitybrowser.spec.css.property.CSSValue;
4+
5+
public interface MathValue extends CSSValue {
6+
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.webicitybrowser.spec.css.property.shared.math;
2+
3+
import java.util.List;
4+
5+
import com.github.webicitybrowser.spec.css.property.CSSValue;
6+
7+
public record MaxMathValue(List<CSSValue> values) implements MathValue {
8+
9+
@Override
10+
public String toString() {
11+
StringBuilder valueStr = new StringBuilder();
12+
for (int i = 0; i < values.size(); i++) {
13+
if (i != 0) {
14+
valueStr.append(", ");
15+
}
16+
valueStr.append(values.get(i));
17+
}
18+
return "max(" + valueStr + ")";
19+
}
20+
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.webicitybrowser.spec.css.property.shared.math;
2+
3+
import java.util.List;
4+
5+
import com.github.webicitybrowser.spec.css.property.CSSValue;
6+
7+
public record MinMathValue(List<CSSValue> values) implements MathValue {
8+
9+
@Override
10+
public String toString() {
11+
return "min(" + values + ")";
12+
}
13+
14+
}

0 commit comments

Comments
 (0)