Skip to content

Interceptors

Sergey Mashkov edited this page Jul 7, 2015 · 3 revisions

Interceptors (filtering and transformation)

You can define interceptors chains that could transform HTML during building or make some observations. There is a default "filter interceptor", so you can filter out some elements.

Below is an example that filters HTML that way so all div will be omited but content will remain

println(document {
    append.filter { if (it.tagName == "div") SKIP else PASS  }.html {
        body {
            div {
                a { +"link1" }
            }
            a { +"link2" }
        }
    }
}.serialize())

The code above will produce the following output

<!DOCTYPE html>
<html>
  <body>
    <a>link1</a><a>link2</a>
  </body>
</html>

The other interceptor doesn't mutate HTML but measures generation time

System.out.appendHTML().measureTime().html {
	head {
		title("Welcome page")
	}
	body {
		div {
			+"<<<special chars & entities goes here>>>"
		}
		div {
			CDATA("Here is my content")
		}
	}
}.let {
	println()
	println("Generated in ${it.time} ms")
}