Class MjmlNode

java.lang.Object
dev.jcputney.mjml.parser.MjmlNode

public class MjmlNode extends Object
Lightweight mutable tree node representing an MJML element. Each node has a tag name, attributes, text content, and children.
  • Constructor Details

    • MjmlNode

      public MjmlNode(String tagName)
  • Method Details

    • getTagName

      public String getTagName()
      Returns the tag name of this node.
      Returns:
      the tag name
    • getAttribute

      public String getAttribute(String name)
      Returns the value of the attribute with the given name, or null if not present.
      Parameters:
      name - the attribute name
      Returns:
      the attribute value, or null if the attribute is not set
    • getAttribute

      public String getAttribute(String name, String defaultValue)
      Returns the value of the attribute with the given name, or the default value if not present.
      Parameters:
      name - the attribute name
      defaultValue - the value to return if the attribute is not set
      Returns:
      the attribute value, or defaultValue if the attribute is not set
    • getMjClassNames

      public String[] getMjClassNames()
      Returns the cached array of mj-class names on this node. The result is cached and invalidated when the "mj-class" attribute is changed via setAttribute(String, String).
      Returns:
      the array of class names, or an empty array if no mj-class attribute is set
    • setAttribute

      public void setAttribute(String name, String value)
      Sets the attribute with the given name to the given value.
      Parameters:
      name - the attribute name
      value - the attribute value
    • getAttributes

      public Map<String,String> getAttributes()
      Returns an unmodifiable view of all attributes on this node.
      Returns:
      an unmodifiable map of attribute names to values
    • getChildren

      public List<MjmlNode> getChildren()
      Returns an unmodifiable view of this node's children. The returned list is cached and invalidated when children are added or replaced.
      Returns:
      an unmodifiable list of child nodes
    • addChild

      public void addChild(MjmlNode child)
      Adds a child node to this node and sets the child's parent reference.
      Parameters:
      child - the child node to add
    • replaceWith

      public void replaceWith(List<MjmlNode> replacements)
      Replaces this node in its parent's children list with the given nodes. Used by include resolution to replace mj-include with resolved content.
      Parameters:
      replacements - the list of nodes to insert in place of this node
    • getParent

      public MjmlNode getParent()
      Returns the parent node, or null if this is a root node.
      Returns:
      the parent node, or null
    • getTextContent

      public String getTextContent()
      Returns the text content of this node.
      Returns:
      the text content, never null
    • setTextContent

      public void setTextContent(String textContent)
      Sets the text content of this node. A null value is treated as empty string.
      Parameters:
      textContent - the text content to set
    • getChildrenByTag

      public List<MjmlNode> getChildrenByTag(String tag)
      Returns direct children with the specified tag name.
      Parameters:
      tag - the tag name to match
      Returns:
      a list of matching child nodes (may be empty)
    • getFirstChildByTag

      public MjmlNode getFirstChildByTag(String tag)
      Returns the first direct child with the specified tag name, or null if none match.
      Parameters:
      tag - the tag name to match
      Returns:
      the first matching child node, or null
    • getInnerHtml

      public String getInnerHtml()
      Returns the inner HTML content - the serialized content of all children. For ending tags with CDATA-wrapped content, this returns the raw HTML.
      Returns:
      the inner HTML string
    • getOuterHtml

      public String getOuterHtml()
      Returns the outer HTML of this node including its tag, attributes, and content.
      Returns:
      the outer HTML string
    • toString

      public String toString()
      Overrides:
      toString in class Object