Interface HTMLRewriter
- All Superinterfaces:
org.teavm.jso.JSObject
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 Summary
Modifier and TypeMethodDescriptionstatic HTMLRewritercreate()Returns a rewriter with no handlers.default HTMLRewriteron(String selector, ElementHandler handler) Attaches a handler for each element the selector matches.default HTMLRewriteronComments(String selector, CommentHandler handler) Attaches a handler for each comment inside what the selector matches.default HTMLRewriteronDoctype(DoctypeHandler handler) Attaches a handler for the document's doctype.default HTMLRewriteronDocumentComments(CommentHandler handler) Attaches a handler for every comment in the document, outside thehtmltag included.default HTMLRewriteronDocumentText(TextHandler handler) Attaches a handler for every chunk of text in the document, outside thehtmltag included.default HTMLRewriteronEnd(EndHandler handler) Attaches a handler for the end of the document, where content can still be appended.default HTMLRewriteronText(String selector, TextHandler handler) Attaches a handler for each chunk of text inside what the selector matches.default ResponseWraps a response so its body is rewritten as it is read.Methods inherited from interface org.teavm.jso.JSObject
cast
-
Method Details
-
create
Returns a rewriter with no handlers.- Returns:
- a rewriter with no handlers
-
on
Attaches a handler for each element the selector matches.A handler that also implements
TextHandlerorCommentHandlergets the text and the comments inside each match as well.- Parameters:
selector- a CSS selector, as in the platform's listhandler- the handler- Returns:
- this rewriter
-
onText
Attaches a handler for each chunk of text inside what the selector matches.- Parameters:
selector- a CSS selectorhandler- the handler- Returns:
- this rewriter
-
onComments
Attaches a handler for each comment inside what the selector matches.- Parameters:
selector- a CSS selectorhandler- the handler- Returns:
- this rewriter
-
onDoctype
Attaches a handler for the document's doctype.- Parameters:
handler- the handler- Returns:
- this rewriter
-
onDocumentComments
Attaches a handler for every comment in the document, outside thehtmltag included.- Parameters:
handler- the handler- Returns:
- this rewriter
-
onDocumentText
Attaches a handler for every chunk of text in the document, outside thehtmltag included.- Parameters:
handler- the handler- Returns:
- this rewriter
-
onEnd
Attaches a handler for the end of the document, where content can still be appended.- Parameters:
handler- the handler- Returns:
- this rewriter
-
transform
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
-