Class HtmlBuilder
java.lang.Object
dev.jcputney.mjml.util.HtmlBuilder
Fluent HTML builder that manages indentation automatically. Designed for use in MJML component
render() methods to replace manual StringBuilder with hardcoded indentation strings.
Usage:
HtmlBuilder html = new HtmlBuilder();
html.open("table", attrs("border", "0", "role", "presentation"));
html.open("tbody");
html.open("tr");
html.open("td", attrs("style", buildStyle(styles)));
html.text("content");
html.close("td");
html.close("tr");
html.close("tbody");
html.close("table");
return html.toString();
-
Field Summary
FieldsModifier and TypeFieldDescriptionBase attributes shared by presentation-only tables in MJML email rendering:border="0", cellpadding="0", cellspacing="0", role="presentation". -
Constructor Summary
ConstructorsConstructorDescriptionCreates a builder at indent depth 0.HtmlBuilder(int startingIndent) Creates a builder with the given starting indent depth in spaces. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringConditionally adds a single attribute if the value is non-null and non-empty.static StringBuilds an attribute string from key/value pairs.static StringBuilds a sorted attribute string from a map.Closes a tag: decreases indent, writes{indent}</tag>\n.voidcloseInline(String tag) Appends a closing tag for the specified inline element to the internal StringBuilder.voidcloseInlineLn(String tag) Appends a closing tag with a newline character to the StringBuilder.voidindent()Increases the current indentation depth by a predefined size.Appends indented text followed by a newline.Wraps the output of a builder block in MSO conditional comments.Wraps a string in MSO conditional comments followed by a newline.newline()Appends a newline character to the internal string builder and returns the current instance of theHtmlBuilder.voidWraps the output of a builder block in non-MSO conditional comments (<!--[if !mso]><!-->...<!--<![endif]-->).voidWraps the output of a builder block in non-MSO/IE conditional comments (<!--[if !mso | IE]><!-->...<!--<![endif]-->).Opens a tag with no attributes.Opens a tag with attributes, writes a newline, and increases indent.openInline(String tag) Opens an inline tag with no attributes.openInline(String tag, String attrs) Opens a tag on the current line WITHOUT a trailing newline.voidoutdent()Reduces the current indentation level by decreasing the depth.Appends raw HTML at the current indent level, followed by a newline.rawVerbatim(String html) Appends raw HTML verbatim with NO indentation or newline added.voidCreates a self-closing HTML tag with optional attributes.Wraps content in a presentation table (border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%").Creates an HTML table element with the specified attributes and content block.Wraps content in an HTML `<table>` tag, applying attributes and executing the provided block within the table.Appends raw text on the current line (no indent, no newline).toString()static StringunsortedAttrs(Map<String, String> attributes) Builds an unsorted attribute string from a map of key-value pairs.Wraps the output of aRunnableblock with the specified HTML tag.Opens a tag with attributes, executes the block, and closes the tag.
-
Field Details
-
PRESENTATION_TABLE_ATTRS
-
-
Constructor Details
-
HtmlBuilder
public HtmlBuilder()Creates a builder at indent depth 0. -
HtmlBuilder
public HtmlBuilder(int startingIndent) Creates a builder with the given starting indent depth in spaces.
-
-
Method Details
-
open
Opens a tag with attributes, writes a newline, and increases indent. Produces:{indent}<tag attrs>\n -
open
Opens a tag with no attributes. -
close
Closes a tag: decreases indent, writes{indent}</tag>\n. -
openInline
Opens a tag on the current line WITHOUT a trailing newline. Used when content follows on the same line. Produces:{indent}<tag attrs> -
openInline
Opens an inline tag with no attributes. -
closeInline
Appends a closing tag for the specified inline element to the internal StringBuilder.- Parameters:
tag- the name of the inline element for which the closing tag should be appended
-
closeInlineLn
Appends a closing tag with a newline character to the StringBuilder.- Parameters:
tag- the name of the tag to be closed
-
text
Appends raw text on the current line (no indent, no newline). -
line
Appends indented text followed by a newline. -
raw
Appends raw HTML at the current indent level, followed by a newline. Only the first line gets the indentation prefix. -
rawVerbatim
Appends raw HTML verbatim with NO indentation or newline added. Used for MSO conditionals, VML, and preformatted content. -
mso
Wraps the output of a builder block in MSO conditional comments. Everything written by the block appears between<!--[if mso | IE]>and<![endif]-->, followed by a newline. -
mso
Wraps a string in MSO conditional comments followed by a newline. Shorthand for simple MSO content that doesn't need builder calls. -
attrs
Builds a sorted attribute string from a map. Keys are sorted alphabetically, with anytrailingKeysplaced at the end in the order specified. Null or empty values cause the attribute to be omitted, except for attributes inALWAYS_EMITwhich are always emitted. Produces:key1="val1" key2="val2"(note leading space).Values are NOT escaped — callers must escape via
escapeAttr()before passing.- Parameters:
attributes- the attribute map to rendertrailingKeys- attribute names that should appear after all alphabetically-sorted keys
-
unsortedAttrs
Builds an unsorted attribute string from a map of key-value pairs. Keys are appended with their corresponding values in the order they are provided in the map. Null or empty values are omitted, except for keys inALWAYS_EMIT, which are always included even if their values are empty.Values are NOT escaped — callers must escape via
escapeAttr()before passing.- Parameters:
attributes- a map of attribute names and their values; keys represent attribute names and values represent attribute values- Returns:
- a space-separated string of unsorted attributes in the format
key1="value1" key2="value2". An empty string is returned if the input map is null or empty.
-
attrs
Builds an attribute string from key/value pairs. Null or empty values cause the attribute to be omitted, except for attributes inALWAYS_EMITwhich are always emitted. Produces:key1="val1" key2="val2"(note leading space).Values are NOT escaped — callers must escape via
escapeAttr()before passing.Note: this overload preserves insertion order. Use
attrs(Map, String...)(Map)} for automatic alphabetical sorting. -
notMso
Wraps the output of a builder block in non-MSO conditional comments (<!--[if !mso]><!-->...<!--<![endif]-->). Content is visible to all clients except Outlook desktop. Followed by a newline. -
notMsoIE
Wraps the output of a builder block in non-MSO/IE conditional comments (<!--[if !mso | IE]><!-->...<!--<![endif]-->). Content is hidden from both Outlook desktop and IE. Followed by a newline. -
selfClose
Creates a self-closing HTML tag with optional attributes. The method appends the tag at the current indentation level, includes the specified attributes if provided, and appends a newline.- Parameters:
tag- the HTML tag name to be self-closedattrs- the attribute string to include within the tag; if null or empty, no attributes are added
-
wrap
Opens a tag with attributes, executes the block, and closes the tag. Manages indentation likewrap(String, Runnable).- Parameters:
tag- the HTML tag nameattrs- the attribute string (fromattrs(Map, String...))block- the block that produces the tag's inner content
-
newline
Appends a newline character to the internal string builder and returns the current instance of theHtmlBuilder.- Returns:
- this
HtmlBuilderinstance for method chaining
-
indent
public void indent()Increases the current indentation depth by a predefined size. This method modifies the internal state of theHtmlBuilderto reflect a deeper nesting level for subsequent content. The increase in depth is determined by the constantINDENT_SIZE. -
outdent
public void outdent()Reduces the current indentation level by decreasing the depth. Ensures that the depth does not fall below zero. The decrease is determined by the constant INDENT_SIZE. -
toString
-
table
Wraps content in a presentation table (border="0" cellpadding="0" cellspacing="0" role="presentation" width="100%").- Parameters:
block- the block that produces the table's inner content
-
table
Creates an HTML table element with the specified attributes and content block.- Parameters:
attrs- the attributes to be applied to the table elementblock- a runnable block that defines the content of the table- Returns:
- an HtmlBuilder instance with the table element wrapped
-
table
Wraps content in an HTML `<table>` tag, applying attributes and executing the provided block within the table. Adds default presentation table attributes unless overridden by the input.- Parameters:
attrs- a map of attributes to be applied to the `` tag; keys represent attribute names and values represent attribute values
block- the block that produces the table's inner content- Returns:
- this
HtmlBuilderinstance for method chainingwrap
Wraps the output of aRunnableblock with the specified HTML tag. Indents the block's content appropriately and closes the tag after execution of the block.- Parameters:
tag- the HTML tag to wrap the block withblock- the block of content to be wrapped in the specified tag- Returns:
- this
HtmlBuilderinstance for method chaining
attrIf
-