Class HtmlElement

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

public final class HtmlElement extends Object
Lightweight HTML element representation for CSS selector matching and style inlining. This is NOT a full DOM - it's a minimal model that supports the operations needed by the CSS inliner: tag name, attributes, parent/child/sibling navigation.
  • Constructor Summary

    Constructors
    Constructor
    Description
    HtmlElement(String tagName, Map<String,String> attributes)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a child element to this element, setting its parent reference and caching its index.
    Returns all descendant elements in document order (depth-first).
    Returns the value of the attribute with the given name, or null if not present.
    Returns an unmodifiable view of all attributes on this element.
    Returns the list of child elements.
    Returns the set of CSS class names on this element, parsed from the class attribute.
    Returns the value of the id attribute, or null if not set.
    Returns the parent element, or null if this is the root element.
    Returns the value of the style attribute, or null if not set.
    int
    Returns the position immediately after the closing quote of the style attribute in the original HTML string, or -1 if not set.
    int
    Returns the position of the style=" substring in the original HTML string, or -1 if not set.
    int
    Returns the position immediately after the closing > character in the original HTML string, or -1 if not set.
    Returns the tag name of this element (always lowercase).
    int
    Returns the position of the opening < character in the original HTML string, or -1 if not set.
    boolean
    Whether this element has position information for in-place style modification.
    int
    Returns the index of this element among its parent's children.
    boolean
    Checks if this element is a descendant of the given ancestor.
    Returns the previous sibling element, or null if this is the first child.
    void
    Sets the inline style attribute on this element.
    void
    setStyleAttrEnd(int styleAttrEnd)
    Sets the position immediately after the closing quote of the style attribute in the original HTML string.
    void
    setStyleAttrStart(int styleAttrStart)
    Sets the position of the style=" substring in the original HTML string.
    void
    setTagEnd(int tagEnd)
    Sets the position immediately after the closing > character in the original HTML string.
    void
    setTagStart(int tagStart)
    Sets the position of the opening < character in the original HTML string.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

  • Method Details

    • getTagName

      public String getTagName()
      Returns the tag name of this element (always lowercase).
      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 (case-insensitive)
      Returns:
      the attribute value, or null if the attribute is not set
    • getAttributes

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

      public String getId()
      Returns the value of the id attribute, or null if not set.
      Returns:
      the element ID, or null
    • getClassNames

      public Set<String> getClassNames()
      Returns the set of CSS class names on this element, parsed from the class attribute. The result is cached after the first call.
      Returns:
      an immutable set of class names, or an empty set if no classes are defined
    • getStyle

      public String getStyle()
      Returns the value of the style attribute, or null if not set.
      Returns:
      the inline style string, or null
    • setStyle

      public void setStyle(String style)
      Sets the inline style attribute on this element.
      Parameters:
      style - the new style string
    • getParent

      public HtmlElement getParent()
      Returns the parent element, or null if this is the root element.
      Returns:
      the parent element, or null
    • getChildren

      public List<HtmlElement> getChildren()
      Returns the list of child elements.
      Returns:
      the mutable list of children
    • addChild

      public void addChild(HtmlElement child)
      Adds a child element to this element, setting its parent reference and caching its index.
      Parameters:
      child - the child element to add
    • indexInParent

      public int indexInParent()
      Returns the index of this element among its parent's children.
      Returns:
      the zero-based index within the parent's children, or 0 if there is no parent
    • previousSibling

      public HtmlElement previousSibling()
      Returns the previous sibling element, or null if this is the first child.
      Returns:
      the previous sibling, or null if none exists
    • isDescendantOf

      public boolean isDescendantOf(HtmlElement ancestor)
      Checks if this element is a descendant of the given ancestor.
      Parameters:
      ancestor - the potential ancestor element
      Returns:
      true if this element is a descendant of the given ancestor
    • allDescendants

      public List<HtmlElement> allDescendants()
      Returns all descendant elements in document order (depth-first). The result is cached since the tree is immutable after parsing.
      Returns:
      a list of all descendant elements in depth-first order
    • getTagStart

      public int getTagStart()
      Returns the position of the opening < character in the original HTML string, or -1 if not set.
      Returns:
      the tag start position, or -1
    • setTagStart

      public void setTagStart(int tagStart)
      Sets the position of the opening < character in the original HTML string.
      Parameters:
      tagStart - the tag start position
    • getTagEnd

      public int getTagEnd()
      Returns the position immediately after the closing > character in the original HTML string, or -1 if not set.
      Returns:
      the tag end position, or -1
    • setTagEnd

      public void setTagEnd(int tagEnd)
      Sets the position immediately after the closing > character in the original HTML string.
      Parameters:
      tagEnd - the tag end position
    • getStyleAttrStart

      public int getStyleAttrStart()
      Returns the position of the style=" substring in the original HTML string, or -1 if not set.
      Returns:
      the style attribute start position, or -1
    • setStyleAttrStart

      public void setStyleAttrStart(int styleAttrStart)
      Sets the position of the style=" substring in the original HTML string.
      Parameters:
      styleAttrStart - the style attribute start position
    • getStyleAttrEnd

      public int getStyleAttrEnd()
      Returns the position immediately after the closing quote of the style attribute in the original HTML string, or -1 if not set.
      Returns:
      the style attribute end position, or -1
    • setStyleAttrEnd

      public void setStyleAttrEnd(int styleAttrEnd)
      Sets the position immediately after the closing quote of the style attribute in the original HTML string.
      Parameters:
      styleAttrEnd - the style attribute end position
    • hasPositionInfo

      public boolean hasPositionInfo()
      Whether this element has position information for in-place style modification.
      Returns:
      true if both tag start and tag end positions are set
    • toString

      public String toString()
      Overrides:
      toString in class Object