Class CssUnitParser

java.lang.Object
dev.jcputney.mjml.util.CssUnitParser

public final class CssUnitParser extends Object
Parses CSS unit values (px, %, em) and converts them to pixel values.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Pattern
    Pre-compiled whitespace pattern for splitting CSS shorthand values.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    formatInt(double value)
    Formats a pixel value as an integer string without any suffix.
    static String
    formatPx(double value)
    Formats a pixel value as an integer string with "px" suffix.
    static String
    formatPxWidth(double width)
    Formats a pixel width: integers without decimals, decimals as-is.
    static int
    Parses an integer from a CSS pixel value like "10px", "10", or "10.5px".
    static int
    parsePixels(String value, int defaultValue)
    Parses an integer pixel value from a string like "600px" or "600".
    static double
    parsePx(String value, double defaultValue)
    Parses a pixel value from a string like "10px" or "10".
    static double[]
    Parses a 1-4 value CSS shorthand into [top, right, bottom, left] pixel values.
    static double
    toPixels(String value, double containerWidth)
    Parses a CSS value and converts it to pixels.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • WHITESPACE

      public static final Pattern WHITESPACE
      Pre-compiled whitespace pattern for splitting CSS shorthand values.
  • Method Details

    • toPixels

      public static double toPixels(String value, double containerWidth)
      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

      public static double parsePx(String value, double defaultValue)
      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 parse
      defaultValue - the default value to return if parsing fails
      Returns:
      the parsed pixel value, or the default value if parsing fails
    • parseShorthand

      public static double[] parseShorthand(String value)
      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

      public static int parseIntPx(String value)
      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

      public static String formatPx(double value)
      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

      public static String formatInt(double value)
      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

      public static String formatPxWidth(double width)
      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

      public static int parsePixels(String value, int defaultValue)
      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 parse
      defaultValue - the default value to return if parsing fails
      Returns:
      the parsed integer pixel value, or the default value if parsing fails