Interface HTMLRewriter

All Superinterfaces:
org.teavm.jso.JSObject

public interface HTMLRewriter extends org.teavm.jso.JSObject
Rewrites an HTML response as it streams, with Java handlers.

transform(Response) returns at once and the platform calls the handlers while whoever holds the new response reads its body, which can be after the handler that built it has returned. A handler that throws stops the parse and errors the new body with the exception; the client sees a truncated response if part of it was already sent.

Each handler call runs on its own fiber and the platform waits for it before it goes on, so a handler can wait on a binding or a fetch, as the platform's async handlers can. A call costs a fiber, so register only the kinds you need: on with a class that implements one interface registers one callback, and each extra interface adds one.

 Response page = HTMLRewriter.create()
     .on("a", link -> link.setAttribute("target", "_blank"))
     .on("body", body -> body.prepend("<p>hello</p>", ContentOptions.html()))
     .transform(origin);
Since:
1.0.3
  • Method Details

    • create

      static HTMLRewriter create()
      Returns a rewriter with no handlers.
      Returns:
      a rewriter with no handlers
    • on

      default HTMLRewriter on(String selector, ElementHandler handler)
      Attaches a handler for each element the selector matches.

      A handler that also implements TextHandler or CommentHandler gets the text and the comments inside each match as well.

      Parameters:
      selector - a CSS selector, as in the platform's list
      handler - the handler
      Returns:
      this rewriter
    • onText

      default HTMLRewriter onText(String selector, TextHandler handler)
      Attaches a handler for each chunk of text inside what the selector matches.
      Parameters:
      selector - a CSS selector
      handler - the handler
      Returns:
      this rewriter
    • onComments

      default HTMLRewriter onComments(String selector, CommentHandler handler)
      Attaches a handler for each comment inside what the selector matches.
      Parameters:
      selector - a CSS selector
      handler - the handler
      Returns:
      this rewriter
    • onDoctype

      default HTMLRewriter onDoctype(DoctypeHandler handler)
      Attaches a handler for the document's doctype.
      Parameters:
      handler - the handler
      Returns:
      this rewriter
    • onDocumentComments

      default HTMLRewriter onDocumentComments(CommentHandler handler)
      Attaches a handler for every comment in the document, outside the html tag included.
      Parameters:
      handler - the handler
      Returns:
      this rewriter
    • onDocumentText

      default HTMLRewriter onDocumentText(TextHandler handler)
      Attaches a handler for every chunk of text in the document, outside the html tag included.
      Parameters:
      handler - the handler
      Returns:
      this rewriter
    • onEnd

      default HTMLRewriter onEnd(EndHandler handler)
      Attaches a handler for the end of the document, where content can still be appended.
      Parameters:
      handler - the handler
      Returns:
      this rewriter
    • transform

      default Response transform(Response response)
      Wraps a response so its body is rewritten as it is read.
      Parameters:
      response - the response to rewrite
      Returns:
      a response with the rewritten body, with the original's status and headers