Skip to content

Commit

Permalink
fix aspect-ratio allowing also only one number, fix #381 (and validat…
Browse files Browse the repository at this point in the history
  • Loading branch information
ylafon committed Apr 9, 2024
1 parent fef61cf commit ad4b711
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion org/w3c/css/properties/css3/CssAspectRatio.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ public CssAspectRatio(ApplContext ac, CssExpression expression, boolean check)
expression.next();
}
// if things are not entirely parsed
if (v.size() == 0 || (ratio_state != 0 && ratio_state != 3)) {
if (ratio_state == 1) {
// so we got only one number
v.add(new CssRatio(dividend));
} else if (v.isEmpty() || (ratio_state != 0 && ratio_state != 3)) {
throw new InvalidParamException("value",
expression.toStringFromStart(),
getPropertyName(), ac);
Expand Down
18 changes: 18 additions & 0 deletions org/w3c/css/values/CssRatio.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public CssRatio(CssValue gw, BigDecimal h) {
this.h = h;
}

public CssRatio(CssValue gw) {
if (gw.getRawType() == CssTypes.CSS_NUMBER) {
try {
this.w = gw.getNumber().getBigDecimalValue();
} catch (Exception ex) {
this.gw = gw;
}
} else {
this.gw = gw;
}
}

public CssRatio(CssValue gw, CssValue gh) {
if (gw.getRawType() == CssTypes.CSS_NUMBER) {
try {
Expand Down Expand Up @@ -145,6 +157,9 @@ public String toString() {
} else {
sb.append(gw.toString()).append(' ');
}
if (h == null && gh == null) {
return sb.toString();
}
sb.append('/');
if (h != null) {
sb.append(h.toPlainString());
Expand All @@ -163,6 +178,9 @@ public boolean equals(Object value) {
try {
CssRatio other = (CssRatio) value;
// check that the ratio are the same
if (h == null && gh == null) {
return (other.h == null) && (other.gh == null) && (w.compareTo(other.w) == 0);
}
BigDecimal ratio, other_ratio;
ratio = w.divide(h, RoundingMode.CEILING);
other_ratio = other.w.divide(other.h, RoundingMode.CEILING);
Expand Down

0 comments on commit ad4b711

Please sign in to comment.