Class HtmlDocumentParser

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

public final class HtmlDocumentParser extends Object
Lightweight HTML tokenizer that builds an HtmlElement tree from HTML text.

This is NOT a full HTML5 parser. It handles the well-structured HTML output from the MJML renderer, including:

  • Opening tags with attributes
  • Self-closing tags
  • Closing tags
  • HTML comments (including MSO conditionals)
  • DOCTYPE declarations

It tracks tag positions in the source string to enable in-place style modification.

  • Method Details

    • parse

      public static HtmlElement parse(String html)
      Parses HTML into an element tree rooted at a virtual document element.
      Parameters:
      html - the HTML string
      Returns:
      the root element containing the document tree
    • extractStyles

      public static HtmlDocumentParser.StyleExtractionResult extractStyles(String html)
      Extracts all CSS from <style> blocks in the HTML and returns the HTML with those blocks removed.
      Parameters:
      html - the HTML string to extract styles from
      Returns:
      a HtmlDocumentParser.StyleExtractionResult containing the cleaned HTML and extracted CSS