Class StyleAttribute

java.lang.Object
dev.jcputney.mjml.css.StyleAttribute

public final class StyleAttribute extends Object
Parses, merges, and serializes CSS inline style attributes. Handles specificity-aware merging where higher specificity or later source order wins, and !important declarations take precedence.
  • Method Details

    • parse

      public static List<CssDeclaration> parse(String style)
      Parses an inline style string into a list of declarations. Example: "color: red; font-size: 14px" -> [CssDeclaration("color","red",false), ...]
      Parameters:
      style - the inline CSS style string to parse, or null
      Returns:
      a list of parsed CssDeclaration instances (empty if input is null or blank)
    • serialize

      public static String serialize(List<CssDeclaration> declarations)
      Serializes a list of declarations back to an inline style string.
      Parameters:
      declarations - the list of CSS declarations to serialize
      Returns:
      the serialized inline style string, or an empty string if declarations is null or empty
    • merge

      public static List<CssDeclaration> merge(List<CssDeclaration> existing, List<CssDeclaration> incoming, CssSpecificity incomingSpec)
      Merges new declarations into existing inline style declarations. Rules:
      1. If existing has !important and new doesn't, existing wins
      2. If new has !important, new wins
      3. If neither or both have !important, new wins (later source order)
      Parameters:
      existing - the current inline style declarations
      incoming - the new declarations to merge
      incomingSpec - specificity of the incoming rule (unused if inline=true)
      Returns:
      merged declarations list