Skip to content

Commit

Permalink
For #38 - Implements validity checking for CSS transform property. [c…
Browse files Browse the repository at this point in the history
…i skip]
  • Loading branch information
danfickle committed Nov 13, 2016
1 parent 235ea1a commit 2d938df
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1488,8 +1488,18 @@ private PropertyValue term(boolean literal) throws IOException {
skip_whitespace();
break;
case Token.ANGLE:
String unit = extractUnit(t);
short type = CSSPrimitiveValue.CSS_UNKNOWN;

if ("deg".equals(unit))
type = CSSPrimitiveValue.CSS_DEG;
else if ("rad".equals(unit))
type = CSSPrimitiveValue.CSS_RAD;
else if ("grad".equals(unit))
type = CSSPrimitiveValue.CSS_GRAD;

result = new PropertyValue(
CSSPrimitiveValue.CSS_DEG,
type,
sign*Float.parseFloat(extractNumber(t)),
sign(sign) + getTokenValue(t));
next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ protected void checkNumberType(CSSName cssName, CSSPrimitiveValue value) {
}
}

protected void checkAngleType(CSSName cssName, CSSPrimitiveValue value) {
if (value.getPrimitiveType() != CSSPrimitiveValue.CSS_DEG &&
value.getPrimitiveType() != CSSPrimitiveValue.CSS_RAD &&
value.getPrimitiveType() != CSSPrimitiveValue.CSS_GRAD) {
throw new CSSParseException("Value for " + cssName + "must be an angle (degrees, radians or grads)", -1);
}
}

protected void checkStringType(CSSName cssName, CSSPrimitiveValue value) {
if (value.getPrimitiveType() != CSSPrimitiveValue.CSS_STRING) {
throw new CSSParseException("Value for " + cssName + " must be a string", -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.w3c.dom.css.CSSPrimitiveValue;

import com.openhtmltopdf.css.constants.CSSName;
Expand Down Expand Up @@ -1625,6 +1624,12 @@ public static class TransformPropertyBuilder extends AbstractPropertyBuilder {
public List buildDeclarations(CSSName cssName, List values, int origin, boolean important,
boolean inheritAllowed) {
checkValueCount(cssName, 1, Integer.MAX_VALUE, values.size());
checkInheritAllowed((CSSPrimitiveValue) values.get(0), inheritAllowed);

if (((PropertyValue) values.get(0)).getCssValueType() == CSSPrimitiveValue.CSS_INHERIT) {
return Collections.singletonList(new PropertyDeclaration(cssName, (CSSPrimitiveValue) values.get(0), important, origin));
}

if(values.size() == 1) {
CSSPrimitiveValue value = (CSSPrimitiveValue) values.get(0);
if(value.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
Expand All @@ -1634,6 +1639,64 @@ public List buildDeclarations(CSSName cssName, List values, int origin, boolean
important, origin));
}
}

for (Object v : values) {
PropertyValue value = (PropertyValue) v;

if (value.getPropertyValueType() != PropertyValue.VALUE_TYPE_FUNCTION) {
throw new CSSParseException("One or more functions must be provided for transform property", -1);
}

String fName = value.getFunction().getName();

int expected = 0;
if (fName.equalsIgnoreCase("matrix")) {
expected = 6;
for (Object p : value.getFunction().getParameters()) {
checkNumberType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("translate")) {
expected = 2;
for (Object p : value.getFunction().getParameters()) {
checkLengthOrPercentType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("translateX")) {
expected = 1;
for (Object p : value.getFunction().getParameters()) {
checkLengthOrPercentType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("translateY")) {
expected = 1;
for (Object p : value.getFunction().getParameters()) {
checkLengthOrPercentType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("scale")) {
expected = 2;
for (Object p : value.getFunction().getParameters()) {
checkNumberType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("scaleX")) {
expected = 1;
for (Object p : value.getFunction().getParameters()) {
checkNumberType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("scaleY")) {
expected = 1;
for (Object p : value.getFunction().getParameters()) {
checkNumberType(cssName, (CSSPrimitiveValue) p);
}
} else if (fName.equalsIgnoreCase("rotate")) {
expected = 1;
for (Object p : value.getFunction().getParameters()) {
checkAngleType(cssName, (CSSPrimitiveValue) p);
}
} else {
throw new CSSParseException("Unsupported function provided in transform property: " + fName, -1);
}

checkValueCount(cssName, expected, value.getFunction().getParameters().size());
}

return Collections.singletonList(new PropertyDeclaration(CSSName.TRANSFORM, new PropertyValue(values),
important, origin));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public void setStackingContext(boolean stackingContext) {
}

public int getZIndex() {
if (_master.getStyle().isIdent(CSSName.Z_INDEX, IdentValue.AUTO)) {
return 0;
}
return (int) _master.getStyle().asFloat(CSSName.Z_INDEX);
}

Expand Down

0 comments on commit 2d938df

Please sign in to comment.