Class CssUnitParser
java.lang.Object
dev.jcputney.mjml.util.CssUnitParser
Parses CSS unit values (px, %, em) and converts them to pixel values.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PatternPre-compiled whitespace pattern for splitting CSS shorthand values. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatInt(double value) Formats a pixel value as an integer string without any suffix.static StringformatPx(double value) Formats a pixel value as an integer string with "px" suffix.static StringformatPxWidth(double width) Formats a pixel width: integers without decimals, decimals as-is.static intparseIntPx(String value) Parses an integer from a CSS pixel value like "10px", "10", or "10.5px".static intparsePixels(String value, int defaultValue) Parses an integer pixel value from a string like "600px" or "600".static doubleParses a pixel value from a string like "10px" or "10".static double[]parseShorthand(String value) Parses a 1-4 value CSS shorthand into [top, right, bottom, left] pixel values.static doubleParses a CSS value and converts it to pixels.
-
Field Details
-
WHITESPACE
Pre-compiled whitespace pattern for splitting CSS shorthand values.
-
-
Method Details
-
toPixels
Parses a CSS value and converts it to pixels. Supports px (direct), % (relative to containerWidth), and unitless (treated as px).- Parameters:
value- the CSS value string to parse (e.g., "100px", "50%", "200")containerWidth- the container width in pixels, used for percentage calculations- Returns:
- the computed pixel value, or 0 if the value is null or empty
-
parsePx
Parses a pixel value from a string like "10px" or "10". Returns the provided default if parsing fails.- Parameters:
value- the CSS pixel value string to parsedefaultValue- the default value to return if parsing fails- Returns:
- the parsed pixel value, or the default value if parsing fails
-
parseShorthand
Parses a 1-4 value CSS shorthand into [top, right, bottom, left] pixel values.- Parameters:
value- the CSS shorthand string (e.g., "10px", "10px 20px", "10px 20px 30px 40px")- Returns:
- a four-element array of pixel values in [top, right, bottom, left] order
-
parseIntPx
Parses an integer from a CSS pixel value like "10px", "10", or "10.5px". Validates the input against a CSS number pattern first, rejecting garbage like "abc123xyz" or "12px34". Returns 0 on failure.- Parameters:
value- the CSS value string to parse- Returns:
- the parsed integer pixel value, or 0 if the value is null, empty, or invalid
-
formatPx
Formats a pixel value as an integer string with "px" suffix.- Parameters:
value- the pixel value to format- Returns:
- the formatted string with "px" suffix (e.g., "10px")
-
formatInt
Formats a pixel value as an integer string without any suffix.- Parameters:
value- the pixel value to format- Returns:
- the formatted integer string (e.g., "10")
-
formatPxWidth
Formats a pixel width: integers without decimals, decimals as-is.- Parameters:
width- the pixel width to format- Returns:
- the formatted width string, without decimals for whole numbers
-
parsePixels
Parses an integer pixel value from a string like "600px" or "600". Returns the default value if parsing fails.- Parameters:
value- the CSS pixel value string to parsedefaultValue- the default value to return if parsing fails- Returns:
- the parsed integer pixel value, or the default value if parsing fails
-